From 8e75e0dd6e010196cf51f9bfcb2abd76d7e212a0 Mon Sep 17 00:00:00 2001 From: Kazuya Takei Date: Mon, 19 Feb 2024 02:22:01 +0900 Subject: [PATCH] feat: Pre-implement to render psuedxml from STDIN by module execute --- .github/workflows/main.yml | 1 + functests/test_cli.py | 25 +++++++++++++++++++++++ src/rst_multi_refs/__main__.py | 36 ++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 functests/test_cli.py create mode 100644 src/rst_multi_refs/__main__.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 40a30cb..91ad3ba 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -36,6 +36,7 @@ jobs: run: | source .venv/bin/activate pytest + pytest functests build: runs-on: ubuntu-latest steps: diff --git a/functests/test_cli.py b/functests/test_cli.py new file mode 100644 index 0000000..42a0a65 --- /dev/null +++ b/functests/test_cli.py @@ -0,0 +1,25 @@ +"""Test cases for CLI testing.""" +import subprocess + + +def test_with_npm(): + """CLI I/O test with ``npm`` role.""" + source = ":npm:`react`" + expected = ( + "\n".join( + [ + '', + " ", + ' ', + " react", + ] + ) + + "\n" + ) + proc = subprocess.run( + ["python", "-m", "rst_multi_refs"], + input=source.encode(), + stdout=subprocess.PIPE, + ) + assert proc.returncode == 0 + assert proc.stdout == expected.encode() diff --git a/src/rst_multi_refs/__main__.py b/src/rst_multi_refs/__main__.py new file mode 100644 index 0000000..712a560 --- /dev/null +++ b/src/rst_multi_refs/__main__.py @@ -0,0 +1,36 @@ +"""CLI entrypoint for simple testing by users. + +.. note:: Current implementatoin is stub to pass functests. +""" +from typing import List, Optional + +from docutils import nodes +from docutils.core import publish_cmdline +from docutils.parsers.rst import roles +from docutils.parsers.rst.states import Inliner + + +def npm_reference_role( + role: str, + rawtext: str, + text: str, + lineno: int, + inliner: Inliner, + options: Optional[dict] = None, + content: Optional[List[str]] = None, +): + """Parse ``npm`` role.""" + options = roles.normalized_role_options(options) + messages = [] + url = f"https://www.npmjs.com/package/{text}" + return [nodes.reference(rawtext, text, refuri=url, **options)], messages + + +def configure(): + """Set up using roles.""" + roles.register_canonical_role("npm", npm_reference_role) + pass + + +configure() +publish_cmdline()