Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: define wiki Sphinx role #14

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading