From e5d91f81656e5f31a2e874d551c890f4e8c68b4a Mon Sep 17 00:00:00 2001 From: "Terence D. Honles" Date: Tue, 6 Apr 2021 15:59:38 -0700 Subject: [PATCH 1/2] Ignore unknown directive options rather than aborting --- readme_renderer/rst.py | 32 ++++++++++++++++++++++++++++++++ tests/fixtures/test_rst_008.html | 9 +++++++++ tests/fixtures/test_rst_008.rst | 13 +++++++++++++ 3 files changed, 54 insertions(+) diff --git a/readme_renderer/rst.py b/readme_renderer/rst.py index 5a9ff69..2662429 100644 --- a/readme_renderer/rst.py +++ b/readme_renderer/rst.py @@ -15,6 +15,7 @@ import io +from docutils import utils from docutils.core import publish_parts from docutils.writers.html4css1 import HTMLTranslator, Writer from docutils.utils import SystemMessage @@ -22,6 +23,37 @@ from .clean import clean +def extract_extension_options(field_list, option_spec): + """ + Overrides `utils.extract_extension_options` and inlines + `utils.assemble_option_dict` to make it ignore unknown options passed to + directives (i.e. ``:caption:`` for ``.. code-block:``). + """ + + dropped = set() + options = {} + for name, value in utils.extract_options(field_list): + convertor = option_spec.get(name) + if name in options or name in dropped: + raise utils.DuplicateOptionError('duplicate option "%s"' % name) + + # silently drop unknown options as long as they are not duplicates + if convertor is None: + dropped.add(name) + continue + + # continue as before + try: + options[name] = convertor(value) + except (ValueError, TypeError) as detail: + raise detail.__class__('(option: "%s"; value: %r)\n%s' + % (name, value, ' '.join(detail.args))) + return options + + +utils.extract_extension_options = extract_extension_options + + class ReadMeHTMLTranslator(HTMLTranslator): # Overrides base class not to output `` tag for SVG images. diff --git a/tests/fixtures/test_rst_008.html b/tests/fixtures/test_rst_008.html index 81d2f48..35ec418 100644 --- a/tests/fixtures/test_rst_008.html +++ b/tests/fixtures/test_rst_008.html @@ -14,3 +14,12 @@ fi

or click SurveyMonkey

+

and an ignored Sphinx option:

+
 def fib(n):
+     if n < 1:
+         return 0
+     elif n <= 2:
+         return 1
+
+     return fib(n - 1) + fib(n - 2)
+
diff --git a/tests/fixtures/test_rst_008.rst b/tests/fixtures/test_rst_008.rst index 183512c..d361e16 100644 --- a/tests/fixtures/test_rst_008.rst +++ b/tests/fixtures/test_rst_008.rst @@ -20,3 +20,16 @@ and then here is some bash: fi or click `SurveyMonkey `_ + +and an ignored Sphinx option: + +.. code-block:: python + :caption: Naive Fibonacci computation + + def fib(n): + if n < 1: + return 0 + elif n <= 2: + return 1 + + return fib(n - 1) + fib(n - 2) From e511ec3f0f0839704dea40cdf5de23d14cf86025 Mon Sep 17 00:00:00 2001 From: "Terence D. Honles" Date: Tue, 6 Apr 2021 16:07:53 -0700 Subject: [PATCH 2/2] fix CI installing latest Python, but tox wanting Python 3.6 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 828b2bf..37e9a54 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,7 +27,7 @@ jobs: - name: Install Python uses: actions/setup-python@v2 with: - python-version: "3.x" + python-version: "3.6" - name: Install dependencies run: python -m pip install tox - name: Run linting