mirror of
https://gitlab.com/octtspacc/staticoso
synced 2025-06-05 22:09:23 +02:00
Id titles with clickable links + some title bugfixes
This commit is contained in:
@ -8,19 +8,12 @@
|
|||||||
| ================================= """
|
| ================================= """
|
||||||
|
|
||||||
import html
|
import html
|
||||||
|
import warnings
|
||||||
from Libs.bs4 import BeautifulSoup
|
from Libs.bs4 import BeautifulSoup
|
||||||
from Modules.Utils import *
|
from Modules.Utils import *
|
||||||
|
|
||||||
"""
|
# Suppress useless bs4 warnings
|
||||||
ClosedTags = (
|
warnings.filterwarnings('ignore', message='The input looks more like a filename than markup.')
|
||||||
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
|
|
||||||
'p', 'span', 'pre', 'code',
|
|
||||||
'a', 'b', 'i', 'del', 'strong',
|
|
||||||
'div', 'details', 'summary',
|
|
||||||
'ol', 'ul', 'li', 'dl', 'dt', 'dd')
|
|
||||||
OpenTags = (
|
|
||||||
'img')
|
|
||||||
"""
|
|
||||||
|
|
||||||
def MkSoup(HTML):
|
def MkSoup(HTML):
|
||||||
return BeautifulSoup(HTML, 'html.parser')
|
return BeautifulSoup(HTML, 'html.parser')
|
||||||
@ -60,7 +53,6 @@ def AddToTagStartEnd(HTML, MatchStart, MatchEnd, AddStart, AddEnd): # This doesn
|
|||||||
break
|
break
|
||||||
if FilterStart == MatchStart:
|
if FilterStart == MatchStart:
|
||||||
StartPos = i
|
StartPos = i
|
||||||
# TagName = FirstRealItem(FirstRealItem(FilterStart.split('<')).split(' '))
|
|
||||||
if AddStart:
|
if AddStart:
|
||||||
HTML = HTML[:i] + AddStart + HTML[i:]
|
HTML = HTML[:i] + AddStart + HTML[i:]
|
||||||
AddStart = None
|
AddStart = None
|
||||||
|
@ -22,14 +22,10 @@ def DashifyTitle(Title, Done=[]):
|
|||||||
def MakeLinkableTitle(Line, Title, DashTitle, Type):
|
def MakeLinkableTitle(Line, Title, DashTitle, Type):
|
||||||
if Type == 'md':
|
if Type == 'md':
|
||||||
Index = Title.split(' ')[0].count('#')
|
Index = Title.split(' ')[0].count('#')
|
||||||
return f'<h{Index} id="{DashTitle}">{Title[Index+1:]}</h{Index}>'
|
return f'<h{Index} id="{DashTitle}" class="SectionTitle"><span class="SectionLink"><a href="#{DashTitle}"><span>>></span></a> </span>{Title[Index+1:]}</h{Index}>'
|
||||||
elif Type == 'pug':
|
elif Type == 'pug':
|
||||||
NewLine = ''
|
|
||||||
Index = Line.find('h')
|
Index = Line.find('h')
|
||||||
NewLine += Line[:Index]
|
return f"{Line[:Index]}{Line[Index:Index+2]}(id='{DashTitle}' class='SectionTitle') #[span(class='SectionLink') #[a(href='#{DashTitle}') #[span >>]] ]{Line[Index+2:]}"
|
||||||
NewLine += f"{Line[Index:Index+2]}(id='{DashTitle}')"
|
|
||||||
NewLine += Line[Index+2:]
|
|
||||||
return NewLine
|
|
||||||
|
|
||||||
def GetTitle(FileName, Meta, Titles, Prefer='MetaTitle', BlogName=None):
|
def GetTitle(FileName, Meta, Titles, Prefer='MetaTitle', BlogName=None):
|
||||||
if Prefer == 'BodyTitle':
|
if Prefer == 'BodyTitle':
|
||||||
@ -180,11 +176,21 @@ def PagePreprocessor(Path, Type, SiteTemplate, SiteRoot, GlobalMacros):
|
|||||||
Content = str(Soup.prettify(formatter=None))
|
Content = str(Soup.prettify(formatter=None))
|
||||||
HTMLTitlesFound = True
|
HTMLTitlesFound = True
|
||||||
elif Path.endswith(FileExtensions['Markdown']):
|
elif Path.endswith(FileExtensions['Markdown']):
|
||||||
if ll.startswith('#'):
|
if ll.startswith('#') or (ll.startswith('<') and ll[1:].startswith(Headings)):
|
||||||
DashTitle = DashifyTitle(l.lstrip('#'), DashyTitles)
|
if ll.startswith('#'):
|
||||||
|
Title = ll
|
||||||
|
#Index = Title.split(' ')[0].count('#')
|
||||||
|
elif ll.startswith('<'):
|
||||||
|
#Index = int(ll[2])
|
||||||
|
Title = '#'*h + str(ll[3:])
|
||||||
|
DashTitle = DashifyTitle(MkSoup(Title.lstrip('#')).get_text(), DashyTitles)
|
||||||
DashyTitles += [DashTitle]
|
DashyTitles += [DashTitle]
|
||||||
Titles += [l]
|
Titles += [Title]
|
||||||
Content += MakeLinkableTitle(None, ll, DashTitle, 'md') + '\n'
|
Title = MakeLinkableTitle(None, Title, DashTitle, 'md')
|
||||||
|
#Title = Title.replace(' </h{Index}>', '</h{Index}>')
|
||||||
|
Title = Title.replace('> </', '> </')
|
||||||
|
Title = Title.replace(' </', '</')
|
||||||
|
Content += Title + '\n'
|
||||||
else:
|
else:
|
||||||
Content += l + '\n'
|
Content += l + '\n'
|
||||||
elif Path.endswith('.pug'):
|
elif Path.endswith('.pug'):
|
||||||
@ -250,7 +256,7 @@ def FormatTitles(Titles, Flatten=False):
|
|||||||
for t in Titles:
|
for t in Titles:
|
||||||
n = t.split(' ')[0].count('#')
|
n = t.split(' ')[0].count('#')
|
||||||
Heading = '- ' * (n if not Flatten else 1)
|
Heading = '- ' * (n if not Flatten else 1)
|
||||||
Title = t.lstrip('#')
|
Title = MkSoup(t.lstrip('#')).get_text()
|
||||||
DashyTitle = DashifyTitle(Title, DashyTitles)
|
DashyTitle = DashifyTitle(Title, DashyTitles)
|
||||||
DashyTitles += [DashyTitle]
|
DashyTitles += [DashyTitle]
|
||||||
Title = f"[{Title}](#{DashyTitle})"
|
Title = f"[{Title}](#{DashyTitle})"
|
||||||
|
@ -24,7 +24,7 @@ def ReadFile(p):
|
|||||||
with open(p, 'r') as f:
|
with open(p, 'r') as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
except Exception:
|
except Exception:
|
||||||
print("[E] Error reading file {}".format(p))
|
print(f"[E] Error reading file {p}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def WriteFile(p, c):
|
def WriteFile(p, c):
|
||||||
@ -33,7 +33,7 @@ def WriteFile(p, c):
|
|||||||
f.write(c)
|
f.write(c)
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
print("[E] Error writing file {}".format(p))
|
print(f"[E] Error writing file {p}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def FileToStr(File, Truncate=''):
|
def FileToStr(File, Truncate=''):
|
||||||
@ -50,7 +50,7 @@ def LoadFromDir(Dir, Matchs):
|
|||||||
for Match in Matchs:
|
for Match in Matchs:
|
||||||
for File in Path(Dir).rglob(Match):
|
for File in Path(Dir).rglob(Match):
|
||||||
File = str(File)[len(Dir)+1:]
|
File = str(File)[len(Dir)+1:]
|
||||||
Contents.update({File: ReadFile('{}/{}'.format(Dir, File))})
|
Contents.update({File: ReadFile(f"{Dir}/{File}")})
|
||||||
return Contents
|
return Contents
|
||||||
|
|
||||||
def StripExt(Path):
|
def StripExt(Path):
|
||||||
@ -67,8 +67,8 @@ def UndupeStr(Str, Known, Split):
|
|||||||
return Str
|
return Str
|
||||||
|
|
||||||
def DashifyStr(s, Limit=32):
|
def DashifyStr(s, Limit=32):
|
||||||
Str, lc = '', Limit
|
Str = ''
|
||||||
for c in s[:Limit].replace(' ','-').replace(' ','-'):
|
for c in s[:Limit].replace('\n','-').replace('\t','-').replace(' ','-'):
|
||||||
if c.lower() in '0123456789qwfpbjluyarstgmneiozxcdvkh-':
|
if c.lower() in '0123456789qwfpbjluyarstgmneiozxcdvkh-':
|
||||||
Str += c
|
Str += c
|
||||||
return '-' + Str
|
return '-' + Str
|
||||||
|
3
TODO
3
TODO
@ -1,3 +1,6 @@
|
|||||||
|
- Fix excess whitespace in some section/menu titles
|
||||||
|
- Change staticoso service tag enclosure from [] to <>
|
||||||
|
- Investigate a strange bug with Macros
|
||||||
- Handle file extensions with any case sensitivity, not just lowercase; currently the bulk of the issue is finding the files on disk
|
- Handle file extensions with any case sensitivity, not just lowercase; currently the bulk of the issue is finding the files on disk
|
||||||
- Test sorting by date for files not starting with date, and dated folders
|
- Test sorting by date for files not starting with date, and dated folders
|
||||||
- Fix arguments - some are only callable from CLI and not Site.ini - make them coherent with INI categories
|
- Fix arguments - some are only callable from CLI and not Site.ini - make them coherent with INI categories
|
||||||
|
Reference in New Issue
Block a user