Skip to content

Commit

Permalink
DX: outsource wiki role to sphinx-api-relink
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Dec 9, 2023
1 parent 2633a15 commit 2a9fa41
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 43 deletions.
44 changes: 1 addition & 43 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@
import sys
from datetime import datetime
from functools import lru_cache
from typing import TYPE_CHECKING, Any

from docutils import nodes

if TYPE_CHECKING:
from docutils.nodes import Node as docutils_Node
from docutils.nodes import system_message
from docutils.parsers.rst.states import Inliner
from sphinx.application import Sphinx
from sphinx.util.typing import RoleFunction

if sys.version_info < (3, 8):
from importlib_metadata import PackageNotFoundError
Expand Down Expand Up @@ -94,6 +84,7 @@ def get_repository_name() -> str:
"sphinx.ext.todo",
"sphinx.ext.viewcode",
"sphinx_codeautolink",
"sphinx_api_relink",
"sphinx_comments",
"sphinx_copybutton",
"sphinx_design",
Expand Down Expand Up @@ -299,36 +290,3 @@ def print_once(message: str) -> None:
"repository_url": html_theme_options["repository_url"],
"repository_branch": html_theme_options["repository_branch"],
}


# Add roles to simplify external links
def setup(app: Sphinx) -> dict[str, Any]:
app.add_role(
"wiki",
wikilink("https://en.wikipedia.org/wiki/%s"),
)
return {
"parallel_read_safe": True,
"parallel_write_safe": True,
}


def wikilink(pattern: str) -> RoleFunction:
def role( # noqa: PLR0913
name: str,
rawtext: str,
text: str,
lineno: int,
inliner: Inliner,
options: dict | None = None,
content: list[str] | None = None,
) -> tuple[list[docutils_Node], list[system_message]]:
output_text = text
output_text = output_text.replace("_", " ")
url = pattern % (text,)
if options is None:
options = {}
reference_node = nodes.reference(rawtext, output_text, refuri=url, **options)
return [reference_node], []

return role
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ doc = [
"matplotlib",
"myst-nb >=0.11", # myst_enable_extensions
"numpy <1.24", # bug in feynman: https://github.com/ComPWA/PWA-pages/actions/runs/3868808372/jobs/6594501436#step:6:177
"sphinx-api-relink >=0.0.8",
"sphinx-book-theme",
"sphinx-codeautolink[ipython]",
"sphinx-comments",
Expand Down

0 comments on commit 2a9fa41

Please sign in to comment.