Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert html_codeblock_linenos_style removal #10922

Merged
merged 1 commit into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/usage/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,20 @@ that use Sphinx's HTMLWriter class.

.. versionadded:: 1.8

.. confval:: html_codeblock_linenos_style

The style of line numbers for code-blocks.

* ``'table'`` -- display line numbers using ``<table>`` tag
* ``'inline'`` -- display line numbers using ``<span>`` tag (default)

.. versionadded:: 3.2
.. versionchanged:: 4.0

It defaults to ``'inline'``.

.. deprecated:: 4.0

.. confval:: html_context

A dictionary of values to pass into the template engine's context for all
Expand Down
4 changes: 3 additions & 1 deletion sphinx/builders/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from sphinx import version_info as sphinx_version
from sphinx.application import Sphinx
from sphinx.builders import Builder
from sphinx.config import Config
from sphinx.config import ENUM, Config
from sphinx.deprecation import RemovedInSphinx70Warning, deprecated_alias
from sphinx.domains import Domain, Index, IndexEntry
from sphinx.environment import BuildEnvironment
Expand Down Expand Up @@ -1371,6 +1371,8 @@ def setup(app: Sphinx) -> Dict[str, Any]:
app.add_config_value('html_search_scorer', '', None)
app.add_config_value('html_scaled_image_link', True, 'html')
app.add_config_value('html_baseurl', '', 'html')
app.add_config_value('html_codeblock_linenos_style', 'inline', 'html', # RemovedInSphinx70Warning # NOQA
ENUM('table', 'inline'))
app.add_config_value('html_math_renderer', None, 'env')
app.add_config_value('html4_writer', False, 'html')

Expand Down
5 changes: 4 additions & 1 deletion sphinx/writers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,14 @@ def visit_literal_block(self, node: Element) -> None:
return super().visit_literal_block(node)

lang = node.get('language', 'default')
linenos = node.get('linenos', False) and "inline"
linenos = node.get('linenos', False)
highlight_args = node.get('highlight_args', {})
highlight_args['force'] = node.get('force', False)
opts = self.config.highlight_options.get(lang, {})

if linenos and self.config.html_codeblock_linenos_style:
linenos = self.config.html_codeblock_linenos_style

highlighted = self.highlighter.highlight_block(
node.rawsource, lang, opts=opts, linenos=linenos,
location=node, **highlight_args
Expand Down
5 changes: 4 additions & 1 deletion sphinx/writers/html5.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,14 @@ def visit_literal_block(self, node: Element) -> None:
return super().visit_literal_block(node)

lang = node.get('language', 'default')
linenos = node.get('linenos', False) and "inline"
linenos = node.get('linenos', False)
highlight_args = node.get('highlight_args', {})
highlight_args['force'] = node.get('force', False)
opts = self.config.highlight_options.get(lang, {})

if linenos and self.config.html_codeblock_linenos_style:
linenos = self.config.html_codeblock_linenos_style

highlighted = self.highlighter.highlight_block(
node.rawsource, lang, opts=opts, linenos=linenos,
location=node, **highlight_args
Expand Down
17 changes: 15 additions & 2 deletions tests/test_build_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,8 +1598,21 @@ def test_html_scaled_image_link(app):
context)


@pytest.mark.sphinx('html', testroot='reST-code-block')
def test_html_codeblock_linenos_style(app):
@pytest.mark.sphinx('html', testroot='reST-code-block',
confoverrides={'html_codeblock_linenos_style': 'table'})
def test_html_codeblock_linenos_style_table(app):
app.build()
content = (app.outdir / 'index.html').read_text(encoding='utf8')

assert ('<div class="linenodiv"><pre><span class="normal">1</span>\n'
'<span class="normal">2</span>\n'
'<span class="normal">3</span>\n'
'<span class="normal">4</span></pre></div>') in content


@pytest.mark.sphinx('html', testroot='reST-code-block',
confoverrides={'html_codeblock_linenos_style': 'inline'})
def test_html_codeblock_linenos_style_inline(app):
app.build()
content = (app.outdir / 'index.html').read_text(encoding='utf8')

Expand Down