Skip to content

Commit

Permalink
Fix parsing of YAML front matter
Browse files Browse the repository at this point in the history
  • Loading branch information
lkluft committed Jul 16, 2024
1 parent 5424982 commit b210b44
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
11 changes: 4 additions & 7 deletions orcestra_book/_ext/category_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from docutils import nodes

import yaml
from frontmatter import Frontmatter
from sphinx.util.docutils import SphinxRole


Expand Down Expand Up @@ -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]

Expand All @@ -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]), []

Expand Down
5 changes: 3 additions & 2 deletions orcestra_book/_ext/create_flight_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import subprocess
from datetime import datetime

from frontmatter import Frontmatter
import yaml
from jinja2 import Template


Expand All @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b210b44

Please sign in to comment.