Skip to content

Commit

Permalink
feat(toolkit): Registry roles area created by creator function from t…
Browse files Browse the repository at this point in the history
…oolkit
  • Loading branch information
attakei committed Feb 24, 2024
1 parent ae274d4 commit 5eebd30
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
28 changes: 9 additions & 19 deletions src/rst_package_refs/registry/npm.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
"""NPM 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

from .. import toolkit


class Package(toolkit.Package):
"""Python package published on PyPI."""

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://www.npmjs.com/package/{text}"
return [nodes.reference(rawtext, text, refuri=url, **options)], messages
@property
def url(self): # noqa: D102
return f"https://www.npmjs.com/package/{self.name}"


def setup(): # noqa: D103
roles.register_canonical_role("npm", reference_role)
roles.register_canonical_role("npm", toolkit.create_reference_role(Package))
28 changes: 9 additions & 19 deletions src/rst_package_refs/registry/pypi.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
"""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

from .. import toolkit


class Package(toolkit.Package):
"""Python package published on PyPI."""

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
@property
def url(self): # noqa: D102
return f"https://pypi.org/project/{self.name}"


def setup(): # noqa: D103
roles.register_canonical_role("pypi", reference_role)
roles.register_canonical_role("pypi", toolkit.create_reference_role(Package))
26 changes: 25 additions & 1 deletion src/rst_package_refs/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import abc
import re
from dataclasses import dataclass
from typing import Tuple
from typing import List, Optional, Tuple, Type

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


@dataclass
Expand All @@ -26,6 +30,26 @@ def parse(cls, target) -> "Package":
return cls(name=target)


def create_reference_role(package_class: Type[Package]) -> callable:
"""Create custom-role function for assigned package type."""

def _reference_role(
role: str,
rawtext: str,
text: str,
lineno: int,
inliner: Inliner,
options: Optional[dict] = None,
content: Optional[List[str]] = None,
):
options = roles.normalized_role_options(options)
messages = []
package = package_class.parse(text)
return [nodes.reference(rawtext, text, refuri=package.url, **options)], messages

return _reference_role


def split_text(source: str) -> Tuple[str, str]:
"""Split from content to displaying text and target text.
Expand Down

0 comments on commit 5eebd30

Please sign in to comment.