From b6eab55c49c43cb749977d1afd9bc9e99309da8d Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Mon, 5 Aug 2024 14:01:25 -0700 Subject: [PATCH] WIP --- src/sunpy_sphinx_theme/__init__.py | 11 +++- src/sunpy_sphinx_theme/cards.py | 83 ++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/sunpy_sphinx_theme/cards.py diff --git a/src/sunpy_sphinx_theme/__init__.py b/src/sunpy_sphinx_theme/__init__.py index 46cba1f4..03e0d34c 100644 --- a/src/sunpy_sphinx_theme/__init__.py +++ b/src/sunpy_sphinx_theme/__init__.py @@ -1,14 +1,16 @@ """ SunPy Sphinx Theme. """ - import os from functools import partial from pathlib import Path from urllib.parse import urljoin - from pydata_sphinx_theme import utils from sphinx.application import Sphinx +from pathlib import Path +from docutils import nodes +from docutils.parsers.rst import Directive, directives +from sphinx.util.fileutil import copy_asset __all__ = ["get_html_theme_path", "ON_RTD", "PNG_ICON", "SVG_ICON"] @@ -134,12 +136,17 @@ def update_html_context(app: Sphinx, pagename: str, templatename: str, context, context["sst_pathto"] = partial(sst_pathto, context) + + def setup(app: Sphinx): # Register theme theme_dir = get_html_theme_path() app.add_html_theme("sunpy", theme_dir) app.add_css_file("sunpy_style.css", priority=600) + app.add_directive("custom-card", Card) + app.add_node(card, html=(visit_card_node, depart_card_node)) + app.connect("build-finished", copy_asset_files) app.connect("builder-inited", update_config) app.connect("html-page-context", update_html_context) diff --git a/src/sunpy_sphinx_theme/cards.py b/src/sunpy_sphinx_theme/cards.py new file mode 100644 index 00000000..4c7275db --- /dev/null +++ b/src/sunpy_sphinx_theme/cards.py @@ -0,0 +1,83 @@ + +class card(nodes.General, nodes.Element): + pass + + +def visit_card_node(self, node): + title = node.get("title", "") + key = title or node["github"] + key = key.lower().replace(" ", "-") + title = f"

{title}

" if len(title) > 0 else "" + col_extra_class = "column-half" if title else "" + body = f"""
+ {title} +
+ {node['name']} +

{node['name']}

+

+ +
""" + self.body.append(body) + + +class Card(Directive): + has_content = True + required_arguments = 1 + optional_arguments = 6 + option_spec = { # NOQA: RUF012 + "img_name": directives.unchanged, + "title": directives.unchanged, + "github": directives.unchanged, + "aff_name": directives.unchanged, + "aff_link": directives.unchanged, + "date": directives.unchanged, + "desc": directives.unchanged, + } + + def run(self): + title = self.options.get("title") if "title" in self.options else "" + img_name = self.options.get("img_name") if "img_name" in self.options else "sunpy_icon.svg" + github = self.options.get("github") if "github" in self.options else "" + aff_name = self.options.get("aff_name") if "aff_name" in self.options else "" + aff_link = self.options.get("aff_link") if "aff_link" in self.options else "" + date = self.options.get("date") if "date" in self.options else "" + desc = self.options.get("desc") if "desc" in self.options else "N/A" + name = " ".join(self.arguments) + out = card( + name=name, + img_name=img_name, + title=title, + github=github, + aff_name=aff_name, + aff_link=aff_link, + date=date, + desc=desc, + ) + self.state.nested_parse(self.content, 0, out) + return [out] + + +def copy_asset_files(app, exc): + if exc is None: # Build succeeded + for path in (Path(__file__).parent / "static").glob("*"): + copy_asset(str(path), str(Path(app.outdir) / Path("_static")))