Tweak pages list generation, add cover data-image attribute to links

This commit is contained in:
octospacc 2024-04-13 15:05:16 +02:00
parent 8e892e096d
commit 923e74954e
2 changed files with 17 additions and 13 deletions

View File

@ -57,12 +57,11 @@ def DashifyTitle(Title:str, Done:list=[]):
# :Item 4 // <li>Item 4</li> # :Item 4 // <li>Item 4</li>
def GenHTMLTreeList(MetaList:str, Type:str='ul', Class:str=""): def GenHTMLTreeList(MetaList:str, Type:str='ul', Class:str=""):
HTML = '' HTML = ''
Lines = MetaList.splitlines()
CurDepth, NextDepth, PrevDepth = 0, 0, 0 CurDepth, NextDepth, PrevDepth = 0, 0, 0
for i,e in enumerate(Lines): for i,item in enumerate(MetaList):
CurDepth = e.find(':') CurDepth = item['text'].find(':')
NextDepth = Lines[i+1].find(':') if i+1 < len(Lines) else 0 NextDepth = MetaList[i+1]['text'].find(':') if i+1 < len(MetaList) else 0
HTML += '\n<li>' + e[CurDepth+1:] HTML += f'\n<li {"attrs" in item and item["attrs"] or ""}>' + item['text'][CurDepth+1:]
if NextDepth == CurDepth: if NextDepth == CurDepth:
HTML += '</li>' HTML += '</li>'
elif NextDepth > CurDepth: elif NextDepth > CurDepth:
@ -144,14 +143,14 @@ def MakeListTitle(File:str, Meta:dict, Titles:list, Prefer:str, BlogName:str, Pa
def FormatTitles(Titles:list, Flatten=False): def FormatTitles(Titles:list, Flatten=False):
# TODO: Somehow titles written in Pug can end up here and don't work, they should be handled # TODO: Somehow titles written in Pug can end up here and don't work, they should be handled
List, DashyTitles = '', [] List, DashyTitles = [], []
for t in Titles: for t in Titles:
n = 0 if Flatten else t.split(' ')[0].count('#') n = 0 if Flatten else t.split(' ')[0].count('#')
Level = '.' * (n-1) + ':' Level = '.' * (n-1) + ':'
Title = MkSoup(t.lstrip('#')).get_text() Title = MkSoup(t.lstrip('#')).get_text()
DashyTitle = DashifyTitle(Title, DashyTitles) DashyTitle = DashifyTitle(Title, DashyTitles)
DashyTitles += [DashyTitle] DashyTitles += [DashyTitle]
List += f'{Level}<a href="#{DashyTitle}">{html.escape(Title)}</a>\n' List += [{ 'text': f'{Level}<a href="#{DashyTitle}">{html.escape(Title)}</a>\n' }]
return GenHTMLTreeList(List) return GenHTMLTreeList(List)
# Clean up a generic HTML tree such that it's compliant with the HTML Journal standard # Clean up a generic HTML tree such that it's compliant with the HTML Journal standard

View File

@ -25,7 +25,7 @@ def GetHTMLPagesList(Flags:dict, Pages:list, PathPrefix:str, CallbackFile=None,
Flatten = True Flatten = True
elif MenuStyle == 'Line': elif MenuStyle == 'Line':
ShowPaths, SingleLine = False, True ShowPaths, SingleLine = False, True
List, ToPop, LastParent = '', [], [] List, ToPop, LastParent = [], [], []
IndexPages = Pages.copy() IndexPages = Pages.copy()
for e in IndexPages: for e in IndexPages:
if e[3]['Index'].lower() in PageIndexStrNeg: if e[3]['Index'].lower() in PageIndexStrNeg:
@ -51,6 +51,7 @@ def GetHTMLPagesList(Flags:dict, Pages:list, PathPrefix:str, CallbackFile=None,
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): 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 Depth = (File.count('/') + 1) if Meta['Order'] != 'Unite' else 1
# Folder names are handled here # Folder names are handled here
if Depth > 1 and Meta['Order'] != 'Unite': if Depth > 1 and Meta['Order'] != 'Unite':
CurParent = File.split('/')[:-1] CurParent = File.split('/')[:-1]
@ -66,12 +67,13 @@ def GetHTMLPagesList(Flags:dict, Pages:list, PathPrefix:str, CallbackFile=None,
else: else:
Title = CurParent[Depth-2+i] Title = CurParent[Depth-2+i]
if SingleLine: if SingleLine:
List += f' <span>{Title}</span> ' List += [{ 'text': f' <span>{Title}</span> ' }]
else: else:
List += f'{Levels}<span class="staticoso-List-Title">{Title}</span>\n' List += [{ 'text': f'{Levels}<span class="staticoso-List-Title">{Title}</span>\n' }]
# Pages with any other path # Pages with any other path
if not (Depth > 1 and StripExt(File).split('/')[-1] == 'index'): if not (Depth > 1 and StripExt(File).split('/')[-1] == 'index'):
attrs = ('Image' in Meta and Meta['Image'] != '' and f'data-image="{html.escape(Meta["Image"])}"' or '')
Levels = '.' * ((Depth-1) if not Flatten else 0) + ':' Levels = '.' * ((Depth-1) if not Flatten else 0) + ':'
DoneCount += 1 DoneCount += 1
if Meta['Order'] == 'Unite': if Meta['Order'] == 'Unite':
@ -79,14 +81,17 @@ def GetHTMLPagesList(Flags:dict, Pages:list, PathPrefix:str, CallbackFile=None,
else: else:
Title = MakeListTitle(File, Meta, Titles, 'HTMLTitle', f.BlogName, PathPrefix) Title = MakeListTitle(File, Meta, Titles, 'HTMLTitle', f.BlogName, PathPrefix)
if SingleLine: if SingleLine:
List += ' <span>' + Title + '</span> ' List += [{ "attrs": attrs, 'text': ' <span>' + Title + '</span> ' }]
else: else:
List += Levels + Title + '\n' List += [{ "attrs": attrs, 'text': Levels + Title + '\n' }]
if MenuStyle in ('Default', 'Flat'): if MenuStyle in ('Default', 'Flat'):
return GenHTMLTreeList(List, Class="staticoso-PagesList") return GenHTMLTreeList(List, Class="staticoso-PagesList")
elif MenuStyle in ('Line', 'Excerpt', 'Image', 'Preview', 'Full'): elif MenuStyle in ('Line', 'Excerpt', 'Image', 'Preview', 'Full'):
return List htmlList = ''
for item in List:
htmlList += item['text']
return htmlList
def CheckHTMLCommentLine(Line:str): def CheckHTMLCommentLine(Line:str):
if Line.startswith('<!--'): if Line.startswith('<!--'):