mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #5508 from jdknight/bugfix/allow-applying-linenos-with-post-transform
directive-code: do not force linenos value on run
This commit is contained in:
commit
2745f1e156
@ -154,8 +154,8 @@ class CodeBlock(SphinxDirective):
|
||||
code = '\n'.join(lines)
|
||||
|
||||
literal = nodes.literal_block(code, code) # type: nodes.Element
|
||||
literal['linenos'] = 'linenos' in self.options or \
|
||||
'lineno-start' in self.options
|
||||
if 'linenos' in self.options or 'lineno-start' in self.options:
|
||||
literal['linenos'] = True
|
||||
literal['classes'] += self.options.get('class', [])
|
||||
if self.arguments:
|
||||
# highlight language specified
|
||||
@ -451,9 +451,9 @@ class LiteralInclude(SphinxDirective):
|
||||
retnode['language'] = 'udiff'
|
||||
elif 'language' in self.options:
|
||||
retnode['language'] = self.options['language']
|
||||
retnode['linenos'] = ('linenos' in self.options or
|
||||
'lineno-start' in self.options or
|
||||
'lineno-match' in self.options)
|
||||
if ('linenos' in self.options or 'lineno-start' in self.options or
|
||||
'lineno-match' in self.options):
|
||||
retnode['linenos'] = True
|
||||
retnode['classes'] += self.options.get('class', [])
|
||||
extra_args = retnode['highlight_args'] = {}
|
||||
if 'emphasize-lines' in self.options:
|
||||
|
23
tests/roots/test-directive-code/linenothreshold.rst
Normal file
23
tests/roots/test-directive-code/linenothreshold.rst
Normal file
@ -0,0 +1,23 @@
|
||||
Code Blocks and Literal Includes with Line Numbers via linenothreshold
|
||||
======================================================================
|
||||
|
||||
.. highlight:: python
|
||||
:linenothreshold: 5
|
||||
|
||||
.. code-block::
|
||||
|
||||
class Foo:
|
||||
pass
|
||||
|
||||
class Bar:
|
||||
def baz():
|
||||
pass
|
||||
|
||||
.. code-block::
|
||||
|
||||
# comment
|
||||
value = True
|
||||
|
||||
.. literalinclude:: literal.inc
|
||||
|
||||
.. literalinclude:: literal-short.inc
|
3
tests/roots/test-directive-code/literal-short.inc
Normal file
3
tests/roots/test-directive-code/literal-short.inc
Normal file
@ -0,0 +1,3 @@
|
||||
# Very small literal include (linenothreshold check)
|
||||
|
||||
value = True
|
@ -565,3 +565,52 @@ def test_code_block_highlighted(app, status, warning):
|
||||
assert codeblocks[1]['language'] == 'python2'
|
||||
assert codeblocks[2]['language'] == 'python3'
|
||||
assert codeblocks[3]['language'] == 'python2'
|
||||
|
||||
|
||||
@pytest.mark.sphinx('html', testroot='directive-code')
|
||||
def test_linenothreshold(app, status, warning):
|
||||
app.builder.build(['linenothreshold'])
|
||||
html = (app.outdir / 'linenothreshold.html').text()
|
||||
|
||||
lineos_head = '<td class="linenos"><div class="linenodiv"><pre>'
|
||||
lineos_tail = '</pre></div></td>'
|
||||
|
||||
# code-block using linenothreshold
|
||||
_, matched, html = html.partition(lineos_head +
|
||||
'1\n'
|
||||
'2\n'
|
||||
'3\n'
|
||||
'4\n'
|
||||
'5\n'
|
||||
'6' + lineos_tail)
|
||||
assert matched
|
||||
|
||||
# code-block not using linenothreshold
|
||||
html, matched, _ = html.partition(lineos_head +
|
||||
'1\n'
|
||||
'2' + lineos_tail)
|
||||
assert not matched
|
||||
|
||||
# literal include using linenothreshold
|
||||
_, matched, html = html.partition(lineos_head +
|
||||
' 1\n'
|
||||
' 2\n'
|
||||
' 3\n'
|
||||
' 4\n'
|
||||
' 5\n'
|
||||
' 6\n'
|
||||
' 7\n'
|
||||
' 8\n'
|
||||
' 9\n'
|
||||
'10\n'
|
||||
'11\n'
|
||||
'12\n'
|
||||
'13' + lineos_tail)
|
||||
assert matched
|
||||
|
||||
# literal include not using linenothreshold
|
||||
html, matched, _ = html.partition(lineos_head +
|
||||
'1\n'
|
||||
'2\n'
|
||||
'3' + lineos_tail)
|
||||
assert not matched
|
||||
|
Loading…
Reference in New Issue
Block a user