Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nabobalis committed Aug 5, 2024
1 parent 95bac35 commit b6eab55
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/sunpy_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
@@ -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"]

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

Expand Down
83 changes: 83 additions & 0 deletions src/sunpy_sphinx_theme/cards.py
Original file line number Diff line number Diff line change
@@ -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"<h4>{title}</h4>" if len(title) > 0 else ""
col_extra_class = "column-half" if title else ""
body = f"""<div class="column {col_extra_class}">
{title}
<div class="card">
<img class="dark-light" src="/_static/img/{node['img_name']}" alt="{node['name']}">
<p>{node['name']}</p>
<p><button type="button" class="btn btn-sunpy btn-sunpy1" data-bs-toggle="modal" data-bs-target="#{key}">More Info</button></p>
<div class="modal fade" id="{key}" tabindex=-1>
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title center">{node['name']}</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
"""
self.body.append(body)


def depart_card_node(self, node):
body = f"""
<p>Affiliation: <a href="{node['aff_link']}">{node['aff_name']}</a></p>
<p>GitHub: <a href="https://github.com/{node['github']}">{node['github']}</a></p>
<p>Start Date: {node['date']}</p>
</div>
</div>
</div>
</div>
</div></div>"""
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")))

0 comments on commit b6eab55

Please sign in to comment.