mirror of https://gitlab.com/octtspacc/staticoso
Fixes for Markdown support
I have been out of my mind for a few weeks and decided that Markdown was inferior to Pug, so I kind of ignored it and my punishment for the day was, since I now want to use Markdown too again, to fix my code.
This commit is contained in:
parent
c4aa8435fc
commit
1de8ed2db7
|
@ -16,7 +16,7 @@ Feel free to experiment with all of this stuff!
|
||||||
|
|
||||||
## Features roadmap
|
## Features roadmap
|
||||||
- [x] Custom static page parts programmable by context
|
- [x] Custom static page parts programmable by context
|
||||||
- [ ] Handle showing creation and modified date for any page
|
- [x] Handle showing creation and modified date for posts
|
||||||
- [x] Generation of category pages (ex. page with list of blog posts)
|
- [x] Generation of category pages (ex. page with list of blog posts)
|
||||||
- [x] Custom title choosing type (HTML/Plaintext)
|
- [x] Custom title choosing type (HTML/Plaintext)
|
||||||
- [x] Custom page ordering
|
- [x] Custom page ordering
|
||||||
|
@ -33,4 +33,3 @@ Feel free to experiment with all of this stuff!
|
||||||
- [x] Pug support for pages
|
- [x] Pug support for pages
|
||||||
- [x] Markdown support for pages
|
- [x] Markdown support for pages
|
||||||
- [x] First working version
|
- [x] First working version
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,9 @@ def WriteFile(p, c):
|
||||||
print("Error writing file {}".format(p))
|
print("Error writing file {}".format(p))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def StripExt(Path):
|
||||||
|
return ".".join(Path.split('.')[:-1])
|
||||||
|
|
||||||
def ResetPublic():
|
def ResetPublic():
|
||||||
try:
|
try:
|
||||||
shutil.rmtree('public')
|
shutil.rmtree('public')
|
||||||
|
@ -56,14 +59,19 @@ def GetTitle(Meta, Titles, Prefer='MetaTitle'):
|
||||||
Title += ' - Blogocto'
|
Title += ' - Blogocto'
|
||||||
return Title
|
return Title
|
||||||
|
|
||||||
def GetTitleIdLine(Line, Title):
|
def GetTitleIdLine(Line, Title, Type):
|
||||||
Title = DashifyStr(Title.lstrip('#'))
|
DashTitle = DashifyStr(Title.lstrip('#'))
|
||||||
Index = Line.find('h')
|
|
||||||
NewLine = ''
|
NewLine = ''
|
||||||
NewLine += Line[:Index]
|
if Type == 'md':
|
||||||
NewLine += "{}(id='{}')".format(Line[Index:Index+2], Title)
|
Index = Title.split(' ')[0].count('#')
|
||||||
NewLine += Line[Index+2:]
|
#return "{} [{}]({})".format('#'*Index, Title, DashTitle)
|
||||||
return NewLine
|
return '<h{} id="{}">{}</h{}>'.format(Index, DashTitle, Title[Index+1:], Index)
|
||||||
|
elif Type == 'pug':
|
||||||
|
Index = Line.find('h')
|
||||||
|
NewLine += Line[:Index]
|
||||||
|
NewLine += "{}(id='{}')".format(Line[Index:Index+2], DashTitle)
|
||||||
|
NewLine += Line[Index+2:]
|
||||||
|
return NewLine
|
||||||
|
|
||||||
def MakeListTitle(File, Meta, Titles, Prefer, SiteRoot):
|
def MakeListTitle(File, Meta, Titles, Prefer, SiteRoot):
|
||||||
Title = GetTitle(Meta, Titles, Prefer)
|
Title = GetTitle(Meta, Titles, Prefer)
|
||||||
|
@ -71,11 +79,11 @@ def MakeListTitle(File, Meta, Titles, Prefer, SiteRoot):
|
||||||
Title = '[{}] [{}]({})'.format(
|
Title = '[{}] [{}]({})'.format(
|
||||||
Meta['CreatedOn'],
|
Meta['CreatedOn'],
|
||||||
Title,
|
Title,
|
||||||
'{}{}html'.format(SiteRoot, File[:-3]))
|
'{}{}.html'.format(SiteRoot, StripExt(File)))
|
||||||
else:
|
else:
|
||||||
Title = '[{}]({})'.format(
|
Title = '[{}]({})'.format(
|
||||||
Title,
|
Title,
|
||||||
'{}{}html'.format(SiteRoot, File[:-3]))
|
'{}{}.html'.format(SiteRoot, StripExt(File)))
|
||||||
return Title
|
return Title
|
||||||
|
|
||||||
def FormatTitles(Titles):
|
def FormatTitles(Titles):
|
||||||
|
@ -95,8 +103,8 @@ def LoadFromDir(Dir, Rglob):
|
||||||
Contents.update({File: ReadFile('{}/{}'.format(Dir, File))})
|
Contents.update({File: ReadFile('{}/{}'.format(Dir, File))})
|
||||||
return Contents
|
return Contents
|
||||||
|
|
||||||
def PreProcessor(p, SiteRoot):
|
def PreProcessor(Path, SiteRoot):
|
||||||
File = ReadFile(p)
|
File = ReadFile(Path)
|
||||||
Content, Titles, Meta = '', [], {
|
Content, Titles, Meta = '', [], {
|
||||||
'Template': 'Standard.html',
|
'Template': 'Standard.html',
|
||||||
'Style': '',
|
'Style': '',
|
||||||
|
@ -121,19 +129,27 @@ def PreProcessor(p, SiteRoot):
|
||||||
Meta['Style'] += ls[len('// Style: '):] + ' '
|
Meta['Style'] += ls[len('// Style: '):] + ' '
|
||||||
elif lss.startswith('Order: '):
|
elif lss.startswith('Order: '):
|
||||||
Meta['Order'] = int(ls[len('// Order: '):])
|
Meta['Order'] = int(ls[len('// Order: '):])
|
||||||
elif ls.startswith(('h1', 'h2', 'h3', 'h4', 'h5', 'h6')):
|
|
||||||
if ls[2:].startswith(("(class='NoTitle", '(class="NoTitle')):
|
|
||||||
Content += l + '\n'
|
|
||||||
else:
|
|
||||||
Title = '#'*int(ls[1]) + str(ls[3:])
|
|
||||||
Titles += [Title]
|
|
||||||
# We should handle headers that for any reason already have parenthesis
|
|
||||||
if ls[2:] == '(':
|
|
||||||
Content += l + '\n'
|
|
||||||
else:
|
|
||||||
Content += GetTitleIdLine(l, Title) + '\n'
|
|
||||||
else:
|
else:
|
||||||
Content += l + '\n'
|
if Path.endswith('.md'):
|
||||||
|
if ls.startswith('#'):
|
||||||
|
Titles += [l]
|
||||||
|
Content += GetTitleIdLine(l, ls, 'md') + '\n'
|
||||||
|
else:
|
||||||
|
Content += l + '\n'
|
||||||
|
elif Path.endswith('.pug'):
|
||||||
|
if ls.startswith(('h1', 'h2', 'h3', 'h4', 'h5', 'h6')):
|
||||||
|
if ls[2:].startswith(("(class='NoTitle", '(class="NoTitle')):
|
||||||
|
Content += l + '\n'
|
||||||
|
else:
|
||||||
|
Title = '#'*int(ls[1]) + str(ls[3:])
|
||||||
|
Titles += [Title]
|
||||||
|
# We should handle headers that for any reason already have parenthesis
|
||||||
|
if ls[2:] == '(':
|
||||||
|
Content += l + '\n'
|
||||||
|
else:
|
||||||
|
Content += GetTitleIdLine(l, Title, 'pug') + '\n'
|
||||||
|
else:
|
||||||
|
Content += l + '\n'
|
||||||
return Content, Titles, Meta
|
return Content, Titles, Meta
|
||||||
|
|
||||||
def PugCompileList(Pages):
|
def PugCompileList(Pages):
|
||||||
|
@ -258,25 +274,17 @@ def MakeSite(TemplatesText, PartsText, ContextParts, ContextPartsText, SiteRoot)
|
||||||
Content, Titles, Meta = PreProcessor('Pages/{}'.format(File), SiteRoot)
|
Content, Titles, Meta = PreProcessor('Pages/{}'.format(File), SiteRoot)
|
||||||
Pages += [[File, Content, Titles, Meta]]
|
Pages += [[File, Content, Titles, Meta]]
|
||||||
PugCompileList(Pages)
|
PugCompileList(Pages)
|
||||||
HTMLPagesList = GetHTMLPagesList(Pages, SiteRoot, 'Page')
|
|
||||||
Macros['BlogPosts'] = GetHTMLPagesList(Pages, SiteRoot, 'Post')
|
|
||||||
for File, Content, Titles, Meta in Pages:
|
|
||||||
Template = TemplatesText[Meta['Template']]
|
|
||||||
Template = Template.replace(
|
|
||||||
'[HTML:Site:AbsoluteRoot]',
|
|
||||||
SiteRoot)
|
|
||||||
Template = Template.replace(
|
|
||||||
'[HTML:Site:RelativeRoot]',
|
|
||||||
'../'*File.count('/'))
|
|
||||||
WriteFile(
|
|
||||||
'public/{}html'.format(File[:-3]),
|
|
||||||
PatchHTML(
|
|
||||||
Template, PartsText, ContextParts, ContextPartsText, HTMLPagesList,
|
|
||||||
ReadFile('public/{}html'.format(File[:-3])),
|
|
||||||
Titles, Meta, SiteRoot, Macros))
|
|
||||||
for File in Path('Pages').rglob('*.md'):
|
for File in Path('Pages').rglob('*.md'):
|
||||||
File = FileToStr(File, 'Pages/')
|
File = FileToStr(File, 'Pages/')
|
||||||
Content, Titles, Meta = PreProcessor('Pages/{}'.format(File), SiteRoot)
|
Content, Titles, Meta = PreProcessor('Pages/{}'.format(File), SiteRoot)
|
||||||
|
Pages += [[File, Content, Titles, Meta]]
|
||||||
|
HTMLPagesList = GetHTMLPagesList(Pages, SiteRoot, 'Page')
|
||||||
|
Macros['BlogPosts'] = GetHTMLPagesList(Pages, SiteRoot, 'Post')
|
||||||
|
for File, Content, Titles, Meta in Pages:
|
||||||
|
if File.endswith('.md'):
|
||||||
|
Content = Markdown().convert(Content)
|
||||||
|
elif File.endswith('.pug'):
|
||||||
|
Content = ReadFile('public/{}.html'.format(StripExt(File)))
|
||||||
Template = TemplatesText[Meta['Template']]
|
Template = TemplatesText[Meta['Template']]
|
||||||
Template = Template.replace(
|
Template = Template.replace(
|
||||||
'[HTML:Site:AbsoluteRoot]',
|
'[HTML:Site:AbsoluteRoot]',
|
||||||
|
@ -285,11 +293,10 @@ def MakeSite(TemplatesText, PartsText, ContextParts, ContextPartsText, SiteRoot)
|
||||||
'[HTML:Site:RelativeRoot]',
|
'[HTML:Site:RelativeRoot]',
|
||||||
'../'*File.count('/'))
|
'../'*File.count('/'))
|
||||||
WriteFile(
|
WriteFile(
|
||||||
'public/{}html'.format(File[:-2]),
|
'public/{}.html'.format(StripExt(File)),
|
||||||
PatchHTML(
|
PatchHTML(
|
||||||
Template, PartsText, ContextParts, ContextPartsText, HTMLPagesList,
|
Template, PartsText, ContextParts, ContextPartsText, HTMLPagesList,
|
||||||
Markdown().convert(Content),
|
Content, Titles, Meta, SiteRoot, Macros))
|
||||||
Titles, Meta, SiteRoot, Macros))
|
|
||||||
DelTmp()
|
DelTmp()
|
||||||
|
|
||||||
def Main(Args):
|
def Main(Args):
|
||||||
|
|
Loading…
Reference in New Issue