-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from lkluft/refactor-extensions
Refactor extensions
- Loading branch information
Showing
8 changed files
with
241 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import pathlib | ||
from docutils import nodes | ||
|
||
from sphinx.util.docutils import SphinxRole | ||
|
||
|
||
class LogoRole(SphinxRole): | ||
"""Add a small campaign logo to the upper-right corner of a page.""" | ||
|
||
def run(self): | ||
src = pathlib.Path(self.env.srcdir) | ||
|
||
logo_path = list((src / "logos").glob(f"*_{self.text}.svg")) | ||
doc_path = pathlib.Path(self.env.doc2path(self.env.docname)) | ||
|
||
if len(logo_path) == 0: | ||
raise Exception(f"No logo found for {self.text}") | ||
else: | ||
rel_path = logo_path[0].relative_to(doc_path.parent, walk_up=True) | ||
|
||
node = nodes.image(uri=rel_path.as_posix(), alt=f"{self.text} logo") | ||
node["classes"].append("campaign-logo") | ||
|
||
return [node], [] | ||
|
||
|
||
def setup(app): | ||
app.add_role("logo", LogoRole()) | ||
|
||
return { | ||
"version": "0.1", | ||
"parallel_read_safe": True, | ||
"parallel_write_safe": True, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import pathlib | ||
from docutils import nodes | ||
from functools import lru_cache | ||
|
||
import yaml | ||
from sphinx.util.docutils import SphinxRole | ||
|
||
|
||
@lru_cache | ||
def load_frontmatter(path): | ||
"""Load and return the front matter section of a YAML file.""" | ||
with open(path, "r") as fp: | ||
frontmatter = next(yaml.safe_load_all(fp)) | ||
|
||
return frontmatter | ||
|
||
|
||
@lru_cache | ||
def create_flight_badge(src, cat_id): | ||
"""Return an HTML node based on a flight category id.""" | ||
with open(src / "reports" / "flight_categories.yaml", "r") as fp: | ||
cat_tier = { | ||
key: attrs["tier"] | ||
for key, attrs in yaml.safe_load(fp)["categories"].items() | ||
}.get(cat_id, "unknown") | ||
|
||
span = f'<span class="badge cat-{cat_tier} cat-{cat_id}">{cat_id}</span>' | ||
href = f'<a href="https://orcestra-campaign.org/search.html?q={cat_id}">{span}</a>' | ||
node = nodes.raw( | ||
text=href, | ||
format="html", | ||
) | ||
|
||
return node | ||
|
||
|
||
class FlightCategoryRole(SphinxRole): | ||
"""Create a badge based on a given flight category.""" | ||
def run(self): | ||
src = pathlib.Path(self.env.srcdir) | ||
node = create_flight_badge(src, self.text) | ||
|
||
return [node], [] | ||
|
||
|
||
class BadgesRole(SphinxRole): | ||
"""Create a list of badges based on all categories defined in YAML fron matter.""" | ||
def run(self): | ||
src = pathlib.Path(self.env.srcdir) | ||
fm = load_frontmatter(self.env.doc2path(self.env.docname)) | ||
categories = fm.get("categories", []) | ||
|
||
node_list = [create_flight_badge(src, cat_id) for cat_id in categories] | ||
|
||
node = nodes.raw( | ||
text=" ".join(n.astext() for n in node_list), | ||
format="html", | ||
) | ||
|
||
return [node], [] | ||
|
||
|
||
def setup(app): | ||
app.add_role("flight-cat", FlightCategoryRole()) | ||
app.add_role("badges", BadgesRole()) | ||
|
||
return { | ||
"version": "0.1", | ||
"parallel_read_safe": True, | ||
"parallel_write_safe": True, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from docutils import nodes | ||
from functools import lru_cache | ||
|
||
import yaml | ||
from sphinx.util.docutils import SphinxRole | ||
|
||
|
||
@lru_cache | ||
def load_frontmatter(path): | ||
"""Load and return the front matter section of a YAML file.""" | ||
with open(path, "r") as fp: | ||
frontmatter = next(yaml.safe_load_all(fp)) | ||
|
||
return frontmatter | ||
|
||
|
||
class FrontmatterRole(SphinxRole): | ||
def run(self): | ||
"""Access variables defined in document front matter.""" | ||
fm = load_frontmatter(self.env.doc2path(self.env.docname)) | ||
|
||
return nodes.raw(text=fm[self.text]), [] | ||
|
||
|
||
def setup(app): | ||
app.add_role("front", FrontmatterRole()) | ||
|
||
return { | ||
"version": "0.1", | ||
"parallel_read_safe": True, | ||
"parallel_write_safe": True, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.