Skip to content

Commit

Permalink
feat: Add support for refer to PyPI packages
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Feb 23, 2024
1 parent e83fb0d commit 1536326
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions functests/roots/test-default/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Using node modules
==================

* :npm:`react`
* :pypi:`sphinx-revealjs`
1 change: 1 addition & 0 deletions functests/test_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ def test_work_on_sphinx(app: SphinxTestApp):
index_html = app.outdir / "index.html"
assert index_html.exists()
assert "https://www.npmjs.com/package/react" in index_html.read_text()
assert "https://pypi.org/project/sphinx-revealjs" in index_html.read_text()
26 changes: 26 additions & 0 deletions src/rst_multi_refs/registry/pypi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""PyPI registry refecence module."""
from typing import List, Optional

from docutils import nodes
from docutils.parsers.rst import roles
from docutils.parsers.rst.states import Inliner


def reference_role(
role: str,
rawtext: str,
text: str,
lineno: int,
inliner: Inliner,
options: Optional[dict] = None,
content: Optional[List[str]] = None,
):
"""Parser."""
options = roles.normalized_role_options(options)
messages = []
url = f"https://pypi.org/project/{text}"
return [nodes.reference(rawtext, text, refuri=url, **options)], messages


def setup(): # noqa: D103
roles.register_canonical_role("pypi", reference_role)

0 comments on commit 1536326

Please sign in to comment.