add method to escape " from YAML frontmatter ruangrupa/lumbung.space#33
This commit is contained in:
parent
6339724510
commit
9d9c87809d
@ -12,6 +12,7 @@ import jinja2
|
|||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from slugify import slugify
|
from slugify import slugify
|
||||||
|
from re import sub
|
||||||
|
|
||||||
|
|
||||||
def write_etag(feed_name, feed_data):
|
def write_etag(feed_name, feed_data):
|
||||||
@ -84,6 +85,27 @@ def create_frontmatter(entry):
|
|||||||
|
|
||||||
return frontmatter
|
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):
|
def create_post(post_dir, entry):
|
||||||
"""
|
"""
|
||||||
@ -105,7 +127,7 @@ def create_post(post_dir, entry):
|
|||||||
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
|
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
|
||||||
template = env.get_template("feed.md")
|
template = env.get_template("feed.md")
|
||||||
with open(os.path.join(post_dir, "index.html"), "w") as f: # n.b. .html
|
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)
|
f.write(post)
|
||||||
print("created post for", entry.title, "({})".format(entry.link))
|
print("created post for", entry.title, "({})".format(entry.link))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user