|
From: <mi...@us...> - 2021-09-11 11:18:18
|
Revision: 8824
http://sourceforge.net/p/docutils/code/8824
Author: milde
Date: 2021-09-11 11:18:15 +0000 (Sat, 11 Sep 2021)
Log Message:
-----------
LaTeX writer: code cleanup and small fixes.
Fix newlines after/before ids_to_labels() (cf. patch #183).
Refactor/revise ToC writing.
Don't write `\phantomsection`, if a local ToC is skipped.
Functional test for "--use-docutils-toc" in XeTeX writer test).
Modified Paths:
--------------
trunk/docutils/HISTORY.txt
trunk/docutils/docutils/writers/latex2e/__init__.py
trunk/docutils/test/functional/expected/latex_memoir.tex
trunk/docutils/test/functional/expected/standalone_rst_latex.tex
trunk/docutils/test/functional/expected/standalone_rst_xetex.tex
trunk/docutils/test/functional/tests/standalone_rst_xetex.py
trunk/docutils/test/test_writers/test_latex2e.py
Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt 2021-09-11 11:18:03 UTC (rev 8823)
+++ trunk/docutils/HISTORY.txt 2021-09-11 11:18:15 UTC (rev 8824)
@@ -109,7 +109,11 @@
- Apply patch #181 "Fix tocdepth when chapter/part in use" by
John Thorvald Wodder II.
+
+ - Fix newlines after/before ids_to_labels() (cf. patch #183).
+ - Refactor/revise ToC writing.
+
* docutils/writers/latex2e/docutils.sty
- Fix excessive padding above sidebar titles.
Modified: trunk/docutils/docutils/writers/latex2e/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/latex2e/__init__.py 2021-09-11 11:18:03 UTC (rev 8823)
+++ trunk/docutils/docutils/writers/latex2e/__init__.py 2021-09-11 11:18:15 UTC (rev 8824)
@@ -1519,9 +1519,8 @@
If `set_anchor` is True, an anchor is set with \\phantomsection.
If `protect` is True, the \\label cmd is made robust.
"""
- labels = ['\\label{%s}' % id for id in node['ids']]
- if protect:
- labels = ['\\protect'+label for label in labels]
+ prefix = '\\protect' if protect else ''
+ labels = [prefix + '\\label{%s}' % id for id in node['ids']]
if set_anchor and labels:
labels.insert(0, '\\phantomsection')
return labels
@@ -1597,13 +1596,12 @@
break
else:
return ''
+ if isinstance(child, (nodes.container, nodes.compound)):
+ return self.term_postfix(child)
if isinstance(child, (nodes.image)):
return '\\leavevmode\n' # Images get an additional newline.
- if isinstance(child, (nodes.container, nodes.compound)):
- return self.term_postfix(child)
- if not isinstance(child,
- (nodes.paragraph, nodes.math_block)):
- return r'\leavevmode'
+ if not isinstance(child, (nodes.paragraph, nodes.math_block)):
+ return '\\leavevmode'
return ''
# Visitor methods
@@ -2064,7 +2062,7 @@
else:
self.context.append('')
- # if line ends with '{', mask line break to prevent spurious whitespace
+ # if line ends with '{', mask line break
if (not self.active_table.colwidths_auto
and self.out[-1].endswith("{")
and node.astext()):
@@ -2228,8 +2226,8 @@
(node['ids'][0], backref, self.encode(num)))
if node['ids'] == node['names']:
self.out += self.ids_to_labels(node)
- # mask newline to prevent spurious whitespace if paragraph follows:
- if node[1:] and isinstance(node[1], nodes.paragraph):
+ # prevent spurious whitespace if footnote starts with paragraph:
+ if len(node) > 1 and isinstance(node[1], nodes.paragraph):
self.out.append('%')
# TODO: "real" LaTeX \footnote{}s (see visit_footnotes_reference())
@@ -2496,9 +2494,9 @@
_use_listings = (literal_env == 'lstlisting') and _use_env
# Labels and classes:
+ self.duclass_open(node)
if node['ids']:
- self.out += ['\n'] + self.ids_to_labels(node)
- self.duclass_open(node)
+ self.out += self.ids_to_labels(node) + ['\n']
# Highlight code?
if (not _plaintext
and 'code' in node['classes']
@@ -3082,7 +3080,7 @@
if (level > len(self.d_class.sections)
and not self.settings.legacy_class_functions):
self.context[-1] += '\\end{DUclass}\n'
- # MAYBE postfix paragraph and subparagraph with \leavemode to
+ # MAYBE postfix paragraph and subparagraph with \leavevmode to
# ensure floats stay in the section and text starts on a new line.
def depart_title(self, node):
@@ -3090,76 +3088,88 @@
if isinstance(node.parent, (nodes.table, nodes.document)):
self.pop_output_collector()
- def minitoc(self, node, title, depth):
- """Generate a local table of contents with LaTeX package minitoc"""
- section_name = self.d_class.section(self.section_level)
- # name-prefix for current section level
- minitoc_names = {'part': 'part', 'chapter': 'mini'}
- if 'chapter' not in self.d_class.sections:
- minitoc_names['section'] = 'sect'
- try:
- minitoc_name = minitoc_names[section_name]
- except KeyError: # minitoc only supports part- and toplevel
- self.warn('Skipping local ToC at %s level.\n' % section_name +
- ' Feature not supported with option "use-latex-toc"',
- base_node=node)
+ def visit_contents(self, node):
+ """Write the table of contents.
+
+ Called from visit_topic() for "contents" topics.
+ """
+ # requirements/setup for local ToC with package "minitoc",
+ if self.use_latex_toc and 'local' in node['classes']:
+ section_name = self.d_class.section(self.section_level)
+ # minitoc only supports "part" and toplevel sections
+ minitoc_names = {'part': 'part',
+ 'chapter': 'mini',
+ 'section': 'sect'}
+ if 'chapter' in self.d_class.sections:
+ del(minitoc_names['section'])
+ try:
+ mtc_name = minitoc_names[section_name]
+ except KeyError:
+ self.warn('Skipping local ToC at "%s" level.\n'
+ ' Feature not supported with option "use-latex-toc"'
+ % section_name, base_node=node)
+ raise nodes.SkipNode
+
+ # labels and PDF bookmark (sidebar entry)
+ self.out.append('\n') # start new paragraph
+ if node['names']: # don't add labels for auto-ids
+ self.out += self.ids_to_labels(node) + ['\n']
+ if (isinstance(node.next_node(), nodes.title)
+ and 'local' not in node['classes']
+ and self.settings.documentclass != 'memoir'):
+ self.out.append('\\pdfbookmark[%d]{%s}{%s}\n' %
+ (self.section_level+1,
+ node.next_node().astext(),
+ node.get('ids', ['contents'])[0]
+ ))
+
+ # Docutils generated contents list (no page numbers)
+ if not self.use_latex_toc:
+ # set flag for visit_bullet_list()
+ self.is_toc_list = True
return
- # Requirements/Setup
- self.requirements['minitoc'] = PreambleCmds.minitoc
- self.requirements['minitoc-'+minitoc_name] = (r'\do%stoc' %
- minitoc_name)
- # depth: (Docutils defaults to unlimited depth)
+
+ # ToC by LaTeX
+ depth = node.get('depth', 0)
maxdepth = len(self.d_class.sections)
- self.requirements['minitoc-%s-depth' % minitoc_name] = (
- r'\mtcsetdepth{%stoc}{%d}' % (minitoc_name, maxdepth))
- # Process 'depth' argument (!Docutils stores a relative depth while
- # minitoc expects an absolute depth!):
- offset = {'sect': 1, 'mini': 0, 'part': 0}
- if 'chapter' in self.d_class.sections:
- offset['part'] = -1
- if depth:
- self.out.append('\\setcounter{%stocdepth}{%d}' %
- (minitoc_name, depth + offset[minitoc_name]))
- # title:
- self.out.append('\\mtcsettitle{%stoc}{%s}\n' % (minitoc_name, title))
- # the toc-generating command:
- self.out.append('\\%stoc\n' % minitoc_name)
+ if isinstance(node.next_node(), nodes.title):
+ title = self.encode(node[0].astext())
+ else:
+ title = ''
+ if 'local' in node['classes']:
+ # use the "minitoc" package
+ self.requirements['minitoc'] = PreambleCmds.minitoc
+ self.requirements['minitoc-'+mtc_name] = r'\do%stoc'%mtc_name
+ self.requirements['minitoc-%s-depth' % mtc_name] = (
+ r'\mtcsetdepth{%stoc}{%d}' % (mtc_name, maxdepth))
+ # "depth" option: Docutils stores a relative depth while
+ # minitoc expects an absolute depth!:
+ offset = {'sect': 1, 'mini': 0, 'part': 0}
+ if 'chapter' in self.d_class.sections:
+ offset['part'] = -1
+ if depth:
+ self.out.append('\\setcounter{%stocdepth}{%d}' %
+ (mtc_name, depth + offset[mtc_name]))
+ # title:
+ self.out.append('\\mtcsettitle{%stoc}{%s}\n' % (mtc_name, title))
+ # the toc-generating command:
+ self.out.append('\\%stoc\n' % mtc_name)
+ else:
+ if depth:
+ self.out.append('\\setcounter{tocdepth}{%d}\n'
+ % self.d_class.latex_section_depth(depth))
+ if title != 'Contents':
+ self.out.append('\\renewcommand{\\contentsname}{%s}\n' % title)
+ self.out.append('\\tableofcontents\n')
+ self.has_latex_toc = True
+ # ignore rest of node content
+ raise nodes.SkipNode
def visit_topic(self, node):
# Topic nodes can be generic topic, abstract, dedication, or ToC.
# table of contents:
if 'contents' in node['classes']:
- self.out.append('\n')
- self.out += self.ids_to_labels(node)
- # add contents to PDF bookmarks sidebar
- if (isinstance(node.next_node(), nodes.title)
- and self.settings.documentclass != 'memoir'):
- self.out.append('\n\\pdfbookmark[%d]{%s}{%s}' %
- (self.section_level+1,
- node.next_node().astext(),
- node.get('ids', ['contents'])[0]
- ))
- if self.use_latex_toc:
- title = ''
- if isinstance(node.next_node(), nodes.title):
- title = self.encode(node.pop(0).astext())
- depth = node.get('depth', 0)
- if 'local' in node['classes']:
- self.minitoc(node, title, depth)
- return
- if depth:
- self.out.append('\n\\setcounter{tocdepth}{%d}\n'
- % self.d_class.latex_section_depth(depth))
- if title != 'Contents':
- self.out.append('\n\\renewcommand{\\contentsname}{%s}'
- % title)
- self.out.append('\n\\tableofcontents\n')
- self.has_latex_toc = True
- # ignore rest of node content
- raise nodes.SkipNode
- else: # Docutils generated contents list
- # set flag for visit_bullet_list() and visit_title()
- self.is_toc_list = True
+ self.visit_contents(node)
elif ('abstract' in node['classes'] and
self.settings.use_latex_abstract):
self.push_output_collector(self.abstract)
Modified: trunk/docutils/test/functional/expected/latex_memoir.tex
===================================================================
--- trunk/docutils/test/functional/expected/latex_memoir.tex 2021-09-11 11:18:03 UTC (rev 8823)
+++ trunk/docutils/test/functional/expected/latex_memoir.tex 2021-09-11 11:18:15 UTC (rev 8824)
@@ -804,7 +804,6 @@
\label{directives}%
}
-\phantomsection\label{contents}
These are just a sample of the many reStructuredText Directives. For
others, please see \href{https://docutils.sourceforge.io/docs/ref/rst/directives.html}{reStructuredText Directives}\DUfootnotemark{footnote-reference-18}{footnote-10}{9}.
Modified: trunk/docutils/test/functional/expected/standalone_rst_latex.tex
===================================================================
--- trunk/docutils/test/functional/expected/standalone_rst_latex.tex 2021-09-11 11:18:03 UTC (rev 8823)
+++ trunk/docutils/test/functional/expected/standalone_rst_latex.tex 2021-09-11 11:18:15 UTC (rev 8824)
@@ -805,7 +805,6 @@
\label{directives}%
}
-\phantomsection\label{contents}
These are just a sample of the many reStructuredText Directives. For
others, please see \href{https://docutils.sourceforge.io/docs/ref/rst/directives.html}{reStructuredText Directives}\DUfootnotemark{footnote-reference-18}{footnote-10}{9}.
Modified: trunk/docutils/test/functional/expected/standalone_rst_xetex.tex
===================================================================
--- trunk/docutils/test/functional/expected/standalone_rst_xetex.tex 2021-09-11 11:18:03 UTC (rev 8823)
+++ trunk/docutils/test/functional/expected/standalone_rst_xetex.tex 2021-09-11 11:18:15 UTC (rev 8824)
@@ -142,10 +142,132 @@
\phantomsection\label{table-of-contents}
\pdfbookmark[1]{Table of Contents}{table-of-contents}
-\renewcommand{\contentsname}{Table of Contents}
-\tableofcontents
+\DUtitle{Table of Contents}
+\begin{DUclass}{auto-toc}
+\begin{list}{}{}
+\item \hyperref[structural-elements]{1 Structural Elements}
+
+\begin{DUclass}{auto-toc}
+\begin{list}{}{}
+\item \hyperref[section-title]{1.1 Section Title}
+
+\item \hyperref[empty-section]{1.2 Empty Section}
+
+\item \hyperref[transitions]{1.3 Transitions}
+\end{list}
+\end{DUclass}
+
+\item \hyperref[body-elements]{2 Body Elements}
+
+\begin{DUclass}{auto-toc}
+\begin{list}{}{}
+\item \hyperref[paragraphs]{2.1 Paragraphs}
+
+\begin{DUclass}{auto-toc}
+\begin{list}{}{}
+\item \hyperref[inline-markup]{2.1.1 Inline Markup}
+\end{list}
+\end{DUclass}
+
+\item \hyperref[bullet-lists]{2.2 Bullet Lists}
+
+\item \hyperref[enumerated-lists]{2.3 Enumerated Lists}
+
+\item \hyperref[definition-lists]{2.4 Definition Lists}
+
+\item \hyperref[field-lists]{2.5 Field Lists}
+
+\item \hyperref[option-lists]{2.6 Option Lists}
+
+\item \hyperref[literal-blocks]{2.7 Literal Blocks}
+
+\item \hyperref[line-blocks]{2.8 Line Blocks}
+
+\item \hyperref[block-quotes]{2.9 Block Quotes}
+
+\item \hyperref[doctest-blocks]{2.10 Doctest Blocks}
+
+\item \hyperref[footnotes]{2.11 Footnotes}
+
+\item \hyperref[citations]{2.12 Citations}
+
+\item \hyperref[targets]{2.13 Targets}
+
+\begin{DUclass}{auto-toc}
+\begin{list}{}{}
+\item \hyperref[duplicate-target-names]{2.13.1 Duplicate Target Names}
+
+\item \hyperref[duplicate-target-names-1]{2.13.2 Duplicate Target Names}
+\end{list}
+\end{DUclass}
+
+\item \hyperref[directives]{2.14 Directives}
+
+\begin{DUclass}{auto-toc}
+\begin{list}{}{}
+\item \hyperref[document-parts]{2.14.1 Document Parts}
+
+\item \hyperref[images-and-figures]{2.14.2 Images and Figures}
+
+\item \hyperref[tables]{2.14.3 Tables}
+
+\item \hyperref[admonitions]{2.14.4 Admonitions}
+
+\item \hyperref[topics-sidebars-and-rubrics]{2.14.5 Topics, Sidebars, and Rubrics}
+
+\item \hyperref[target-footnotes]{2.14.6 Target Footnotes}
+
+\item \hyperref[replacement-text]{2.14.7 Replacement Text}
+
+\item \hyperref[compound-paragraph]{2.14.8 Compound Paragraph}
+
+\item \hyperref[parsed-literal-blocks]{2.14.9 Parsed Literal Blocks}
+
+\item \hyperref[code]{2.14.10 Code}
+
+\item \hyperref[meta]{2.14.11 Meta}
+\end{list}
+\end{DUclass}
+
+\item \hyperref[substitution-definitions]{2.15 Substitution Definitions}
+
+\item \hyperref[comments]{2.16 Comments}
+
+\item \hyperref[raw-text]{2.17 Raw text}
+
+\item \hyperref[container]{2.18 Container}
+
+\item \hyperref[colspanning-tables]{2.19 Colspanning tables}
+
+\item \hyperref[rowspanning-tables]{2.20 Rowspanning tables}
+
+\item \hyperref[list-tables]{2.21 List Tables}
+
+\item \hyperref[custom-roles]{2.22 Custom Roles}
+
+\item \hyperref[mathematics]{2.23 Mathematics}
+\end{list}
+\end{DUclass}
+
+\item \hyperref[tests-for-the-latex-writer]{3 Tests for the LaTeX writer}
+
+\begin{DUclass}{auto-toc}
+\begin{list}{}{}
+\item \hyperref[custom-roles-in-latex]{3.1 Custom Roles in LaTeX}
+
+\item \hyperref[class-handling]{3.2 class handling}
+\end{list}
+\end{DUclass}
+
+\item \hyperref[tests-for-the-xetex-writer]{4 Tests for the XeTeX writer}
+
+\item \hyperref[error-handling]{5 Error Handling}
+\end{list}
+\end{DUclass}
+
+
\section{1 Structural Elements%
\label{structural-elements}%
}
@@ -674,6 +796,33 @@
}
\phantomsection\label{contents}
+
+\begin{DUclass}{auto-toc}
+\begin{list}{}{}
+\item \hyperref[document-parts]{2.14.1 Document Parts}
+
+\item \hyperref[images-and-figures]{2.14.2 Images and Figures}
+
+\item \hyperref[tables]{2.14.3 Tables}
+
+\item \hyperref[admonitions]{2.14.4 Admonitions}
+
+\item \hyperref[topics-sidebars-and-rubrics]{2.14.5 Topics, Sidebars, and Rubrics}
+
+\item \hyperref[target-footnotes]{2.14.6 Target Footnotes}
+
+\item \hyperref[replacement-text]{2.14.7 Replacement Text}
+
+\item \hyperref[compound-paragraph]{2.14.8 Compound Paragraph}
+
+\item \hyperref[parsed-literal-blocks]{2.14.9 Parsed Literal Blocks}
+
+\item \hyperref[code]{2.14.10 Code}
+
+\item \hyperref[meta]{2.14.11 Meta}
+\end{list}
+\end{DUclass}
+
These are just a sample of the many reStructuredText Directives. For
others, please see \href{https://docutils.sourceforge.io/docs/ref/rst/directives.html}{reStructuredText Directives}\DUfootnotemark{footnote-reference-19}{footnote-11}{10}.
Modified: trunk/docutils/test/functional/tests/standalone_rst_xetex.py
===================================================================
--- trunk/docutils/test/functional/tests/standalone_rst_xetex.py 2021-09-11 11:18:03 UTC (rev 8823)
+++ trunk/docutils/test/functional/tests/standalone_rst_xetex.py 2021-09-11 11:18:15 UTC (rev 8824)
@@ -13,3 +13,5 @@
settings_overrides['smart_quotes'] = True
# use docutils.sty and up-to-date class functions:
settings_overrides['stylesheet'] = 'docutils'
+# Test the ToC generation by Docutils:
+settings_overrides['use_latex_toc'] = False
Modified: trunk/docutils/test/test_writers/test_latex2e.py
===================================================================
--- trunk/docutils/test/test_writers/test_latex2e.py 2021-09-11 11:18:03 UTC (rev 8823)
+++ trunk/docutils/test/test_writers/test_latex2e.py 2021-09-11 11:18:15 UTC (rev 8824)
@@ -198,6 +198,7 @@
""")) + r"""
\phantomsection\label{table-of-contents}
\pdfbookmark[1]{Table of Contents}{table-of-contents}
+
\DUtitle{Table of Contents}
\begin{list}{}{}
@@ -335,7 +336,6 @@
\phantomsection\label{contents}
\pdfbookmark[1]{Contents}{contents}
\setcounter{tocdepth}{1}
-
\tableofcontents
@@ -365,7 +365,6 @@
\phantomsection\label{contents}
\pdfbookmark[1]{Contents}{contents}
\setcounter{tocdepth}{0}
-
\tableofcontents
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|