From 0ded648c1a1aa4f7649d89f209f070cef5bbaa7f Mon Sep 17 00:00:00 2001 From: James Knight Date: Thu, 4 Oct 2018 02:55:35 -0400 Subject: [PATCH 01/16] directive-code: do not force linenos value on run Now that `highlightlang` directive is deprecated [1], the `linenothreshold` option is to be used via the `highlight` directive. The `highlight` directive will walk-through literal blocks to apply a `linenos` value if: 1) The literal block has not been explicitly configured with the `linenos` option. 2) If there is enough content (when comparing literal block's line count to `linenothreshold`) that `linenos` should be explicitly enabled or disabled [2]. While the `hightlight` directive should be able to explicitly define if a literal block needs to enable line numbers, the logic is always ignored since the code block and literal include directives already configures `linenos` when checking for line number-specific options on the node [3][4]. This effectively prevents `linenothreshold` from being used. To allow `linenothreshold` to be used in literal blocks, this commit disables the explicit configuration `linenos` on a literal block instance when the `CodeBlock` and `LiteralInclude` directives are processed. [1]: b35198d8475fe89aaf9a5224a9832fb394e73cca [2]: https://github.com/sphinx-doc/sphinx/blob/v1.8.1/sphinx/transforms/post_transforms/code.py#L95-L97 [3]: https://github.com/sphinx-doc/sphinx/blob/v1.8.1/sphinx/directives/code.py#L156-L157 [4]: https://github.com/sphinx-doc/sphinx/blob/v1.8.1/sphinx/directives/code.py#L442-L444 Signed-off-by: James Knight --- sphinx/directives/code.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py index 9ac704c55..73afac4ba 100644 --- a/sphinx/directives/code.py +++ b/sphinx/directives/code.py @@ -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: From 3858a62814f1a3d78f7c2cb7b84e34b3dd056b80 Mon Sep 17 00:00:00 2001 From: James Knight Date: Sat, 16 Feb 2019 21:44:32 -0500 Subject: [PATCH 02/16] test: verify linenothreshold usage Adding a unit test to verify the use of the `linenothreshold` option provided by the `highlight` directive [1]. The included document to test will introduce two sets of `code-block` and `literalinclude` directives where the first entry generates contents using line numbers and the second entry generates contents not using line numbers, respectfully. [1]: https://github.com/sphinx-doc/sphinx/blob/v1.8.4/sphinx/transforms/post_transforms/code.py#L85 Signed-off-by: James Knight --- .../test-directive-code/linenothreshold.rst | 23 +++++++++ .../test-directive-code/literal-short.inc | 3 ++ tests/test_directive_code.py | 49 +++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 tests/roots/test-directive-code/linenothreshold.rst create mode 100644 tests/roots/test-directive-code/literal-short.inc diff --git a/tests/roots/test-directive-code/linenothreshold.rst b/tests/roots/test-directive-code/linenothreshold.rst new file mode 100644 index 000000000..09ee67efc --- /dev/null +++ b/tests/roots/test-directive-code/linenothreshold.rst @@ -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 diff --git a/tests/roots/test-directive-code/literal-short.inc b/tests/roots/test-directive-code/literal-short.inc new file mode 100644 index 000000000..7a07a3f7a --- /dev/null +++ b/tests/roots/test-directive-code/literal-short.inc @@ -0,0 +1,3 @@ +# Very small literal include (linenothreshold check) + +value = True diff --git a/tests/test_directive_code.py b/tests/test_directive_code.py index 8627820b9..2e1a1fde2 100644 --- a/tests/test_directive_code.py +++ b/tests/test_directive_code.py @@ -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 = '
'
+    lineos_tail = '
' + + # 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 From 55c5168f338d6a9625c789af6c4b9ab04eb5ec93 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen Date: Thu, 21 Feb 2019 20:11:24 -0500 Subject: [PATCH 03/16] #3620: Defer searchindex.js rather than loading it via ajax --- sphinx/themes/basic/search.html | 7 +------ sphinx/themes/basic/static/searchtools.js | 10 ---------- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/sphinx/themes/basic/search.html b/sphinx/themes/basic/search.html index 04f0d1de2..7446a8f25 100644 --- a/sphinx/themes/basic/search.html +++ b/sphinx/themes/basic/search.html @@ -14,12 +14,7 @@ {%- endblock %} {% block extrahead %} - - {# this is used when loading the search index using $.ajax fails, - such as on Chrome for documents on localhost #} - + {{ super() }} {% endblock %} {% block body %} diff --git a/sphinx/themes/basic/static/searchtools.js b/sphinx/themes/basic/static/searchtools.js index bdc270655..4c5826411 100644 --- a/sphinx/themes/basic/static/searchtools.js +++ b/sphinx/themes/basic/static/searchtools.js @@ -75,16 +75,6 @@ var Search = { } }, - loadIndex : function(url) { - $.ajax({type: "GET", url: url, data: null, - dataType: "script", cache: true, - complete: function(jqxhr, textstatus) { - if (textstatus != "success") { - document.getElementById("searchindexloader").src = url; - } - }}); - }, - setIndex : function(index) { var q; this._index = index; From 524ac7ff0a1b1ccd81365c1a3857cd70947acaf1 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 24 Feb 2019 17:44:59 +0900 Subject: [PATCH 04/16] Fix #6096: html: Anchor links are not added to figures --- CHANGES | 2 ++ sphinx/writers/html.py | 4 +--- sphinx/writers/html5.py | 4 +--- tests/test_build_html.py | 9 +++++++++ 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index 2781cddf8..e6ccfad27 100644 --- a/CHANGES +++ b/CHANGES @@ -16,6 +16,8 @@ Features added Bugs fixed ---------- +* #6096: html: Anchor links are not added to figures + Testing -------- diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index 454e7c4d2..3eb9a0911 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -509,9 +509,7 @@ class HTMLTranslator(SphinxTranslator, BaseTranslator): if isinstance(node.parent, nodes.container) and node.parent.get('literal_block'): self.add_permalink_ref(node.parent, _('Permalink to this code')) elif isinstance(node.parent, nodes.figure): - image_nodes = node.parent.traverse(nodes.image) - target_node = image_nodes and image_nodes[0] or node.parent - self.add_permalink_ref(target_node, _('Permalink to this image')) + self.add_permalink_ref(node.parent, _('Permalink to this image')) elif node.parent.get('toctree'): self.add_permalink_ref(node.parent.parent, _('Permalink to this toctree')) diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py index 5a10c990f..646d6cc31 100644 --- a/sphinx/writers/html5.py +++ b/sphinx/writers/html5.py @@ -455,9 +455,7 @@ class HTML5Translator(SphinxTranslator, BaseTranslator): if isinstance(node.parent, nodes.container) and node.parent.get('literal_block'): self.add_permalink_ref(node.parent, _('Permalink to this code')) elif isinstance(node.parent, nodes.figure): - image_nodes = node.parent.traverse(nodes.image) - target_node = image_nodes and image_nodes[0] or node.parent - self.add_permalink_ref(target_node, _('Permalink to this image')) + self.add_permalink_ref(node.parent, _('Permalink to this image')) elif node.parent.get('toctree'): self.add_permalink_ref(node.parent.parent, _('Permalink to this toctree')) diff --git a/tests/test_build_html.py b/tests/test_build_html.py index 77c86962e..99d7bf8d4 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -1254,6 +1254,15 @@ def test_html_inventory(app): 'The basic Sphinx documentation for testing') +@pytest.mark.sphinx('html', testroot='images', confoverrides={'html_sourcelink_suffix': ''}) +def test_html_anchor_for_figure(app): + app.builder.build_all() + content = (app.outdir / 'index.html').text() + assert ('

The caption of pic' + '

' + in content) + + @pytest.mark.sphinx('html', testroot='directives-raw') def test_html_raw_directive(app, status, warning): app.builder.build_all() From 0dec09a0651a458ca5e3830d45f9e1ff1cb156f3 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Fri, 22 Feb 2019 13:15:42 +0900 Subject: [PATCH 05/16] Update warning messages for script_files --- sphinx/builders/html.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index bb11bfb4a..2c5ebcd4d 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -109,24 +109,24 @@ class JSContainer(list): """The container for JavaScript scripts.""" def insert(self, index, obj): # type: (int, str) -> None - warnings.warn('builder.script_files is deprecated. ' - 'Please use app.add_js_file() instead.', - RemovedInSphinx30Warning, stacklevel=2) + warnings.warn('To modify script_files in the theme is deprecated. ' + 'Please insert a