From 07fe654ddda22120d8c7f56f255d43f10e2b1a53 Mon Sep 17 00:00:00 2001 From: rra Date: Wed, 16 Feb 2022 12:05:50 +0100 Subject: [PATCH] add method to escape " from YAML frontmatter --- rss_aggregator.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/rss_aggregator.py b/rss_aggregator.py index b584b20..6b2eb60 100644 --- a/rss_aggregator.py +++ b/rss_aggregator.py @@ -14,7 +14,7 @@ from slugify import slugify from bs4 import BeautifulSoup import time import arrow - +from re import sub def write_etag(feed_name, feed_data): """ @@ -107,6 +107,28 @@ 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): """ 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) 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)) -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 if download succeeds return new local path otherwise return url @@ -217,7 +239,7 @@ def create_opds_post(post_dir, entry): summary = "" 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) print('created post for Book', entry.title)