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