Debian Codegraph @

test/test_writers/test_latex2e_parts.py

raw symbols SCIP
1 #! /usr/bin/env python3
2 # $Id: test_latex2e_parts.py 10331 2026-05-08 14:57:28Z milde $
3 # Author: Günter Milde
4 # Maintainer: [email protected]
5 # :Copyright: 2024 Günter Milde,
6 # :License: Released under the terms of the `2-Clause BSD license`_, in short:
7 #
8 # Copying and distribution of this file, with or without modification,
9 # are permitted in any medium without royalty provided the copyright
10 # notice and this notice are preserved.
11 # This file is offered as-is, without any warranty.
12 #
13 # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
14
15 """
16 Test `core.publish_parts()`__ with the LaTeX writer.
17
18 __ https://docutils.sourceforge.io/docs/api/publisher.html#publish-parts
19 """
20
21 from pathlib import Path
22 import os
23 import sys
24 import unittest
25
26
27 if __name__ == '__main__':
28 # prepend the "docutils root" to the Python library path
29 # so we import the local `docutils` package.
30 sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
31
32 import docutils
33 from docutils.core import publish_parts
34 from docutils.writers import latex2e
35
36 # DATA_ROOT is ./test/data from the docutils root
37 DATA_ROOT = Path(__file__).resolve().parents[1] / 'data'
38
39 ham = os.path.relpath(DATA_ROOT/'ham.tex').replace('\\', '/')
40 spam = os.path.relpath(DATA_ROOT/'spam').replace('\\', '/')
41 # workaround for PyPy (cf. https://sourceforge.net/p/docutils/bugs/471/)
42 if sys.implementation.name == "pypy" and sys.version_info < (3, 10):
43 spampath = f"PosixPath('{spam}.sty')"
44 else:
45 spampath = f"'{spam}.sty'"
46
47 DEFAULT_PARTS = {
48 'abstract': '',
49 'body': '',
50 'body_pre_docinfo': '',
51 'dedication': '',
52 'docinfo': '',
53 'encoding': 'utf-8',
54 'errors': 'strict',
55 'fallbacks': '',
56 'head_prefix': '\\documentclass[a4paper]{article}\n',
57 'latex_preamble': '% PDF Standard Fonts\n'
58 '\\usepackage{mathptmx} % Times\n'
59 '\\usepackage[scaled=.90]{helvet}\n'
60 '\\usepackage{courier}\n',
61 'pdfsetup': r"""% hyperlinks:
62 \ifdefined\hypersetup
63 \else
64 \usepackage[hyperfootnotes=false,
65 colorlinks=true,linkcolor=blue,urlcolor=blue]{hyperref}
66 \usepackage{bookmark}
67 \urlstyle{same} % normal text font (alternatives: tt, rm, sf)
68 \fi
69 """,
70 'requirements': '\\usepackage[T1]{fontenc}\n',
71 'stylesheet': '',
72 'subtitle': '',
73 'template': """\
74 $head_prefix% generated by Docutils <https://docutils.sourceforge.io/>
75 \\usepackage{cmap} % fix search and cut-and-paste in Acrobat
76 $requirements
77 %%% Custom LaTeX preamble
78 $latex_preamble
79 %%% User specified packages and stylesheets
80 $stylesheet
81 %%% Fallback definitions for Docutils-specific commands
82 $fallbacks
83 $pdfsetup
84 %%% Body
85 \\begin{document}
86 $titledata$body_pre_docinfo$docinfo$dedication$abstract$body
87 \\end{document}
88 """,
89 'title': '',
90 'titledata': '',
91 'version': f'{docutils.__version__}',
92 }
93
94
95 REQUIREMENTS_TABLE = r"""\usepackage{longtable,ltcaption,array}
96 \setlength{\extrarowheight}{2pt}
97 \newlength{\DUtablewidth} % internal use in tables
98 """
99
100
101 class LaTeXWriterPublishPartsTestCase(unittest.TestCase):
102 """Test LaTeX writer `publish_parts()` interface."""
103
104 maxDiff = None
105 settings = {'_disable_config': True,
106 'strict_visitor': True,
107 # avoid latex writer future warnings:
108 'use_latex_citations': False,
109 'legacy_column_widths': True,
110 }
111
112 def test_publish_parts(self):
113 for name, (settings_overrides, cases) in samples.items():
114 for casenum, (case_input, expected) in enumerate(cases):
115 parts = publish_parts(
116 source=case_input,
117 writer=latex2e.Writer(),
118 settings_overrides=self.settings|settings_overrides,
119 )
120 expected = DEFAULT_PARTS | expected
121 with self.subTest(id=f'samples[{name!r}][{casenum}]'):
122 for key in parts.keys():
123 if key == 'whole':
124 continue # assembly tested in functional tests
125 self.assertEqual(f'{expected[key]}', f'{parts[key]}',
126 msg=f'Differences in part "{key}"!')
127
128
129 samples = {}
130
131 samples['default'] = ({}, [
132 ['', # empty input string
133 {} # results in default parts
134 ],
135 ['2 µm is just 2/1000000 m',
136 {'body': '\n2 µm is just 2/1000000 m\n',
137 'requirements': '\\usepackage[T1]{fontenc}\n'
138 '\\usepackage{textcomp} % text symbol macros\n',
139 }
140 ],
141 # load "babel" if there is a text part in a foreign language
142 ["""\
143 .. role:: language-es
144
145 Und damit :language-es:`basta`!
146 """,
147 {'body': r"""
148 Und damit \foreignlanguage{spanish}{basta}!
149 """,
150 'requirements': '\\usepackage[T1]{fontenc}\n'
151 '\\usepackage[spanish,main=english]{babel}\n'
152 '\\AtBeginDocument{\\shorthandoff{.<>}}\n'
153 }],
154 # load requirements for code syntax higlight
155 [':code:`x=1`',
156 {'body': '\n\\texttt{\\DUrole{code}{x=1}}\n',
157 'fallbacks': r"""
158 % basic code highlight:
159 \providecommand*\DUrolecomment[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
160 \providecommand*\DUroledeleted[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
161 \providecommand*\DUrolekeyword[1]{\textbf{#1}}
162 \providecommand*\DUrolestring[1]{\textit{#1}}
163
164 % custom inline roles: \DUrole{#1}{#2} tries \DUrole#1{#2}
165 \providecommand*{\DUrole}[2]{%
166 \ifcsname DUrole#1\endcsname%
167 \csname DUrole#1\endcsname{#2}%
168 \else%
169 #2%
170 \fi%
171 }
172 """,
173 'requirements': '\\usepackage[T1]{fontenc}\n'
174 '\\usepackage{color}\n'
175 }],
176 # footnote text
177 ["""\
178 .. [1] paragraph
179
180 .. [2] 1. enumeration
181 """,
182 {'body': r"""%
183 \DUfootnotetext{footnote-1}{}{1}{%
184 paragraph
185 }
186 %
187 \DUfootnotetext{footnote-2}{}{2}{%
188 \begin{enumerate}
189 \item enumeration
190 \end{enumerate}
191 }
192 """,
193 'fallbacks': r"""
194 % numbered or symbol footnotes with hyperlinks and backlinks
195 \providecommand*{\DUfootnotemark}[3]{%
196 \raisebox{1em}{\hypertarget{#1}{}}%
197 \hyperref[#2]{\textsuperscript{#3}}%
198 }
199 \providecommand{\DUfootnotetext}[4]{%
200 \begingroup%
201 \renewcommand{\thefootnote}{%
202 \protect\phantomsection\protect\label{#1}
203 \protect\hyperlink{#2}{#3}}%
204 \footnotetext{#4}%
205 \endgroup%
206 }
207 """,
208 }],
209 # no section numbering: switch off section numbering in requirements
210 ["""\
211 .. contents::
212
213 unnumbered section
214 ------------------
215 """,
216 {'body': r"""
217 \pdfbookmark[1]{Contents}{contents}
218 \tableofcontents
219
220
221 \section{unnumbered section}
222 """,
223 'requirements': '\\usepackage[T1]{fontenc}\n'
224 '\\setcounter{secnumdepth}{0}\n',
225 }],
226 # Docutils section numbering: switch off section numbering in requirements
227 ["""\
228 .. contents::
229 .. sectnum::
230
231 first section
232 -------------
233 """,
234 {'body': r"""
235 \pdfbookmark[1]{Contents}{contents}
236 \tableofcontents
237
238
239 \section{1   first section}
240 """,
241 'requirements': '\\usepackage[T1]{fontenc}\n'
242 '\\setcounter{secnumdepth}{0}\n',
243 }],
244 # LaTeX ToC with limited depth, no section numbers
245 ["""\
246 .. contents::
247 :depth: 1
248
249 first section
250 -------------
251 """,
252 {'body': r"""
253 \pdfbookmark[1]{Contents}{contents}
254 \setcounter{tocdepth}{1}
255 \tableofcontents
256
257
258 \section{first section}
259 """,
260 'requirements': '\\usepackage[T1]{fontenc}\n'
261 '\\setcounter{secnumdepth}{0}\n',
262 }],
263 # local ToC reqires "minitoc" and \tableofcontents or \faketableofcontents
264 ["""\
265 section with local ToC
266 ======================
267
268 .. contents::
269 :local:
270
271 section not in local toc
272 ========================
273 """,
274 {'body': r"""
275
276 \section{section with local ToC}
277
278 \mtcsettitle{secttoc}{}
279 \secttoc
280
281
282 \section{section not in local toc}
283
284 \faketableofcontents % for local ToCs
285 """,
286 'requirements': '\\usepackage[T1]{fontenc}\n'
287 '%% local table of contents\n'
288 '\\usepackage{minitoc}\n'
289 '\\dosecttoc\n'
290 '\\mtcsetdepth{secttoc}{5}\n'
291 '\\setcounter{secnumdepth}{0}\n'
292 }],
293 # include images with "\includegraphics", load "graphicx" package
294 ["""\
295 .. image:: blue%20square.png
296 .. image:: /images/vectors.svg
297 """,
298 {'body': r"""
299 \includegraphics{blue square.png}
300
301 \includegraphics{/images/vectors.svg}
302 """,
303 'requirements': '\\usepackage[T1]{fontenc}\n'
304 '\\usepackage{graphicx}\n',
305 }],
306 # table with caption
307 ["""\
308 .. table:: Foo
309
310 +-----+-----+
311 | | |
312 +-----+-----+
313 | | |
314 +-----+-----+
315 """,
316 {'body': r"""
317 \setlength{\DUtablewidth}{\linewidth}%
318 \begin{longtable}{|p{0.075\DUtablewidth}|p{0.075\DUtablewidth}|}
319 \caption{Foo}\\
320 \hline
321 & \\
322 \hline
323 & \\
324 \hline
325 \end{longtable}
326 """,
327 'requirements': '\\usepackage[T1]{fontenc}\n'
328 + REQUIREMENTS_TABLE,
329 }],
330 # table style "booktabs"
331 ["""\
332 .. table::
333 :class: booktabs
334
335 +-----+-+
336 | 1 |2|
337 +-----+-+
338 """,
339 {'body': """
340 \\setlength{\\DUtablewidth}{\\linewidth}%
341 \\begin{longtable*}{p{0.075\\DUtablewidth}p{0.028\\DUtablewidth}}
342 \\toprule
343
344 1
345 & \n\
346 2
347 \\\\
348 \\bottomrule
349 \\end{longtable*}
350 """,
351 'requirements': '\\usepackage[T1]{fontenc}\n'
352 '\\usepackage{booktabs}\n'
353 + REQUIREMENTS_TABLE
354 }],
355 # literal block requires alltt
356 ["""\
357 Test special characters { [ \\\\ ] } in literal block::
358
359 { [ ( \\macro
360
361 } ] )
362 """,
363 {'body': r"""
364 Test special characters \{ {[} \textbackslash{} {]} \} in literal block:
365
366 \begin{quote}
367 \begin{alltt}
368 \{ [ ( \textbackslash{}macro
369
370 \} ] )
371 \end{alltt}
372 \end{quote}
373 """,
374 'requirements': '\\usepackage[T1]{fontenc}\n'
375 '\\usepackage{alltt}\n'
376 }],
377 # raw block in compound directive
378 ["""\
379 .. compound::
380
381 Compound paragraph
382
383 .. raw:: LaTeX
384
385 raw LaTeX block
386
387 compound paragraph continuation.
388 """,
389 {'body': r"""
390 \begin{DUclass}{compound}
391 Compound paragraph
392 raw LaTeX block
393 compound paragraph continuation.
394 \end{DUclass}
395 """,
396 'fallbacks': r"""
397 % class handling for environments (block-level elements)
398 % \begin{DUclass}{spam} tries \DUCLASSspam and
399 % \end{DUclass}{spam} tries \endDUCLASSspam
400 \ifdefined\DUclass
401 \else % poor man's "provideenvironment"
402 \newenvironment{DUclass}[1]%
403 {% "#1" does not work in end-part of environment.
404 \def\DocutilsClassFunctionName{DUCLASS#1}
405 \csname \DocutilsClassFunctionName \endcsname}%
406 {\csname end\DocutilsClassFunctionName \endcsname}%
407 \fi
408 """,
409 }],
410 # titles with inline markup
411 ["""\
412 This is the *Title*
413 ===================
414
415 This is the *Subtitle*
416 ----------------------
417
418 This is a *section title*
419 ~~~~~~~~~~~~~~~~~~~~~~~~~
420
421 This is the *document*.
422 """,
423 {'body': r"""
424
425 \section{This is a \emph{section title}}
426
427 This is the \emph{document}.
428 """,
429 'body_pre_docinfo': '\\maketitle\n',
430 'fallbacks': r"""
431 % subtitle (in document title)
432 \providecommand*{\DUdocumentsubtitle}[1]{{\large #1}}
433 """,
434 'pdfsetup': DEFAULT_PARTS['pdfsetup'] + r"""\hypersetup{
435 pdftitle={This is the Title},
436 }
437 """,
438 'requirements': '\\usepackage[T1]{fontenc}\n'
439 '\\setcounter{secnumdepth}{0}\n',
440 'subtitle': 'This is the \\emph{Subtitle}',
441 'title': 'This is the \\emph{Title}',
442 'titledata': r"""\title{This is the \emph{Title}%
443 \\%
444 \DUdocumentsubtitle{This is the \emph{Subtitle}}}
445 \author{}
446 \date{}
447 """
448 }],
449 # document title and subtitle with labels
450 ["""\
451 .. _top:
452
453 The Document Title
454 ==================
455
456 .. _what-for:
457
458 for test purposes
459 -----------------
460
461 Links to top_ and what-for_.
462 """,
463 {'body': r"""
464 Links to \hyperref[top]{top} and \hyperref[what-for]{what-for}.
465 """,
466 'body_pre_docinfo': '\\maketitle\n',
467 'fallbacks': r"""
468 % subtitle (in document title)
469 \providecommand*{\DUdocumentsubtitle}[1]{{\large #1}}
470 """,
471 'pdfsetup': DEFAULT_PARTS['pdfsetup'] + r"""\hypersetup{
472 pdftitle={The Document Title},
473 }
474 """,
475 'subtitle': 'for test purposes',
476 'title': 'The Document Title',
477 'titledata': r"""\title{The Document Title%
478 \label{top}%
479 \\%
480 \DUdocumentsubtitle{for test purposes}%
481 \label{what-for}}
482 \author{}
483 \date{}
484 """
485 }],
486 # template
487 ["""\
488 """,
489 {'body': '',
490 'requirements': '\\usepackage[T1]{fontenc}\n'
491 }],
492 # bibliographic fields
493 ["""
494 :contact: here@home
495 :organization: Example & Cie.
496 :author: Mr. Smith
497 :date: yesterday
498 :address: 0231 Abendglanz
499 Milky Way 23 b
500 """,
501 {'requirements': '\\usepackage[T1]{fontenc}\n'
502 '\\usepackage{tabularx}\n',
503 'fallbacks': r"""
504 % Provide a length variable and set default, if it is new
505 \providecommand*{\DUprovidelength}[2]{%
506 \ifdefined#1
507 \else
508 \newlength{#1}\setlength{#1}{#2}%
509 \fi
510 }
511
512 % width of docinfo table
513 \DUprovidelength{\DUdocinfowidth}{0.9\linewidth}
514 """,
515 'pdfsetup': DEFAULT_PARTS['pdfsetup']
516 + '\\hypersetup{\n pdfauthor={Mr. Smith}\n}\n',
517 'docinfo': r"""
518 % Docinfo
519 \begin{center}
520 \begin{tabularx}{\DUdocinfowidth}{lX}
521 \textbf{Contact}: & \href{mailto:here@home}{here@home} \\
522 \textbf{Organization}: & Example \& Cie. \\
523 \textbf{Author}: & Mr. Smith \\
524 \textbf{Date}: & yesterday \\
525 \textbf{Address}: & {\raggedright
526 0231 Abendglanz\\
527 Milky Way 23 b} \\
528 \end{tabularx}
529 \end{center}
530 """,
531 }],
532 ["""
533 :authors: * \\A. *Smith*
534 * \\B. Miller
535 :organization: Example & Cie.
536 :author: C. Baker
537 :organization: Another Example
538 """,
539 {'requirements': '\\usepackage[T1]{fontenc}\n'
540 '\\usepackage{tabularx}\n',
541 'fallbacks': r"""
542 % Provide a length variable and set default, if it is new
543 \providecommand*{\DUprovidelength}[2]{%
544 \ifdefined#1
545 \else
546 \newlength{#1}\setlength{#1}{#2}%
547 \fi
548 }
549
550 % width of docinfo table
551 \DUprovidelength{\DUdocinfowidth}{0.9\linewidth}
552 """,
553 'pdfsetup': DEFAULT_PARTS['pdfsetup']
554 + '\\hypersetup{\n pdfauthor={A. Smith; B. Miller; C. Baker}\n}\n',
555 'docinfo': r"""
556 % Docinfo
557 \begin{center}
558 \begin{tabularx}{\DUdocinfowidth}{lX}
559 \textbf{Authors}: & A. \emph{Smith}, B. Miller \\
560 \textbf{Organization}: & Example \& Cie. \\
561 \textbf{Author}: & C. Baker \\
562 \textbf{Organization}: & Another Example \\
563 \end{tabularx}
564 \end{center}
565 """,
566 }],
567 ])
568
569 samples['book'] = ({'documentclass': 'book'}, [
570 # Top section level in LaTeX "book" class is 0 (chapter)
571 ["""\
572 .. contents::
573 :depth: 1
574
575 first chapter
576 -------------
577 """,
578 {'body': r"""
579 \pdfbookmark[1]{Contents}{contents}
580 \setcounter{tocdepth}{0}
581 \tableofcontents
582
583
584 \chapter{first chapter}
585 """,
586 'head_prefix': '\\documentclass[a4paper]{book}\n',
587 'requirements': '\\usepackage[T1]{fontenc}\n'
588 '\\setcounter{secnumdepth}{-1}\n',
589 }],
590 ])
591
592 samples['booktabs'] = ({'table_style': ['booktabs']}, [
593 # column width determined by LaTeX
594 ["""\
595 .. table::
596 :widths: auto
597
598 +-----+-+
599 | 1 |2|
600 +-----+-+
601 """,
602 {'body': r"""
603 \begin{longtable*}{ll}
604 \toprule
605 1 & 2 \\
606 \bottomrule
607 \end{longtable*}
608 """,
609 'requirements': '\\usepackage[T1]{fontenc}\n'
610 '\\usepackage{booktabs}\n'
611 + REQUIREMENTS_TABLE
612 }],
613 # column widhts specified via option
614 ["""\
615 .. table::
616 :widths: 15, 30
617
618 +-----+-----+
619 | 1 | 2 |
620 +-----+-----+
621 """,
622 {'body': """
623 \\setlength{\\DUtablewidth}{\\linewidth}%
624 \\begin{longtable*}{p{0.191\\DUtablewidth}p{0.365\\DUtablewidth}}
625 \\toprule
626
627 1
628 & \n\
629 2
630 \\\\
631 \\bottomrule
632 \\end{longtable*}
633 """,
634 'requirements': '\\usepackage[T1]{fontenc}\n'
635 '\\usepackage{booktabs}\n'
636 + REQUIREMENTS_TABLE
637 }],
638 # borderless overrides "booktabs" table_style
639 ["""\
640 .. table::
641 :class: borderless
642
643 +-----+-----+
644 | 1 | 2 |
645 +-----+-----+
646 | 3 | 4 |
647 +-----+-----+
648 """,
649 {'body': """
650 \\setlength{\\DUtablewidth}{\\linewidth}%
651 \\begin{longtable*}{p{0.075\\DUtablewidth}p{0.075\\DUtablewidth}}
652
653 1
654 & \n\
655 2
656 \\\\
657
658 3
659 & \n\
660 4
661 \\\\
662 \\end{longtable*}
663 """,
664 'requirements': '\\usepackage[T1]{fontenc}\n'
665 + REQUIREMENTS_TABLE
666 }],
667 ])
668
669
670 samples['Docutils ToC and sectnum'] = ({'use_latex_toc': False}, [
671 ["""\
672 .. contents:: Table of Contents
673
674 Title 1
675 =======
676 Paragraph 1.
677
678 Title 2
679 -------
680 Paragraph 2.
681 """,
682 {'body': r"""
683 \pdfbookmark[1]{Table of Contents}{table-of-contents}
684
685 \begin{DUclass}{contents}
686
687 \DUtitle{Table of Contents}
688
689 \begin{itemize}
690 \item \hyperref[title-1]{Title 1}
691
692 \begin{itemize}
693 \item \hyperref[title-2]{Title 2}
694 \end{itemize}
695 \end{itemize}
696 \end{DUclass}
697
698
699 \section{Title 1%
700 \label{title-1}%
701 }
702
703 Paragraph 1.
704
705
706 \subsection{Title 2%
707 \label{title-2}%
708 }
709
710 Paragraph 2.
711 """,
712 'fallbacks': r"""
713 % class handling for environments (block-level elements)
714 % \begin{DUclass}{spam} tries \DUCLASSspam and
715 % \end{DUclass}{spam} tries \endDUCLASSspam
716 \ifdefined\DUclass
717 \else % poor man's "provideenvironment"
718 \newenvironment{DUclass}[1]%
719 {% "#1" does not work in end-part of environment.
720 \def\DocutilsClassFunctionName{DUCLASS#1}
721 \csname \DocutilsClassFunctionName \endcsname}%
722 {\csname end\DocutilsClassFunctionName \endcsname}%
723 \fi
724
725 % title for topics, admonitions, unsupported section levels, and sidebar
726 \providecommand*{\DUtitle}[1]{%
727 \smallskip\noindent\textbf{#1}\smallskip}
728
729 \providecommand*{\DUCLASScontents}{%
730 \renewenvironment{itemize}%
731 {\begin{list}{}{\setlength{\partopsep}{0pt}
732 \setlength{\parsep}{0pt}}
733 }%
734 {\end{list}}%
735 }
736 """,
737 'requirements': '\\usepackage[T1]{fontenc}\n'
738 '\\setcounter{secnumdepth}{0}\n',
739 }],
740 ])
741
742 samples['LaTeX docinfo'] = ({'use_latex_docinfo': True}, [
743 # bibliographic fields
744 ["""
745 :contact: here@home
746 :organization: Example & Cie.
747 :author: Mr. Smith
748 :date: yesterday
749 :address: 0231 Abendglanz
750 Milky Way 23 b
751 """,
752 {'pdfsetup': DEFAULT_PARTS['pdfsetup']
753 + '\\hypersetup{\n pdfauthor={Mr. Smith}\n}\n',
754 'titledata': r"""\title{}
755 \author{Mr. Smith\\
756 \href{mailto:here@home}{here@home}\\
757 Example \& Cie.\\
758 0231 Abendglanz\\
759 Milky Way 23 b}
760 \date{yesterday}
761 """,
762 'body_pre_docinfo': '\\maketitle\n',
763 }],
764 ["""
765 :authors: * \\A. *Smith*
766 * \\B. Miller
767 :organization: Example & Cie.
768 :author: C. Baker
769 :organization: Another Example
770 """,
771 {'pdfsetup': DEFAULT_PARTS['pdfsetup']
772 + '\\hypersetup{\n pdfauthor={A. Smith; B. Miller; C. Baker}\n}\n',
773 'titledata': r"""\title{}
774 \author{A. \emph{Smith} \quad B. Miller\\
775 Example \& Cie. \and
776 C. Baker\\
777 Another Example}
778 \date{}
779 """,
780 'body_pre_docinfo': '\\maketitle\n',
781 }],
782 ["""
783 :keywords: custom, docinfo, field
784 """,
785 {'requirements': '\\usepackage[T1]{fontenc}\n'
786 '\\usepackage{tabularx}\n',
787 'fallbacks': r"""
788 % Provide a length variable and set default, if it is new
789 \providecommand*{\DUprovidelength}[2]{%
790 \ifdefined#1
791 \else
792 \newlength{#1}\setlength{#1}{#2}%
793 \fi
794 }
795
796 % width of docinfo table
797 \DUprovidelength{\DUdocinfowidth}{0.9\linewidth}
798 """,
799 'docinfo': r"""
800 % Docinfo
801 \begin{center}
802 \begin{tabularx}{\DUdocinfowidth}{lX}
803 \textbf{keywords}: &
804 custom, docinfo, field
805 \\
806 \end{tabularx}
807 \end{center}
808 """,
809 }],
810 ])
811
812
813 samples['embed_stylesheet'] = ({'stylesheet_path': f'{spam},{ham}',
814 'embed_stylesheet': True,
815 'warning_stream': ''}, [
816 ['two stylesheets embedded in the header',
817 {'body': '\ntwo stylesheets embedded in the header\n',
818 'stylesheet': f"""\
819 % Cannot embed stylesheet:
820 % [Errno 2] No such file or directory: {spampath}
821 % embedded stylesheet: {ham}
822 \\newcommand{{\\ham}}{{wonderful ham}}
823
824 """,
825 }],
826 ])
827
828
829 # section numbering by LaTeX
830 samples['sectnum_xform False'] = ({'sectnum_xform': False,
831 # ignore str values of internal settings:
832 'sectnum_start': '42',
833 'sectnum_depth': '3'
834 }, [
835 ["""\
836 no sectnum directive -> suppress section numbers
837
838 section
839 -------
840 """,
841 {'body': r"""
842 no sectnum directive -> suppress section numbers
843
844
845 \section{section}
846 """,
847 'requirements': '\\usepackage[T1]{fontenc}\n'
848 '\\setcounter{secnumdepth}{0}\n',
849 }],
850 ['no sectnum directive and no section -> no requirements',
851 {'body': '\nno sectnum directive and no section -> no requirements\n'
852 }],
853
854 ["""\
855 default section numbers -> no requirements
856
857 .. sectnum::
858
859 section
860 -------
861 """,
862 {'body': r"""
863 default section numbers -> no requirements
864
865
866 \section{section}
867 """,
868 }],
869 ["""\
870 section numbers with custom start and depth
871
872 .. sectnum::
873 :start: 7
874 :depth: 2
875
876 section
877 -------
878 """,
879 {'body': r"""
880 section numbers with custom start and depth
881
882
883 \section{section}
884 """,
885 'requirements': '\\usepackage[T1]{fontenc}\n'
886 '\\setcounter{secnumdepth}{2}\n'
887 '\\setcounter{section}{6}\n',
888 }],
889 ])
890
891 samples['stylesheet_path'] = ({'stylesheet_path': f'{spam},{ham}'}, [
892 ['two stylesheet links in the header',
893 {'body': '\ntwo stylesheet links in the header\n',
894 'stylesheet': f'\\usepackage{{{spam}}}\n'
895 f'\\input{{{ham}}}\n'
896 }],
897 ])
898
899 # if "svg" package is listed, include SVG images with "\includesvg"
900 samples['svg-image'] = ({'stylesheet': 'svg'}, [
901 ["""\
902 .. image:: blue%20square.png
903 .. image:: /images/vectors.svg
904 """,
905 {'body': r"""
906 \includegraphics{blue square.png}
907
908 \includesvg{/images/vectors.svg}
909 """,
910 'requirements': '\\usepackage[T1]{fontenc}\n'
911 '\\usepackage{graphicx}\n',
912 'stylesheet': '\\usepackage{svg}\n'
913 }],
914 ])
915
916 # TODO: test for quote replacing if the language uses "ASCII-quotes"
917 # as active character (e.g. de (ngerman)).
918
919
920 if __name__ == '__main__':
921 unittest.main()