From 6dfbc5108e8883dfc0c5347759e45af2cfb71694 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 28 Jun 2020 02:37:29 +0900 Subject: [PATCH] Close #7849: html: Add html_codeblock_linenos_style --- CHANGES | 2 ++ doc/usage/configuration.rst | 9 +++++++++ sphinx/builders/html/__init__.py | 4 +++- sphinx/writers/html.py | 3 +++ sphinx/writers/html5.py | 3 +++ tests/roots/test-reST-code-block/conf.py | 0 tests/roots/test-reST-code-block/index.rst | 7 +++++++ tests/test_build_html.py | 18 ++++++++++++++++++ 8 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/roots/test-reST-code-block/conf.py create mode 100644 tests/roots/test-reST-code-block/index.rst diff --git a/CHANGES b/CHANGES index 7c2748b98ab..562bc8006b5 100644 --- a/CHANGES +++ b/CHANGES @@ -13,6 +13,8 @@ Deprecated Features added -------------- +* #7849: html: Add :confval:`html_codeblock_linenos_style` to change the style + of line numbers for code-blocks * #7853: C and C++, support parameterized GNU style attributes. * #7888: napoleon: Add aliases Warn and Raise. * C, added :rst:dir:`c:alias` directive for inserting copies diff --git a/doc/usage/configuration.rst b/doc/usage/configuration.rst index 8f6280b8ba3..84e12cbb0a5 100644 --- a/doc/usage/configuration.rst +++ b/doc/usage/configuration.rst @@ -926,6 +926,15 @@ 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 ```` tag (default) + * ``'inline'`` -- display line numbers using ```` tag + + .. versionadded:: 3.2 + .. confval:: html_context A dictionary of values to pass into the template engine's context for all diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 99d7aa13b21..c0c4f847590 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -26,7 +26,7 @@ from sphinx import package_dir, __display_version__ from sphinx.application import Sphinx from sphinx.builders import Builder -from sphinx.config import Config +from sphinx.config import Config, ENUM from sphinx.deprecation import RemovedInSphinx40Warning from sphinx.domains import Domain, Index, IndexEntry from sphinx.environment.adapters.asset import ImageAdapter @@ -1226,6 +1226,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', 'table', 'html', + ENUM('table', 'inline')) app.add_config_value('html_math_renderer', None, 'env') app.add_config_value('html4_writer', False, 'html') diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index 85eeb43760b..4375bf32637 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -445,6 +445,9 @@ def visit_literal_block(self, node: Element) -> None: else: opts = {} + if linenos and self.builder.config.html_codeblock_linenos_style: + linenos = self.builder.config.html_codeblock_linenos_style + highlighted = self.highlighter.highlight_block( node.rawsource, lang, opts=opts, linenos=linenos, location=(self.builder.current_docname, node.line), **highlight_args diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py index 80cedd3bdd8..c1252f4d047 100644 --- a/sphinx/writers/html5.py +++ b/sphinx/writers/html5.py @@ -397,6 +397,9 @@ def visit_literal_block(self, node: Element) -> None: else: opts = {} + if linenos and self.builder.config.html_codeblock_linenos_style: + linenos = self.builder.config.html_codeblock_linenos_style + highlighted = self.highlighter.highlight_block( node.rawsource, lang, opts=opts, linenos=linenos, location=(self.builder.current_docname, node.line), **highlight_args diff --git a/tests/roots/test-reST-code-block/conf.py b/tests/roots/test-reST-code-block/conf.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/roots/test-reST-code-block/index.rst b/tests/roots/test-reST-code-block/index.rst new file mode 100644 index 00000000000..a7c7df03a09 --- /dev/null +++ b/tests/roots/test-reST-code-block/index.rst @@ -0,0 +1,7 @@ +.. code-block:: + :linenos: + + def hello(name) + print("hello", name) + + hello("Sphinx") diff --git a/tests/test_build_html.py b/tests/test_build_html.py index 8d616adaf37..36dae0d0e50 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -1575,3 +1575,21 @@ def test_html_scaled_image_link(app): assert re.search('\n_images/img.png', context) + + +@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() + + assert '
1\n2\n3\n4
' 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() + + assert '1 ' in content