summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormilde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2023-01-21 13:13:11 +0000
committermilde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2023-01-21 13:13:11 +0000
commit151057de56d9489200d06d939f7460c71ceab2f4 (patch)
treef273ac1011383a5ef48eea12bcb70b62702fd628
parentdb4031b247646f29c744859540c23f7bb097510d (diff)
downloaddocutils-151057de56d9489200d06d939f7460c71ceab2f4.tar.gz
LaTeX writer: outsourcing of some code from `depart_document()`.
Move parts of the spaghetti code in `LaTeXTranslator.depart_document() to new auxiliary methods `LaTeXTranslator.make_title()` and `LaTeXTranslator.append_bibliography()`. git-svn-id: https://svn.code.sf.net/p/docutils/code/trunk@9324 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
-rw-r--r--docutils/HISTORY.txt4
-rw-r--r--docutils/docutils/writers/latex2e/__init__.py75
2 files changed, 48 insertions, 31 deletions
diff --git a/docutils/HISTORY.txt b/docutils/HISTORY.txt
index 558fd3a41..e48567485 100644
--- a/docutils/HISTORY.txt
+++ b/docutils/HISTORY.txt
@@ -94,7 +94,9 @@ Changes Since 0.19
- Do not insert ``\usepackage[utf8]{inputenc}`` into UTF-8 encoded
LaTeX sources. UTF-8 is the default encoding for LaTeX2e since 2018.
- Fix handling of the "use_bibtex" setting.
- - Use POSIX paths in stylesheet loading macros [report Alan G. Isaac].
+ - Outsource parts of `depart_document()` to new auxiliary methods
+ `make_title()` and `append_bibliography()`.
+ - Ensure POSIX paths in stylesheet loading macros.
* docutils/writers/latex2e/titlepage.tex
diff --git a/docutils/docutils/writers/latex2e/__init__.py b/docutils/docutils/writers/latex2e/__init__.py
index d46272a19..4cf49d3f1 100644
--- a/docutils/docutils/writers/latex2e/__init__.py
+++ b/docutils/docutils/writers/latex2e/__init__.py
@@ -1994,7 +1994,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
protect=protect)
def depart_document(self, node):
- # Complete header with information gained from walkabout
+ # Complete "parts" with information gained from walkabout
# * language setup
if (self.babel.otherlanguages
or self.babel.language not in ('', 'english')):
@@ -2010,36 +2010,54 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.pdfinfo.append(' pdfauthor={%s}' % authors)
if self.pdfinfo:
self.pdfsetup += [r'\hypersetup{'] + self.pdfinfo + ['}']
- # Complete body
- # * document title (with "use_latex_docinfo" also
- # 'author', 'organization', 'contact', 'address' and 'date')
- if self.title or (
- self.use_latex_docinfo and (self.author_stack or self.date)):
- # \title (empty \title prevents error with \maketitle)
- title = [''.join(self.title)]
- if self.title:
- title += self.title_labels
- if self.subtitle:
- title += [r'\\',
+ # * title (including author(s) and date if using "latex_docinfo")
+ if self.title or (self.use_latex_docinfo
+ and (self.author_stack or self.date)):
+ self.make_title() # see below
+ # * bibliography
+ if self._bibitems:
+ self.append_bibliogaphy() # see below
+ # * make sure to generate a toc file if needed for local contents:
+ if 'minitoc' in self.requirements and not self.has_latex_toc:
+ self.out.append('\n\\faketableofcontents % for local ToCs\n')
+
+ def make_title(self):
+ # Auxiliary function called by `self.depart_document()`.
+ #
+ # Append ``\title``, ``\author``, and ``\date`` to "titledata".
+ # (We need all three, even if empty, to prevent errors
+ # and/or automatic display of the current date by \maketitle.)
+ # Append ``\maketitle`` to "body_pre_docinfo" parts.
+ #
+ # \title
+ title_arg = [''.join(self.title)] # ensure len == 1
+ if self.title:
+ title_arg += self.title_labels
+ if self.subtitle:
+ title_arg += [r'\\',
r'\DUdocumentsubtitle{%s}' % ''.join(self.subtitle),
] + self.subtitle_labels
- self.titledata.append(r'\title{%s}' % '%\n '.join(title))
- # \author (empty \author prevents warning with \maketitle)
- authors = ['\\\\\n'.join(author_entry)
- for author_entry in self.author_stack]
- self.titledata.append(r'\author{%s}' %
- ' \\and\n'.join(authors))
- # \date (empty \date prevents defaulting to \today)
- self.titledata.append(r'\date{%s}' % ', '.join(self.date))
- # \maketitle in the body formats title with LaTeX
- self.body_pre_docinfo.append('\\maketitle\n')
-
- # * bibliography
- # TODO insertion point of bibliography should be configurable.
- if self.bibtex and self._bibitems:
+ self.titledata.append(r'\title{%s}' % '%\n '.join(title_arg))
+ # \author
+ author_arg = ['\\\\\n'.join(author_entry)
+ for author_entry in self.author_stack]
+ self.titledata.append(r'\author{%s}' %
+ ' \\and\n'.join(author_arg))
+ # \date
+ self.titledata.append(r'\date{%s}' % ', '.join(self.date))
+ # \maketitle
+ # Must be in the document body. We add it to `body_pre_docinfo`
+ # to allow templates to put `titledata` into the document preamble.
+ self.body_pre_docinfo.append('\\maketitle\n')
+
+ def append_bibliogaphy(self):
+ # Add bibliography at end of document.
+ # TODO insertion point should be configurable.
+ # Auxiliary function called by `depart_document`.
+ if self.bibtex:
self.out.append('\n\\bibliographystyle{%s}\n' % self.bibtex[0])
self.out.append('\\bibliography{%s}\n' % ','.join(self.bibtex[1:]))
- elif self.use_latex_citations and self._bibitems:
+ elif self.use_latex_citations:
# TODO: insert citations at point of definition.
widest_label = ''
for bibitem in self._bibitems:
@@ -2053,9 +2071,6 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.out.append('\\bibitem[%s]{%s}{%s}\n' %
(bibitem[0], cite_key, bibitem[1]))
self.out.append('\\end{thebibliography}\n')
- # * make sure to generate a toc file if needed for local contents:
- if 'minitoc' in self.requirements and not self.has_latex_toc:
- self.out.append('\n\\faketableofcontents % for local ToCs\n')
def visit_emphasis(self, node):
self.out.append('\\emph{')