From 7a19485f98af6963f71d2479be1cfd2841266e86 Mon Sep 17 00:00:00 2001 From: "Terence D. Honles" Date: Tue, 9 Mar 2021 14:10:30 -0800 Subject: [PATCH] fix unknown directive options removing the directive entirely --- lib/github/commands/rest2html | 31 ++++++++++++++++++++++++++++++- test/markups/README.rst | 6 ++++++ test/markups/README.rst.html | 3 +++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/github/commands/rest2html b/lib/github/commands/rest2html index 0efea607..c7c89dce 100755 --- a/lib/github/commands/rest2html +++ b/lib/github/commands/rest2html @@ -47,7 +47,7 @@ except: import codecs import io -from docutils import nodes +from docutils import nodes, utils from docutils.parsers.rst import directives, roles from docutils.parsers.rst.directives.body import CodeBlock, Directive from docutils.core import publish_parts @@ -76,6 +76,35 @@ from docutils import nodes original_behavior = False # Documents original docutils behavior github_display = True +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 + def unknown_directive(self, type_name): lineno = self.state_machine.abs_line_number() indented, indent, offset, blank_finish = \ diff --git a/test/markups/README.rst b/test/markups/README.rst index 54bd71ce..53cba9e0 100644 --- a/test/markups/README.rst +++ b/test/markups/README.rst @@ -39,6 +39,12 @@ The UTF-8 quote character in this table used to cause python to go boom. Now doc python.code('hooray') +.. code:: python + :caption: An ignored Sphinx option + :made-up-option: An ignored made up option + + python.code('hello world') + .. doctest:: ignored >>> some_function() diff --git a/test/markups/README.rst.html b/test/markups/README.rst.html index 031c3c4a..3ddfab76 100644 --- a/test/markups/README.rst.html +++ b/test/markups/README.rst.html @@ -64,6 +64,9 @@

Header 2

python.code('hooray')
+python.code('hello world')
+
+
 >>> some_function()
 'result'