Re: latex write docinfo

Guenter Milde via Docutils-users <[email protected]> Wed, 18 Jan 2023 10:05:52 -0000 (UTC)
Newsgroups gmane.text.docutils.user
Message-ID <[email protected]>
Dear Alan,

On 2023-01-14, Alan G. Isaac wrote:
> In the latex writer, in
> 	LaTeXTranslator.depart_document
> might it be considered to move the docinfo section
> to a separate method? Say `depart_document_docinfo`
> or something like that?

> Motivation: it is the only part of the method that
> rst2beamer changes, but there has been churn in
> the other parts of `depart_document`. (So there
> was really no need for the rst2beamer method to
> get out of sync as it has.)

Something like the patch below?

Günter

--

From: milde <[email protected]>
Date: Wed, 18 Jan 2023 10:57:51 +0100
Subject: [PATCH] LaTeX writer: outsourcing of some code from `depart_document()`.

Move parts of the spaghetti code in `LaTeXTranslator.depart_document()
to new auxiliary methods `LaTeXTranslator.append_title()` and
`LaTeXTranslator.append_bibliography()`.
---
 docutils/docutils/writers/latex2e/__init__.py | 72 ++++++++++++++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 44 insertions(+), 28 deletions(-)

diff --git a/docutils/docutils/writers/latex2e/__init__.py b/docutils/docutils/writers/latex2e/__init__.py
index d46272a..4386620 100644
--- a/docutils/docutils/writers/latex2e/__init__.py
+++ b/docutils/docutils/writers/latex2e/__init__.py
@@ -1995,5 +1995,5 @@ class LaTeXTranslator(nodes.NodeVisitor):
 
     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
@@ -2011,30 +2011,49 @@ class LaTeXTranslator(nodes.NodeVisitor):
         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")
+        self.append_title()  # see below
+        # * bibliography
+        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 append_title(self):
+        # Append ``\title``, ``\author``, and ``\date`` to "titledata"
+        # and ``\maketitle`` to "body_pre_docinfo" parts.
+        #
+        # Auxiliary function called by `self.depart_document()`.
+        if not (self.title
+                or (self.use_latex_docinfo
+                    and (self.author_stack or self.date))):
+            return  # do nothing, there is no title data
+        # We need all three of ``\title``, ``\author``, and ``\date``
+        # (even if empty) to prevent errors and/or automatic display
+        # of the current date by \maketitle.
+        # \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.
+        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 and self._bibitems:
             self.out.append('\n\\bibliographystyle{%s}\n' % self.bibtex[0])
@@ -2054,7 +2073,4 @@ class LaTeXTranslator(nodes.NodeVisitor):
                                 (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):
--
libgit2 1.1.0






_______________________________________________
Docutils-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/docutils-users

Please use "Reply All" to reply to the list.