Skip to content

Commit 4690850

Browse files
authored
Merge pull request #1452 from terencehonles/fix-rst-directives-with-unknown-options
fix unknown directive options removing the directive entirely
2 parents b864244 + 7a19485 commit 4690850

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: CI
2-
on: push
2+
on: [push, pull_request]
33

44
env:
55
JRUBY_OPTS: -Xcext.enabled=true

lib/github/commands/rest2html

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ except:
4747
import codecs
4848
import io
4949

50-
from docutils import nodes
50+
from docutils import nodes, utils
5151
from docutils.parsers.rst import directives, roles
5252
from docutils.parsers.rst.directives.body import CodeBlock, Directive
5353
from docutils.core import publish_parts
@@ -76,6 +76,35 @@ from docutils import nodes
7676
original_behavior = False # Documents original docutils behavior
7777
github_display = True
7878

79+
def extract_extension_options(field_list, option_spec):
80+
"""
81+
Overrides `utils.extract_extension_options` and inlines
82+
`utils.assemble_option_dict` to make it ignore unknown options passed to
83+
directives (i.e. ``:caption:`` for ``.. code-block:``).
84+
"""
85+
86+
dropped = set()
87+
options = {}
88+
for name, value in utils.extract_options(field_list):
89+
convertor = option_spec.get(name)
90+
if name in options or name in dropped:
91+
raise utils.DuplicateOptionError('duplicate option "%s"' % name)
92+
93+
# silently drop unknown options as long as they are not duplicates
94+
if convertor is None:
95+
dropped.add(name)
96+
continue
97+
98+
# continue as before
99+
try:
100+
options[name] = convertor(value)
101+
except (ValueError, TypeError) as detail:
102+
raise detail.__class__('(option: "%s"; value: %r)\n%s'
103+
% (name, value, ' '.join(detail.args)))
104+
return options
105+
106+
utils.extract_extension_options = extract_extension_options
107+
79108
def unknown_directive(self, type_name):
80109
lineno = self.state_machine.abs_line_number()
81110
indented, indent, offset, blank_finish = \

test/markups/README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ The UTF-8 quote character in this table used to cause python to go boom. Now doc
3939
4040
python.code('hooray')
4141
42+
.. code:: python
43+
:caption: An ignored Sphinx option
44+
:made-up-option: An ignored made up option
45+
46+
python.code('hello world')
47+
4248
.. doctest:: ignored
4349

4450
>>> some_function()

test/markups/README.rst.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ <h2><a href="#id1">Header 2</a></h2>
6464
python.code('hooray')
6565
</pre>
6666
<pre lang="python">
67+
python.code('hello world')
68+
</pre>
69+
<pre lang="python">
6770
&gt;&gt;&gt; some_function()
6871
'result'
6972
</pre>

0 commit comments

Comments
 (0)