Skip to content

Commit

Permalink
FEAT: define wiki Sphinx role
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Dec 9, 2023
1 parent be05b51 commit b8c73dd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"refspecific",
"reftarget",
"reftype",
"refuri",
"rtfd",
"srcdir",
"templatedir"
Expand Down
8 changes: 1 addition & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,5 @@
"python.testing.unittestEnabled": false,
"rewrap.wrappingColumn": 88,
"ruff.enable": true,
"ruff.organizeImports": true,
"search.exclude": {
"**/tests/**/__init__.py": true,
"*/.pydocstyle": true,
"src/*/*/__init__.py": true,
"src/*/__init__.py": true
}
"ruff.organizeImports": true
}
24 changes: 24 additions & 0 deletions src/sphinx_api_relink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
from sphinx_api_relink.linkcode import get_linkcode_resolve

if TYPE_CHECKING:
from docutils.parsers.rst.states import Inliner
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment
from sphinx.util.typing import RoleFunction


def setup(app: Sphinx) -> dict[str, Any]:
Expand All @@ -29,6 +31,7 @@ def setup(app: Sphinx) -> dict[str, Any]:
app.connect("config-inited", set_linkcode_resolve)
app.connect("config-inited", generate_apidoc)
app.connect("config-inited", replace_type_to_xref)
app.add_role("wiki", wiki_role("https://en.wikipedia.org/wiki/%s"))
return {
"parallel_read_safe": True,
"parallel_write_safe": True,
Expand Down Expand Up @@ -187,3 +190,24 @@ def _create_nodes(env: BuildEnvironment, title: str) -> list[nodes.Node]:
pending_xref_condition("", title, condition="*"),
]
return [nodes.Text(short_name)]


def wiki_role(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[nodes.Node], list[nodes.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

0 comments on commit b8c73dd

Please sign in to comment.