From 6123ea49e53b57e200138744dc618a797633d4d6 Mon Sep 17 00:00:00 2001 From: jfbu Date: Sun, 3 Apr 2016 15:14:13 +0200 Subject: [PATCH 01/10] Fix #2405: numref link in PDF jumps to the wrong location The hypertarget insertion by the LaTeX writer for literal blocks is moved from depart_container to visit_caption. modified: sphinx/writers/latex.py --- sphinx/writers/latex.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 6019d04ed..943f5a170 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1444,8 +1444,9 @@ class LaTeXTranslator(nodes.NodeVisitor): self.in_caption += 1 if self.in_container_literal_block: self.body.append('\\needspace{\\literalblockneedspace}') - self.body.append('\\vspace{\\literalblockcaptiontopvspace}%') - self.body.append('\n\\SphinxSetupCaptionForVerbatim{literal-block}{') + self.body.append('\\vspace{\\literalblockcaptiontopvspace}\n') + self.body.append(self.context.pop()) + self.body.append('\\SphinxSetupCaptionForVerbatim{literal-block}{') elif self.in_minipage and isinstance(node.parent, nodes.figure): self.body.append('\\captionof{figure}{') else: @@ -1998,7 +1999,7 @@ class LaTeXTranslator(nodes.NodeVisitor): def depart_container(self, node): if node.get('literal_block'): self.in_container_literal_block -= 1 - self.body.append(self.context.pop()) + # self.body.append(self.context.pop()) moved to visit_caption def visit_decoration(self, node): pass From a1647b6bb9f89141a9ba5b3579cc1bce8d4b18c2 Mon Sep 17 00:00:00 2001 From: jfbu Date: Sun, 3 Apr 2016 16:21:28 +0200 Subject: [PATCH 02/10] Fix broken numeric reference in PDF hyperlinks to "Listing " code-blocks The `\label` is now inserted into the caption on top of the Verbatim, and `\ref` will work as expected. No need for `\phantomsection`. modified: sphinx/writers/latex.py modified: tests/test_directive_code.py --- sphinx/writers/latex.py | 11 ++++++----- tests/test_directive_code.py | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 943f5a170..801726f4e 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1445,8 +1445,8 @@ class LaTeXTranslator(nodes.NodeVisitor): if self.in_container_literal_block: self.body.append('\\needspace{\\literalblockneedspace}') self.body.append('\\vspace{\\literalblockcaptiontopvspace}\n') - self.body.append(self.context.pop()) - self.body.append('\\SphinxSetupCaptionForVerbatim{literal-block}{') + self.body.append('\\SphinxSetupCaptionForVerbatim{literal-block}{' + + self.context.pop()) elif self.in_minipage and isinstance(node.parent, nodes.figure): self.body.append('\\captionof{figure}{') else: @@ -1992,14 +1992,15 @@ class LaTeXTranslator(nodes.NodeVisitor): for id in self.pop_hyperlink_ids('code-block'): ids += self.hypertarget(id, anchor=False) if node['ids']: - ids += self.hypertarget(node['ids'][0]) + # suppress with anchor=False \phantomsection generation + ids += self.hypertarget(node['ids'][0], anchor=False) self.body.append('\n') - self.context.append(ids + '\n') + if ids: + self.context.append(ids) # will be used in visit_caption def depart_container(self, node): if node.get('literal_block'): self.in_container_literal_block -= 1 - # self.body.append(self.context.pop()) moved to visit_caption def visit_decoration(self, node): pass diff --git a/tests/test_directive_code.py b/tests/test_directive_code.py index 6f7b749b1..59a440b53 100644 --- a/tests/test_directive_code.py +++ b/tests/test_directive_code.py @@ -64,7 +64,7 @@ def test_code_block_caption_html(app, status, warning): def test_code_block_caption_latex(app, status, warning): app.builder.build_all() latex = (app.outdir / 'Python.tex').text(encoding='utf-8') - caption = '\\SphinxSetupCaptionForVerbatim{literal-block}{caption \\emph{test} rb}' + caption = '\\SphinxSetupCaptionForVerbatim{literal-block}{\label{caption:caption-test-rb}caption \\emph{test} rb}' assert caption in latex @@ -229,7 +229,7 @@ def test_literalinclude_caption_html(app, status, warning): def test_literalinclude_caption_latex(app, status, warning): app.builder.build('index') latex = (app.outdir / 'Python.tex').text(encoding='utf-8') - caption = '\\SphinxSetupCaptionForVerbatim{literal-block}{caption \\textbf{test} py}' + caption = '\\SphinxSetupCaptionForVerbatim{literal-block}{\label{caption:caption-test-py}caption \\textbf{test} py}' assert caption in latex From a124a396257310d308a7298b89aa7bf6583c1405 Mon Sep 17 00:00:00 2001 From: jfbu Date: Sun, 3 Apr 2016 16:50:47 +0200 Subject: [PATCH 03/10] Remove unneeded test "if 'ids':" from previous commit. modified: sphinx/writers/latex.py --- sphinx/writers/latex.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 801726f4e..544fb60b6 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1995,8 +1995,7 @@ class LaTeXTranslator(nodes.NodeVisitor): # suppress with anchor=False \phantomsection generation ids += self.hypertarget(node['ids'][0], anchor=False) self.body.append('\n') - if ids: - self.context.append(ids) # will be used in visit_caption + self.context.append(ids) # will be used in visit_caption def depart_container(self, node): if node.get('literal_block'): From ad1cc969d33ea09f8baac677cf8953b8ece51b6c Mon Sep 17 00:00:00 2001 From: jfbu Date: Mon, 4 Apr 2016 10:10:05 +0200 Subject: [PATCH 04/10] Fix two issues in latex.py reported by Travis CI modified: sphinx/writers/latex.py --- sphinx/writers/latex.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index f0c51ced5..2ac92c149 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1445,8 +1445,7 @@ class LaTeXTranslator(nodes.NodeVisitor): if self.in_container_literal_block: self.body.append('\\needspace{\\literalblockneedspace}') self.body.append('\\vspace{\\literalblockcaptiontopvspace}\n') - self.body.append('\\SphinxSetupCaptionForVerbatim{literal-block}{' - + self.context.pop()) + self.body.append('\\SphinxSetupCaptionForVerbatim{literal-block}{' + self.context.pop()) elif self.in_minipage and isinstance(node.parent, nodes.figure): self.body.append('\\captionof{figure}{') else: @@ -1995,7 +1994,8 @@ class LaTeXTranslator(nodes.NodeVisitor): # suppress with anchor=False \phantomsection generation ids += self.hypertarget(node['ids'][0], anchor=False) self.body.append('\n') - self.context.append(ids) # will be used in visit_caption + # context.pop will be done in visit_caption + self.context.append(ids) def depart_container(self, node): if node.get('literal_block'): From 5cad1481aab59d67fff900eeaedc32fb2dc3242e Mon Sep 17 00:00:00 2001 From: jfbu Date: Mon, 4 Apr 2016 10:14:20 +0200 Subject: [PATCH 05/10] Fix line too long issue reported by Travis CI modified: sphinx/writers/latex.py --- sphinx/writers/latex.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 2ac92c149..d587c1fbe 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1445,7 +1445,8 @@ class LaTeXTranslator(nodes.NodeVisitor): if self.in_container_literal_block: self.body.append('\\needspace{\\literalblockneedspace}') self.body.append('\\vspace{\\literalblockcaptiontopvspace}\n') - self.body.append('\\SphinxSetupCaptionForVerbatim{literal-block}{' + self.context.pop()) + self.body.append('\\SphinxSetupCaptionForVerbatim{literal-block}{' + '' + self.context.pop()) elif self.in_minipage and isinstance(node.parent, nodes.figure): self.body.append('\\captionof{figure}{') else: From 261ff1a9d2f1794f72c275718f31555089fd7e99 Mon Sep 17 00:00:00 2001 From: jfbu Date: Thu, 7 Apr 2016 11:27:33 +0200 Subject: [PATCH 06/10] Enable pdf hyperlinks to named literal-blocks without caption. This is tango between latex.py and sphinx.sty to handle PDF hyperlinks to literal-blocks (both code-block and literalinclude) either with name or caption (then numref can be used) or both. modified: sphinx/texinputs/sphinx.sty modified: sphinx/writers/latex.py This test file is now back to original. modified: tests/test_directive_code.py --- sphinx/texinputs/sphinx.sty | 16 +++++++++++++++- sphinx/writers/latex.py | 26 ++++++++++++++++++-------- tests/test_directive_code.py | 4 ++-- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/sphinx/texinputs/sphinx.sty b/sphinx/texinputs/sphinx.sty index 1e8f43978..3cd81fdca 100644 --- a/sphinx/texinputs/sphinx.sty +++ b/sphinx/texinputs/sphinx.sty @@ -186,8 +186,11 @@ \newcommand*\SphinxVerbatimTitle {} \newcommand*\SphinxSetupCaptionForVerbatim [2] {% - \def\SphinxVerbatimTitle{\captionof{#1}{#2}\smallskip }% + \needspace{\literalblockneedspace}\vspace{\literalblockcaptiontopvspace}% + \def\SphinxVerbatimTitle + {\captionof{#1}{\SphinxLiteralBlockLabel #2}\smallskip }% } +\newcommand*\SphinxLiteralBlockLabel {} % \SphinxCustomFBox is copied from framed.sty's \CustomFBox, but % #1=title/caption is to be set _above_ the top rule, not _below_ @@ -246,6 +249,16 @@ \bgroup\parskip\z@skip \smallskip % use customized framed environment + % first, check if has caption + \ifx\SphinxVerbatimTitle\empty + % no caption. Require space if at bottom of page + \needspace{\literalblockwithoutcaptionneedspace}% + % if there is a label insert hypertarget. + \ifx\SphinxLiteralBlockLabel\empty\else + \phantomsection\SphinxLiteralBlockLabel + \fi + \fi + % non empty \SphinxVerbatimTitle has label inside if there is one. \let\SphinxFrameTitle\SphinxVerbatimTitle \global\Sphinx@myfirstframedpasstrue % The list environement is needed to control perfectly the vertical @@ -625,5 +638,6 @@ \RequirePackage{needspace} % if the left page space is less than \literalblockneedsapce, insert page-break \newcommand{\literalblockneedspace}{5\baselineskip} +\newcommand{\literalblockwithoutcaptionneedspace}{1.5\baselineskip} % margin before the caption of literal-block \newcommand{\literalblockcaptiontopvspace}{0.5\baselineskip} diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index d587c1fbe..9a1760ce5 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1443,10 +1443,7 @@ class LaTeXTranslator(nodes.NodeVisitor): def visit_caption(self, node): self.in_caption += 1 if self.in_container_literal_block: - self.body.append('\\needspace{\\literalblockneedspace}') - self.body.append('\\vspace{\\literalblockcaptiontopvspace}\n') - self.body.append('\\SphinxSetupCaptionForVerbatim{literal-block}{' - '' + self.context.pop()) + self.body.append('\\SphinxSetupCaptionForVerbatim{literal-block}{') elif self.in_minipage and isinstance(node.parent, nodes.figure): self.body.append('\\captionof{figure}{') else: @@ -1801,6 +1798,16 @@ class LaTeXTranslator(nodes.NodeVisitor): # most probably a parsed-literal block -- don't highlight self.body.append('\\begin{alltt}\n') else: + ids = '' + for id in self.pop_hyperlink_ids('code-block'): + ids += self.hypertarget(id, anchor=False) + if node['ids']: + # suppress with anchor=False \phantomsection insertion + ids += self.hypertarget(node['ids'][0], anchor=False) + # define label for use in caption, or directly if none exist. + # LaTeX code will add the \phantomsection in latter case. + if ids: + self.body.append('\n\\def\\SphinxLiteralBlockLabel{' + ids + '}') code = node.astext() lang = self.hlsettingstack[-1][0] linenos = code.count('\n') >= self.hlsettingstack[-1][1] - 1 @@ -1834,6 +1841,8 @@ class LaTeXTranslator(nodes.NodeVisitor): hlcode = hlcode.rstrip()[:-14] # strip \end{Verbatim} self.body.append('\n' + hlcode + '\\end{%sVerbatim}\n' % (self.table and 'Original' or '')) + if ids: + self.body.append('\\let\\SphinxLiteralBlockLabel\empty\n') raise nodes.SkipNode def depart_literal_block(self, node): @@ -1992,15 +2001,16 @@ class LaTeXTranslator(nodes.NodeVisitor): for id in self.pop_hyperlink_ids('code-block'): ids += self.hypertarget(id, anchor=False) if node['ids']: - # suppress with anchor=False \phantomsection generation + # suppress with anchor=False \phantomsection insertion ids += self.hypertarget(node['ids'][0], anchor=False) - self.body.append('\n') - # context.pop will be done in visit_caption - self.context.append(ids) + # define label for use in caption. + if ids: + self.body.append('\n\\def\\SphinxLiteralBlockLabel{' + ids + '}\n') def depart_container(self, node): if node.get('literal_block'): self.in_container_literal_block -= 1 + self.body.append('\\let\\SphinxLiteralBlockLabel\\empty\n') def visit_decoration(self, node): pass diff --git a/tests/test_directive_code.py b/tests/test_directive_code.py index 59a440b53..6f7b749b1 100644 --- a/tests/test_directive_code.py +++ b/tests/test_directive_code.py @@ -64,7 +64,7 @@ def test_code_block_caption_html(app, status, warning): def test_code_block_caption_latex(app, status, warning): app.builder.build_all() latex = (app.outdir / 'Python.tex').text(encoding='utf-8') - caption = '\\SphinxSetupCaptionForVerbatim{literal-block}{\label{caption:caption-test-rb}caption \\emph{test} rb}' + caption = '\\SphinxSetupCaptionForVerbatim{literal-block}{caption \\emph{test} rb}' assert caption in latex @@ -229,7 +229,7 @@ def test_literalinclude_caption_html(app, status, warning): def test_literalinclude_caption_latex(app, status, warning): app.builder.build('index') latex = (app.outdir / 'Python.tex').text(encoding='utf-8') - caption = '\\SphinxSetupCaptionForVerbatim{literal-block}{\label{caption:caption-test-py}caption \\textbf{test} py}' + caption = '\\SphinxSetupCaptionForVerbatim{literal-block}{caption \\textbf{test} py}' assert caption in latex From 267c266561cd6a19da1eca564c67be47d6dd9719 Mon Sep 17 00:00:00 2001 From: jfbu Date: Thu, 7 Apr 2016 11:56:43 +0200 Subject: [PATCH 07/10] Fix comment modified: sphinx/writers/latex.py --- sphinx/writers/latex.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 9a1760ce5..32d71c99b 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1804,8 +1804,7 @@ class LaTeXTranslator(nodes.NodeVisitor): if node['ids']: # suppress with anchor=False \phantomsection insertion ids += self.hypertarget(node['ids'][0], anchor=False) - # define label for use in caption, or directly if none exist. - # LaTeX code will add the \phantomsection in latter case. + # LaTeX code will insert \phantomsection prior to \label if ids: self.body.append('\n\\def\\SphinxLiteralBlockLabel{' + ids + '}') code = node.astext() From afb83c249f7904ba00ad1d262b7d3a0d8c620bd6 Mon Sep 17 00:00:00 2001 From: jfbu Date: Sun, 10 Apr 2016 18:43:55 +0200 Subject: [PATCH 08/10] Make reset of \SphinxVerbatimTitle to \empty explicit in produced LaTeX files modified: sphinx/texinputs/sphinx.sty modified: sphinx/writers/latex.py --- sphinx/texinputs/sphinx.sty | 15 +++++++-------- sphinx/writers/latex.py | 1 + 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/sphinx/texinputs/sphinx.sty b/sphinx/texinputs/sphinx.sty index 3cd81fdca..bbcf73ecc 100644 --- a/sphinx/texinputs/sphinx.sty +++ b/sphinx/texinputs/sphinx.sty @@ -190,6 +190,7 @@ \def\SphinxVerbatimTitle {\captionof{#1}{\SphinxLiteralBlockLabel #2}\smallskip }% } +% \SphinxLiteralBlockLabel will be set dynamically to hold the label for links \newcommand*\SphinxLiteralBlockLabel {} % \SphinxCustomFBox is copied from framed.sty's \CustomFBox, but @@ -248,17 +249,17 @@ % list starts new par, but we don't want it to be set apart vertically \bgroup\parskip\z@skip \smallskip - % use customized framed environment - % first, check if has caption + % first, let's check if there is a caption \ifx\SphinxVerbatimTitle\empty - % no caption. Require space if at bottom of page - \needspace{\literalblockwithoutcaptionneedspace}% - % if there is a label insert hypertarget. + % there was no caption. Check if nevertheless a label was set. \ifx\SphinxLiteralBlockLabel\empty\else + % we require some space to be sure hyperlink target from \phantomsection + % will not be separated from upcoming verbatim by a page break + \needspace{\literalblockwithoutcaptionneedspace}% \phantomsection\SphinxLiteralBlockLabel \fi \fi - % non empty \SphinxVerbatimTitle has label inside if there is one. + % non-empty \SphinxVerbatimTitle has label inside it (in case there is one) \let\SphinxFrameTitle\SphinxVerbatimTitle \global\Sphinx@myfirstframedpasstrue % The list environement is needed to control perfectly the vertical @@ -280,8 +281,6 @@ \endlist % close group to restore \parskip (and \SphinxFrameTitle) \egroup - % reset to empty \SphinxVerbatimTitle - \global\let\SphinxVerbatimTitle\empty } diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 32d71c99b..98a68d70d 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -2009,6 +2009,7 @@ class LaTeXTranslator(nodes.NodeVisitor): def depart_container(self, node): if node.get('literal_block'): self.in_container_literal_block -= 1 + self.body.append('\\let\\SphinxVerbatimTitle\\empty\n') self.body.append('\\let\\SphinxLiteralBlockLabel\\empty\n') def visit_decoration(self, node): From a4aefd454a6a1b4350f5a2cda4b159573b4f707a Mon Sep 17 00:00:00 2001 From: jfbu Date: Sun, 10 Apr 2016 18:48:52 +0200 Subject: [PATCH 09/10] Remove unneeded \bgroup/\egroup in legacy sphinx.sty definition of Verbatim Indeed, sphinx.sty redefines _environment_ Verbatim, and LaTeX's environments always are scope-limiting for local assignments. modified: sphinx/texinputs/sphinx.sty --- sphinx/texinputs/sphinx.sty | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sphinx/texinputs/sphinx.sty b/sphinx/texinputs/sphinx.sty index bbcf73ecc..ce17407bb 100644 --- a/sphinx/texinputs/sphinx.sty +++ b/sphinx/texinputs/sphinx.sty @@ -247,7 +247,7 @@ \renewcommand{\Verbatim}[1][1]{% % list starts new par, but we don't want it to be set apart vertically - \bgroup\parskip\z@skip + \parskip\z@skip \smallskip % first, let's check if there is a caption \ifx\SphinxVerbatimTitle\empty @@ -279,8 +279,7 @@ \endOriginalVerbatim \endMakeFramed \endlist - % close group to restore \parskip (and \SphinxFrameTitle) - \egroup + % LaTeX environments always revert local changes on exit, here e.g. \parskip } From 673222f20a14fd2a3270f330379bba586008f277 Mon Sep 17 00:00:00 2001 From: jfbu Date: Sun, 10 Apr 2016 20:42:00 +0200 Subject: [PATCH 10/10] Added Tests for latex(pdf) links to captioned or named literal blocks As conf.py has now numfig = True, needed to modify accordingly test for links in html output. This topic branch fixes #2405, #2414, and also makes for latex(pdf) functional hyperlinks to named references, but only for literal blocks (code-block or literalinclude). modified: tests/roots/test-directive-code/caption.rst modified: tests/roots/test-directive-code/conf.py new file: tests/roots/test-directive-code/namedblocks.rst modified: tests/test_directive_code.py --- tests/roots/test-directive-code/caption.rst | 35 ++++++++++++++- tests/roots/test-directive-code/conf.py | 2 + .../roots/test-directive-code/namedblocks.rst | 28 ++++++++++++ tests/test_directive_code.py | 44 +++++++++++++++++++ 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 tests/roots/test-directive-code/namedblocks.rst diff --git a/tests/roots/test-directive-code/caption.rst b/tests/roots/test-directive-code/caption.rst index 5a2fe4a1f..e8389b994 100644 --- a/tests/roots/test-directive-code/caption.rst +++ b/tests/roots/test-directive-code/caption.rst @@ -1,5 +1,13 @@ -Dedent -====== +Caption +======= + +References +---------- + +See :numref:`caption *test* rb` and :numref:`caption **test** py`. + +See :ref:`Ruby ` and :ref:`Python `. + Code blocks ----------- @@ -19,3 +27,26 @@ Literal Include :language: python :caption: caption **test** py :lines: 10-11 + + +Named Code blocks +----------------- + +.. code-block:: ruby + :name: name *test* rb + :caption: caption *test* rbnamed + + def ruby? + false + end + + +Named Literal Include +--------------------- + +.. literalinclude:: literal.inc + :language: python + :name: name **test** py + :caption: caption **test** pynamed + :lines: 10-11 + diff --git a/tests/roots/test-directive-code/conf.py b/tests/roots/test-directive-code/conf.py index e10f5e5fb..6601c76a9 100644 --- a/tests/roots/test-directive-code/conf.py +++ b/tests/roots/test-directive-code/conf.py @@ -2,3 +2,5 @@ master_doc = 'index' exclude_patterns = ['_build'] +numfig = True + diff --git a/tests/roots/test-directive-code/namedblocks.rst b/tests/roots/test-directive-code/namedblocks.rst new file mode 100644 index 000000000..5779bc972 --- /dev/null +++ b/tests/roots/test-directive-code/namedblocks.rst @@ -0,0 +1,28 @@ +Named Blocks +============ + +References to named blocks +-------------------------- + +See :ref:`the ruby code ` and +also :ref:`the python code `. + + +Named Code block +---------------- + +.. code-block:: ruby + :name: some ruby code + + def ruby? + false + end + + +Named Literal Include +--------------------- + +.. literalinclude:: literal.inc + :language: python + :name: some python code + diff --git a/tests/test_directive_code.py b/tests/test_directive_code.py index 6f7b749b1..84341a28c 100644 --- a/tests/test_directive_code.py +++ b/tests/test_directive_code.py @@ -54,6 +54,7 @@ def test_code_block_caption_html(app, status, warning): app.builder.build(['caption']) html = (app.outdir / 'caption.html').text(encoding='utf-8') caption = (u'
' + u'Listing 1 ' u'caption test rb' u'\xb6
') @@ -65,7 +66,28 @@ def test_code_block_caption_latex(app, status, warning): app.builder.build_all() latex = (app.outdir / 'Python.tex').text(encoding='utf-8') caption = '\\SphinxSetupCaptionForVerbatim{literal-block}{caption \\emph{test} rb}' + label = '\\def\\SphinxLiteralBlockLabel{\\label{caption:caption-test-rb}}' + link = '\hyperref[caption:caption-test-rb]' \ + '{Listing \\ref{caption:caption-test-rb}}' assert caption in latex + assert label in latex + assert link in latex + + +@with_app('latex', testroot='directive-code') +def test_code_block_namedlink_latex(app, status, warning): + app.builder.build_all() + latex = (app.outdir / 'Python.tex').text(encoding='utf-8') + label1 = '\def\SphinxLiteralBlockLabel{\label{caption:name-test-rb}}' + link1 = '\\hyperref[caption:name\\string-test\\string-rb]'\ + '{\\crossref{\\DUrole{std,std-ref}{Ruby}}' + label2 = '\def\SphinxLiteralBlockLabel{\label{namedblocks:some-ruby-code}}' + link2 = '\\hyperref[namedblocks:some\\string-ruby\\string-code]'\ + '{\\crossref{\\DUrole{std,std-ref}{the ruby code}}}' + assert label1 in latex + assert link1 in latex + assert label2 in latex + assert link2 in latex @with_app('xml', testroot='directive-code') @@ -219,6 +241,7 @@ def test_literalinclude_caption_html(app, status, warning): app.builder.build('index') html = (app.outdir / 'caption.html').text(encoding='utf-8') caption = (u'
' + u'Listing 2 ' u'caption test py' u'\xb6
') @@ -230,7 +253,28 @@ def test_literalinclude_caption_latex(app, status, warning): app.builder.build('index') latex = (app.outdir / 'Python.tex').text(encoding='utf-8') caption = '\\SphinxSetupCaptionForVerbatim{literal-block}{caption \\textbf{test} py}' + label = '\\def\\SphinxLiteralBlockLabel{\\label{caption:caption-test-py}}' + link = '\hyperref[caption:caption-test-py]' \ + '{Listing \\ref{caption:caption-test-py}}' assert caption in latex + assert label in latex + assert link in latex + + +@with_app('latex', testroot='directive-code') +def test_literalinclude_namedlink_latex(app, status, warning): + app.builder.build('index') + latex = (app.outdir / 'Python.tex').text(encoding='utf-8') + label1 = '\def\SphinxLiteralBlockLabel{\label{caption:name-test-py}}' + link1 = '\\hyperref[caption:name\\string-test\\string-py]'\ + '{\\crossref{\\DUrole{std,std-ref}{Python}}' + label2 = '\def\SphinxLiteralBlockLabel{\label{namedblocks:some-python-code}}' + link2 = '\\hyperref[namedblocks:some\\string-python\\string-code]'\ + '{\\crossref{\\DUrole{std,std-ref}{the python code}}}' + assert label1 in latex + assert link1 in latex + assert label2 in latex + assert link2 in latex @with_app('xml', testroot='directive-code')