build_a_blog: convert reuse

This commit is contained in:
Collin Lefeber 2024-06-18 10:06:20 -04:00
parent 91fcf8036a
commit 363ca6c6b6

View file

@ -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. 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 ```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): def rss_post_xml(post):
tpl = """ tpl = """
<item> <item>
@ -587,8 +605,7 @@ def rss_post_xml(post):
with open(post['fpath'], 'r') as inf: with open(post['fpath'], 'r') as inf:
text = inf.read() text = inf.read()
md = markdown.Markdown(extensions=['extra', 'meta']) converted = convert(text)
converted = md.convert(text)
link = "https://cfebs.com/" + post['destpath'] link = "https://cfebs.com/" + post['destpath']
pubdate = email.utils.format_datetime(datetime.datetime.fromisoformat(post['date'])) pubdate = email.utils.format_datetime(datetime.datetime.fromisoformat(post['date']))