From 363ca6c6b675b66bd0ef9dc9411115abd8592731 Mon Sep 17 00:00:00 2001 From: Collin Lefeber Date: Tue, 18 Jun 2024 10:06:20 -0400 Subject: [PATCH] build_a_blog: convert reuse --- posts/build_a_blog.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/posts/build_a_blog.md b/posts/build_a_blog.md index 95fac75..f8ceded 100644 --- a/posts/build_a_blog.md +++ b/posts/build_a_blog.md @@ -572,7 +572,25 @@ After this initial test and a `python3 ./main.py` run, should see xml filled out Now lets finish up by generating each item entry and collecting them to be replaced in the template. +And this adds another `md.convert()` call, so why not add a util to reuse a single Markdown instance. + ```python +# add a module var and helper method to reuse Markdown instance +md = markdown.Markdown(extensions=['extra', 'meta', TocExtension(anchorlink=True)]) +def convert(text): + md.reset() + return md.convert() + +def render_post(fpath): + ... + out = convert(text) + + title = md.Meta.get('title')[0] + date = md.Meta.get('date')[0] + + out = convert('# ' + title) + out + ... + def rss_post_xml(post): tpl = """ @@ -587,8 +605,7 @@ def rss_post_xml(post): with open(post['fpath'], 'r') as inf: text = inf.read() - md = markdown.Markdown(extensions=['extra', 'meta']) - converted = md.convert(text) + converted = convert(text) link = "https://cfebs.com/" + post['destpath'] pubdate = email.utils.format_datetime(datetime.datetime.fromisoformat(post['date']))