|
From: <mi...@us...> - 2021-09-11 11:18:43
|
Revision: 8826
http://sourceforge.net/p/docutils/code/8826
Author: milde
Date: 2021-09-11 11:18:39 +0000 (Sat, 11 Sep 2021)
Log Message:
-----------
LaTeX writer: revise Docutils-generated table of contents
Improve spacing,
allow customization.
Modified Paths:
--------------
trunk/docutils/HISTORY.txt
trunk/docutils/docutils/transforms/parts.py
trunk/docutils/docutils/writers/latex2e/__init__.py
trunk/docutils/test/functional/expected/standalone_rst_xetex.tex
trunk/docutils/test/test_writers/test_latex2e.py
Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt 2021-09-11 11:18:27 UTC (rev 8825)
+++ trunk/docutils/HISTORY.txt 2021-09-11 11:18:39 UTC (rev 8826)
@@ -115,6 +115,9 @@
- Refactor/revise ToC writing.
- Don't add ``\phantomsection`` to labels in math-blocks.
+
+ - Improve spacing and allow customization of Docutils-generated table
+ of contents.
* docutils/writers/latex2e/docutils.sty
Modified: trunk/docutils/docutils/transforms/parts.py
===================================================================
--- trunk/docutils/docutils/transforms/parts.py 2021-09-11 11:18:27 UTC (rev 8825)
+++ trunk/docutils/docutils/transforms/parts.py 2021-09-11 11:18:39 UTC (rev 8826)
@@ -141,8 +141,8 @@
entries.append(item)
if entries:
contents = nodes.bullet_list('', *entries)
- if auto:
- contents['classes'].append('auto-toc')
+ if auto: # auto-numbered sections
+ contents['classes'].append('auto-toc') # auto-numbered sections
return contents
else:
return []
Modified: trunk/docutils/docutils/writers/latex2e/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/latex2e/__init__.py 2021-09-11 11:18:27 UTC (rev 8825)
+++ trunk/docutils/docutils/writers/latex2e/__init__.py 2021-09-11 11:18:39 UTC (rev 8826)
@@ -563,6 +563,15 @@
\fi
}"""
+PreambleCmds.toc_list = r"""
+\providecommand*{\DUCLASScontents}{%
+ \renewenvironment{itemize}%
+ {\begin{list}{}{\setlength{\partopsep}{0pt}
+ \setlength{\parsep}{0pt}}
+ }%
+ {\end{list}}%
+}"""
+
## PreambleCmds.caption = r"""% configure caption layout
## \usepackage{caption}
## \captionsetup{singlelinecheck=false}% no exceptions for one-liners"""
@@ -1098,7 +1107,6 @@
# -------------------
has_latex_toc = False # is there a toc in the doc? (needed by minitoc)
- is_toc_list = False # is the current bullet_list a ToC?
section_level = 0
# Flags to encode():
@@ -1685,16 +1693,10 @@
def visit_bullet_list(self, node):
self.duclass_open(node)
- if self.is_toc_list:
- self.out.append('\\begin{list}{}{}')
- else:
- self.out.append('\\begin{itemize}')
+ self.out.append('\\begin{itemize}')
def depart_bullet_list(self, node):
- if self.is_toc_list:
- self.out.append('\\end{list}\n')
- else:
- self.out.append('\\end{itemize}\n')
+ self.out.append('\\end{itemize}\n')
self.duclass_close(node)
def visit_superscript(self, node):
@@ -2909,8 +2911,6 @@
if self.active_table.is_open():
self.table_stack.append(self.active_table)
# nesting longtable does not work (e.g. 2007-04-18)
- # TODO: don't use a longtable or add \noindent before
- # the next paragraph, when in a "compound paragraph".
self.active_table = Table(self, 'tabular')
# A longtable moves before \paragraph and \subparagraph
# section titles if it immediately follows them:
@@ -2936,8 +2936,11 @@
width = self.to_latex_length(node['width'])
except KeyError:
width = r'\linewidth'
- if isinstance(node.parent, nodes.compound):
- self.out.append('\n')
+ # TODO: Don't use a longtable or add \noindent before
+ # the next paragraph, when in a "compound paragraph".
+ # Start a new line or a new paragraph?
+ # if (isinstance(node.parent, nodes.compound)
+ # and self._latex_type != 'longtable')?
self.out.append(self.active_table.get_opening(width))
self.out += content
self.out.append(self.active_table.get_closing() + '\n')
@@ -3123,8 +3126,8 @@
# Docutils generated contents list (no page numbers)
if not self.use_latex_toc:
- # set flag for visit_bullet_list()
- self.is_toc_list = True
+ self.fallbacks['toc-list'] = PreambleCmds.toc_list
+ self.duclass_open(node)
return
# ToC by LaTeX
@@ -3191,11 +3194,12 @@
self.visit_block_quote(node)
def depart_topic(self, node):
- self.is_toc_list = False
if ('abstract' in node['classes']
and self.settings.use_latex_abstract):
self.out.append('\\end{abstract}\n')
- elif not 'contents' in node['classes']:
+ elif 'contents' in node['classes']:
+ self.duclass_close(node)
+ else:
self.depart_block_quote(node)
if ('abstract' in node['classes'] or
'dedication' in node['classes']):
Modified: trunk/docutils/test/functional/expected/standalone_rst_xetex.tex
===================================================================
--- trunk/docutils/test/functional/expected/standalone_rst_xetex.tex 2021-09-11 11:18:27 UTC (rev 8825)
+++ trunk/docutils/test/functional/expected/standalone_rst_xetex.tex 2021-09-11 11:18:39 UTC (rev 8826)
@@ -39,6 +39,14 @@
\DUprovidelength{\pdfpxdimen}{1bp}
+
+\providecommand*{\DUCLASScontents}{%
+ \renewenvironment{itemize}%
+ {\begin{list}{}{\setlength{\partopsep}{0pt}
+ \setlength{\parsep}{0pt}}
+ }%
+ {\end{list}}%
+}
% hyperlinks:
\ifthenelse{\isundefined{\hypersetup}}{
\usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue]{hyperref}
@@ -143,32 +151,34 @@
\phantomsection\label{table-of-contents}
\pdfbookmark[1]{Table of Contents}{table-of-contents}
+\begin{DUclass}{contents}
+
\DUtitle{Table of Contents}
\begin{DUclass}{auto-toc}
-\begin{list}{}{}
+\begin{itemize}
\item \hyperref[structural-elements]{1 Structural Elements}
\begin{DUclass}{auto-toc}
-\begin{list}{}{}
+\begin{itemize}
\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{itemize}
\end{DUclass}
\item \hyperref[body-elements]{2 Body Elements}
\begin{DUclass}{auto-toc}
-\begin{list}{}{}
+\begin{itemize}
\item \hyperref[paragraphs]{2.1 Paragraphs}
\begin{DUclass}{auto-toc}
-\begin{list}{}{}
+\begin{itemize}
\item \hyperref[inline-markup]{2.1.1 Inline Markup}
-\end{list}
+\end{itemize}
\end{DUclass}
\item \hyperref[bullet-lists]{2.2 Bullet Lists}
@@ -196,17 +206,17 @@
\item \hyperref[targets]{2.13 Targets}
\begin{DUclass}{auto-toc}
-\begin{list}{}{}
+\begin{itemize}
\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{itemize}
\end{DUclass}
\item \hyperref[directives]{2.14 Directives}
\begin{DUclass}{auto-toc}
-\begin{list}{}{}
+\begin{itemize}
\item \hyperref[document-parts]{2.14.1 Document Parts}
\item \hyperref[images-and-figures]{2.14.2 Images and Figures}
@@ -228,7 +238,7 @@
\item \hyperref[code]{2.14.10 Code}
\item \hyperref[meta]{2.14.11 Meta}
-\end{list}
+\end{itemize}
\end{DUclass}
\item \hyperref[substitution-definitions]{2.15 Substitution Definitions}
@@ -248,24 +258,25 @@
\item \hyperref[custom-roles]{2.22 Custom Roles}
\item \hyperref[mathematics]{2.23 Mathematics}
-\end{list}
+\end{itemize}
\end{DUclass}
\item \hyperref[tests-for-the-latex-writer]{3 Tests for the LaTeX writer}
\begin{DUclass}{auto-toc}
-\begin{list}{}{}
+\begin{itemize}
\item \hyperref[custom-roles-in-latex]{3.1 Custom Roles in LaTeX}
\item \hyperref[class-handling]{3.2 class handling}
-\end{list}
+\end{itemize}
\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{itemize}
\end{DUclass}
+\end{DUclass}
\section{1 Structural Elements%
@@ -797,8 +808,11 @@
\phantomsection\label{contents}
+\begin{DUclass}{contents}
+\begin{DUclass}{local}
+
\begin{DUclass}{auto-toc}
-\begin{list}{}{}
+\begin{itemize}
\item \hyperref[document-parts]{2.14.1 Document Parts}
\item \hyperref[images-and-figures]{2.14.2 Images and Figures}
@@ -820,8 +834,10 @@
\item \hyperref[code]{2.14.10 Code}
\item \hyperref[meta]{2.14.11 Meta}
-\end{list}
+\end{itemize}
\end{DUclass}
+\end{DUclass}
+\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/test_writers/test_latex2e.py
===================================================================
--- trunk/docutils/test/test_writers/test_latex2e.py 2021-09-11 11:18:27 UTC (rev 8825)
+++ trunk/docutils/test/test_writers/test_latex2e.py 2021-09-11 11:18:39 UTC (rev 8826)
@@ -192,22 +192,44 @@
head_template.substitute(dict(parts,
requirements=parts['requirements'] + '\\setcounter{secnumdepth}{0}\n',
fallbacks=r"""
+% class handling for environments (block-level elements)
+% \begin{DUclass}{spam} tries \DUCLASSspam and
+% \end{DUclass}{spam} tries \endDUCLASSspam
+\ifx\DUclass\undefined % poor man's "provideenvironment"
+ \newenvironment{DUclass}[1]%
+ {% "#1" does not work in end-part of environment.
+ \def\DocutilsClassFunctionName{DUCLASS#1}
+ \csname \DocutilsClassFunctionName \endcsname}%
+ {\csname end\DocutilsClassFunctionName \endcsname}%
+\fi
+
% title for topics, admonitions, unsupported section levels, and sidebar
\providecommand*{\DUtitle}[1]{%
\smallskip\noindent\textbf{#1}\smallskip}
+
+\providecommand*{\DUCLASScontents}{%
+ \renewenvironment{itemize}%
+ {\begin{list}{}{\setlength{\partopsep}{0pt}
+ \setlength{\parsep}{0pt}}
+ }%
+ {\end{list}}%
+}
""")) + r"""
\phantomsection\label{table-of-contents}
\pdfbookmark[1]{Table of Contents}{table-of-contents}
+\begin{DUclass}{contents}
+
\DUtitle{Table of Contents}
-\begin{list}{}{}
+\begin{itemize}
\item \hyperref[title-1]{Title 1}
-\begin{list}{}{}
+\begin{itemize}
\item \hyperref[title-2]{Title 2}
-\end{list}
-\end{list}
+\end{itemize}
+\end{itemize}
+\end{DUclass}
\section{Title 1%
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|