build_a_blog: safer markdown

This commit is contained in:
Collin Lefeber 2024-06-19 16:08:40 -04:00
parent 3e06bfd9ae
commit 906a5e04ea
2 changed files with 65 additions and 21 deletions

19
main.py
View file

@ -14,12 +14,12 @@ from markdown.extensions.toc import TocExtension
destpath_re = re.compile(r'\.md$')
logging.basicConfig(encoding='utf-8', level=logging.INFO)
md = markdown.Markdown(extensions=['extra', 'meta', TocExtension(anchorlink=True)])
cpu_count = os.cpu_count()
def convert(text):
md.reset()
return md.convert(text)
md = markdown.Markdown(extensions=['extra', 'meta', TocExtension(anchorlink=True)])
res = md.convert(text)
return res, md.Meta
def render_post(fpath):
destpath = destpath_re.sub('.html', fpath)
@ -30,15 +30,16 @@ def render_post(fpath):
text = input_file.read()
logging.info("parsing %s", fpath)
out = convert(text)
out, meta = convert(text)
title = md.Meta.get('title')[0]
date = md.Meta.get('date')[0]
title = meta.get('title')[0]
date = meta.get('date')[0]
draft = False
if md.Meta.get('draft'):
if meta.get('draft'):
draft = True
out = convert('# ' + title) + out
title_out, _ = convert(text)
out = title_out + out
logging.info("writing to %s", destpath)
render_template('index.html.tmpl', destpath, {'content': out, 'more_title': ' - ' + title})
@ -105,7 +106,7 @@ def rss_post_xml(post):
text = inf.read()
converted = convert(text)
converted, _ = convert(text)
pubdate = email.utils.format_datetime(datetime.datetime.fromisoformat(post['date']))
subs = {