Skip to content

Commit

Permalink
Merge pull request #7879 from tk0miya/7849_html_codeblock_linenos
Browse files Browse the repository at this point in the history
Close #7849: html: Add html_codeblock_linenos
  • Loading branch information
tk0miya committed Jul 11, 2020
2 parents 5f1e948 + 6dfbc51 commit 1a31a7c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions doc/usage/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``<table>`` tag (default)
* ``'inline'`` -- display line numbers using ``<span>`` tag

.. versionadded:: 3.2

.. 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 @@ -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
Expand Down Expand Up @@ -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')

Expand Down
3 changes: 3 additions & 0 deletions sphinx/writers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions sphinx/writers/html5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file.
7 changes: 7 additions & 0 deletions tests/roots/test-reST-code-block/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. code-block::
:linenos:
def hello(name)
print("hello", name)
hello("Sphinx")
18 changes: 18 additions & 0 deletions tests/test_build_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575,3 +1575,21 @@ def test_html_scaled_image_link(app):
assert re.search('\n<img alt="_images/img.png" class="no-scaled-link"'
' src="_images/img.png" style="[^"]+" />',
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 '<div class="linenodiv"><pre>1\n2\n3\n4</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()

assert '<span class="lineno">1 </span>' in content

0 comments on commit 1a31a7c

Please sign in to comment.