mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Refactor: Add DocumentTargetTransform to simplify LaTeX writer
This commit is contained in:
1
CHANGES
1
CHANGES
@@ -65,6 +65,7 @@ Deprecated
|
||||
* ``sphinx.writers.latex.Table.header_footnotetexts`` is deprecated
|
||||
* ``sphinx.writers.latex.LaTeXWriter.footnotestack`` is deprecated
|
||||
* ``sphinx.writers.latex.LaTeXWriter.in_container_literal_block`` is deprecated
|
||||
* ``sphinx.writers.latex.LaTeXWriter.next_section_ids`` is deprecated
|
||||
* ``sphinx.writers.latex.LaTeXWriter.next_hyperlink_ids`` is deprecated
|
||||
* ``sphinx.writers.latex.LaTeXWriter.restrict_footnote()`` is deprecated
|
||||
* ``sphinx.writers.latex.LaTeXWriter.unrestrict_footnote()`` is deprecated
|
||||
|
||||
@@ -176,6 +176,11 @@ The following is a list of deprecated interface.
|
||||
- 3.0
|
||||
- N/A
|
||||
|
||||
* - ``sphinx.writers.latex.LaTeXWriter.next_section_ids``
|
||||
- 1.8
|
||||
- 3.0
|
||||
- N/A
|
||||
|
||||
* - ``sphinx.writers.latex.LaTeXWriter.next_hyperlink_ids``
|
||||
- 1.8
|
||||
- 3.0
|
||||
|
||||
@@ -21,7 +21,7 @@ from sphinx.builders import Builder
|
||||
from sphinx.builders.latex.transforms import (
|
||||
BibliographyTransform, CitationReferenceTransform, MathReferenceTransform,
|
||||
FootnoteDocnameUpdater, LaTeXFootnoteTransform, LiteralBlockTransform,
|
||||
ShowUrlsTransform,
|
||||
ShowUrlsTransform, DocumentTargetTransform,
|
||||
)
|
||||
from sphinx.config import string_classes, ENUM
|
||||
from sphinx.environment import NoUri
|
||||
@@ -225,7 +225,8 @@ class LaTeXBuilder(Builder):
|
||||
transformer.add_transforms([BibliographyTransform,
|
||||
ShowUrlsTransform,
|
||||
LaTeXFootnoteTransform,
|
||||
LiteralBlockTransform])
|
||||
LiteralBlockTransform,
|
||||
DocumentTargetTransform])
|
||||
transformer.apply_transforms()
|
||||
|
||||
def finish(self):
|
||||
|
||||
@@ -581,3 +581,18 @@ class LiteralBlockTransform(SphinxTransform):
|
||||
if node['literal_block'] is True:
|
||||
newnode = captioned_literal_block('', *node.children, **node.attributes)
|
||||
node.replace_self(newnode)
|
||||
|
||||
|
||||
class DocumentTargetTransform(SphinxTransform):
|
||||
"""Add :doc label to the first section of each document."""
|
||||
default_priority = 400
|
||||
|
||||
def apply(self):
|
||||
# type: () -> None
|
||||
if self.app.builder.name != 'latex':
|
||||
return
|
||||
|
||||
for node in self.document.traverse(addnodes.start_of_file):
|
||||
section = node.next_node(nodes.section)
|
||||
if section:
|
||||
section['ids'].append(':doc') # special label for :doc:
|
||||
|
||||
@@ -691,7 +691,6 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
||||
self.pending_footnotes = [] # type: List[nodes.footnote_reference]
|
||||
self.curfilestack = [] # type: List[unicode]
|
||||
self.handled_abbrs = set() # type: Set[unicode]
|
||||
self.next_section_ids = set() # type: Set[unicode]
|
||||
|
||||
def pushbody(self, newbody):
|
||||
# type: (List[unicode]) -> None
|
||||
@@ -921,8 +920,6 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
||||
|
||||
def visit_start_of_file(self, node):
|
||||
# type: (nodes.Node) -> None
|
||||
# also add a document target
|
||||
self.next_section_ids.add(':doc')
|
||||
self.curfilestack.append(node['docname'])
|
||||
# use default highlight settings for new file
|
||||
self.hlsettingstack.append(self.hlsettingstack[0])
|
||||
@@ -1057,11 +1054,6 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
||||
# just use "subparagraph", it's not numbered anyway
|
||||
self.body.append(r'\%s%s{' % (self.sectionnames[-1], short))
|
||||
self.context.append('}\n' + self.hypertarget_to(node.parent))
|
||||
|
||||
if self.next_section_ids:
|
||||
for id in self.next_section_ids:
|
||||
self.context[-1] += self.hypertarget(id, anchor=False)
|
||||
self.next_section_ids.clear()
|
||||
elif isinstance(parent, nodes.topic):
|
||||
self.body.append(r'\sphinxstyletopictitle{')
|
||||
self.context.append('}\n')
|
||||
@@ -2577,6 +2569,13 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
||||
RemovedInSphinx30Warning)
|
||||
return 0
|
||||
|
||||
@property
|
||||
def next_section_ids(self):
|
||||
# type: () -> Set[unicode]
|
||||
warnings.warn('LaTeXTranslator.next_section_ids is deprecated.',
|
||||
RemovedInSphinx30Warning)
|
||||
return set()
|
||||
|
||||
@property
|
||||
def next_hyperlink_ids(self):
|
||||
# type: () -> Dict
|
||||
|
||||
Reference in New Issue
Block a user