Handling of duplicate section titles in pages

This commit is contained in:
2022-06-20 18:47:31 +02:00
parent 2cc2bfc62a
commit a8e6e09d18
4 changed files with 38 additions and 15 deletions

View File

@ -1,4 +1,5 @@
{
"CreatedOn": "Created on",
"EditedOn": "Edited on"
"EditedOn": "Edited on",
"Categories": "Categories"
}

View File

@ -1,4 +1,5 @@
{
"CreatedOn": "Creato in data",
"EditedOn": "Modificato in data"
"EditedOn": "Modificato in data",
"Categories": "Categorie"
}

View File

@ -5,7 +5,7 @@
I'm making this because I need a simple and kind-of-minimal program to serve me as a static site generator with (limited) CMS features. Just the features I need.
This won't replace any of the big projects out there that do the same thing but, as all of my projects, I'm releasing it as free libre software, in the hope that someone would find it useful.
Also, this software is needed for someone to edit and compile my personal sub-website [sitoctt](https://octtspacc.gitlab.io/sitoctt) from its source. Being that site too released under a libre license that allows modifications, I have to also release the tools I use to build it.
Also, this software is needed for someone to edit and compile my personal sub-website [sitoctt](https://gitlab.com/octtspacc/sitoctt) from its source. Being that site too released under a libre license that allows modifications, I have to also release the tools I use to build it.
Feel free to experiment with all of this stuff!

View File

@ -65,6 +65,17 @@ def DashifyStr(s, Limit=32):
Str += c
return '-' + Str
def DashifyTitle(Title, Done=[]):
Title = DashifyStr(Title)
while Title in Done:
Sections = Title.split('-')
try:
Sections[-1] = str(int(Sections[-1]) + 1)
except ValueError:
Sections[-1] = Sections[-1] + '-1'
Title = '-'.join(Sections)
return Title
def GetTitle(Meta, Titles, Prefer='MetaTitle'):
if Prefer == 'Title':
Title = Titles[0].lstrip('#') if Titles else Meta['Title'] if Meta['Title'] else 'Untitled'
@ -84,8 +95,7 @@ def GetDescription(Meta, Prefer='MetaDescription'):
Description = Meta['Description']
return Description
def GetTitleIdLine(Line, Title, Type):
DashTitle = DashifyStr(Title.lstrip('#'))
def MakeLinkableTitle(Line, Title, DashTitle, Type):
if Type == 'md':
Index = Title.split(' ')[0].count('#')
return '<h{} id="{}">{}</h{}>'.format(Index, DashTitle, Title[Index+1:], Index)
@ -111,12 +121,15 @@ def MakeListTitle(File, Meta, Titles, Prefer, SiteRoot, PathPrefix=''):
return Title
def FormatTitles(Titles):
MDTitles = ''
# TODO: Somehow titles written in Pug can end up here and don't work, they should be handled
MDTitles, DashyTitles = '', []
for t in Titles:
n = t.split(' ')[0].count('#')
Heading = '- ' * n
Title = t.lstrip('#')
Title = '[{}](#{})'.format(Title, DashifyStr(Title))
DashyTitle = DashifyTitle(Title, DashyTitles)
DashyTitles += [DashyTitle]
Title = '[{}](#{})'.format(Title, DashyTitle)
MDTitles += Heading + Title + '\n'
return Markdown().convert(MDTitles)
@ -129,7 +142,7 @@ def LoadFromDir(Dir, Rglob):
def PreProcessor(Path, SiteRoot):
File = ReadFile(Path)
Content, Titles, Meta = '', [], {
Content, Titles, DashyTitles, Meta = '', [], [], {
'Template': 'Standard.html',
'Style': '',
'Type': 'Page',
@ -162,8 +175,10 @@ def PreProcessor(Path, SiteRoot):
else:
if Path.endswith('.md'):
if ls.startswith('#'):
DashTitle = DashifyTitle(l.lstrip('#'), DashyTitles)
DashyTitles += [DashTitle]
Titles += [l]
Content += GetTitleIdLine(l, ls, 'md') + '\n'
Content += MakeLinkableTitle(l, ls, DashTitle, 'md') + '\n'
else:
Content += l + '\n'
elif Path.endswith('.pug'):
@ -172,12 +187,14 @@ def PreProcessor(Path, SiteRoot):
Content += l + '\n'
else:
Title = '#'*int(ls[1]) + str(ls[3:])
DashTitle = DashifyTitle(Title.lstrip('#'), DashyTitles)
DashyTitles += [DashTitle]
Titles += [Title]
# We should handle headers that for any reason already have parenthesis
# TODO: We should handle headers that for any reason already have parenthesis
if ls[2:] == '(':
Content += l + '\n'
else:
Content += GetTitleIdLine(l, Title, 'pug') + '\n'
Content += MakeLinkableTitle(l, Title, DashTitle, 'pug') + '\n'
else:
Content += l + '\n'
return Content, Titles, Meta
@ -195,10 +212,14 @@ def PugCompileList(Pages):
def MakeContentHeader(Meta, Locale):
Header = ''
if Meta['Type'] == 'Post':
if Meta['CreatedOn']:
Header += "{} {} \n".format(Locale['CreatedOn'], Meta['CreatedOn'])
if Meta['EditedOn']:
Header += "{} {} \n".format(Locale['EditedOn'], Meta['EditedOn'])
for i in ['CreatedOn', 'EditedOn']:
if Meta[i]:
Header += "{} {} \n".format(Locale[i], Meta[i])
if Meta['Categories']:
Categories = ''
for i in Meta['Categories']:
Categories += i + ' '
Header += "{}: {} \n".format(Locale['Categories'], Categories)
return Markdown().convert(Header)
def PatchHTML(Template, PartsText, ContextParts, ContextPartsText, HTMLPagesList, PagePath, Content, Titles, Meta, SiteRoot, FolderRoots, Categories, Locale):