""" ================================= |
| This file is part of |
| staticoso |
| Just a simple Static Site Generator |
| |
| Licensed under the AGPLv3 license |
| Copyright (C) 2022, OctoSpacc |
| ================================= """
from datetime import datetime
from multiprocessing import Pool, cpu_count
from Libs.bs4 import BeautifulSoup
from Modules.Config import *
from Modules.Elements import *
from Modules.HTML import *
from Modules.Logging import *
from Modules.Markdown import *
from Modules.Pug import *
from Modules.Utils import *
def GetHTMLPagesList(Pages, BlogName, SiteRoot, PathPrefix, Unite=[], Type='Page', Category=None, For='Menu', MarkdownExts=(), MenuStyle='Default'):
ShowPaths, Flatten, SingleLine = True, False, False
if MenuStyle == 'Flat':
Flatten = True
elif MenuStyle == 'Line':
ShowPaths, SingleLine = False, True
List, ToPop, LastParent = '', [], []
IndexPages = Pages.copy()
for e in IndexPages:
if e[3]['Index'] == 'False' or e[3]['Index'] == 'None':
IndexPages.remove(e)
for i,e in enumerate(IndexPages):
if e[3]['Type'] != Type:
ToPop += [i]
ToPop = RevSort(ToPop)
for i in ToPop:
IndexPages.pop(i)
if Type == 'Page':
IndexPages = OrderPages(IndexPages)
for i,e in enumerate(Unite):
if e:
IndexPages.insert(i,[e,None,None,{'Type':Type,'Index':'True','Order':'Unite'}])
for File, Content, Titles, Meta in IndexPages:
if Meta['Type'] == Type and CanIndex(Meta['Index'], For) and (not Category or Category in Meta['Categories']):
Depth = (File.count('/') + 1) if Meta['Order'] != 'Unite' else 1
if Depth > 1 and Meta['Order'] != 'Unite': # Folder names are handled here
CurParent = File.split('/')[:-1]
for i,s in enumerate(CurParent):
if LastParent != CurParent and ShowPaths:
LastParent = CurParent
Levels = '- ' * ((Depth-1+i) if not Flatten else 1)
# Folders with else without an index file
if StripExt(File).endswith('index'):
Title = MakeListTitle(File, Meta, Titles, 'HTMLTitle', SiteRoot, BlogName, PathPrefix)
else:
Title = CurParent[Depth-2+i]
if SingleLine:
List += ' ' + Title + ' '
else:
List += Levels + Title + '\n'
if not (Depth > 1 and StripExt(File).split('/')[-1] == 'index'):
Levels = '- ' * (Depth if not Flatten else 1)
if Meta['Order'] == 'Unite':
Title = File
else:
Title = MakeListTitle(File, Meta, Titles, 'HTMLTitle', SiteRoot, BlogName, PathPrefix)
if SingleLine:
List += ' ' + Title + ' '
else:
List += Levels + Title + '\n'
return markdown(MarkdownHTMLEscape(List, MarkdownExts), extensions=MarkdownExts)
def CheckHTMLCommentLine(Line):
if Line.startswith(''):
return Line
return None
def TemplatePreprocessor(Text):
Meta, MetaDefault = '', {
'MenuStyle': 'Default'}
for l in Text.splitlines():
ll = l.lstrip().rstrip()
lll = CheckHTMLCommentLine(ll)
if lll:
if lll.startswith('%'):
Meta += lll[1:-3].lstrip().rstrip() + '\n'
Meta = dict(ReadConf(LoadConfStr('[Meta]\n' + Meta), 'Meta'))
for i in MetaDefault:
if not i in Meta:
Meta.update({i:MetaDefault[i]})
return Meta
def PagePreprocessor(Path, TempPath, Type, SiteTemplate, SiteRoot, GlobalMacros, CategoryUncategorized, LightRun=False):
File = ReadFile(Path)
Path = Path.lower()
Content, Titles, DashyTitles, HTMLTitlesFound, Macros, Meta, MetaDefault = '', [], [], False, '', '', {
'Template': SiteTemplate,
'Style': '',
'Type': Type,
'Index': 'Unspecified',
'Feed': 'True',
'Title': '',
'HTMLTitle': '',
'Description': '',
'Image': '',
'Macros': {},
'Categories': [],
'URLs': [],
'CreatedOn': '',
'UpdatedOn': '',
'EditedOn': '',
'Order': None}
# Find all positions of '', add them in a list=[[pos0,pos1,line0,line1],...]
for l in File.splitlines():
ll = l.lstrip().rstrip()
lll = CheckHTMLCommentLine(ll)
if ll.startswith('//') or lll: # Find preprocessor lines
lll = ll[2:].lstrip()
if lll.startswith('%'):
Meta += lll[1:].lstrip() + '\n'
elif lll.startswith('$'):
Macros += lll[1:].lstrip() + '\n'
#if ll.startswith(''): # Find comment and code blocks
# IgnoreBlocksStart += [l]
else: # Find headings
#if line in ignore block:
# continue
Headings = ('h1', 'h2', 'h3', 'h4', 'h5', 'h6')
if Path.endswith(FileExtensions['HTML']) and not HTMLTitlesFound:
Soup = BeautifulSoup(File, 'html.parser')
Tags = Soup.find_all()
for t in Tags:
if t.name in Headings:
Title = '#'*int(t.name[1]) + ' ' + str(t.text)
DashTitle = DashifyTitle(Title.lstrip('#'), DashyTitles)
DashyTitles += [DashTitle]
Titles += [Title]
t.replace_with(MakeLinkableTitle(None, Title, DashTitle, 'md'))
Content = str(Soup.prettify(formatter=None))
HTMLTitlesFound = True
elif Path.endswith(FileExtensions['Markdown']):
lsuffix = ''
if ll.startswith(('-', '+', '*')):
lsuffix += ll[0]
ll = ll[1:].lstrip()
if ll.startswith('#') or (ll.startswith('<') and ll[1:].startswith(Headings)):
if ll.startswith('#'):
Title = ll
elif ll.startswith('<'):
if ll[3:].startswith((" class='NoTitle", ' class="NoTitle')):
Content += l + '\n'
continue
else:
Title = '#'*h + str(ll[3:])
DashTitle = DashifyTitle(MkSoup(Title.lstrip('#')).get_text(), DashyTitles)
DashyTitles += [DashTitle]
Titles += [Title]
Title = MakeLinkableTitle(None, Title, DashTitle, 'md')
Title = Title.replace('> ', '> ')
Title = Title.replace(' ', '')
Content += lsuffix + Title + '\n'
else:
Content += l + '\n'
elif Path.endswith('.pug'):
if ll.startswith(Headings):
if ll[2:].startswith(("(class='NoTitle", '(class="NoTitle')):
Content += l + '\n'
else:
Title = '#'*int(ll[1]) + str(ll[3:])
DashTitle = DashifyTitle(Title.lstrip('#'), DashyTitles)
DashyTitles += [DashTitle]
Titles += [Title]
# TODO: We should handle headers that for any reason already have parenthesis
if ll[2:] == '(':
Content += l + '\n'
else:
Content += MakeLinkableTitle(l, Title, DashTitle, 'pug') + '\n'
else:
Content += l + '\n'
elif Path.endswith('.txt'):
Content += l + '\n'
Meta = dict(ReadConf(LoadConfStr('[Meta]\n' + Meta), 'Meta'))
for i in MetaDefault:
if i in Meta:
# TODO: Handle strings with spaces but wrapped in quotes
if i == 'Categories':
Categories = Meta['Categories'].split(' ')
Meta['Categories'] = []
for j in Categories:
Meta['Categories'] += [j]
elif i == 'URLs':
URLs = Meta['URLs'].split(' ')
Meta['URLs'] = []
for j in URLs:
Meta['URLs'] += [j]
else:
Meta.update({i:MetaDefault[i]})
if Meta['UpdatedOn']:
Meta['EditedOn'] = Meta['UpdatedOn']
if Meta['Index'] in ('Default', 'Unspecified', 'Categories'):
if not Meta['Categories']:
Meta['Categories'] = [CategoryUncategorized]
if Meta['Type'] == 'Page':
Meta['Index'] = 'Categories'
elif Meta['Type'] == 'Post':
Meta['Index'] = 'True'
if GlobalMacros:
Meta['Macros'].update(GlobalMacros)
Meta['Macros'].update(ReadConf(LoadConfStr('[Macros]\n' + Macros), 'Macros'))
return [TempPath, Content, Titles, Meta]
def PagePostprocessor(FileType, Text, Meta):
for e in Meta['Macros']:
Text = ReplWithEsc(Text, f"[: {e} :]", f"[:{e}:]")
return Text
def OrderPages(Old):
New, NoOrder, Max = [], [], 0
for i,e in enumerate(Old):
Curr = e[3]['Order']
if Curr:
if int(Curr) > Max:
Max = int(Curr)
else:
NoOrder += [e]
New = [None] * (Max+1)
for i,e in enumerate(Old):
Curr = e[3]['Order']
if Curr:
New[int(Curr)] = e
while None in New:
New.remove(None)
return New + NoOrder
def CanIndex(Index, For):
if Index in ('False', 'None'):
return False
elif Index in ('True', 'All', 'Unlinked'):
return True
else:
return True if Index == For else False
def PatchHTML(File, HTML, StaticPartsText, DynamicParts, DynamicPartsText, HTMLPagesList, PagePath, Content, Titles, Meta, SiteDomain, SiteRoot, SiteName, BlogName, FolderRoots, Categories, SiteLang, Locale, LightRun):
HTMLTitles = FormatTitles(Titles)
BodyDescription, BodyImage = '', ''
if not File.lower().endswith('.txt'):
Soup = BeautifulSoup(Content, 'html.parser')
if not BodyDescription and Soup.p:
BodyDescription = Soup.p.get_text()[:150].replace('\n', ' ').replace('"', "'") + '...'
if not BodyImage and Soup.img and Soup.img['src']:
BodyImage = Soup.img['src']
#Content = SquareFnrefs(Content)
if '