Fix #2113: Allow `:class:` option to code-block directive

This commit is contained in:
Takeshi KOMIYA 2015-12-20 22:44:32 +09:00
parent bf1cc70996
commit 4234c88f21
4 changed files with 43 additions and 0 deletions

View File

@ -18,6 +18,7 @@ Features added
using HTTP basic auth
* C++, added support for template parameter in function info field lists.
* C++, added support for pointers to member (function).
* #2113: Allow ``:class:`` option to code-block directive
Bugs fixed
----------

View File

@ -93,6 +93,7 @@ class CodeBlock(Directive):
'lineno-start': int,
'emphasize-lines': directives.unchanged_required,
'caption': directives.unchanged_required,
'class': directives.class_option,
'name': directives.unchanged,
}
@ -119,6 +120,7 @@ class CodeBlock(Directive):
literal['language'] = self.arguments[0]
literal['linenos'] = 'linenos' in self.options or \
'lineno-start' in self.options
literal['classes'] += self.options.get('class', [])
extra_args = literal['highlight_args'] = {}
if hl_lines is not None:
extra_args['hl_lines'] = hl_lines
@ -165,6 +167,7 @@ class LiteralInclude(Directive):
'append': directives.unchanged_required,
'emphasize-lines': directives.unchanged_required,
'caption': directives.unchanged,
'class': directives.class_option,
'name': directives.unchanged,
'diff': directives.unchanged_required,
}
@ -322,6 +325,7 @@ class LiteralInclude(Directive):
retnode['linenos'] = 'linenos' in self.options or \
'lineno-start' in self.options or \
'lineno-match' in self.options
retnode['classes'] += self.options.get('class', [])
extra_args = retnode['highlight_args'] = {}
if hl_lines is not None:
extra_args['hl_lines'] = hl_lines

View File

@ -0,0 +1,21 @@
classes
=======
Code blocks
-----------
.. code-block:: ruby
:class: foo bar
:name: code_block
def ruby?
false
end
Literal Includes
----------------
.. literalinclude:: literal.inc
:class: bar baz
:name: literal_include

View File

@ -207,3 +207,20 @@ def test_literalinclude_caption_latex(app, status, warning):
latex = (app.outdir / 'Python.tex').text(encoding='utf-8')
caption = '\\captionof{literal-block}{caption \\textbf{test} py}'
assert caption in latex
@with_app('xml', testroot='directive-code')
def test_literalinclude_classes(app, status, warning):
app.builder.build(['classes'])
et = ElementTree.parse(app.outdir / 'classes.xml')
secs = et.findall('./section/section')
code_block = secs[0].findall('literal_block')
assert len(code_block) > 0
assert 'foo bar' == code_block[0].get('classes')
assert 'code_block' == code_block[0].get('names')
literalinclude = secs[1].findall('literal_block')
assert len(literalinclude) > 0
assert 'bar baz' == literalinclude[0].get('classes')
assert 'literal_include' == literalinclude[0].get('names')