From b210b44c5c950fc0c2ca2dd569f8bb8eca5f13d9 Mon Sep 17 00:00:00 2001 From: Lukas Kluft Date: Mon, 15 Jul 2024 16:02:08 +0200 Subject: [PATCH] Fix parsing of YAML front matter --- orcestra_book/_ext/category_role.py | 11 ++++------- orcestra_book/_ext/create_flight_table.py | 5 +++-- requirements.txt | 1 + 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/orcestra_book/_ext/category_role.py b/orcestra_book/_ext/category_role.py index e52aa1de..ffed558c 100644 --- a/orcestra_book/_ext/category_role.py +++ b/orcestra_book/_ext/category_role.py @@ -2,7 +2,6 @@ from docutils import nodes import yaml -from frontmatter import Frontmatter from sphinx.util.docutils import SphinxRole @@ -33,9 +32,8 @@ def run(self): class BadgesRole(SphinxRole): def run(self): """(Re-)parse the frontmatter of the current document and create badges.""" - categories = Frontmatter.read_file( - self.env.doc2path(self.env.docname) - )["attributes"].get("categories", []) + with open(self.env.doc2path(self.env.docname), "r") as fp: + categories = next(yaml.safe_load_all(fp)).get("categories", []) nodes = [create_badge(cat_id) for cat_id in categories] @@ -47,9 +45,8 @@ def run(self): """Access variables defined in document front matter.""" # TODO: It is likely not a good practice to parse the document again and again. # However, it is a working solution with no sensible performance degradation. - frontmatter = Frontmatter.read_file( - self.env.doc2path(self.env.docname) - )["attributes"] + with open(self.env.doc2path(self.env.docname), "r") as fp: + frontmatter = next(yaml.safe_load_all(fp)) return nodes.raw(text=frontmatter[self.text]), [] diff --git a/orcestra_book/_ext/create_flight_table.py b/orcestra_book/_ext/create_flight_table.py index 74166479..38be7ce6 100644 --- a/orcestra_book/_ext/create_flight_table.py +++ b/orcestra_book/_ext/create_flight_table.py @@ -4,7 +4,7 @@ import subprocess from datetime import datetime -from frontmatter import Frontmatter +import yaml from jinja2 import Template @@ -20,7 +20,8 @@ def read_frontmatter(path): - attrs = Frontmatter.read_file(path)["attributes"] + with open(path, "r") as fp: + attrs = next(yaml.safe_load_all(fp)) takeoff = attrs["takeoff"] landing = attrs["landing"] diff --git a/requirements.txt b/requirements.txt index 34e7d1ed..be14136e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,3 +14,4 @@ pybtex-apa-style docutils==0.17.1 # see https://github.com/executablebooks/jupyter-book/issues/1997 easygems>=0.0.3 cmocean +jinja2