mirror of
https://github.com/venthur/blag.git
synced 2025-11-26 05:02:58 +00:00
Added YAML frontmatter support.
This commit is contained in:
44
sg.py
44
sg.py
@@ -21,6 +21,7 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
import string
|
import string
|
||||||
import codecs
|
import codecs
|
||||||
|
import re
|
||||||
|
|
||||||
import markdown
|
import markdown
|
||||||
|
|
||||||
@@ -89,6 +90,44 @@ def process_embed_content(template, content):
|
|||||||
return html
|
return html
|
||||||
|
|
||||||
|
|
||||||
|
def process_embed_meta(template, content):
|
||||||
|
"""Embedd meta info into html template."""
|
||||||
|
txt = string.Template(template)
|
||||||
|
html = txt.safe_substitute(content)
|
||||||
|
return html
|
||||||
|
|
||||||
|
|
||||||
|
def get_meta(txt):
|
||||||
|
"""Parse meta information from text if available and return as dict.
|
||||||
|
|
||||||
|
meta information is a block imbedded in "---\n" lines having the format:
|
||||||
|
|
||||||
|
key: value
|
||||||
|
|
||||||
|
both are treated as strings the value starts after the ": " end ends with
|
||||||
|
the newline.
|
||||||
|
"""
|
||||||
|
SEP = '---\n'
|
||||||
|
meta = dict()
|
||||||
|
if txt.count(SEP) > 1 and txt.startswith(SEP):
|
||||||
|
stuff = txt[len(SEP):txt.find(SEP, 1)]
|
||||||
|
txt = txt[txt.find((SEP), 1)+len(SEP):]
|
||||||
|
for i in stuff.splitlines():
|
||||||
|
if i.count(':') > 0:
|
||||||
|
key, value = i.split(':', 1)
|
||||||
|
value = value.strip()
|
||||||
|
meta[key] = value
|
||||||
|
return meta, txt
|
||||||
|
|
||||||
|
|
||||||
|
def check_unused_variables(txt):
|
||||||
|
"""Search for unused $foo variables and print a warning."""
|
||||||
|
template = '\\$[_a-z][_a-z0=9]*'
|
||||||
|
f = re.findall(template, txt)
|
||||||
|
if len(f) > 0:
|
||||||
|
print "Unconsumed variables in template found:", f
|
||||||
|
|
||||||
|
|
||||||
def render_page(path):
|
def render_page(path):
|
||||||
"""Render page.
|
"""Render page.
|
||||||
|
|
||||||
@@ -104,9 +143,14 @@ def render_page(path):
|
|||||||
template = ''.join(fh.readlines())
|
template = ''.join(fh.readlines())
|
||||||
fh.close()
|
fh.close()
|
||||||
|
|
||||||
|
# get meta information
|
||||||
|
meta, txt = get_meta(txt)
|
||||||
|
|
||||||
# currently we only process markdown, other stuff can be added easyly
|
# currently we only process markdown, other stuff can be added easyly
|
||||||
txt = process_markdown(txt)
|
txt = process_markdown(txt)
|
||||||
txt = process_embed_content(template, txt)
|
txt = process_embed_content(template, txt)
|
||||||
|
txt = process_embed_meta(txt, meta)
|
||||||
|
check_unused_variables(txt)
|
||||||
return txt
|
return txt
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user