add method to escape " from YAML frontmatter ruangrupa/lumbung.space#33

This commit is contained in:
rra 2022-02-16 12:12:41 +01:00
parent 6339724510
commit 9d9c87809d

View File

@ -12,6 +12,7 @@ import jinja2
import requests
from bs4 import BeautifulSoup
from slugify import slugify
from re import sub
def write_etag(feed_name, feed_data):
@ -84,6 +85,27 @@ def create_frontmatter(entry):
return frontmatter
def sanitize_yaml (frontmatter):
"""
Escapes any occurences of double quotes
in any of the frontmatter fields
See: https://docs.octoprint.org/en/master/configuration/yaml.html#interesting-data-types
"""
for k, v in frontmatter.items():
if type(v) == type([]):
#some fields are lists
l = []
for i in v:
i = sub('"', '\\"', i)
l.append(i)
frontmatter[k] = l
else:
v = sub('"', '\\"', v)
frontmatter[k] = v
return frontmatter
def create_post(post_dir, entry):
"""
@ -105,7 +127,7 @@ def create_post(post_dir, entry):
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
template = env.get_template("feed.md")
with open(os.path.join(post_dir, "index.html"), "w") as f: # n.b. .html
post = template.render(frontmatter=frontmatter, content=parsed_content)
post = template.render(frontmatter=sanitize_yaml(frontmatter), content=parsed_content)
f.write(post)
print("created post for", entry.title, "({})".format(entry.link))