Close #4980: latex: Improve label handling of LaTeX builder (section)

This commit is contained in:
Takeshi KOMIYA
2018-05-19 01:21:57 +09:00
parent 1b5e910059
commit 0ba5c24f5e
4 changed files with 40 additions and 18 deletions

View File

@@ -53,6 +53,7 @@ HYPERLINK_SUPPORT_NODES = (
nodes.figure,
nodes.literal_block,
nodes.table,
nodes.section,
)
DEFAULT_SETTINGS = {
@@ -950,8 +951,6 @@ class LaTeXTranslator(nodes.NodeVisitor):
if not self.this_is_the_title:
self.sectionlevel += 1
self.body.append('\n\n')
if node.get('ids'):
self.next_section_ids.update(node['ids'])
def depart_section(self, node):
# type: (nodes.Node) -> None
@@ -1046,9 +1045,9 @@ class LaTeXTranslator(nodes.NodeVisitor):
except IndexError:
# just use "subparagraph", it's not numbered anyway
self.body.append(r'\%s%s{' % (self.sectionnames[-1], short))
self.context.append('}\n')
self.context.append('}\n' + self.hypertarget_to(node.parent))
self.restrict_footnote(node)
if self.next_section_ids:
for id in self.next_section_ids:
self.context[-1] += self.hypertarget(id, anchor=False)
@@ -1917,22 +1916,16 @@ class LaTeXTranslator(nodes.NodeVisitor):
node.parent.parent.index(node.parent)]
else:
raise
if isinstance(next, nodes.section):
domain = self.builder.env.get_domain('std')
figtype = domain.get_figtype(next)
if figtype and domain.get_numfig_title(next):
ids = set()
# labels for figures go in the figure body, not before
if node.get('refid'):
self.next_section_ids.add(node['refid'])
self.next_section_ids.update(node['ids'])
ids.add(node['refid'])
ids.update(node['ids'])
self.push_hyperlink_ids(figtype, ids)
return
else:
domain = self.builder.env.get_domain('std')
figtype = domain.get_figtype(next)
if figtype and domain.get_numfig_title(next):
ids = set()
# labels for figures go in the figure body, not before
if node.get('refid'):
ids.add(node['refid'])
ids.update(node['ids'])
self.push_hyperlink_ids(figtype, ids)
return
except IndexError:
pass
if 'refuri' in node:

View File

@@ -51,3 +51,18 @@ tables
head head
cell cell
==== ====
.. _section1:
.. _section2:
subsection
----------
.. _section3:
subsubsection
~~~~~~~~~~~~~
.. toctree::
otherdoc

View File

@@ -0,0 +1,2 @@
otherdoc
========

View File

@@ -1296,3 +1296,15 @@ def test_latex_labels(app, status, warning):
r'\label{\detokenize{index:table1}}' in result)
assert (r'\sphinxcaption{table caption}'
r'\label{\detokenize{index:table3}}' in result)
# sections
assert ('\\chapter{subsection}\n'
r'\label{\detokenize{index:subsection}}'
r'\label{\detokenize{index:section2}}'
r'\label{\detokenize{index:section1}}' in result)
assert ('\\section{subsubsection}\n'
r'\label{\detokenize{index:subsubsection}}'
r'\label{\detokenize{index:section3}}' in result)
assert ('\\subsection{otherdoc}\n'
r'\label{\detokenize{otherdoc:otherdoc}}'
r'\label{\detokenize{otherdoc::doc}}' in result)