add method to escape " from YAML frontmatter

This commit is contained in:
rra 2022-02-16 12:05:50 +01:00
parent f950f03792
commit 07fe654ddd

View File

@ -14,7 +14,7 @@ from slugify import slugify
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import time import time
import arrow import arrow
from re import sub
def write_etag(feed_name, feed_data): def write_etag(feed_name, feed_data):
""" """
@ -107,6 +107,28 @@ 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):
""" """
write hugo post based on RSS entry write hugo post based on RSS entry
@ -124,11 +146,11 @@ def create_post(post_dir, entry):
parsed_content = parse_posts(post_dir, post_content) parsed_content = parse_posts(post_dir, post_content)
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))
def grab_media(post_directory, url, prefered_name): def grab_media(post_directory, url, prefered_name=None):
""" """
download media linked in post to have local copy download media linked in post to have local copy
if download succeeds return new local path otherwise return url if download succeeds return new local path otherwise return url
@ -217,7 +239,7 @@ def create_opds_post(post_dir, entry):
summary = "" summary = ""
with open(os.path.join(post_dir,'index.md'),'w') as f: with open(os.path.join(post_dir,'index.md'),'w') as f:
post = template.render(frontmatter=frontmatter, content=summary) post = template.render(frontmatter=sanitize_yaml(frontmatter), content=summary)
f.write(post) f.write(post)
print('created post for Book', entry.title) print('created post for Book', entry.title)