diff --git a/CHANGES b/CHANGES index 0355e9e4c..71a2b137f 100644 --- a/CHANGES +++ b/CHANGES @@ -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 ---------- diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py index f31ba7a99..5b4bcde6d 100644 --- a/sphinx/directives/code.py +++ b/sphinx/directives/code.py @@ -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 diff --git a/tests/roots/test-directive-code/classes.rst b/tests/roots/test-directive-code/classes.rst new file mode 100644 index 000000000..e9aa5d9c4 --- /dev/null +++ b/tests/roots/test-directive-code/classes.rst @@ -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 diff --git a/tests/test_directive_code.py b/tests/test_directive_code.py index 4f07beb99..25bed6c05 100644 --- a/tests/test_directive_code.py +++ b/tests/test_directive_code.py @@ -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')