mirror of https://gitlab.com/octtspacc/staticoso
Misc fixes, Add progress bars, Add redirects
This commit is contained in:
parent
052c1d130a
commit
8ffad6c99c
|
@ -4,5 +4,7 @@
|
|||
"Categories": "Categories",
|
||||
"ReadFullPost": "Read the full post",
|
||||
"Comments": "Comments",
|
||||
"OpenInNewTab": "Open in a new tab"
|
||||
"OpenInNewTab": "Open in a new tab",
|
||||
"ClickHere": "Click here",
|
||||
"IfNotRedirected": "if you aren't automatically redirected"
|
||||
}
|
||||
|
|
|
@ -4,5 +4,7 @@
|
|||
"Categories": "Categorie",
|
||||
"ReadFullPost": "Leggi il post intero",
|
||||
"Comments": "Commenti",
|
||||
"OpenInNewTab": "Apri in una nuova scheda"
|
||||
"OpenInNewTab": "Apri in una nuova scheda",
|
||||
"ClickHere": "Clicca qui",
|
||||
"IfNotRedirected": "se non subisci il reindirizzamento automatico"
|
||||
}
|
||||
|
|
|
@ -104,10 +104,12 @@ def GetModifiedFiles(OutDir):
|
|||
return Mod
|
||||
|
||||
def Main(Args, FeedEntries):
|
||||
Flags, Snippets = {}, {}
|
||||
Flags, Snippets, FinalPaths = {}, {}, []
|
||||
HavePages, HavePosts = False, False
|
||||
SiteConf = LoadConfFile('Site.ini')
|
||||
|
||||
ConfigLogging(OptionChoose(None, Args.Logging, ReadConf(SiteConf, 'Main', 'Logging')))
|
||||
|
||||
#if Args.InputDir:
|
||||
# os.chdir(Args.InputDir)
|
||||
# print(f"[I] Current directory: {Args.InputDir}")
|
||||
|
@ -228,13 +230,29 @@ def Main(Args, FeedEntries):
|
|||
Post = ''
|
||||
for p in MastodonPosts:
|
||||
if p['Link'] == SiteDomain + '/' + File[len(f"{OutDir}/"):]:
|
||||
Post = '<br><h3>{StrComments}</h3><a href="{URL}" rel="noopener" target="_blank">{StrOpen} ↗️</a>'.format(
|
||||
Post = HTMLCommentsBlock.format(
|
||||
StrComments=Locale['Comments'],
|
||||
StrOpen=Locale['OpenInNewTab'],
|
||||
URL=p['Post'])
|
||||
break
|
||||
Content = ReplWithEsc(Content, '[staticoso:Comments]', Post)
|
||||
Content = ReplWithEsc(Content, '<staticoso:Comments>', Post)
|
||||
WriteFile(File, Content)
|
||||
FinalPaths += [File]
|
||||
|
||||
logging.info("Creating Redirects")
|
||||
for File, Content, Titles, Meta, ContentHTML, SlimHTML, Description, Image in Pages:
|
||||
for URL in Meta['URLs']:
|
||||
DestFile = f"{OutDir}/{URL}"
|
||||
if DestFile not in FinalPaths:
|
||||
DestURL = f"{GetPathLevels(URL)}{StripExt(File)}.html"
|
||||
WriteFile(
|
||||
DestFile,
|
||||
RedirectPageTemplate.format(
|
||||
DestURL=DestURL,
|
||||
TitlePrefix=f"{SiteName} - " if SiteName else '',
|
||||
StrClick=Locale["ClickHere"],
|
||||
StrRedirect=Locale["IfNotRedirected"]))
|
||||
|
||||
if Flags['GemtextOutput']:
|
||||
logging.info("Generating Gemtext")
|
||||
|
@ -302,4 +320,4 @@ if __name__ == '__main__':
|
|||
Main(
|
||||
Args=Args,
|
||||
FeedEntries=FeedEntries)
|
||||
logging.info(f"✅ Done! ({round(time.time()-StartTime,3)}s)")
|
||||
logging.info(f"✅ Done! ({round(time.time()-StartTime, 3)}s)")
|
||||
|
|
|
@ -19,8 +19,21 @@ CategoryPageTemplate = """\
|
|||
|
||||
# {Name}
|
||||
|
||||
<div>[staticoso:Category:{Name}]</div>
|
||||
<div><staticoso:Category:{Name}></div>
|
||||
"""
|
||||
RedirectPageTemplate = """\
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>{TitlePrefix}Redirect</title>
|
||||
<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} ↗️</a>'
|
||||
|
||||
def DashifyTitle(Title, Done=[]):
|
||||
return UndupeStr(DashifyStr(Title.lstrip(' ').rstrip(' ')), Done, '-')
|
||||
|
|
|
@ -107,7 +107,9 @@ def PagePreprocessor(Path, TempPath, Type, SiteTemplate, SiteRoot, GlobalMacros,
|
|||
'Image': '',
|
||||
'Macros': {},
|
||||
'Categories': [],
|
||||
'URLs': [],
|
||||
'CreatedOn': '',
|
||||
'UpdatedOn': '',
|
||||
'EditedOn': '',
|
||||
'Order': None}
|
||||
# Find all positions of '<!--', '-->', add them in a list=[[pos0,pos1,line0,line1],...]
|
||||
|
@ -182,13 +184,21 @@ def PagePreprocessor(Path, TempPath, Type, SiteTemplate, SiteRoot, GlobalMacros,
|
|||
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]
|
||||
|
@ -199,7 +209,6 @@ def PagePreprocessor(Path, TempPath, Type, SiteTemplate, SiteRoot, GlobalMacros,
|
|||
if GlobalMacros:
|
||||
Meta['Macros'].update(GlobalMacros)
|
||||
Meta['Macros'].update(ReadConf(LoadConfStr('[Macros]\n' + Macros), 'Macros'))
|
||||
#PrintPercentDots(ProcPercent)
|
||||
return [TempPath, Content, Titles, Meta]
|
||||
|
||||
def PagePostprocessor(FileType, Text, Meta):
|
||||
|
@ -434,15 +443,16 @@ def HandlePage(Flags, Page, Pages, Categories, LimitFiles, Snippets, ConfMenu, L
|
|||
if not LightRun:
|
||||
WriteFile(PagePath, HTML)
|
||||
|
||||
#PrintPercentDots(ProcPercent)
|
||||
return [File, Content, Titles, Meta, ContentHTML, SlimHTML, Description, Image]
|
||||
|
||||
def MultiprocHandlePage(d):
|
||||
return HandlePage(d['Flags'], d['Page'], d['Pages'], d['Categories'], d['LimitFiles'], d['Snippets'], d['ConfMenu'], d['Locale'])
|
||||
|
||||
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
|
||||
|
@ -476,13 +486,15 @@ def MakeSite(Flags, LimitFiles, Snippets, ConfMenu, GlobalMacros, Locale, Thread
|
|||
elif Type == 'Post':
|
||||
Files = PostsPaths
|
||||
PathPrefix = 'Posts/'
|
||||
for File in Files:
|
||||
for i,File in enumerate(Files):
|
||||
TempPath = f"{PathPrefix}{File}"
|
||||
LightRun = False if LimitFiles == False or TempPath in LimitFiles else True
|
||||
MultiprocPages += [{'Path':f"{Type}s/{File}", 'TempPath':TempPath, 'Type':Type, 'Template':SiteTemplate, 'SiteRoot':SiteRoot, 'GlobalMacros':GlobalMacros, 'CategoryUncategorized':CategoryUncategorized, 'LightRun':LightRun}]
|
||||
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)
|
||||
#print() # Make newline after percentage dots
|
||||
os.system('printf "]\n"') #print("]") # Make newline after percentage dots
|
||||
|
||||
for File, Content, Titles, Meta in Pages:
|
||||
for Cat in Meta['Categories']:
|
||||
Categories.update({Cat:''})
|
||||
|
@ -513,8 +525,8 @@ def MakeSite(Flags, LimitFiles, Snippets, ConfMenu, GlobalMacros, Locale, Thread
|
|||
if not Exists:
|
||||
File = f"Categories/{Cat}.md"
|
||||
FilePath = f"{OutDir}/{File}"
|
||||
WriteFile(FilePath, CategoryPageTemplate.format(Title=Cat))
|
||||
Content, Titles, Meta = PagePreprocessor(FilePath, 'Page', SiteTemplate, SiteRoot, GlobalMacros, CategoryUncategorized, LightRun=LightRun)
|
||||
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):
|
||||
|
@ -525,10 +537,11 @@ def MakeSite(Flags, LimitFiles, Snippets, ConfMenu, GlobalMacros, Locale, Thread
|
|||
|
||||
logging.info("Writing Pages")
|
||||
MultiprocPages = []
|
||||
for Page in Pages:
|
||||
MultiprocPages += [{'Flags':Flags, 'Page':Page, 'Pages':Pages, 'Categories':Categories, 'LimitFiles':LimitFiles, 'Snippets':Snippets, 'ConfMenu':ConfMenu, 'Locale':Locale}]
|
||||
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)
|
||||
#print() # Make newline after percentage dots
|
||||
os.system('printf "]\n"') #print("]") # Make newline after percentage dots
|
||||
|
||||
return MadePages
|
||||
|
|
|
@ -158,10 +158,8 @@ def LoadLocale(Lang):
|
|||
def IsLightRun(File, LimitFiles):
|
||||
return False if LimitFiles == False or File in LimitFiles else True
|
||||
|
||||
def PrintPercentDots(Percent):
|
||||
Cur, Tot = Percent
|
||||
CalcCur = int((Cur/Tot)*100)
|
||||
if CalcCur % 10 == 0:
|
||||
CalcPrev = int((Cur-1/Tot)*100)
|
||||
if CalcPrev % 10 != 0:
|
||||
print('.', end='')
|
||||
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 "="') # For some reason print() without newline breaks here (doesn't print everytime)
|
||||
|
|
Loading…
Reference in New Issue