mirror of
https://gitlab.com/octtspacc/staticoso
synced 2025-06-05 22:09:23 +02:00
Transition to monorepo on a new Dev branch
This commit is contained in:
98
App/Source/Modules/ActivityPub.py
Normal file
98
App/Source/Modules/ActivityPub.py
Normal file
@@ -0,0 +1,98 @@
|
||||
""" ================================= |
|
||||
| This file is part of |
|
||||
| staticoso |
|
||||
| Just a simple Static Site Generator |
|
||||
| |
|
||||
| Licensed under the AGPLv3 license |
|
||||
| Copyright (C) 2022, OctoSpacc |
|
||||
| ================================= """
|
||||
|
||||
import time
|
||||
from Libs.dateutil.parser import parse as date_parse
|
||||
from Libs.mastodon import Mastodon
|
||||
from Modules.HTML import *
|
||||
from Modules.Utils import *
|
||||
|
||||
def MastodonGetSession(InstanceURL, Token):
|
||||
return Mastodon(
|
||||
api_base_url=InstanceURL,
|
||||
access_token=Token)
|
||||
|
||||
def MastodonGetMyID(Session):
|
||||
return Session.me()['id']
|
||||
|
||||
def MastodonGetPostsFromUserID(Session, UserID):
|
||||
return Session.account_statuses(
|
||||
UserID,
|
||||
exclude_replies=True)
|
||||
|
||||
def MastodonDoPost(Session, Text, Lang=None, Visibility='public'):
|
||||
if Text:
|
||||
return Session.status_post(
|
||||
Text,
|
||||
language=Lang,
|
||||
visibility=Visibility)
|
||||
|
||||
def MastodonGetLinkFromPost(Post, Domain=None):
|
||||
Parse = MkSoup(Post['content'])
|
||||
if Parse.a:
|
||||
Link = Parse.find_all('a')[-1]['href']
|
||||
if not Domain or (Domain and Link.startswith(Domain)):
|
||||
return {
|
||||
'Post': Post['uri'],
|
||||
'Link': Link}
|
||||
return None
|
||||
|
||||
def MastodonGetAllLinkPosts(Session, Domain=None):
|
||||
Posts = []
|
||||
for p in MastodonGetPostsFromUserID(Session, MastodonGetMyID(Session)):
|
||||
Post = MastodonGetLinkFromPost(p, Domain)
|
||||
if Post:
|
||||
Posts += [Post]
|
||||
return Posts
|
||||
|
||||
def MastodonShare(Flags, Pages, Locale):
|
||||
SaidPosting = False
|
||||
SiteDomain, SiteLang = Flags['SiteDomain'], Flags['SiteLang']
|
||||
InstanceURL, Token = Flags['MastodonURL'], Flags['MastodonToken']
|
||||
TypeFilter, HoursLimit, CategoryFilter = Flags['ActivityPubTypeFilter'], Flags['ActivityPubHoursLimit'], Flags['FeedCategoryFilter']
|
||||
Session = MastodonGetSession(InstanceURL, Token)
|
||||
Posts = MastodonGetAllLinkPosts(Session, SiteDomain)
|
||||
Pages.sort()
|
||||
for File, Content, Titles, Meta, ContentHTML, SlimHTML, Description, Image in Pages:
|
||||
if (not TypeFilter or (TypeFilter and (Meta['Type'] == TypeFilter or TypeFilter == '*'))) and (not CategoryFilter or (CategoryFilter and (CategoryFilter in Meta['Categories'] or CategoryFilter == '*'))):
|
||||
URL = f"{SiteDomain}/{StripExt(File)}.html"
|
||||
DoPost = True
|
||||
for p in Posts:
|
||||
if p['Link'] in [URL]+Meta['URLs']:
|
||||
DoPost = False
|
||||
break
|
||||
|
||||
if DoPost and Meta['Feed'] == 'True' and (not HoursLimit or (Meta['CreatedOn'] and time.time() - time.mktime(date_parse(Meta['CreatedOn']).timetuple()) < 60*60*HoursLimit)):
|
||||
Desc = ''
|
||||
Paragraphs = MkSoup(ContentHTML).p.get_text().split('\n')
|
||||
Read = '...' + Locale['ReadFullPost'] + ':\n'
|
||||
for p in Paragraphs:
|
||||
if p and len(Read+Desc+p)+25 < 500:
|
||||
Desc += p + '\n\n'
|
||||
else:
|
||||
if Desc:
|
||||
break
|
||||
else:
|
||||
Desc = p[:500-25-5-len(Read)] + '...'
|
||||
|
||||
if not SaidPosting:
|
||||
print("[I] Posting to Mastodon")
|
||||
SaidPosting = True
|
||||
|
||||
time.sleep(5) # Prevent flooding
|
||||
Post = MastodonGetLinkFromPost(
|
||||
Post=MastodonDoPost(
|
||||
Session,
|
||||
Text=Desc+Read+URL,
|
||||
Lang=SiteLang),
|
||||
Domain=SiteDomain)
|
||||
if Post:
|
||||
Posts += [Post]
|
||||
|
||||
return Posts
|
77
App/Source/Modules/Config.py
Normal file
77
App/Source/Modules/Config.py
Normal file
@@ -0,0 +1,77 @@
|
||||
""" ================================= |
|
||||
| This file is part of |
|
||||
| staticoso |
|
||||
| Just a simple Static Site Generator |
|
||||
| |
|
||||
| Licensed under the AGPLv3 license |
|
||||
| Copyright (C) 2022, OctoSpacc |
|
||||
| ================================= """
|
||||
|
||||
import configparser
|
||||
from ast import literal_eval
|
||||
|
||||
DefConf = {
|
||||
"Logging": 20,
|
||||
"Threads": 0,
|
||||
"DiffBuild": False,
|
||||
"OutDir": "public",
|
||||
"SiteLang": "en",
|
||||
"SiteTemplate": "Default.html",
|
||||
"ActivityPubTypeFilter": "Post",
|
||||
"ActivityPubHoursLimit": 168,
|
||||
"CategoriesUncategorized": "Uncategorized",
|
||||
"FeedCategoryFilter": "Blog",
|
||||
"FeedEntries": 10,
|
||||
"JournalRedirect": False
|
||||
}
|
||||
|
||||
def LoadConfFile(File):
|
||||
Conf = configparser.ConfigParser()
|
||||
Conf.optionxform = str
|
||||
Conf.read(File)
|
||||
return Conf
|
||||
|
||||
def LoadConfStr(Str):
|
||||
Conf = configparser.ConfigParser()
|
||||
Conf.optionxform = str
|
||||
Conf.read_string(Str)
|
||||
return Conf
|
||||
|
||||
def ReadConf(Conf, Sect, Opt=None):
|
||||
if Opt:
|
||||
if Conf.has_option(Sect, Opt):
|
||||
return Conf[Sect][Opt]
|
||||
else:
|
||||
if Conf.has_section(Sect):
|
||||
return Conf[Sect]
|
||||
return None
|
||||
|
||||
def EvalOpt(Opt):
|
||||
if Opt:
|
||||
return literal_eval(Opt)
|
||||
else:
|
||||
return None
|
||||
|
||||
# TODO: Cleaning
|
||||
|
||||
def OptionChoose(Default, Primary, Secondary, Tertiary=None):
|
||||
return Primary if Primary != None else Secondary if Secondary != None else Tertiary if Tertiary != None else Default
|
||||
def OptChoose(Default, Primary, Secondary, Tertiary=None):
|
||||
return OptionChoose(Default, Primary, Secondary, Tertiary=None)
|
||||
|
||||
def DefConfOptChoose(Key, Primary, Secondary):
|
||||
return OptChoose(DefConf[Key], Primary, Secondary)
|
||||
|
||||
def StringBoolChoose(Default, Primary, Secondary):
|
||||
Var = Default
|
||||
Check = Primary if Primary != None else Secondary
|
||||
if type(Check) == bool:
|
||||
Var = Check
|
||||
elif type(Check) == str:
|
||||
if Check in ('True', 'All', '*'):
|
||||
Var = True
|
||||
elif Check in ('False', 'None'):
|
||||
Var = False
|
||||
return Var
|
||||
def StrBoolChoose(Default, Primary, Secondary):
|
||||
return StringBoolChoose(Default, Primary, Secondary)
|
221
App/Source/Modules/Elements.py
Normal file
221
App/Source/Modules/Elements.py
Normal file
@@ -0,0 +1,221 @@
|
||||
""" ================================= |
|
||||
| This file is part of |
|
||||
| staticoso |
|
||||
| Just a simple Static Site Generator |
|
||||
| |
|
||||
| Licensed under the AGPLv3 license |
|
||||
| Copyright (C) 2022, OctoSpacc |
|
||||
| ================================= """
|
||||
|
||||
from base64 import b64encode
|
||||
from Modules.Globals import *
|
||||
from Modules.HTML import *
|
||||
from Modules.Utils import *
|
||||
|
||||
JournalHeadings = ('h2','h3','h4','h5')
|
||||
JournalTitleDecorators = {'(':')', '[':']', '{':'}'}
|
||||
#JournalStyles = {
|
||||
# "Default": {},
|
||||
# "details": {}
|
||||
#}
|
||||
HTMLSectionTitleLine = '<h{Index} class="SectionHeading staticoso-SectionHeading"><span class="SectionLink staticoso-SectionLink"><a href="#{DashTitle}"><span>»</span></a> </span><span class="SectionTitle staticoso-SectionTitle" id="{DashTitle}">{Title}</span></h{Index}>'
|
||||
PugSectionTitleLine = "{Start}{Heading}.SectionHeading.staticoso-SectionHeading #[span.SectionLink.staticoso-SectionLink #[a(href='#{DashTitle}') #[span »]] ]#[span#{DashTitle}.SectionTitle.staticoso-SectionTitle {Rest}]"
|
||||
CategoryPageTemplate = """\
|
||||
// Title: {Name}
|
||||
// Type: Page
|
||||
// Index: True
|
||||
|
||||
# {Name}
|
||||
|
||||
<div><staticoso:Category:{Name}></div>
|
||||
"""
|
||||
RedirectPageTemplate = """\
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<title>{TitlePrefix}Redirect</title>
|
||||
<link rel="canonical" href="{SiteDomain}/{DestURL}"/>
|
||||
<meta http-equiv="refresh" content="0; url='{DestURL}'"/>
|
||||
</head>
|
||||
<body>
|
||||
<p><a href="{DestURL}">{StrClick}</a> {StrRedirect}.</p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
HTMLCommentsBlock = '<br><h3>{StrComments}</h3><a href="{URL}" rel="noopener" target="_blank">{StrOpen} <span class="twa twa-↗️"><span>↗️</span></span></a>'
|
||||
|
||||
def DashifyTitle(Title:str, Done:list=[]):
|
||||
return UndupeStr(DashifyStr(Title.lstrip(' ').rstrip(' ')), Done, '-')
|
||||
|
||||
# Generate HTML tree list (nested list) from our internal metaformat, such as:
|
||||
# :Item 1 \\ <li>Item 1<ul>
|
||||
# .:Item 2 ============\\ <li>Item 2<ul>
|
||||
# ..:Item 3 ============// <li>Item 3</li></ul></li></ul></li>
|
||||
# :Item 4 // <li>Item 4</li>
|
||||
def GenHTMLTreeList(MetaList:str, Type:str='ul', Class:str=""):
|
||||
HTML = ''
|
||||
Lines = MetaList.splitlines()
|
||||
CurDepth, NextDepth, PrevDepth = 0, 0, 0
|
||||
for i,e in enumerate(Lines):
|
||||
CurDepth = e.find(':')
|
||||
NextDepth = Lines[i+1].find(':') if i+1 < len(Lines) else 0
|
||||
HTML += '\n<li>' + e[CurDepth+1:]
|
||||
if NextDepth == CurDepth:
|
||||
HTML += '</li>'
|
||||
elif NextDepth > CurDepth:
|
||||
HTML += f'\n<{Type}>' * (NextDepth - CurDepth)
|
||||
elif NextDepth < CurDepth:
|
||||
HTML += f'</li>\n</{Type}>' * (CurDepth - NextDepth) + '</li>'
|
||||
PrevDepth = CurDepth
|
||||
return f'<{Type} class="staticoso-TreeList {Class}">{HTML}\n</{Type}>'
|
||||
|
||||
def MakeLinkableTitle(Line:str, Title:str, DashTitle:str, Type:str):
|
||||
if Type == 'md':
|
||||
Index = Title.split(' ')[0].count('#')
|
||||
return HTMLSectionTitleLine.format(
|
||||
Index=Index,
|
||||
DashTitle=DashTitle,
|
||||
Title=Title[Index+1:])
|
||||
elif Type == 'pug':
|
||||
Index = Line.find('h')
|
||||
return PugSectionTitleLine.format(
|
||||
Start=Line[:Index],
|
||||
Heading=Line[Index:Index+2],
|
||||
Rest=Line[Index+2:],
|
||||
DashTitle=DashTitle)
|
||||
|
||||
def GetTitle(FileName:str, Meta:dict, Titles:list, Prefer:str='MetaTitle', BlogName:str=None):
|
||||
if Prefer == 'BodyTitle':
|
||||
Title = Titles[0].lstrip('#') if Titles else Meta['Title'] if Meta['Title'] else FileName
|
||||
elif Prefer == 'MetaTitle':
|
||||
Title = Meta['Title'] if Meta['Title'] else Titles[0].lstrip('#') if Titles else FileName
|
||||
elif Prefer == 'HTMLTitle':
|
||||
Title = Meta['HTMLTitle'] if Meta['HTMLTitle'] else Meta['Title'] if Meta['Title'] else Titles[0].lstrip('#') if Titles else FileName
|
||||
if Meta['Type'] == 'Post' and BlogName and 'Blog' in Meta['Categories']:
|
||||
Title += ' - ' + BlogName
|
||||
return Title
|
||||
|
||||
def GetDescription(Meta:dict, BodyDescription:str, Prefer:str='MetaDescription'):
|
||||
if Prefer == 'BodyDescription':
|
||||
Description = BodyDescription if BodyDescription else Meta['Description'] if Meta['Description'] else ''
|
||||
elif Prefer == 'MetaDescription':
|
||||
Description = Meta['Description'] if Meta['Description'] else BodyDescription if BodyDescription else ''
|
||||
return Description
|
||||
|
||||
def GetImage(Meta:dict, BodyImage:str, Prefer:str='MetaImage'):
|
||||
if Prefer == 'BodyImage':
|
||||
Image = BodyImage if BodyImage else Meta['Image'] if Meta['Image'] else ''
|
||||
elif Prefer == 'MetaImage':
|
||||
Image = Meta['Image'] if Meta['Image'] else BodyImage if BodyImage else ''
|
||||
return Image
|
||||
|
||||
def MakeContentHeader(Meta:dict, Locale:dict, Categories:str=''):
|
||||
Header = ''
|
||||
for e in ['CreatedOn', 'EditedOn']:
|
||||
if Meta[e]:
|
||||
Header += f'<span class="staticoso-ContentHeader-{e}" id="staticoso-ContentHeader-{e}"><span class="staticoso-Label">{Locale[e]}</span>: <span class="staticoso-Value">{Meta[e]}</span></span><br>'
|
||||
if Categories:
|
||||
Header += f'<span class="staticoso-ContentHeader-Categories" id="staticoso-ContentHeader-Categories"><span class="staticoso-Label">{Locale["Categories"]}</span>:<span class="staticoso-Value">{Categories.removesuffix(" ")}</span></span><br>'
|
||||
if Meta['Index'].lower() in PageIndexStrNeg:
|
||||
Header += f'<span class="staticoso-ContentHeader-Index" id="staticoso-ContentHeader-Index"><span class="staticoso-Value">{Locale["Unlisted"]}</span></span><br>'
|
||||
return f'<p>{Header}</p>'
|
||||
|
||||
def MakeCategoryLine(File:str, Meta:dict):
|
||||
Categories = ''
|
||||
for Cat in Meta['Categories']:
|
||||
Categories += f' <a href="{GetPathLevels(File)}Categories/{Cat}.html">{html.escape(Cat)}</a> '
|
||||
return Categories
|
||||
|
||||
def MakeListTitle(File:str, Meta:dict, Titles:list, Prefer:str, BlogName:str, PathPrefix:str=''):
|
||||
Title = GetTitle(File.split('/')[-1], Meta, Titles, Prefer, BlogName).lstrip().rstrip()
|
||||
Link = False if Meta['Index'] == 'Unlinked' else True
|
||||
if Link:
|
||||
Href = f'{PathPrefix}{StripExt(File)}.html'
|
||||
Title = f'<a href="{Href}">{Title}</a>'
|
||||
#else:
|
||||
# Title = f'<span class="staticoso-ListItem-Plain">{Title}</span>'
|
||||
if Meta['Type'] == 'Post':
|
||||
CreatedOn = Meta['CreatedOn'] if Meta['CreatedOn'] else '?'
|
||||
Title = f"<span>[<time>{CreatedOn}</time>]</span> {Title}"
|
||||
return Title
|
||||
|
||||
def FormatTitles(Titles:list, Flatten=False):
|
||||
# TODO: Somehow titles written in Pug can end up here and don't work, they should be handled
|
||||
List, DashyTitles = '', []
|
||||
for t in Titles:
|
||||
n = 0 if Flatten else t.split(' ')[0].count('#')
|
||||
Level = '.' * (n-1) + ':'
|
||||
Title = MkSoup(t.lstrip('#')).get_text()
|
||||
DashyTitle = DashifyTitle(Title, DashyTitles)
|
||||
DashyTitles += [DashyTitle]
|
||||
List += f'{Level}<a href="#{DashyTitle}">{html.escape(Title)}</a>\n'
|
||||
return GenHTMLTreeList(List)
|
||||
|
||||
# Clean up a generic HTML tree such that it's compliant with the HTML Journal standard
|
||||
# (https://m15o.ichi.city/site/subscribing-to-a-journal-page.html);
|
||||
# basis is: find an element with the JournalBody attr., and group its direct children as <article>s
|
||||
def MakeHTMLJournal(Flags, Locale, FilePath, HTML):
|
||||
Soup, Journal, Entries = MkSoup(HTML), '', []
|
||||
for t in Soup.find_all(attrs={"htmljournal":True}):
|
||||
#JournalStyle = JournalStyles[t.attrs["journalstyle"]] if 'journalstyle' in t.attrs and t.attrs["journalstyle"] in JournalStyles else JournalStyles['Default']
|
||||
for c in t.children: # Entries, some might be entirely grouped in their own element but others could not, use headings as separators
|
||||
for ct in MkSoup(str(c)).find_all():
|
||||
# Transform (almost, for now I reserve some) any heading into h2 and remove any attributes
|
||||
if ct.name in JournalHeadings:
|
||||
Title = ct.text.strip().removeprefix('»').strip()
|
||||
Chr0 = Title[0]
|
||||
# Remove leading symbols before date
|
||||
if Chr0 in JournalTitleDecorators.keys():
|
||||
Idx = Title.find(JournalTitleDecorators[Chr0])
|
||||
Title = Title[1:Idx] + ' - ' + Title[Idx+2:]
|
||||
if Journal:
|
||||
Journal += '\n</article><br>\n'
|
||||
Journal += f'\n<article>\n<h2>{Title}</h2>\n'
|
||||
elif ct.name == 'p': # We should handle any type to preserve <details> and things
|
||||
Journal += str(ct)
|
||||
FileName = FilePath.split('/')[-1]
|
||||
URL = f'{Flags["SiteDomain"]}/{StripExt(FilePath)}.Journal.html'
|
||||
Redirect = f"""<meta http-equiv="refresh" content="0; url='./{FileName}'">""" if Flags["JournalRedirect"] else ''
|
||||
|
||||
# Instead of copying stuff from the full page, for now we use dedicated title, header, footer, and pagination
|
||||
Title = t.attrs["journaltitle"] if 'journaltitle' in t.attrs else f'"{StripExt(FileName)}" Journal - {Flags["SiteName"]}' if Flags["SiteName"] else f'"{StripExt(FileName)}" Journal'
|
||||
FeedLink = f"""<a title="Journal Atom Feed" href="https://journal.miso.town/atom?url={URL}" target="_blank" rel="noopener"><img width="88" height="31" alt="Journal Atom Feed" title="Journal Atom Feed" src="data:image/png;base64,{b64encode(ReadFile(staticosoBaseDir()+'Assets/Feed-88x31.png', 'rb')).decode()}"></a>""" if Flags["SiteDomain"] else ''
|
||||
Header = t.attrs["journalheader"] if 'journalheader' in t.attrs else f"""\
|
||||
<p>
|
||||
<i>{Locale["StrippedDownNotice"].format(Link="./"+FileName)}</i>
|
||||
<a title="Valid HTML Journal" href="https://journal.miso.town" target="_blank" rel="noopener"><img alt="Valid HTML Journal" title="Valid HTML Journal" width="88" height="31" src="data:image/png;base64,{b64encode(ReadFile(staticosoBaseDir()+'Assets/Valid-HTML-Journal-88x31.png', 'rb')).decode()}"></a>
|
||||
{FeedLink}
|
||||
</p>
|
||||
"""
|
||||
Journal = f"""\
|
||||
<!--
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<title>{Title}</title>
|
||||
<link rel="canonical" href="{URL}"/>
|
||||
{Redirect}
|
||||
</head>
|
||||
<body>
|
||||
--->
|
||||
<h1>{Title}</h1>
|
||||
<header id="Header">
|
||||
{Header}
|
||||
<div id="staticoso-LinkToFooter"><b>[<big><a href="#Footer"><span class="twa twa-⬇️"><span>⬇️</span></span> Footer</a></big>]</b></div>
|
||||
</header><br>
|
||||
{Journal}
|
||||
</article><br>
|
||||
<footer id="Footer">
|
||||
<div id="staticoso-LinkToHeader"><b>[<big><a href="#Header"><span class="twa twa-⬆️"><span>⬆️</span></span> Header</a></big>]</b></div>
|
||||
{t.attrs["journalfooter"] if "journalfooter" in t.attrs else ""}
|
||||
</footer>
|
||||
<!--
|
||||
</body>
|
||||
</html>
|
||||
--->
|
||||
"""
|
||||
return Journal
|
61
App/Source/Modules/Feed.py
Normal file
61
App/Source/Modules/Feed.py
Normal file
@@ -0,0 +1,61 @@
|
||||
""" ================================= |
|
||||
| This file is part of |
|
||||
| staticoso |
|
||||
| Just a simple Static Site Generator |
|
||||
| |
|
||||
| Licensed under the AGPLv3 license |
|
||||
| Copyright (C) 2022, OctoSpacc |
|
||||
| ================================= """
|
||||
|
||||
# TODO: Either switch feed generation lib, or rewrite the 'lxml' module, so that no modules have to be compiled and the program is 100% portable
|
||||
|
||||
from Libs.feedgen.feed import FeedGenerator
|
||||
from Modules.Utils import *
|
||||
|
||||
def MakeFeed(Flags, Pages, FullSite=False):
|
||||
CategoryFilter = Flags['FeedCategoryFilter']
|
||||
MaxEntries = Flags['FeedEntries']
|
||||
|
||||
Feed = FeedGenerator()
|
||||
Link = Flags['SiteDomain'] if Flags['SiteDomain'] else ' '
|
||||
Feed.id(Link)
|
||||
Feed.title(Flags['SiteName'] if Flags['SiteName'] else 'Untitled Site')
|
||||
Feed.link(href=Link, rel='alternate')
|
||||
Feed.description(Flags['SiteTagline'] if Flags['SiteTagline'] else ' ')
|
||||
if Flags['SiteDomain']:
|
||||
Feed.logo(Flags['SiteDomain'] + '/favicon.png')
|
||||
Feed.language(Flags['SiteLang'])
|
||||
|
||||
DoPages = []
|
||||
for e in Pages:
|
||||
if FullSite or (not FullSite and MaxEntries != 0 and e[3]['Type'] == 'Post' and e[3]['Feed'] == 'True'): # No entry limit if site feed
|
||||
DoPages += [e]
|
||||
MaxEntries -= 1
|
||||
DoPages.reverse()
|
||||
|
||||
for File, Content, Titles, Meta, ContentHTML, SlimHTML, Description, Image in DoPages:
|
||||
if FullSite or (not FullSite and Meta['Type'] == 'Post' and (not CategoryFilter or (CategoryFilter and (CategoryFilter in Meta['Categories'] or CategoryFilter == '*')))):
|
||||
Entry = Feed.add_entry()
|
||||
FileName = File.split('/')[-1]
|
||||
File = f"{StripExt(File)}.html"
|
||||
Content = ReadFile(f"{Flags['OutDir']}/{File}")
|
||||
Link = Flags['SiteDomain'] + '/' + File if Flags['SiteDomain'] else ' '
|
||||
CreatedOn = GetFullDate(Meta['CreatedOn'])
|
||||
EditedOn = GetFullDate(Meta['EditedOn'])
|
||||
Entry.id(Link)
|
||||
Title = Meta['Title'] if Meta['Title'] else Titles[0].lstrip('#') if Titles else FileName
|
||||
Entry.title(Title.lstrip().rstrip())
|
||||
Entry.description(Description)
|
||||
Entry.link(href=Link, rel='alternate')
|
||||
if not FullSite: # Avoid making an enormous site feed file...
|
||||
Entry.content(ContentHTML, type='html')
|
||||
if CreatedOn:
|
||||
Entry.pubDate(CreatedOn)
|
||||
EditedOn = EditedOn if EditedOn else CreatedOn if CreatedOn and not EditedOn else '1970-01-01T00:00+00:00'
|
||||
Entry.updated(EditedOn)
|
||||
|
||||
if not os.path.exists(f"{Flags['OutDir']}/feed"):
|
||||
os.mkdir(f"{Flags['OutDir']}/feed")
|
||||
FeedType = 'site.' if FullSite else ''
|
||||
Feed.atom_file(f"{Flags['OutDir']}/feed/{FeedType}atom.xml", pretty=(not Flags['MinifyOutput']))
|
||||
Feed.rss_file(f"{Flags['OutDir']}/feed/{FeedType}rss.xml", pretty=(not Flags['MinifyOutput']))
|
59
App/Source/Modules/Gemini.py
Normal file
59
App/Source/Modules/Gemini.py
Normal file
@@ -0,0 +1,59 @@
|
||||
""" ================================= |
|
||||
| This file is part of |
|
||||
| staticoso |
|
||||
| Just a simple Static Site Generator |
|
||||
| |
|
||||
| Licensed under the AGPLv3 license |
|
||||
| Copyright (C) 2022, OctoSpacc |
|
||||
| ================================= """
|
||||
|
||||
# TODO: Write the Python HTML2Gemtext converter
|
||||
|
||||
from Libs.bs4 import BeautifulSoup
|
||||
from Modules.HTML import *
|
||||
from Modules.Utils import *
|
||||
|
||||
def FixGemlogDateLine(Line):
|
||||
if len(Line) >= 2 and Line[0] == '[' and Line[1].isdigit():
|
||||
Line = Line[1:]
|
||||
else:
|
||||
Words = Line.split(' ')
|
||||
if len(Words) >= 2 and len(Words[1]) >= 2 and Words[1][0] == '[' and Words[1][1].isdigit():
|
||||
Line = Words[0] + '\n' + Words[1][1:] + ' ' + ' '.join(Words[2:])
|
||||
return Line
|
||||
|
||||
def GemtextCompileList(Flags, Pages, LimitFiles):
|
||||
Cmd = ''
|
||||
for File, Content, Titles, Meta, ContentHTML, SlimHTML, Description, Image in Pages:
|
||||
if IsLightRun(File, LimitFiles):
|
||||
continue
|
||||
Src = f"{Flags['OutDir']}.gmi/{StripExt(File)}.html.tmp"
|
||||
Dst = f"{Flags['OutDir']}.gmi/{StripExt(File)}.gmi"
|
||||
SlimHTML = StripAttrs(SlimHTML)
|
||||
for i in ('ol', 'ul', 'li'):
|
||||
for j in ('<'+i+'>', '</'+i+'>'):
|
||||
SlimHTML = SlimHTML.replace(j, '')
|
||||
WriteFile(Src, SlimHTML.replace('</a>', '</a><br>').replace('.html', '.gmi')) # TODO: Adjust links properly..
|
||||
Cmd += f'cat "{Src}" | html2gmi > "{Dst}"; '
|
||||
if Cmd:
|
||||
os.system(Cmd)
|
||||
for File, Content, Titles, Meta, ContentHTML, SlimHTML, Description, Image in Pages:
|
||||
if IsLightRun(File, LimitFiles):
|
||||
continue
|
||||
Dst = f"{Flags['OutDir']}.gmi/{StripExt(File)}.gmi"
|
||||
Gemtext = ''
|
||||
for Line in ReadFile(Dst).splitlines():
|
||||
Line = FixGemlogDateLine(Line)
|
||||
Gemtext += Line + '\n'
|
||||
WriteFile(Dst, Flags['GemtextHeader'] + Gemtext)
|
||||
|
||||
def FindEarliest(Str, Items):
|
||||
Pos, Item = 0, ''
|
||||
for Item in Items:
|
||||
Str.find(Item)
|
||||
return Pos, Item
|
||||
|
||||
def ParseTag(Content):
|
||||
print(Content)
|
||||
Parse = BeautifulSoup(str(Content), 'html.parser')
|
||||
Tag = Parse.find()
|
23
App/Source/Modules/Globals.py
Normal file
23
App/Source/Modules/Globals.py
Normal file
@@ -0,0 +1,23 @@
|
||||
""" ================================= |
|
||||
| This file is part of |
|
||||
| staticoso |
|
||||
| Just a simple Static Site Generator |
|
||||
| |
|
||||
| Licensed under the AGPLv3 license |
|
||||
| Copyright (C) 2022, OctoSpacc |
|
||||
| ================================= """
|
||||
|
||||
ReservedPaths = ('Site.ini', 'Assets', 'Pages', 'Posts', 'Templates', 'StaticParts', 'DynamicParts')
|
||||
FileExtensions = {
|
||||
'Pages': ('htm', 'html', 'markdown', 'md', 'pug', 'txt'),
|
||||
'HTML': ('.htm', '.html'),
|
||||
'Markdown': ('.markdown', '.md'),
|
||||
'Tmp': ('htm', 'markdown', 'md', 'pug', 'txt')}
|
||||
|
||||
PosStrBools = ('true', 'yes', 'on', '1', 'enabled')
|
||||
NegStrBools = ('false', 'no', 'off', '0', 'disabled')
|
||||
|
||||
PageIndexStrPos = tuple(list(PosStrBools) + ['all', 'listed', 'indexed', 'unlinked'])
|
||||
PageIndexStrNeg = tuple(list(NegStrBools) + ['none', 'unlisted', 'unindexed', 'hidden'])
|
||||
|
||||
InternalMacrosWraps = [['[', ']'], ['<', '>']]
|
100
App/Source/Modules/HTML.py
Normal file
100
App/Source/Modules/HTML.py
Normal file
@@ -0,0 +1,100 @@
|
||||
""" ================================= |
|
||||
| This file is part of |
|
||||
| staticoso |
|
||||
| Just a simple Static Site Generator |
|
||||
| |
|
||||
| Licensed under the AGPLv3 license |
|
||||
| Copyright (C) 2022, OctoSpacc |
|
||||
| ================================= """
|
||||
|
||||
import html
|
||||
import warnings
|
||||
from Libs import htmlmin
|
||||
from Libs.bs4 import BeautifulSoup
|
||||
from Modules.Utils import *
|
||||
|
||||
# Suppress useless bs4 warnings
|
||||
warnings.filterwarnings('ignore', message='The input looks more like a filename than markup.')
|
||||
|
||||
def MkSoup(HTML):
|
||||
return BeautifulSoup(HTML, 'html.parser')
|
||||
|
||||
def StripAttrs(HTML):
|
||||
Soup = MkSoup(HTML)
|
||||
Tags = Soup.find_all()
|
||||
for t in Tags:
|
||||
if 'href' not in t.attrs and 'src' not in t.attrs:
|
||||
t.attrs = {}
|
||||
return str(Soup)
|
||||
|
||||
def StripTags(HTML, ToStrip): # Remove desired tags from the HTML
|
||||
Soup = MkSoup(HTML)
|
||||
Tags = Soup.find_all()
|
||||
for t in Tags:
|
||||
if t.name in ToStrip:
|
||||
t.replace_with('')
|
||||
return str(Soup)
|
||||
|
||||
def DoHTMLFixPre(HTML):
|
||||
if not ("<pre>" in HTML or "<pre " in HTML):
|
||||
return HTML
|
||||
Soup = MkSoup(HTML)
|
||||
Tags = Soup.find_all('pre')
|
||||
for t in Tags:
|
||||
FirstLine = str(t).splitlines()[0].lstrip().rstrip()
|
||||
if FirstLine.endswith('>'):
|
||||
New = MkSoup(str(t).replace('\n', '', 1))
|
||||
t.replace_with(New.pre)
|
||||
return str(Soup)
|
||||
|
||||
def WriteImgAltAndTitle(HTML, AltToTitle, TitleToAlt): # Adds alt or title attr. to <img> which only have one of them
|
||||
Soup = MkSoup(HTML)
|
||||
Tags = Soup.find_all('img')
|
||||
for t in Tags:
|
||||
if AltToTitle and 'alt' in t.attrs and 'title' not in t.attrs:
|
||||
t.attrs.update({'title': t.attrs['alt']})
|
||||
elif TitleToAlt and 'title' in t.attrs and 'alt' not in t.attrs:
|
||||
t.attrs.update({'alt': t.attrs['title']})
|
||||
return str(Soup)
|
||||
|
||||
def AddToTagStartEnd(HTML, MatchStart, MatchEnd, AddStart, AddEnd): # This doesn't handle nested tags
|
||||
StartPos, DidStart, DidEnd = None, 0, 0
|
||||
for i,e in enumerate(HTML):
|
||||
FilterStart = HTML[i:i+len(MatchStart)]
|
||||
FilterEnd = HTML[i:i+len(MatchEnd)]
|
||||
if DidStart == 0 and FilterStart == MatchStart:
|
||||
StartPos = i
|
||||
if AddStart:
|
||||
HTML = HTML[:i] + AddStart + HTML[i:]
|
||||
DidStart = 2
|
||||
if DidEnd == 0 and FilterEnd == MatchEnd and StartPos and i > StartPos:
|
||||
StartPos = None
|
||||
if AddEnd:
|
||||
HTML = HTML[:i+len(MatchEnd)] + AddEnd + HTML[i+len(MatchEnd):]
|
||||
DidEnd = 2
|
||||
if DidStart > 0:
|
||||
DidStart -= 1
|
||||
if DidEnd > 0:
|
||||
DidEnd -= 1
|
||||
return HTML
|
||||
|
||||
def SquareFnrefs(HTML): # Different combinations of formatting for Soup .prettify, .encode, .decode break different page elements, don't use this for now
|
||||
Soup = MkSoup(HTML)
|
||||
Tags = Soup.find_all('sup')
|
||||
for t in Tags:
|
||||
if 'id' in t.attrs and t.attrs['id'].startswith('fnref:'):
|
||||
s = t.find('a')
|
||||
s.replace_with(f'[{t}]')
|
||||
return str(Soup.prettify(formatter=None))
|
||||
|
||||
def DoMinifyHTML(HTML, KeepComments):
|
||||
return htmlmin.minify(
|
||||
input=HTML,
|
||||
remove_comments=not KeepComments,
|
||||
remove_empty_space=True,
|
||||
remove_all_empty_space=False,
|
||||
reduce_empty_attributes=True,
|
||||
reduce_boolean_attributes=True,
|
||||
remove_optional_attribute_quotes=True,
|
||||
convert_charrefs=True,
|
||||
keep_pre=True)
|
36
App/Source/Modules/Logging.py
Normal file
36
App/Source/Modules/Logging.py
Normal file
@@ -0,0 +1,36 @@
|
||||
""" ================================= |
|
||||
| This file is part of |
|
||||
| staticoso |
|
||||
| Just a simple Static Site Generator |
|
||||
| |
|
||||
| Licensed under the AGPLv3 license |
|
||||
| Copyright (C) 2022, OctoSpacc |
|
||||
| ================================= """
|
||||
|
||||
import logging
|
||||
import sys
|
||||
from Modules.Config import *
|
||||
|
||||
LoggingFormat = '[%(levelname)s] %(message)s'
|
||||
LoggingLevels = {
|
||||
"Debug": {"Name":"D", "Num":15},
|
||||
"Info": {"Name":"I", "Num":20},
|
||||
"Warning": {"Name":"W", "Num":30},
|
||||
"Error": {"Name":"E", "Num":40}}
|
||||
|
||||
def SetupLogging(Level):
|
||||
logging.basicConfig(format=LoggingFormat, stream=sys.stdout, level=Level)
|
||||
logging.addLevelName(15, 'D') # Standard (10) Debug level makes third-party modules spam
|
||||
logging.addLevelName(20, 'I')
|
||||
logging.addLevelName(30, 'W')
|
||||
logging.addLevelName(40, 'E')
|
||||
|
||||
def ConfigLogging(Level):
|
||||
Num = DefConf['Logging']
|
||||
if type(Level) == str:
|
||||
if Level.isdecimal():
|
||||
Num = int(Level)
|
||||
else:
|
||||
if Level.lower() in LoggingLevels:
|
||||
Num = LoggingLevels['Level']['Num']
|
||||
SetupLogging(Num)
|
20
App/Source/Modules/Markdown.py
Normal file
20
App/Source/Modules/Markdown.py
Normal file
@@ -0,0 +1,20 @@
|
||||
""" ================================= |
|
||||
| This file is part of |
|
||||
| staticoso |
|
||||
| Just a simple Static Site Generator |
|
||||
| |
|
||||
| Licensed under the AGPLv3 license |
|
||||
| Copyright (C) 2022, OctoSpacc |
|
||||
| ================================= """
|
||||
|
||||
from Libs.markdown import markdown
|
||||
|
||||
MarkdownExtsDefault = ('attr_list', 'def_list', 'fenced_code', 'footnotes', 'md_in_html', 'tables')
|
||||
|
||||
def MarkdownHTMLEscape(Str, Extensions=()): # WIP
|
||||
Text = ''
|
||||
for i,e in enumerate(Str):
|
||||
if ('mdx_subscript' or 'markdown_del_ins') in Extensions and e == '~':
|
||||
e = '~'
|
||||
Text += e
|
||||
return Text
|
24
App/Source/Modules/Pug.py
Normal file
24
App/Source/Modules/Pug.py
Normal file
@@ -0,0 +1,24 @@
|
||||
""" ================================= |
|
||||
| This file is part of |
|
||||
| staticoso |
|
||||
| Just a simple Static Site Generator |
|
||||
| |
|
||||
| Licensed under the AGPLv3 license |
|
||||
| Copyright (C) 2022, OctoSpacc |
|
||||
| ================================= """
|
||||
|
||||
# TODO: Write a native Pug parser; There is one already available for Python but seems broken / out-of-date
|
||||
|
||||
import os
|
||||
from Modules.Utils import *
|
||||
|
||||
def PugCompileList(OutputDir, Pages, LimitFiles):
|
||||
# Pug-cli seems to shit itself with folder paths as input, so we pass ALL the files as arguments
|
||||
Paths = ''
|
||||
for File, Content, Titles, Meta in Pages:
|
||||
if File.lower().endswith('.pug') and (LimitFiles == False or File in LimitFiles):
|
||||
Path = f'{OutputDir}/{File}'
|
||||
WriteFile(Path, Content)
|
||||
Paths += f'"{Path}" '
|
||||
if Paths:
|
||||
os.system(f'pug -P {Paths} > /dev/null')
|
707
App/Source/Modules/Site.py
Normal file
707
App/Source/Modules/Site.py
Normal file
@@ -0,0 +1,707 @@
|
||||
""" ================================= |
|
||||
| This file is part of |
|
||||
| staticoso |
|
||||
| Just a simple Static Site Generator |
|
||||
| |
|
||||
| Licensed under the AGPLv3 license |
|
||||
| Copyright (C) 2022, OctoSpacc |
|
||||
| ================================= """
|
||||
|
||||
import shutil
|
||||
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.Globals import *
|
||||
from Modules.HTML import *
|
||||
from Modules.Logging import *
|
||||
from Modules.Markdown import *
|
||||
from Modules.Pug import *
|
||||
from Modules.Utils import *
|
||||
|
||||
# Menu styles:
|
||||
# - Simple: Default, Flat, Line
|
||||
# - Others: Excerpt, Image, Preview (Excerpt + Image), Full
|
||||
def GetHTMLPagesList(Pages, BlogName, SiteRoot, PathPrefix, CallbackFile=None, Unite=[], Type=None, Limit=None, PathFilter='', Category=None, For='Menu', MarkdownExts=(), MenuStyle='Default', ShowPaths=True):
|
||||
Flatten, SingleLine, DoneCount, PrevDepth = False, False, 0, 0
|
||||
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'].lower() in PageIndexStrNeg:
|
||||
IndexPages.remove(e)
|
||||
for i,e in enumerate(IndexPages):
|
||||
if Type and 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:
|
||||
# Allow for the virtual "Pages/" prefix to be used in path filtering
|
||||
TmpPathFilter = PathFilter
|
||||
if TmpPathFilter.startswith('Pages/'):
|
||||
TmpPathFilter = TmpPathFilter[len('Pages/'):]
|
||||
if File.startswith('Posts/'):
|
||||
continue
|
||||
|
||||
if (not Type or (Meta['Type'] == Type and CanIndex(Meta['Index'], For))) and (not Category or Category in Meta['Categories']) and File.startswith(TmpPathFilter) and File != CallbackFile and (not Limit or Limit > DoneCount):
|
||||
Depth = (File.count('/') + 1) if Meta['Order'] != 'Unite' else 1
|
||||
# Folder names are handled here
|
||||
if Depth > 1 and Meta['Order'] != 'Unite':
|
||||
CurParent = File.split('/')[:-1]
|
||||
for i,s in enumerate(CurParent):
|
||||
if LastParent != CurParent and ShowPaths:
|
||||
LastParent = CurParent
|
||||
Levels = '.' * ((Depth-2+i) if not Flatten else 0) + ':'
|
||||
# If search node endswith index, it's a page; else, it's a folder
|
||||
if StripExt(File).endswith('index'):
|
||||
Title = MakeListTitle(File, Meta, Titles, 'HTMLTitle', BlogName, PathPrefix)
|
||||
DoneCount += 1
|
||||
else:
|
||||
Title = CurParent[Depth-2+i]
|
||||
if SingleLine:
|
||||
List += f' <span>{Title}</span> '
|
||||
else:
|
||||
List += f'{Levels}<span>{Title}</span>\n'
|
||||
|
||||
# Pages with any other path
|
||||
if not (Depth > 1 and StripExt(File).split('/')[-1] == 'index'):
|
||||
Levels = '.' * ((Depth-1) if not Flatten else 0) + ':'
|
||||
DoneCount += 1
|
||||
if Meta['Order'] == 'Unite':
|
||||
Title = markdown(MarkdownHTMLEscape(File, MarkdownExts), extensions=MarkdownExts).removeprefix('<p>').removesuffix('<p>')
|
||||
else:
|
||||
Title = MakeListTitle(File, Meta, Titles, 'HTMLTitle', BlogName, PathPrefix)
|
||||
if SingleLine:
|
||||
List += ' <span>' + Title + '</span> '
|
||||
else:
|
||||
List += Levels + Title + '\n'
|
||||
|
||||
if MenuStyle in ('Default', 'Flat'):
|
||||
return GenHTMLTreeList(List, Class="staticoso-PagesList")
|
||||
elif MenuStyle in ('Line', 'Excerpt', 'Image', 'Preview', 'Full'):
|
||||
return List
|
||||
|
||||
def CheckHTMLCommentLine(Line):
|
||||
if Line.startswith('<!--'):
|
||||
Line = Line[4:].lstrip()
|
||||
if Line.endswith('-->'):
|
||||
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 FindPreprocLine(Line, Meta, Macros):
|
||||
Changed = False
|
||||
Line = Line.lstrip().rstrip()
|
||||
lll = CheckHTMLCommentLine(Line)
|
||||
if Line.startswith('//') or lll: # Find preprocessor lines
|
||||
lll = Line[2:].lstrip()
|
||||
if lll.startswith('%'):
|
||||
Meta += lll[1:].lstrip() + '\n'
|
||||
Changed = True
|
||||
elif lll.startswith('$'):
|
||||
Macros += lll[1:].lstrip() + '\n'
|
||||
Changed = True
|
||||
#if ll.startswith('<!--') and not ll.endswith('-->'): # Find comment and code blocks
|
||||
# IgnoreBlocksStart += [l]
|
||||
return (Meta, Macros, Changed)
|
||||
|
||||
def PagePreprocessor(Path:str, TempPath:str, Type, SiteTemplate, SiteRoot, GlobalMacros, CategoryUncategorized, LightRun=False):
|
||||
File = ReadFile(Path)
|
||||
Path = Path.lower()
|
||||
Content, Titles, DashyTitles, HTMLTitlesFound, Macros, Meta, MetaDefault = '', [], [], False, '', '', {
|
||||
'Template': SiteTemplate,
|
||||
'Head': '',
|
||||
'Style': '',
|
||||
'Type': Type,
|
||||
'Index': 'Unspecified',
|
||||
'Feed': 'True',
|
||||
'Title': '',
|
||||
'HTMLTitle': '',
|
||||
'Description': '',
|
||||
'Image': '',
|
||||
'Macros': {},
|
||||
'Categories': [],
|
||||
'URLs': [],
|
||||
'CreatedOn': '',
|
||||
'UpdatedOn': '',
|
||||
'EditedOn': '',
|
||||
'Order': None,
|
||||
'Language': None,
|
||||
'Downsync': None}
|
||||
# Find all positions of '<!--', '-->', add them in a list=[[pos0,pos1,line0,line1],...]
|
||||
for l in File.splitlines():
|
||||
ll = l.lstrip().rstrip()
|
||||
Meta, Macros, Changed = FindPreprocLine(ll, Meta, Macros)
|
||||
if not Changed: # Find headings
|
||||
#if line in ignore block:
|
||||
# continue
|
||||
Headings = ('h1', 'h2', 'h3', 'h4', 'h5', 'h6')
|
||||
#if Path.endswith(FileExtensions['HTML']):
|
||||
# if ll[1:].startswith(Headings):
|
||||
# if ll[3:].startswith((" class='NoTitle", ' class="NoTitle')):
|
||||
# Content += l + '\n'
|
||||
# elif ll.replace(' ', ' ').startswith('// %'):
|
||||
# pass
|
||||
# else:
|
||||
# Title = '#'*int(ll[2]) + ' ' + ll[4:]
|
||||
# DashTitle = DashifyTitle(Title.lstrip('#'), DashyTitles)
|
||||
# DashyTitles += [DashTitle]
|
||||
# Titles += [Title]
|
||||
# Content += MakeLinkableTitle(l, Title, DashTitle, 'pug') + '\n'
|
||||
# else:
|
||||
# Content += l + '\n'
|
||||
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'))
|
||||
HTMLTitlesFound = True
|
||||
Content = ''
|
||||
TmpContent = str(Soup.prettify(formatter=None))
|
||||
for cl in TmpContent.splitlines():
|
||||
_, _, IsMetaLine = FindPreprocLine(cl, Meta, Macros)
|
||||
if not IsMetaLine:
|
||||
#print(cl)
|
||||
Content += cl + '\n'
|
||||
break
|
||||
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 = '#'*int(ll[2]) + ' ' + ll[4:]
|
||||
DashTitle = DashifyTitle(MkSoup(Title.lstrip('#')).get_text(), DashyTitles)
|
||||
DashyTitles += [DashTitle]
|
||||
Titles += [Title]
|
||||
Title = MakeLinkableTitle(None, Title, DashTitle, 'md')
|
||||
# I can't remember why I put this but it was needed
|
||||
Title = Title.replace('> </', '> </').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]) + 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'].lower() in ('default', 'unspecified', 'categories'):
|
||||
if not Meta['Categories']:
|
||||
Meta['Categories'] = [CategoryUncategorized]
|
||||
if Meta['Type'].lower() == 'page':
|
||||
Meta['Index'] = 'Categories'
|
||||
elif Meta['Type'].lower() == '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:str, Meta:dict):
|
||||
for e in Meta['Macros']:
|
||||
Text = ReplWithEsc(Text, f"[: {e} :]", f"[:{e}:]")
|
||||
return Text
|
||||
|
||||
def OrderPages(Old:list):
|
||||
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:str, For:str):
|
||||
if Index.lower() in PageIndexStrNeg:
|
||||
return False
|
||||
elif Index.lower() in PageIndexStrPos:
|
||||
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 = MkSoup(Content)
|
||||
if not BodyDescription:# and Soup.p:
|
||||
#BodyDescription = Soup.p.get_text()[:150].replace('\n', ' ').replace('"', "'") + '...'
|
||||
for t in Soup.find_all('p'):
|
||||
if t.get_text():
|
||||
BodyDescription = t.get_text()[:150].replace('\n', ' ').replace('"', "'") + '...'
|
||||
break
|
||||
if not BodyImage and Soup.img and Soup.img['src']:
|
||||
BodyImage = Soup.img['src']
|
||||
|
||||
#Content = SquareFnrefs(Content)
|
||||
if '<a class="footnote-ref" ' in Content:
|
||||
Content = AddToTagStartEnd(Content, '<a class="footnote-ref" ', '</a>', '[', ']')
|
||||
|
||||
if any(_ in Content for _ in ('<!-- noprocess />', '<!--noprocess/>', '</ noprocess -->', '</ noprocess --->', '</noprocess-->', '</noprocess--->')):
|
||||
Content = DictReplWithEsc(
|
||||
Content, {
|
||||
'<!--<%noprocess>': '',
|
||||
'<noprocess%>-->': '',
|
||||
'<noprocess%>--->': '',
|
||||
'<!-- noprocess />': '',
|
||||
'<!--noprocess/>': '',
|
||||
'</ noprocess -->': '',
|
||||
'</ noprocess --->': '',
|
||||
'</noprocess-->': '',
|
||||
'</noprocess--->': ''})
|
||||
|
||||
Title = GetTitle(File.split('/')[-1], Meta, Titles, 'MetaTitle', BlogName)
|
||||
Description = GetDescription(Meta, BodyDescription, 'MetaDescription')
|
||||
Image = GetImage(Meta, BodyImage, 'MetaImage')
|
||||
ContentHeader = MakeContentHeader(Meta, Locale, MakeCategoryLine(File, Meta))
|
||||
TimeNow = datetime.now().strftime('%Y-%m-%d %H:%M')
|
||||
RelativeRoot = GetPathLevels(PagePath)
|
||||
|
||||
if 'staticoso:DynamicPart:' in HTML: # Reduce risk of unnecessary cycles
|
||||
for Line in HTML.splitlines():
|
||||
Line = Line.lstrip().rstrip()
|
||||
if (Line.startswith('[staticoso:DynamicPart:') and Line.endswith(']')) or (Line.startswith('<staticoso:DynamicPart:') and Line.endswith('>')):
|
||||
Path = Line[len('<staticoso:DynamicPart:'):-1]
|
||||
Section = Path.split('/')[-1]
|
||||
if Section in DynamicParts:
|
||||
Part = DynamicParts[Section]
|
||||
Text = ''
|
||||
if type(Part) == list:
|
||||
for e in Part:
|
||||
Text += DynamicPartsText[f"{Path}/{e}"] + '\n'
|
||||
elif type(Part) == str:
|
||||
Text = DynamicPartsText[f"{Path}/{Part}"]
|
||||
else:
|
||||
Text = ''
|
||||
HTML = ReplWithEsc(HTML, f"[staticoso:DynamicPart:{Path}]", Text)
|
||||
HTML = ReplWithEsc(HTML, f"<staticoso:DynamicPart:{Path}>", Text)
|
||||
|
||||
for i in range(2):
|
||||
for e in StaticPartsText:
|
||||
HTML = ReplWithEsc(HTML, f"[staticoso:StaticPart:{e}]", StaticPartsText[e])
|
||||
HTML = ReplWithEsc(HTML, f"<staticoso:StaticPart:{e}>", StaticPartsText[e])
|
||||
|
||||
if LightRun:
|
||||
HTML = None
|
||||
else:
|
||||
HTML = WrapDictReplWithEsc(HTML, {
|
||||
#'[staticoso:PageHead]': Meta['Head'],
|
||||
#'<staticoso:PageHead>': Meta['Head'],
|
||||
# #DEPRECATION #
|
||||
'staticoso:Site:Menu': HTMLPagesList,
|
||||
'staticoso:Page:Lang': Meta['Language'] if Meta['Language'] else SiteLang,
|
||||
'staticoso:Page:Chapters': HTMLTitles,
|
||||
'staticoso:Page:Title': Title,
|
||||
'staticoso:Page:Description': Description,
|
||||
'staticoso:Page:Image': Image,
|
||||
'staticoso:Page:Path': PagePath,
|
||||
'staticoso:Page:Style': Meta['Style'],
|
||||
################
|
||||
'staticoso:SiteMenu': HTMLPagesList,
|
||||
'staticoso:PageLang': Meta['Language'] if Meta['Language'] else SiteLang,
|
||||
'staticoso:PageLanguage': Meta['Language'] if Meta['Language'] else SiteLang,
|
||||
'staticoso:PageSections': HTMLTitles,
|
||||
'staticoso:PageTitle': Title,
|
||||
'staticoso:PageDescription': Description,
|
||||
'staticoso:PageImage': Image,
|
||||
'staticoso:PagePath': PagePath,
|
||||
'staticoso:PageHead': Meta['Head'],
|
||||
'staticoso:PageStyle': Meta['Style'],
|
||||
# NOTE: Content is injected in page only at this point! Keep in mind for other substitutions
|
||||
# #DEPRECATION #
|
||||
'staticoso:Page:Content': Content,
|
||||
'staticoso:Page:ContentInfo': ContentHeader,
|
||||
'staticoso:Site:Name': SiteName,
|
||||
'staticoso:Site:AbsoluteRoot': SiteRoot,
|
||||
'staticoso:Site:RelativeRoot': RelativeRoot,
|
||||
################
|
||||
'staticoso:PageContent': Content,
|
||||
'staticoso:PageContentInfo': ContentHeader,
|
||||
'staticoso:BuildTime': TimeNow,
|
||||
'staticoso:SiteDomain': SiteDomain,
|
||||
'staticoso:SiteName': SiteName,
|
||||
'staticoso:SiteAbsoluteRoot': SiteRoot,
|
||||
'staticoso:SiteRelativeRoot': RelativeRoot,
|
||||
}, InternalMacrosWraps)
|
||||
for e in Meta['Macros']:
|
||||
HTML = ReplWithEsc(HTML, f"[:{e}:]", Meta['Macros'][e])
|
||||
for e in FolderRoots:
|
||||
HTML = WrapDictReplWithEsc(HTML, {
|
||||
f'staticoso:CustomPath:{e}': FolderRoots[e],
|
||||
f'staticoso:Folder:{e}:AbsoluteRoot': FolderRoots[e], #DEPRECATED
|
||||
}, InternalMacrosWraps)
|
||||
for e in Categories:
|
||||
HTML = WrapDictReplWithEsc(HTML, {
|
||||
f'staticoso:Category:{e}': Categories[e],
|
||||
f'staticoso:CategoryList:{e}': Categories[e],
|
||||
}, InternalMacrosWraps)
|
||||
HTML = ReplWithEsc(HTML, f'<span>[staticoso:Category:{e}]</span>', Categories[e]) #DEPRECATED
|
||||
|
||||
# TODO: Clean this doubling?
|
||||
ContentHTML = Content
|
||||
ContentHTML = WrapDictReplWithEsc(ContentHTML, {
|
||||
# #DEPRECATION #
|
||||
'[staticoso:Page:Title]': Title,
|
||||
'[staticoso:Page:Description]': Description,
|
||||
'[staticoso:Site:Name]': SiteName,
|
||||
'[staticoso:Site:AbsoluteRoot]': SiteRoot,
|
||||
'[staticoso:Site:RelativeRoot]': RelativeRoot,
|
||||
################
|
||||
'<staticoso:PageTitle>': Title,
|
||||
'<staticoso:PageDescription>': Description,
|
||||
'<staticoso:SiteDomain>': SiteDomain,
|
||||
'<staticoso:SiteName>': SiteName,
|
||||
'<staticoso:SiteAbsoluteRoot>': SiteRoot,
|
||||
'<staticoso:SiteRelativeRoot>': RelativeRoot,
|
||||
}, InternalMacrosWraps)
|
||||
for e in Meta['Macros']:
|
||||
ContentHTML = ReplWithEsc(ContentHTML, f"[:{e}:]", Meta['Macros'][e])
|
||||
for e in FolderRoots:
|
||||
ContentHTML = WrapDictReplWithEsc(ContentHTML, {
|
||||
f'staticoso:CustomPath:{e}': FolderRoots[e],
|
||||
f'staticoso:Folder:{e}:AbsoluteRoot': FolderRoots[e], #DEPRECATED
|
||||
}, InternalMacrosWraps)
|
||||
for e in Categories:
|
||||
ContentHTML = WrapDictReplWithEsc(ContentHTML, {
|
||||
f'staticoso:Category:{e}': Categories[e],
|
||||
f'staticoso:CategoryList:{e}': Categories[e],
|
||||
}, InternalMacrosWraps)
|
||||
ContentHTML = ReplWithEsc(ContentHTML, f'<span>[staticoso:Category:{e}]</span>', Categories[e]) #DEPRECATED
|
||||
|
||||
return HTML, ContentHTML, Description, Image
|
||||
|
||||
def HandlePage(Flags, Page, Pages, Categories, LimitFiles, Snippets, ConfMenu, Locale):
|
||||
File, Content, Titles, Meta = Page
|
||||
OutDir, MarkdownExts, Sorting, MinifyKeepComments = Flags['OutDir'], Flags['MarkdownExts'], Flags['Sorting'], Flags['MinifyKeepComments']
|
||||
SiteName, BlogName, SiteTagline = Flags['SiteName'], Flags['BlogName'], Flags['SiteTagline']
|
||||
SiteTemplate, SiteLang = Flags['SiteTemplate'], Flags['SiteLang']
|
||||
SiteDomain, SiteRoot, FolderRoots = Flags['SiteDomain'], Flags['SiteRoot'], Flags['FolderRoots']
|
||||
AutoCategories, CategoryUncategorized = Flags['CategoriesAutomatic'], Flags['CategoriesUncategorized']
|
||||
ImgAltToTitle, ImgTitleToAlt = Flags['ImgAltToTitle'], Flags['ImgTitleToAlt']
|
||||
DynamicParts, DynamicPartsText, StaticPartsText, TemplatesText = Flags['DynamicParts'], Snippets['DynamicParts'], Snippets['StaticParts'], Snippets['Templates']
|
||||
|
||||
FileLower = File.lower()
|
||||
PagePath = f"{OutDir}/{StripExt(File)}.html"
|
||||
LightRun = False if LimitFiles == False or File in LimitFiles else True
|
||||
|
||||
if FileLower.endswith(FileExtensions['Markdown']):
|
||||
Content = markdown(PagePostprocessor('md', Content, Meta), extensions=MarkdownExts)
|
||||
elif FileLower.endswith(('.pug')):
|
||||
Content = PagePostprocessor('pug', ReadFile(PagePath), Meta)
|
||||
elif FileLower.endswith(('.txt')):
|
||||
Content = '<pre>' + html.escape(Content) + '</pre>'
|
||||
elif FileLower.endswith(FileExtensions['HTML']):
|
||||
Content = ReadFile(PagePath)
|
||||
|
||||
if LightRun:
|
||||
HTMLPagesList = None
|
||||
else:
|
||||
TemplateMeta = TemplatePreprocessor(TemplatesText[Meta['Template']])
|
||||
HTMLPagesList = GetHTMLPagesList(
|
||||
Pages=Pages,
|
||||
BlogName=BlogName,
|
||||
SiteRoot=SiteRoot,
|
||||
PathPrefix=GetPathLevels(File),
|
||||
Unite=ConfMenu,
|
||||
Type='Page',
|
||||
For='Menu',
|
||||
MarkdownExts=MarkdownExts,
|
||||
MenuStyle=TemplateMeta['MenuStyle'])
|
||||
|
||||
HTML, ContentHTML, Description, Image = PatchHTML(
|
||||
File=File,
|
||||
HTML=TemplatesText[Meta['Template']],
|
||||
StaticPartsText=StaticPartsText,
|
||||
DynamicParts=DynamicParts,
|
||||
DynamicPartsText=DynamicPartsText,
|
||||
HTMLPagesList=HTMLPagesList,
|
||||
PagePath=PagePath[len(f"{OutDir}/"):],
|
||||
Content=Content,
|
||||
Titles=Titles,
|
||||
Meta=Meta,
|
||||
SiteDomain=SiteDomain,
|
||||
SiteRoot=SiteRoot,
|
||||
SiteName=SiteName,
|
||||
BlogName=BlogName,
|
||||
FolderRoots=FolderRoots,
|
||||
Categories=Categories,
|
||||
SiteLang=SiteLang,
|
||||
Locale=Locale,
|
||||
LightRun=LightRun)
|
||||
|
||||
HTML = ReplWithEsc(HTML, f"<staticoso:Feed>", GetHTMLPagesList(
|
||||
Limit=Flags['FeedEntries'],
|
||||
Type='Post',
|
||||
Category=None if Flags['FeedCategoryFilter'] == '*' else Flags['FeedCategoryFilter'],
|
||||
Pages=Pages,
|
||||
BlogName=BlogName,
|
||||
SiteRoot=SiteRoot,
|
||||
PathPrefix=GetPathLevels(File),
|
||||
For='Categories',
|
||||
MarkdownExts=MarkdownExts,
|
||||
MenuStyle='Flat',
|
||||
ShowPaths=False))
|
||||
if 'staticoso:DirectoryList:' in HTML: # Reduce risk of unnecessary cycles
|
||||
for Line in HTML.splitlines():
|
||||
Line = Line.lstrip().rstrip()
|
||||
if Line.startswith('<staticoso:DirectoryList:') and Line.endswith('>'):
|
||||
Path = Line[len('<staticoso:DirectoryList:'):-1]
|
||||
DirectoryList = GetHTMLPagesList(
|
||||
CallbackFile=File,
|
||||
Pages=Pages,
|
||||
BlogName=BlogName,
|
||||
SiteRoot=SiteRoot,
|
||||
PathPrefix=GetPathLevels(File),
|
||||
PathFilter=Path,
|
||||
For='Categories',
|
||||
MarkdownExts=MarkdownExts,
|
||||
MenuStyle='Flat')
|
||||
HTML = ReplWithEsc(HTML, f"<staticoso:DirectoryList:{Path}>", DirectoryList)
|
||||
|
||||
if Flags['MinifyOutput']:
|
||||
if not LightRun:
|
||||
HTML = DoMinifyHTML(HTML, MinifyKeepComments)
|
||||
ContentHTML = DoMinifyHTML(ContentHTML, MinifyKeepComments)
|
||||
if Flags['NoScripts'] and ('<script' in ContentHTML.lower() or '<script' in HTML.lower()):
|
||||
if not LightRun:
|
||||
HTML = StripTags(HTML, ['script'])
|
||||
ContentHTML = StripTags(ContentHTML, ['script'])
|
||||
if ImgAltToTitle or ImgTitleToAlt:
|
||||
if not LightRun:
|
||||
HTML = WriteImgAltAndTitle(HTML, ImgAltToTitle, ImgTitleToAlt)
|
||||
ContentHTML = WriteImgAltAndTitle(ContentHTML, ImgAltToTitle, ImgTitleToAlt)
|
||||
if Flags['HTMLFixPre']:
|
||||
if not LightRun:
|
||||
HTML = DoHTMLFixPre(HTML)
|
||||
ContentHTML = DoHTMLFixPre(ContentHTML)
|
||||
|
||||
if LightRun:
|
||||
SlimHTML = None
|
||||
else:
|
||||
SlimHTML = HTMLPagesList + ContentHTML
|
||||
if not LightRun:
|
||||
WriteFile(PagePath, HTML)
|
||||
|
||||
if not LightRun and 'htmljournal' in ContentHTML.lower(): # Avoid extra cycles
|
||||
HTML, _, _, _ = PatchHTML(
|
||||
File=File,
|
||||
HTML=TemplatesText[Meta['Template']],
|
||||
StaticPartsText=StaticPartsText,
|
||||
DynamicParts=DynamicParts,
|
||||
DynamicPartsText=DynamicPartsText,
|
||||
HTMLPagesList=HTMLPagesList,
|
||||
PagePath=f'{StripExt(File)}.Journal.html',
|
||||
Content=MakeHTMLJournal(Flags, Locale, f'{StripExt(File)}.html', ContentHTML),
|
||||
Titles='',
|
||||
Meta=Meta,
|
||||
SiteDomain=SiteDomain,
|
||||
SiteRoot=SiteRoot,
|
||||
SiteName=SiteName,
|
||||
BlogName=BlogName,
|
||||
FolderRoots=FolderRoots,
|
||||
Categories=Categories,
|
||||
SiteLang=SiteLang,
|
||||
Locale=Locale,
|
||||
LightRun=LightRun)
|
||||
if Flags["JournalRedirect"]:
|
||||
HTML = HTML.replace('</head>', f"""<meta http-equiv="refresh" content="0; url='./{PagePath.split('''/''')[-1]}'"></head>""")
|
||||
WriteFile(StripExt(PagePath)+'.Journal.html', HTML)
|
||||
|
||||
return [File, Content, Titles, Meta, ContentHTML, SlimHTML, Description, Image]
|
||||
|
||||
def MultiprocPagePreprocessor(d):
|
||||
PrintProcPercentDots(d['Process'], 2)
|
||||
return PagePreprocessor(d['Path'], d['TempPath'], d['Type'], d['Template'], d['SiteRoot'], d['GlobalMacros'], d['CategoryUncategorized'], d['LightRun'])
|
||||
|
||||
def MultiprocHandlePage(d):
|
||||
PrintProcPercentDots(d['Process'])
|
||||
return HandlePage(d['Flags'], d['Page'], d['Pages'], d['Categories'], d['LimitFiles'], d['Snippets'], d['ConfMenu'], d['Locale'])
|
||||
|
||||
def MakeSite(Flags, LimitFiles, Snippets, ConfMenu, GlobalMacros, Locale, Threads):
|
||||
PagesPaths, PostsPaths, Pages, MadePages, Categories = [], [], [], [], {}
|
||||
PoolSize = cpu_count() if Threads <= 0 else Threads
|
||||
OutDir, MarkdownExts, Sorting = Flags['OutDir'], Flags['MarkdownExts'], Flags['Sorting']
|
||||
SiteName, BlogName, SiteTagline = Flags['SiteName'], Flags['BlogName'], Flags['SiteTagline']
|
||||
SiteTemplate, SiteLang = Flags['SiteTemplate'], Flags['SiteLang']
|
||||
SiteDomain, SiteRoot, FolderRoots = Flags['SiteDomain'], Flags['SiteRoot'], Flags['FolderRoots']
|
||||
AutoCategories, CategoryUncategorized = Flags['CategoriesAutomatic'], Flags['CategoriesUncategorized']
|
||||
ImgAltToTitle, ImgTitleToAlt = Flags['ImgAltToTitle'], Flags['ImgTitleToAlt']
|
||||
DynamicParts, DynamicPartsText, StaticPartsText, TemplatesText = Flags['DynamicParts'], Snippets['DynamicParts'], Snippets['StaticParts'], Snippets['Templates']
|
||||
|
||||
for Ext in FileExtensions['Pages']:
|
||||
for File in Path('Pages').rglob(f"*.{Ext}"):
|
||||
PagesPaths += [FileToStr(File, 'Pages/')]
|
||||
for File in Path('Posts').rglob(f"*.{Ext}"):
|
||||
PostsPaths += [FileToStr(File, 'Posts/')]
|
||||
logging.info(f"Pages Found: {len(PagesPaths+PostsPaths)}")
|
||||
|
||||
PagesPaths = FileNameDateSort(PagesPaths)
|
||||
if Sorting['Pages'] == 'Inverse':
|
||||
PagesPaths.reverse()
|
||||
PostsPaths = FileNameDateSort(PostsPaths)
|
||||
if Sorting['Posts'] == 'Inverse':
|
||||
PostsPaths.reverse()
|
||||
|
||||
logging.info("Preprocessing Source Pages")
|
||||
MultiprocPages = []
|
||||
for Type in ['Page', 'Post']:
|
||||
if Type == 'Page':
|
||||
Files = PagesPaths
|
||||
PathPrefix = ''
|
||||
elif Type == 'Post':
|
||||
Files = PostsPaths
|
||||
PathPrefix = 'Posts/'
|
||||
for i,File in enumerate(Files):
|
||||
TempPath = f"{PathPrefix}{File}"
|
||||
LightRun = False if LimitFiles == False or TempPath in LimitFiles else True
|
||||
MultiprocPages += [{'Process':{'Num':i, 'Count':len(Files)}, 'Path':f"{Type}s/{File}", 'TempPath':TempPath, 'Type':Type, 'Template':SiteTemplate, 'SiteRoot':SiteRoot, 'GlobalMacros':GlobalMacros, 'CategoryUncategorized':CategoryUncategorized, 'LightRun':LightRun}]
|
||||
os.system('printf "["')
|
||||
with Pool(PoolSize) as MultiprocPool:
|
||||
Pages = MultiprocPool.map(MultiprocPagePreprocessor, MultiprocPages)
|
||||
os.system('printf "]\n"') # Make newline after percentage dots
|
||||
|
||||
for File, Content, Titles, Meta in Pages:
|
||||
for Cat in Meta['Categories']:
|
||||
Categories.update({Cat:''})
|
||||
PugCompileList(OutDir, Pages, LimitFiles)
|
||||
|
||||
if Categories:
|
||||
logging.info("Generating Category Lists")
|
||||
for Cat in Categories:
|
||||
for Type in ('Page', 'Post'):
|
||||
Categories[Cat] += GetHTMLPagesList(
|
||||
Pages=Pages,
|
||||
BlogName=BlogName,
|
||||
SiteRoot=SiteRoot,
|
||||
PathPrefix=GetPathLevels('Categories/'),
|
||||
Type=Type,
|
||||
Category=Cat,
|
||||
For='Categories',
|
||||
MarkdownExts=MarkdownExts,
|
||||
MenuStyle='Flat')
|
||||
|
||||
if AutoCategories:
|
||||
Dir = f"{OutDir}/Categories"
|
||||
for Cat in Categories:
|
||||
Exists = False
|
||||
for File in Path(Dir).rglob(str(Cat)+'.*'):
|
||||
Exists = True
|
||||
break
|
||||
if not Exists:
|
||||
File = f"Categories/{Cat}.md"
|
||||
FilePath = f"{OutDir}/{File}"
|
||||
WriteFile(FilePath, CategoryPageTemplate.format(Name=Cat))
|
||||
_, Content, Titles, Meta = PagePreprocessor(FilePath, FilePath, Type, SiteTemplate, SiteRoot, GlobalMacros, CategoryUncategorized, LightRun=LightRun)
|
||||
Pages += [[File, Content, Titles, Meta]]
|
||||
|
||||
for i,e in enumerate(ConfMenu):
|
||||
for File, Content, Titles, Meta in Pages:
|
||||
File = StripExt(File)+'.html'
|
||||
if e == File:
|
||||
ConfMenu[i] = None
|
||||
|
||||
logging.info("Writing Pages")
|
||||
MultiprocPages = []
|
||||
for i,Page in enumerate(Pages):
|
||||
MultiprocPages += [{'Process':{'Num':i, 'Count':len(Pages)}, 'Flags':Flags, 'Page':Page, 'Pages':Pages, 'Categories':Categories, 'LimitFiles':LimitFiles, 'Snippets':Snippets, 'ConfMenu':ConfMenu, 'Locale':Locale}]
|
||||
os.system('printf "["')
|
||||
with Pool(PoolSize) as MultiprocPool:
|
||||
MadePages = MultiprocPool.map(MultiprocHandlePage, MultiprocPages)
|
||||
os.system('printf "]\n"') # Make newline after percentage dots
|
||||
|
||||
# Do page transclusions here (?)
|
||||
#while True:
|
||||
# Operated = False
|
||||
# for di,Dest in enumerate(MadePages):
|
||||
# #print(Dest[0])
|
||||
# #TempPath = f'{PathPrefix}{Dest["File"]}'
|
||||
# #LightRun = False if LimitFiles == False or TempPath in LimitFiles else True
|
||||
# #if not LightRun:
|
||||
# if '[staticoso:Transclude:' in Dest[4] and (LimitFiles == False or f'{PathPrefix}{Dest[0]}' in LimitFiles):
|
||||
# for Item in MadePages:
|
||||
# SrcPrefix = '' if Item[0].startswith('Posts/') else 'Pages/'
|
||||
# print(SrcPrefix, Item[0])
|
||||
# if Item[0] != Dest[0] and f'[staticoso:Transclude:{SrcPrefix}{Item[0]}]' in Dest[4]:
|
||||
# MadePages[di][4] = ReplWithEsc(Dest[4], f'<staticoso:Transclude:{Item[0]}>', Item[4])
|
||||
# print(f'[staticoso:Transclude:{SrcPrefix}{Item[0]}]', Item[4])
|
||||
# Operated = True
|
||||
# if not Operated:
|
||||
# break
|
||||
|
||||
return MadePages
|
19
App/Source/Modules/Sitemap.py
Normal file
19
App/Source/Modules/Sitemap.py
Normal file
@@ -0,0 +1,19 @@
|
||||
""" ================================= |
|
||||
| This file is part of |
|
||||
| staticoso |
|
||||
| Just a simple Static Site Generator |
|
||||
| |
|
||||
| Licensed under the AGPLv3 license |
|
||||
| Copyright (C) 2022, OctoSpacc |
|
||||
| ================================= """
|
||||
|
||||
from urllib.parse import quote as URLEncode
|
||||
from Modules.Utils import *
|
||||
|
||||
def MakeSitemap(Flags, Pages):
|
||||
Map = ''
|
||||
Domain = Flags['SiteDomain'] + '/' if Flags['SiteDomain'] else ''
|
||||
for File, Content, Titles, Meta, ContentHTML, SlimHTML, Description, Image in Pages:
|
||||
File = f"{StripExt(File)}.html"
|
||||
Map += Domain + URLEncode(File) + '\n'
|
||||
WriteFile(f"{Flags['OutDir']}/sitemap.txt", Map)
|
177
App/Source/Modules/Utils.py
Normal file
177
App/Source/Modules/Utils.py
Normal file
@@ -0,0 +1,177 @@
|
||||
|
||||
""" ================================= |
|
||||
| This file is part of |
|
||||
| staticoso |
|
||||
| Just a simple Static Site Generator |
|
||||
| |
|
||||
| Licensed under the AGPLv3 license |
|
||||
| Copyright (C) 2022, OctoSpacc |
|
||||
| ================================= """
|
||||
|
||||
import json
|
||||
import os
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from Modules.Globals import *
|
||||
|
||||
def SureList(e):
|
||||
return e if type(e) == list else [e]
|
||||
|
||||
# Get base directory path of the staticoso program
|
||||
def staticosoBaseDir():
|
||||
return f"{os.path.dirname(os.path.abspath(__file__))}/../../"
|
||||
|
||||
def ReadFile(p, m='r'):
|
||||
try:
|
||||
with open(p, m) as f:
|
||||
return f.read()
|
||||
except Exception:
|
||||
logging.error(f"Error reading file {p}")
|
||||
return None
|
||||
|
||||
def WriteFile(p, c, m='w'):
|
||||
try:
|
||||
with open(p, m) as f:
|
||||
return f.write(c)
|
||||
except Exception:
|
||||
logging.error(f"[E] Error writing file {p}")
|
||||
return False
|
||||
|
||||
def FileToStr(File, Truncate=''):
|
||||
return str(File)[len(Truncate):]
|
||||
|
||||
# With shutil.copytree copy only folder struct, no files; https://stackoverflow.com/a/15664273
|
||||
def IgnoreFiles(Dir, Files):
|
||||
return [f for f in Files if os.path.isfile(os.path.join(Dir, f))]
|
||||
|
||||
def LoadFromDir(Dir, Matchs):
|
||||
Contents = {}
|
||||
Matchs = SureList(Matchs)
|
||||
for Match in Matchs:
|
||||
for File in Path(Dir).rglob(Match):
|
||||
File = str(File)[len(Dir)+1:]
|
||||
Contents.update({File: ReadFile(f"{Dir}/{File}")})
|
||||
return Contents
|
||||
|
||||
def mkdirps(Dir):
|
||||
return Path(Dir).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def StripExt(Path):
|
||||
return ".".join(Path.split('.')[:-1])
|
||||
|
||||
def UndupeStr(Str, Known, Split):
|
||||
while Str in Known:
|
||||
Sections = Str.split(Split)
|
||||
try:
|
||||
Sections[-1] = str(int(Sections[-1]) + 1)
|
||||
except ValueError:
|
||||
Sections[-1] = Sections[-1] + str(Split) + '2'
|
||||
Str = Split.join(Sections)
|
||||
return Str
|
||||
|
||||
def DashifyStr(s, Limit=32):
|
||||
Str = ''
|
||||
for c in s[:Limit].replace('\n','-').replace('\t','-').replace(' ','-'):
|
||||
if c.lower() in '0123456789qwfpbjluyarstgmneiozxcdvkh-':
|
||||
Str += c
|
||||
return '-' + Str
|
||||
|
||||
def GetPathLevels(Path, AsNum=False, Add=0, Sub=0):
|
||||
n = Path.count('/') + Add - Sub
|
||||
return n if AsNum else '../' * n
|
||||
|
||||
# https://stackoverflow.com/a/34445090
|
||||
def FindAllIndex(Str, Sub):
|
||||
i = Str.find(Sub)
|
||||
while i != -1:
|
||||
yield i
|
||||
i = Str.find(Sub, i+1)
|
||||
|
||||
# Replace substrings in a string, except when an escape char is prepended
|
||||
def ReplWithEsc(Str, Find, Repl, Esc='\\'):
|
||||
New = ''
|
||||
Sects = Str.split(Find)
|
||||
for i,e in enumerate(Sects):
|
||||
if i == 0:
|
||||
New += e
|
||||
elif i > 0:
|
||||
if Sects[i-1].endswith(Esc*2):
|
||||
New = New[:-1]
|
||||
New += Repl + e
|
||||
elif Sects[i-1].endswith(Esc):
|
||||
New = New[:-1]
|
||||
New += Find + e
|
||||
else:
|
||||
New += Repl + e
|
||||
return New
|
||||
|
||||
def DictReplWithEsc(Str:str, Dict:dict, Esc:str='\\'):
|
||||
for Item in Dict:
|
||||
Str = ReplWithEsc(Str, Item, Dict[Item], Esc='\\')
|
||||
return Str
|
||||
|
||||
def WrapDictReplWithEsc(Str:str, Dict:dict, Wraps:list=[], Esc:str='\\'):
|
||||
NewDict = {}
|
||||
for Item in Dict:
|
||||
for Wrap in Wraps:
|
||||
NewDict.update({f'{Wrap[0]}{Item}{Wrap[1]}': Dict[Item]})
|
||||
return DictReplWithEsc(Str, NewDict, Esc)
|
||||
|
||||
def NumsFromFileName(Path):
|
||||
Name = Path.split('/')[-1]
|
||||
Split = len(Name)
|
||||
for i,e in enumerate(Name):
|
||||
if e.lower() in 'qwfpbjluyarstgmneiozxcdvkh':
|
||||
return Name[:i]
|
||||
return Path
|
||||
|
||||
def RevSort(List):
|
||||
List.sort()
|
||||
List.reverse()
|
||||
return List
|
||||
|
||||
def FileNameDateSort(Old): # TODO: Test this for files not starting with date, and dated folders
|
||||
New = []
|
||||
if Old:
|
||||
Old.sort()
|
||||
New.insert(0, Old[0])
|
||||
for i,e in enumerate(Old):
|
||||
if i == 0:
|
||||
continue
|
||||
Done = False
|
||||
for j,f in enumerate(New):
|
||||
if NumsFromFileName(e) != e and NumsFromFileName(f) != f and NumsFromFileName(e) < NumsFromFileName(f):
|
||||
New.insert(j, e)
|
||||
Done = True
|
||||
break
|
||||
if not Done:
|
||||
New += [e]
|
||||
return New
|
||||
|
||||
def FirstRealItem(List):
|
||||
return next(e for e in List if e)
|
||||
|
||||
def GetFullDate(Date):
|
||||
if not Date:
|
||||
return None
|
||||
return datetime.strftime(datetime.strptime(Date, '%Y-%m-%d'), '%Y-%m-%dT%H:%M+00:00')
|
||||
|
||||
def LoadLocale(Lang):
|
||||
Lang = Lang + '.json'
|
||||
Folder = f'{staticosoBaseDir()}Locale/'
|
||||
File = ReadFile(Folder + Lang)
|
||||
if File:
|
||||
return json.loads(File)
|
||||
else:
|
||||
return json.loads(ReadFile(Folder + 'en.json'))
|
||||
|
||||
def IsLightRun(File, LimitFiles):
|
||||
return False if LimitFiles == False or File in LimitFiles else True
|
||||
|
||||
def PrintProcPercentDots(Proc, DivMult=1):
|
||||
Div = 5 * DivMult # 100/5 = 20 chars
|
||||
Num, Count = Proc['Num'], Proc['Count']
|
||||
if int(((Num/Count)*100)/Div) != int((((Num+1)/Count)*100)/Div):
|
||||
os.system('printf "="') # Using sys shell since for some reason print() without newline breaks here (doesn't print everytime)
|
||||
return True
|
||||
return False
|
Reference in New Issue
Block a user