SF.net SVN: docutils:[9998 ] trunk/docutils
milde--- via Docutils-checkins <[email protected]>
| Newsgroups | gmane.text.docutils.cvs |
|---|---|
| Message-ID | <[email protected]> |
Revision: 9998
http://sourceforge.net/p/docutils/code/9998
Author: milde
Date: 2024-12-11 14:36:47 +0000 (Wed, 11 Dec 2024)
Log Message:
-----------
Test and fix `publish_parts()` with the LaTeX writer.
New test module for the LaTeX writer "parts" interface.
Make `writers.latex2e.Writer.assemble_parts()` idempotent
The `assemble_parts()` method adds newlines to "head" parts for better formatting
of the output file. However, is called in `translate()` and again by
publish_parts(), leading to spurious double trailing newlines.
Use super() when calling methods from the parent class.
Use f-strings
Modified Paths:
--------------
trunk/docutils/docutils/writers/_html_base.py
trunk/docutils/docutils/writers/latex2e/__init__.py
Added Paths:
-----------
trunk/docutils/test/test_writers/test_latex2e_parts.py
Modified: trunk/docutils/docutils/writers/_html_base.py
===================================================================
--- trunk/docutils/docutils/writers/_html_base.py 2024-12-11 14:36:22 UTC (rev 9997)
+++ trunk/docutils/docutils/writers/_html_base.py 2024-12-11 14:36:47 UTC (rev 9998)
@@ -179,7 +179,7 @@
return subs
def assemble_parts(self) -> None:
- writers.Writer.assemble_parts(self)
+ super().assemble_parts()
for part in self.visitor_attributes:
self.parts[part] = ''.join(getattr(self, part))
Modified: trunk/docutils/docutils/writers/latex2e/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/latex2e/__init__.py 2024-12-11 14:36:22 UTC (rev 9997)
+++ trunk/docutils/docutils/writers/latex2e/__init__.py 2024-12-11 14:36:47 UTC (rev 9998)
@@ -294,8 +294,10 @@
def assemble_parts(self) -> None:
"""Assemble the `self.parts` dictionary of output fragments."""
- writers.Writer.assemble_parts(self)
+ super().assemble_parts()
for part in self.visitor_attributes:
+ if part in self.parts:
+ continue # make the function idempotent
lines = getattr(self, part)
if part in self.head_parts:
if lines:
@@ -1256,9 +1258,8 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~
# Document parts
- self.head_prefix = [r'\documentclass[%s]{%s}' %
- (self.documentoptions,
- settings.documentclass)]
+ self.head_prefix = [f'\\documentclass[{self.documentoptions}]'
+ f'{{{settings.documentclass}}}']
self.requirements = {} # converted to a list in depart_document()
self.latex_preamble = [settings.latex_preamble]
self.fallbacks = {} # converted to a list in depart_document()
Added: trunk/docutils/test/test_writers/test_latex2e_parts.py
===================================================================
--- trunk/docutils/test/test_writers/test_latex2e_parts.py (rev 0)
+++ trunk/docutils/test/test_writers/test_latex2e_parts.py 2024-12-11 14:36:47 UTC (rev 9998)
@@ -0,0 +1,117 @@
+#! /usr/bin/env python3
+# $Id$
+# Author: Günter Milde
+# Maintainer: [email protected]
+# :Copyright: 2024 Günter Milde,
+# :License: Released under the terms of the `2-Clause BSD license`_, in short:
+#
+# Copying and distribution of this file, with or without modification,
+# are permitted in any medium without royalty provided the copyright
+# notice and this notice are preserved.
+# This file is offered as-is, without any warranty.
+#
+# .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
+
+"""
+Test `core.publish_parts()`__ with the LaTeX writer.
+
+__ https://docutils.sourceforge.io/docs/api/publisher.html#publish-parts
+"""
+
+from pathlib import Path
+import sys
+import unittest
+
+
+if __name__ == '__main__':
+ # prepend the "docutils root" to the Python library path
+ # so we import the local `docutils` package.
+ sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
+
+import docutils
+from docutils.core import publish_parts
+from docutils.writers import latex2e
+
+
+class LaTeXWriterPublishPartsTestCase(unittest.TestCase):
+ """Test LaTeX writer `publish_parts()` interface."""
+
+ maxDiff = None
+ settings = {'_disable_config': True,
+ 'strict_visitor': True,
+ # avoid latex writer future warnings:
+ 'use_latex_citations': False,
+ 'legacy_column_widths': True,
+ }
+
+ def test_publish_parts(self):
+ for name, (settings_overrides, cases) in samples.items():
+ for casenum, (case_input, expected_parts) in enumerate(cases):
+ parts = publish_parts(
+ source=case_input,
+ writer=latex2e.Writer(),
+ settings_overrides=self.settings|settings_overrides,
+ )
+ expected = default_parts | expected_parts
+ expected['whole'] = expected['whole'].format(**expected)
+
+ for key in parts.keys():
+ with self.subTest(id=f'samples[{name!r}][{casenum}][{key}]'):
+ self.assertEqual(f'{expected[key]}', f'{parts[key]}')
+
+
+default_parts = {
+ 'abstract': '',
+ 'body': '',
+ 'body_pre_docinfo': '',
+ 'dedication': '',
+ 'docinfo': '',
+ 'encoding': 'utf-8',
+ 'errors': 'strict',
+ 'fallbacks': '',
+ 'head_prefix': '\\documentclass[a4paper]{article}\n',
+ 'latex_preamble': '% PDF Standard Fonts\n'
+ '\\usepackage{mathptmx} % Times\n'
+ '\\usepackage[scaled=.90]{helvet}\n'
+ '\\usepackage{courier}\n',
+ 'pdfsetup': '% hyperlinks:\n'
+ '\\ifdefined\\hypersetup\n'
+ '\\else\n'
+ ' \\usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue]{hyperref}\n'
+ ' \\usepackage{bookmark}\n'
+ ' \\urlstyle{same} % normal text font (alternatives: tt, rm, sf)\n'
+ '\\fi\n',
+ 'requirements': '\\usepackage[T1]{fontenc}\n',
+ 'stylesheet': '',
+ 'subtitle': '',
+ 'title': '',
+ 'titledata': '',
+ 'version': f'{docutils.__version__}',
+ 'whole': '{head_prefix}'
+ '% generated by Docutils <https://docutils.sourceforge.io/>\n'
+ '\\usepackage{{cmap}} % fix search and cut-and-paste in Acrobat\n'
+ '{requirements}\n'
+ '%%% Custom LaTeX preamble\n'
+ '{latex_preamble}\n'
+ '%%% User specified packages and stylesheets\n'
+ '{stylesheet}\n'
+ '%%% Fallback definitions for Docutils-specific commands\n'
+ '{fallbacks}\n'
+ '{pdfsetup}\n'
+ '%%% Body\n'
+ '\\begin{{document}}\n'
+ '{body}\n'
+ '\\end{{document}}\n'
+ }
+
+samples = {}
+
+samples['default'] = ({}, [
+['', # empty input string
+ {} # results in default parts
+ ],
+])
+
+
+if __name__ == '__main__':
+ unittest.main()
Property changes on: trunk/docutils/test/test_writers/test_latex2e_parts.py
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Author Date Id Revision
\ No newline at end of property
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
_______________________________________________
Docutils-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/docutils-checkins