1# .. coding: utf-8 2# $Id: __init__.py 8676 2021-04-08 16:36:09Z milde $ 3# Author: Engelbert Gruber, Günter Milde 4# Maintainer: docutils-develop@lists.sourceforge.net 5# Copyright: This module has been placed in the public domain. 6 7"""LaTeX2e document tree Writer.""" 8 9__docformat__ = 'reStructuredText' 10 11# code contributions from several people included, thanks to all. 12# some named: David Abrahams, Julien Letessier, Lele Gaifax, and others. 13# 14# convention deactivate code by two # i.e. ##. 15 16import os 17import re 18import string 19import sys 20 21if sys.version_info < (3, 0): 22 from io import open 23 from urllib import url2pathname 24else: 25 from urllib.request import url2pathname 26 27try: 28 import roman 29except ImportError: 30 import docutils.utils.roman as roman 31 32import docutils 33from docutils import frontend, nodes, languages, writers, utils 34from docutils.utils.error_reporting import SafeString 35from docutils.transforms import writer_aux 36from docutils.utils.math import pick_math_environment, unichar2tex 37 38if sys.version_info >= (3, 0): 39 unicode = str # noqa 40 41 42class Writer(writers.Writer): 43 44 supported = ('latex', 'latex2e') 45 """Formats this writer supports.""" 46 47 default_template = 'default.tex' 48 default_template_path = os.path.dirname(os.path.abspath(__file__)) 49 default_preamble = '\n'.join([r'% PDF Standard Fonts', 50 r'\usepackage{mathptmx} % Times', 51 r'\usepackage[scaled=.90]{helvet}', 52 r'\usepackage{courier}']) 53 table_style_values = ('standard', 'booktabs', 'nolines', 'borderless', 54 'colwidths-auto', 'colwidths-given') 55 56 settings_spec = ( 57 'LaTeX-Specific Options', 58 None, 59 (('Specify LaTeX documentclass. Default: "article".', 60 ['--documentclass'], 61 {'default': 'article', }), 62 ('Specify document options. Multiple options can be given, ' 63 'separated by commas. Default: "a4paper".', 64 ['--documentoptions'], 65 {'default': 'a4paper', }), 66 ('Format for footnote references: one of "superscript" or ' 67 '"brackets". Default: "superscript".', 68 ['--footnote-references'], 69 {'choices': ['superscript', 'brackets'], 'default': 'superscript', 70 'metavar': '<format>', 71 'overrides': 'trim_footnote_reference_space'}), 72 ('Use \\cite command for citations. (future default)', 73 ['--use-latex-citations'], 74 {'default': False, 'action': 'store_true', 75 'validator': frontend.validate_boolean}), 76 ('Use figure floats for citations ' 77 '(might get mixed with real figures). (current default)', 78 ['--figure-citations'], 79 {'dest': 'use_latex_citations', 'action': 'store_false', 80 'validator': frontend.validate_boolean}), 81 ('Format for block quote attributions: one of "dash" (em-dash ' 82 'prefix), "parentheses"/"parens", or "none". Default: "dash".', 83 ['--attribution'], 84 {'choices': ['dash', 'parentheses', 'parens', 'none'], 85 'default': 'dash', 'metavar': '<format>'}), 86 ('Specify LaTeX packages/stylesheets. ' 87 'A style is referenced with "\\usepackage" if extension is ' 88 '".sty" or omitted and with "\\input" else. ' 89 ' Overrides previous --stylesheet and --stylesheet-path settings.', 90 ['--stylesheet'], 91 {'default': '', 'metavar': '<file[,file,...]>', 92 'overrides': 'stylesheet_path', 93 'validator': frontend.validate_comma_separated_list}), 94 ('Comma separated list of LaTeX packages/stylesheets. ' 95 'Relative paths are expanded if a matching file is found in ' 96 'the --stylesheet-dirs. With --link-stylesheet, ' 97 'the path is rewritten relative to the output *.tex file. ', 98 ['--stylesheet-path'], 99 {'metavar': '<file[,file,...]>', 'overrides': 'stylesheet', 100 'validator': frontend.validate_comma_separated_list}), 101 ('Link to the stylesheet(s) in the output file. (default)', 102 ['--link-stylesheet'], 103 {'dest': 'embed_stylesheet', 'action': 'store_false'}), 104 ('Embed the stylesheet(s) in the output file. ' 105 'Stylesheets must be accessible during processing. ', 106 ['--embed-stylesheet'], 107 {'default': False, 'action': 'store_true', 108 'validator': frontend.validate_boolean}), 109 ('Comma-separated list of directories where stylesheets are found. ' 110 'Used by --stylesheet-path when expanding relative path arguments. ' 111 'Default: ".".', 112 ['--stylesheet-dirs'], 113 {'metavar': '<dir[,dir,...]>', 114 'validator': frontend.validate_comma_separated_list, 115 'default': ['.']}), 116 ('Customization by LaTeX code in the preamble. ' 117 'Default: select PDF standard fonts (Times, Helvetica, Courier).', 118 ['--latex-preamble'], 119 {'default': default_preamble}), 120 ('Specify the template file. Default: "%s".' % default_template, 121 ['--template'], 122 {'default': default_template, 'metavar': '<file>'}), 123 ('Table of contents by LaTeX. (default)', 124 ['--use-latex-toc'], 125 {'default': True, 'action': 'store_true', 126 'validator': frontend.validate_boolean}), 127 ('Table of contents by Docutils (without page numbers).', 128 ['--use-docutils-toc'], 129 {'dest': 'use_latex_toc', 'action': 'store_false', 130 'validator': frontend.validate_boolean}), 131 ('Add parts on top of the section hierarchy.', 132 ['--use-part-section'], 133 {'default': False, 'action': 'store_true', 134 'validator': frontend.validate_boolean}), 135 ('Attach author and date to the document info table. (default)', 136 ['--use-docutils-docinfo'], 137 {'dest': 'use_latex_docinfo', 'action': 'store_false', 138 'validator': frontend.validate_boolean}), 139 ('Attach author and date to the document title.', 140 ['--use-latex-docinfo'], 141 {'default': False, 'action': 'store_true', 142 'validator': frontend.validate_boolean}), 143 ("Typeset abstract as topic. (default)", 144 ['--topic-abstract'], 145 {'dest': 'use_latex_abstract', 'action': 'store_false', 146 'validator': frontend.validate_boolean}), 147 ("Use LaTeX abstract environment for the document's abstract.", 148 ['--use-latex-abstract'], 149 {'default': False, 'action': 'store_true', 150 'validator': frontend.validate_boolean}), 151 ('Color of any hyperlinks embedded in text. ' 152 'Default: "blue" (use "false" to disable).', 153 ['--hyperlink-color'], {'default': 'blue'}), 154 ('Additional options to the "hyperref" package.', 155 ['--hyperref-options'], {'default': ''}), 156 ('Enable compound enumerators for nested enumerated lists ' 157 '(e.g. "1.2.a.ii").', 158 ['--compound-enumerators'], 159 {'default': False, 'action': 'store_true', 160 'validator': frontend.validate_boolean}), 161 ('Disable compound enumerators for nested enumerated lists. ' 162 '(default)', 163 ['--no-compound-enumerators'], 164 {'action': 'store_false', 'dest': 'compound_enumerators'}), 165 ('Enable section ("." subsection ...) prefixes for compound ' 166 'enumerators. This has no effect without --compound-enumerators.', 167 ['--section-prefix-for-enumerators'], 168 {'default': None, 'action': 'store_true', 169 'validator': frontend.validate_boolean}), 170 ('Disable section prefixes for compound enumerators. (default)', 171 ['--no-section-prefix-for-enumerators'], 172 {'action': 'store_false', 'dest': 'section_prefix_for_enumerators'}), 173 ('Set the separator between section number and enumerator ' 174 'for compound enumerated lists. Default: "-".', 175 ['--section-enumerator-separator'], 176 {'default': '-', 'metavar': '<char>'}), 177 ('When possible, use the specified environment for literal-blocks. ' 178 'Default: "" (fall back to "alltt").', 179 ['--literal-block-env'], 180 {'default': ''}), 181 ('When possible, use "verbatim" for literal-blocks. ' 182 'Compatibility alias for "--literal-block-env=verbatim".', 183 ['--use-verbatim-when-possible'], 184 {'default': False, 'action': 'store_true', 185 'validator': frontend.validate_boolean}), 186 ('Table style. "standard" with horizontal and vertical lines, ' 187 '"booktabs" (LaTeX booktabs style) only horizontal lines ' 188 'above and below the table and below the header, or "borderless". ' 189 'Default: "standard"', 190 ['--table-style'], 191 {'default': ['standard'], 192 'metavar': '<format>', 193 'action': 'append', 194 'validator': frontend.validate_comma_separated_list, 195 'choices': table_style_values}), 196 ('LaTeX graphicx package option. ' 197 'Possible values are "dvipdfmx", "dvips", "dvisvgm", ' 198 '"luatex", "pdftex", and "xetex".' 199 'Default: "".', 200 ['--graphicx-option'], 201 {'default': ''}), 202 ('LaTeX font encoding. ' 203 'Possible values are "", "T1" (default), "OT1", "LGR,T1" or ' 204 'any other combination of options to the `fontenc` package. ', 205 ['--font-encoding'], 206 {'default': 'T1'}), 207 ('Per default the latex-writer puts the reference title into ' 208 'hyperreferences. Specify "ref*" or "pageref*" to get the section ' 209 'number or the page number.', 210 ['--reference-label'], 211 {'default': ''}), 212 ('Specify style and database for bibtex, for example ' 213 '"--use-bibtex=mystyle,mydb1,mydb2".', 214 ['--use-bibtex'], 215 {'default': ''}), 216 ('Use legacy functions with class value list for ' 217 '\\DUtitle and \\DUadmonition (current default). ', 218 ['--legacy-class-functions'], 219 {'default': True, 220 'action': 'store_true', 221 'validator': frontend.validate_boolean}), 222 ('Use \\DUrole and "DUclass" wrappers for class values. ' 223 'Place admonition content in an environment (future default).', 224 ['--new-class-functions'], 225 {'dest': 'legacy_class_functions', 226 'action': 'store_false', 227 'validator': frontend.validate_boolean}), 228 # TODO: implement "latex footnotes" alternative 229 ('Footnotes with numbers/symbols by Docutils. (default) ' 230 '(The alternative, --latex-footnotes, is not implemented yet.)', 231 ['--docutils-footnotes'], 232 {'default': True, 233 'action': 'store_true', 234 'validator': frontend.validate_boolean}), 235 ),) 236 237 settings_defaults = {'sectnum_depth': 0 # updated by SectNum transform 238 } 239 config_section = 'latex2e writer' 240 config_section_dependencies = ('writers', 'latex writers') 241 242 head_parts = ('head_prefix', 'requirements', 'latex_preamble', 243 'stylesheet', 'fallbacks', 'pdfsetup', 'titledata') 244 visitor_attributes = head_parts + ('title', 'subtitle', 245 'body_pre_docinfo', 'docinfo', 246 'dedication', 'abstract', 'body') 247 248 output = None 249 """Final translated form of `document`.""" 250 251 def __init__(self): 252 writers.Writer.__init__(self) 253 self.translator_class = LaTeXTranslator 254 255 # Override parent method to add latex-specific transforms 256 def get_transforms(self): 257 return writers.Writer.get_transforms(self) + [ 258 # Convert specific admonitions to generic one 259 writer_aux.Admonitions, 260 # TODO: footnote collection transform 261 ] 262 263 def translate(self): 264 visitor = self.translator_class(self.document) 265 self.document.walkabout(visitor) 266 # copy parts 267 for part in self.visitor_attributes: 268 setattr(self, part, getattr(visitor, part)) 269 # get template string from file 270 try: 271 template_file = open(self.document.settings.template, 272 encoding='utf8') 273 except IOError: 274 template_file = open(os.path.join(self.default_template_path, 275 self.document.settings.template), encoding= 'utf8') 276 template = string.Template(template_file.read()) 277 template_file.close() 278 # fill template 279 self.assemble_parts() # create dictionary of parts 280 self.output = template.substitute(self.parts) 281 282 def assemble_parts(self): 283 """Assemble the `self.parts` dictionary of output fragments.""" 284 writers.Writer.assemble_parts(self) 285 for part in self.visitor_attributes: 286 lines = getattr(self, part) 287 if part in self.head_parts: 288 if lines: 289 lines.append('') # to get a trailing newline 290 self.parts[part] = '\n'.join(lines) 291 else: 292 # body contains inline elements, so join without newline 293 self.parts[part] = ''.join(lines) 294 295 296class Babel(object): 297 """Language specifics for LaTeX.""" 298 299 # TeX (babel) language names: 300 # ! not all of these are supported by Docutils! 301 # 302 # based on LyX' languages file with adaptions to `BCP 47`_ 303 # (http://www.rfc-editor.org/rfc/bcp/bcp47.txt) and 304 # http://www.tug.org/TUGboat/Articles/tb29-3/tb93miklavec.pdf 305 # * the key without subtags is the default 306 # * case is ignored 307 # cf. http://docutils.sourceforge.net/docs/howto/i18n.html 308 # http://www.w3.org/International/articles/language-tags/ 309 # and http://www.iana.org/assignments/language-subtag-registry 310 language_codes = { 311 # code TeX/Babel-name comment 312 'af': 'afrikaans', 313 'ar': 'arabic', 314 # 'be': 'belarusian', 315 'bg': 'bulgarian', 316 'br': 'breton', 317 'ca': 'catalan', 318 # 'cop': 'coptic', 319 'cs': 'czech', 320 'cy': 'welsh', 321 'da': 'danish', 322 'de': 'ngerman', # new spelling (de_1996) 323 'de-1901': 'german', # old spelling 324 'de-AT': 'naustrian', 325 'de-AT-1901': 'austrian', 326 'dsb': 'lowersorbian', 327 'el': 'greek', # monotonic (el-monoton) 328 'el-polyton': 'polutonikogreek', 329 'en': 'english', # TeX' default language 330 'en-AU': 'australian', 331 'en-CA': 'canadian', 332 'en-GB': 'british', 333 'en-NZ': 'newzealand', 334 'en-US': 'american', 335 'eo': 'esperanto', 336 'es': 'spanish', 337 'et': 'estonian', 338 'eu': 'basque', 339 # 'fa': 'farsi', 340 'fi': 'finnish', 341 'fr': 'french', 342 'fr-CA': 'canadien', 343 'ga': 'irish', # Irish Gaelic 344 # 'grc': # Ancient Greek 345 'grc-ibycus': 'ibycus', # Ibycus encoding 346 'gl': 'galician', 347 'he': 'hebrew', 348 'hr': 'croatian', 349 'hsb': 'uppersorbian', 350 'hu': 'magyar', 351 'ia': 'interlingua', 352 'id': 'bahasai', # Bahasa (Indonesian) 353 'is': 'icelandic', 354 'it': 'italian', 355 'ja': 'japanese', 356 'kk': 'kazakh', 357 'la': 'latin', 358 'lt': 'lithuanian', 359 'lv': 'latvian', 360 'mn': 'mongolian', # Mongolian, Cyrillic script (mn-cyrl) 361 'ms': 'bahasam', # Bahasa (Malay) 362 'nb': 'norsk', # Norwegian Bokmal 363 'nl': 'dutch', 364 'nn': 'nynorsk', # Norwegian Nynorsk 365 'no': 'norsk', # Norwegian (Bokmal) 366 'pl': 'polish', 367 'pt': 'portuges', 368 'pt-BR': 'brazil', 369 'ro': 'romanian', 370 'ru': 'russian', 371 'se': 'samin', # North Sami 372 'sh-Cyrl': 'serbianc', # Serbo-Croatian, Cyrillic script 373 'sh-Latn': 'serbian', # Serbo-Croatian, Latin script see also 'hr' 374 'sk': 'slovak', 375 'sl': 'slovene', 376 'sq': 'albanian', 377 'sr': 'serbianc', # Serbian, Cyrillic script (contributed) 378 'sr-Latn': 'serbian', # Serbian, Latin script 379 'sv': 'swedish', 380 # 'th': 'thai', 381 'tr': 'turkish', 382 'uk': 'ukrainian', 383 'vi': 'vietnam', 384 # zh-Latn: Chinese Pinyin 385 } 386 # normalize (downcase) keys 387 language_codes = dict([(k.lower(), v) for (k, v) in language_codes.items()]) 388 389 warn_msg = 'Language "%s" not supported by LaTeX (babel)' 390 391 # "Active characters" are shortcuts that start a LaTeX macro and may need 392 # escaping for literals use. Characters that prevent literal use (e.g. 393 # starting accent macros like "a -> ä) will be deactivated if one of the 394 # defining languages is used in the document. 395 # Special cases: 396 # ~ (tilde) -- used in estonian, basque, galician, and old versions of 397 # spanish -- cannot be deactivated as it denotes a no-break space macro, 398 # " (straight quote) -- used in albanian, austrian, basque 399 # brazil, bulgarian, catalan, czech, danish, dutch, estonian, 400 # finnish, galician, german, icelandic, italian, latin, naustrian, 401 # ngerman, norsk, nynorsk, polish, portuges, russian, serbian, slovak, 402 # slovene, spanish, swedish, ukrainian, and uppersorbian -- 403 # is escaped as ``\textquotedbl``. 404 active_chars = {# TeX/Babel-name: active characters to deactivate 405 # 'breton': ':;!?' # ensure whitespace 406 # 'esperanto': '^', 407 # 'estonian': '~"`', 408 # 'french': ':;!?' # ensure whitespace 409 'galician': '.<>', # also '~"' 410 # 'magyar': '`', # for special hyphenation cases 411 'spanish': '.<>', # old versions also '~' 412 # 'turkish': ':!=' # ensure whitespace 413 } 414 415 def __init__(self, language_code, reporter=None): 416 self.reporter = reporter 417 self.language = self.language_name(language_code) 418 self.otherlanguages = {} 419 420 def __call__(self): 421 """Return the babel call with correct options and settings""" 422 languages = sorted(self.otherlanguages.keys()) 423 languages.append(self.language or 'english') 424 self.setup = [r'\usepackage[%s]{babel}' % ','.join(languages)] 425 # Deactivate "active characters" 426 shorthands = [] 427 for c in ''.join([self.active_chars.get(l, '') for l in languages]): 428 if c not in shorthands: 429 shorthands.append(c) 430 if shorthands: 431 self.setup.append(r'\AtBeginDocument{\shorthandoff{%s}}' 432 % ''.join(shorthands)) 433 # Including '~' in shorthandoff prevents its use as no-break space 434 if 'galician' in languages: 435 self.setup.append(r'\deactivatetilden % restore ~ in Galician') 436 if 'estonian' in languages: 437 self.setup.extend([r'\makeatletter', 438 r' \addto\extrasestonian{\bbl@deactivate{~}}', 439 r'\makeatother']) 440 if 'basque' in languages: 441 self.setup.extend([r'\makeatletter', 442 r' \addto\extrasbasque{\bbl@deactivate{~}}', 443 r'\makeatother']) 444 if (languages[-1] == 'english' and 445 'french' in self.otherlanguages.keys()): 446 self.setup += ['% Prevent side-effects if French hyphenation ' 447 'patterns are not loaded:', 448 r'\frenchbsetup{StandardLayout}', 449 r'\AtBeginDocument{\selectlanguage{%s}' 450 r'\noextrasfrench}' % self.language] 451 return '\n'.join(self.setup) 452 453 def language_name(self, language_code): 454 """Return TeX language name for `language_code`""" 455 for tag in utils.normalize_language_tag(language_code): 456 try: 457 return self.language_codes[tag] 458 except KeyError: 459 pass 460 if self.reporter is not None: 461 self.reporter.warning(self.warn_msg % language_code) 462 return '' 463 464 def get_language(self): 465 # Obsolete, kept for backwards compatibility with Sphinx 466 return self.language 467 468 469# Building blocks for the latex preamble 470# -------------------------------------- 471 472class SortableDict(dict): 473 """Dictionary with additional sorting methods 474 475 Tip: use key starting with with '_' for sorting before small letters 476 and with '~' for sorting after small letters. 477 """ 478 def sortedkeys(self): 479 """Return sorted list of keys""" 480 keys = sorted(self.keys()) 481 return keys 482 483 def sortedvalues(self): 484 """Return list of values sorted by keys""" 485 return [self[key] for key in self.sortedkeys()] 486 487 488# PreambleCmds 489# ````````````` 490# A container for LaTeX code snippets that can be 491# inserted into the preamble if required in the document. 492# 493# .. The package 'makecmds' would enable shorter definitions using the 494# \providelength and \provideenvironment commands. 495# However, it is pretty non-standard (texlive-latex-extra). 496 497class PreambleCmds(object): 498 """Building blocks for the latex preamble.""" 499 500# Requirements 501 502PreambleCmds.color = r"""\usepackage{color}""" 503 504PreambleCmds.float = r"""\usepackage{float} % extended float configuration 505\floatplacement{figure}{H} % place figures here definitely""" 506 507PreambleCmds.linking = r"""%% hyperlinks: 508\ifthenelse{\isundefined{\hypersetup}}{ 509 \usepackage[%s]{hyperref} 510 \usepackage{bookmark} 511 \urlstyle{same} %% normal text font (alternatives: tt, rm, sf) 512}{}""" 513 514PreambleCmds.minitoc = r"""%% local table of contents 515\usepackage{minitoc}""" 516 517PreambleCmds.table = r"""\usepackage{longtable,ltcaption,array} 518\setlength{\extrarowheight}{2pt} 519\newlength{\DUtablewidth} % internal use in tables""" 520 521PreambleCmds.textcomp = r"""\usepackage{textcomp} % text symbol macros""" 522# TODO? Options [force,almostfull] prevent spurious error messages, 523# see de.comp.text.tex/2005-12/msg01855 524 525# backwards compatibility definitions 526 527PreambleCmds.abstract_legacy = r""" 528% abstract title 529\providecommand*{\DUtitleabstract}[1]{\centerline{\textbf{#1}}}""" 530 531# see https://sourceforge.net/p/docutils/bugs/339/ 532PreambleCmds.admonition_legacy = r""" 533% admonition (specially marked topic) 534\providecommand{\DUadmonition}[2][class-arg]{% 535 % try \DUadmonition#1{#2}: 536 \ifcsname DUadmonition#1\endcsname% 537 \csname DUadmonition#1\endcsname{#2}% 538 \else 539 \begin{center} 540 \fbox{\parbox{0.9\linewidth}{#2}} 541 \end{center} 542 \fi 543}""" 544 545PreambleCmds.error_legacy = r""" 546% error admonition title 547\providecommand*{\DUtitleerror}[1]{\DUtitle{\color{red}#1}}""" 548 549PreambleCmds.title_legacy = r""" 550% title for topics, admonitions, unsupported section levels, and sidebar 551\providecommand*{\DUtitle}[2][class-arg]{% 552 % call \DUtitle#1{#2} if it exists: 553 \ifcsname DUtitle#1\endcsname% 554 \csname DUtitle#1\endcsname{#2}% 555 \else 556 \smallskip\noindent\textbf{#2}\smallskip% 557 \fi 558}""" 559 560## PreambleCmds.caption = r"""% configure caption layout 561## \usepackage{caption} 562## \captionsetup{singlelinecheck=false}% no exceptions for one-liners""" 563 564 565# Definitions from docutils.sty:: 566 567def _read_block(fp): 568 block = [next(fp)] # first line (empty) 569 for line in fp: 570 if not line.strip(): 571 break 572 block.append(line) 573 return ''.join(block).rstrip() 574 575_du_sty = os.path.join(os.path.dirname(os.path.abspath(__file__)), 576 'docutils.sty') 577with open(_du_sty, encoding='utf8') as fp: 578 for line in fp: 579 line = line.strip('% \n') 580 if not line.endswith('::'): 581 continue 582 block_name = line.rstrip(':') 583 if not block_name: 584 continue 585 definitions = _read_block(fp) 586 if block_name in ('color', 'float', 'table', 'textcomp'): 587 definitions = definitions.strip() 588 # print('Block: `%s`'% block_name) 589 # print(definitions) 590 setattr(PreambleCmds, block_name, definitions) 591 592 593# LaTeX encoding maps 594# ------------------- 595# :: 596 597class CharMaps(object): 598 """LaTeX representations for active and Unicode characters.""" 599 600 # characters that need escaping even in `alltt` environments: 601 alltt = { 602 ord('\\'): u'\\textbackslash{}', 603 ord('{'): u'\\{', 604 ord('}'): u'\\}', 605 } 606 # characters that normally need escaping: 607 special = { 608 ord('#'): u'\\#', 609 ord('$'): u'\\$', 610 ord('%'): u'\\%', 611 ord('&'): u'\\&', 612 ord('~'): u'\\textasciitilde{}', 613 ord('_'): u'\\_', 614 ord('^'): u'\\textasciicircum{}', 615 # straight double quotes are 'active' in many languages 616 ord('"'): u'\\textquotedbl{}', 617 # Square brackets are ordinary chars and cannot be escaped with '\', 618 # so we put them in a group '{[}'. (Alternative: ensure that all 619 # macros with optional arguments are terminated with {} and text 620 # inside any optional argument is put in a group ``[{text}]``). 621 # Commands with optional args inside an optional arg must be put in a 622 # group, e.g. ``\item[{\hyperref[label]{text}}]``. 623 ord('['): u'{[}', 624 ord(']'): u'{]}', 625 # the soft hyphen is unknown in 8-bit text 626 # and not properly handled by XeTeX 627 0x00AD: u'\\-', # SOFT HYPHEN 628 } 629 # Unicode chars that are not recognized by LaTeX's utf8 encoding 630 unsupported_unicode = { 631 # TODO: ensure white space also at the beginning of a line? 632 # 0x00A0: u'\\leavevmode\\nobreak\\vadjust{}~' 633 0x2000: u'\\enskip', # EN QUAD 634 0x2001: u'\\quad', # EM QUAD 635 0x2002: u'\\enskip', # EN SPACE 636 0x2003: u'\\quad', # EM SPACE 637 0x2008: u'\\,', # PUNCTUATION SPACE 638 0x200b: u'\\hspace{0pt}', # ZERO WIDTH SPACE 639 0x202F: u'\\,', # NARROW NO-BREAK SPACE 640 # 0x02d8: u'\\\u{ }', # BREVE 641 0x2011: u'\\hbox{-}', # NON-BREAKING HYPHEN 642 0x212b: u'\\AA', # ANGSTROM SIGN 643 0x21d4: u'\\ensuremath{\\Leftrightarrow}', 644 # Docutils footnote symbols: 645 0x2660: u'\\ensuremath{\\spadesuit}', 646 0x2663: u'\\ensuremath{\\clubsuit}', 647 0xfb00: u'ff', # LATIN SMALL LIGATURE FF 648 0xfb01: u'fi', # LATIN SMALL LIGATURE FI 649 0xfb02: u'fl', # LATIN SMALL LIGATURE FL 650 0xfb03: u'ffi', # LATIN SMALL LIGATURE FFI 651 0xfb04: u'ffl', # LATIN SMALL LIGATURE FFL 652 } 653 # Unicode chars that are recognized by LaTeX's utf8 encoding 654 utf8_supported_unicode = { 655 0x00A0: u'~', # NO-BREAK SPACE 656 0x00AB: u'\\guillemotleft{}', # LEFT-POINTING DOUBLE ANGLE QUOTATION MARK 657 0x00bb: u'\\guillemotright{}', # RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK 658 0x200C: u'\\textcompwordmark{}', # ZERO WIDTH NON-JOINER 659 0x2013: u'\\textendash{}', 660 0x2014: u'\\textemdash{}', 661 0x2018: u'\\textquoteleft{}', 662 0x2019: u'\\textquoteright{}', 663 0x201A: u'\\quotesinglbase{}', # SINGLE LOW-9 QUOTATION MARK 664 0x201C: u'\\textquotedblleft{}', 665 0x201D: u'\\textquotedblright{}', 666 0x201E: u'\\quotedblbase{}', # DOUBLE LOW-9 QUOTATION MARK 667 0x2030: u'\\textperthousand{}', # PER MILLE SIGN 668 0x2031: u'\\textpertenthousand{}', # PER TEN THOUSAND SIGN 669 0x2039: u'\\guilsinglleft{}', 670 0x203A: u'\\guilsinglright{}', 671 0x2423: u'\\textvisiblespace{}', # OPEN BOX 672 0x2020: u'\\dag{}', 673 0x2021: u'\\ddag{}', 674 0x2026: u'\\dots{}', 675 0x2122: u'\\texttrademark{}', 676 } 677 # recognized with 'utf8', if textcomp is loaded 678 textcomp = { 679 # Latin-1 Supplement 680 0x00a2: u'\\textcent{}', # ¢ CENT SIGN 681 0x00a4: u'\\textcurrency{}', # ¤ CURRENCY SYMBOL 682 0x00a5: u'\\textyen{}', # ¥ YEN SIGN 683 0x00a6: u'\\textbrokenbar{}', # ¦ BROKEN BAR 684 0x00a7: u'\\textsection{}', # § SECTION SIGN 685 0x00a8: u'\\textasciidieresis{}', # ¨ DIAERESIS 686 0x00a9: u'\\textcopyright{}', # © COPYRIGHT SIGN 687 0x00aa: u'\\textordfeminine{}', # ª FEMININE ORDINAL INDICATOR 688 0x00ac: u'\\textlnot{}', # ¬ NOT SIGN 689 0x00ae: u'\\textregistered{}', # ® REGISTERED SIGN 690 0x00af: u'\\textasciimacron{}', # ¯ MACRON 691 0x00b0: u'\\textdegree{}', # ° DEGREE SIGN 692 0x00b1: u'\\textpm{}', # ± PLUS-MINUS SIGN 693 0x00b2: u'\\texttwosuperior{}', # ² SUPERSCRIPT TWO 694 0x00b3: u'\\textthreesuperior{}', # ³ SUPERSCRIPT THREE 695 0x00b4: u'\\textasciiacute{}', # ´ ACUTE ACCENT 696 0x00b5: u'\\textmu{}', # µ MICRO SIGN 697 0x00b6: u'\\textparagraph{}', # ¶ PILCROW SIGN # != \textpilcrow 698 0x00b9: u'\\textonesuperior{}', # ¹ SUPERSCRIPT ONE 699 0x00ba: u'\\textordmasculine{}', # º MASCULINE ORDINAL INDICATOR 700 0x00bc: u'\\textonequarter{}', # 1/4 FRACTION 701 0x00bd: u'\\textonehalf{}', # 1/2 FRACTION 702 0x00be: u'\\textthreequarters{}', # 3/4 FRACTION 703 0x00d7: u'\\texttimes{}', # × MULTIPLICATION SIGN 704 0x00f7: u'\\textdiv{}', # ÷ DIVISION SIGN 705 # others 706 0x0192: u'\\textflorin{}', # LATIN SMALL LETTER F WITH HOOK 707 0x02b9: u'\\textasciiacute{}', # MODIFIER LETTER PRIME 708 0x02ba: u'\\textacutedbl{}', # MODIFIER LETTER DOUBLE PRIME 709 0x2016: u'\\textbardbl{}', # DOUBLE VERTICAL LINE 710 0x2022: u'\\textbullet{}', # BULLET 711 0x2032: u'\\textasciiacute{}', # PRIME 712 0x2033: u'\\textacutedbl{}', # DOUBLE PRIME 713 0x2035: u'\\textasciigrave{}', # REVERSED PRIME 714 0x2036: u'\\textgravedbl{}', # REVERSED DOUBLE PRIME 715 0x203b: u'\\textreferencemark{}', # REFERENCE MARK 716 0x203d: u'\\textinterrobang{}', # INTERROBANG 717 0x2044: u'\\textfractionsolidus{}', # FRACTION SLASH 718 0x2045: u'\\textlquill{}', # LEFT SQUARE BRACKET WITH QUILL 719 0x2046: u'\\textrquill{}', # RIGHT SQUARE BRACKET WITH QUILL 720 0x2052: u'\\textdiscount{}', # COMMERCIAL MINUS SIGN 721 0x20a1: u'\\textcolonmonetary{}', # COLON SIGN 722 0x20a3: u'\\textfrenchfranc{}', # FRENCH FRANC SIGN 723 0x20a4: u'\\textlira{}', # LIRA SIGN 724 0x20a6: u'\\textnaira{}', # NAIRA SIGN 725 0x20a9: u'\\textwon{}', # WON SIGN 726 0x20ab: u'\\textdong{}', # DONG SIGN 727 0x20ac: u'\\texteuro{}', # EURO SIGN 728 0x20b1: u'\\textpeso{}', # PESO SIGN 729 0x20b2: u'\\textguarani{}', # GUARANI SIGN 730 0x2103: u'\\textcelsius{}', # DEGREE CELSIUS 731 0x2116: u'\\textnumero{}', # NUMERO SIGN 732 0x2117: u'\\textcircledP{}', # SOUND RECORDING COYRIGHT 733 0x211e: u'\\textrecipe{}', # PRESCRIPTION TAKE 734 0x2120: u'\\textservicemark{}', # SERVICE MARK 735 0x2122: u'\\texttrademark{}', # TRADE MARK SIGN 736 0x2126: u'\\textohm{}', # OHM SIGN 737 0x2127: u'\\textmho{}', # INVERTED OHM SIGN 738 0x212e: u'\\textestimated{}', # ESTIMATED SYMBOL 739 0x2190: u'\\textleftarrow{}', # LEFTWARDS ARROW 740 0x2191: u'\\textuparrow{}', # UPWARDS ARROW 741 0x2192: u'\\textrightarrow{}', # RIGHTWARDS ARROW 742 0x2193: u'\\textdownarrow{}', # DOWNWARDS ARROW 743 0x2212: u'\\textminus{}', # MINUS SIGN 744 0x2217: u'\\textasteriskcentered{}', # ASTERISK OPERATOR 745 0x221a: u'\\textsurd{}', # SQUARE ROOT 746 0x2422: u'\\textblank{}', # BLANK SYMBOL 747 0x25e6: u'\\textopenbullet{}', # WHITE BULLET 748 0x25ef: u'\\textbigcircle{}', # LARGE CIRCLE 749 0x266a: u'\\textmusicalnote{}', # EIGHTH NOTE 750 0x26ad: u'\\textmarried{}', # MARRIAGE SYMBOL 751 0x26ae: u'\\textdivorced{}', # DIVORCE SYMBOL 752 0x27e8: u'\\textlangle{}', # MATHEMATICAL LEFT ANGLE BRACKET 753 0x27e9: u'\\textrangle{}', # MATHEMATICAL RIGHT ANGLE BRACKET 754 } 755 # Unicode chars that require a feature/package to render 756 pifont = { 757 0x2665: u'\\ding{170}', # black heartsuit 758 0x2666: u'\\ding{169}', # black diamondsuit 759 0x2713: u'\\ding{51}', # check mark 760 0x2717: u'\\ding{55}', # check mark 761 } 762 # TODO: greek alphabet ... ? 763 # see also LaTeX codec 764 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252124 765 # and unimap.py from TeXML 766 767 768class DocumentClass(object): 769 """Details of a LaTeX document class.""" 770 771 def __init__(self, document_class, with_part=False): 772 self.document_class = document_class 773 self._with_part = with_part 774 self.sections = ['section', 'subsection', 'subsubsection', 775 'paragraph', 'subparagraph'] 776 if self.document_class in ('book', 'memoir', 'report', 777 'scrbook', 'scrreprt'): 778 self.sections.insert(0, 'chapter') 779 if self._with_part: 780 self.sections.insert(0, 'part') 781 782 def section(self, level): 783 """Return the LaTeX section name for section `level`. 784 785 The name depends on the specific document class. 786 Level is 1,2,3..., as level 0 is the title. 787 """ 788 if level <= len(self.sections): 789 return self.sections[level-1] 790 # unsupported levels 791 return 'DUtitle' 792 793 794class Table(object): 795 """Manage a table while traversing. 796 797 Maybe change to a mixin defining the visit/departs, but then 798 class Table internal variables are in the Translator. 799 800 Table style might be 801 802 :standard: horizontal and vertical lines 803 :booktabs: only horizontal lines (requires "booktabs" LaTeX package) 804 :borderless: no borders around table cells 805 :nolines: alias for borderless 806 807 :colwidths-auto: column widths determined by LaTeX 808 :colwidths-given: use colum widths from rST source 809 """ 810 def __init__(self, translator, latex_type): 811 self._translator = translator 812 self._latex_type = latex_type 813 self._open = False 814 # miscellaneous attributes 815 self._attrs = {} 816 self._col_width = [] 817 self._rowspan = [] 818 self.stubs = [] 819 self.colwidths_auto = False 820 self._in_thead = 0 821 822 def open(self): 823 self._open = True 824 self._col_specs = [] 825 self.caption = [] 826 self._attrs = {} 827 self._in_head = False # maybe context with search 828 def close(self): 829 self._open = False 830 self._col_specs = None 831 self.caption = [] 832 self._attrs = {} 833 self.stubs = [] 834 self.colwidths_auto = False 835 836 def is_open(self): 837 return self._open 838 839 def set_table_style(self, table_style, classes): 840 borders = [cls.replace('nolines', 'borderless') 841 for cls in table_style+classes 842 if cls in ('standard', 'booktabs', 'borderless', 'nolines')] 843 try: 844 self.borders = borders[-1] 845 except IndexError: 846 self.borders = 'standard' 847 self.colwidths_auto = (('colwidths-auto' in classes 848 and 'colwidths-given' not in table_style) 849 or ('colwidths-auto' in table_style 850 and ('colwidths-given' not in classes))) 851 852 def get_latex_type(self): 853 if self._latex_type == 'longtable' and not self.caption: 854 # do not advance the "table" counter (requires "ltcaption" package) 855 return('longtable*') 856 return self._latex_type 857 858 def set(self, attr, value): 859 self._attrs[attr] = value 860 def get(self, attr): 861 if attr in self._attrs: 862 return self._attrs[attr] 863 return None 864 865 def get_vertical_bar(self): 866 if self.borders == 'standard': 867 return '|' 868 return '' 869 870 # horizontal lines are drawn below a row, 871 def get_opening(self, width=r'\linewidth'): 872 align_map = {'left': '[l]', 873 'center': '[c]', 874 'right': '[r]', 875 None: ''} 876 align = align_map.get(self.get('align')) 877 latex_type = self.get_latex_type() 878 if align and latex_type not in ("longtable", "longtable*"): 879 opening = [r'\noindent\makebox[\linewidth]%s{%%' % (align,), 880 r'\begin{%s}' % (latex_type,), 881 ] 882 else: 883 opening = [r'\begin{%s}%s' % (latex_type, align)] 884 if not self.colwidths_auto: 885 opening.insert(-1, r'\setlength{\DUtablewidth}{%s}%%'%width) 886 return '\n'.join(opening) 887 888 def get_closing(self): 889 closing = [] 890 if self.borders == 'booktabs': 891 closing.append(r'\bottomrule') 892 # elif self.borders == 'standard': 893 # closing.append(r'\hline') 894 closing.append(r'\end{%s}' % self.get_latex_type()) 895 if self.get('align') and self.get_latex_type() not in ("longtable", "longtable*"): 896 closing.append('}') 897 return '\n'.join(closing) 898 899 def visit_colspec(self, node): 900 self._col_specs.append(node) 901 # "stubs" list is an attribute of the tgroup element: 902 self.stubs.append(node.attributes.get('stub')) 903 904 def get_colspecs(self, node): 905 """Return column specification for longtable. 906 907 Assumes reST line length being 80 characters. 908 Table width is hairy. 909 910 === === 911 ABC DEF 912 === === 913 914 usually gets to narrow, therefore we add 1 (fiddlefactor). 915 """ 916 bar = self.get_vertical_bar() 917 self._rowspan= [0] * len(self._col_specs) 918 self._col_width = [] 919 if self.colwidths_auto: 920 latex_table_spec = (bar+'l')*len(self._col_specs) 921 return latex_table_spec+bar 922 width = 80 923 total_width = 0.0 924 # first see if we get too wide. 925 for node in self._col_specs: 926 colwidth = float(node['colwidth']+1) / width 927 total_width += colwidth 928 # donot make it full linewidth 929 factor = 0.93 930 if total_width > 1.0: 931 factor /= total_width 932 latex_table_spec = '' 933 for node in self._col_specs: 934 colwidth = factor * float(node['colwidth']+1) / width 935 self._col_width.append(colwidth+0.005) 936 latex_table_spec += '%sp{%.3f\\DUtablewidth}' % (bar, colwidth+0.005) 937 return latex_table_spec+bar 938 939 def get_column_width(self): 940 """Return columnwidth for current cell (not multicell).""" 941 try: 942 return '%.2f\\DUtablewidth' % self._col_width[self._cell_in_row] 943 except IndexError: 944 return '*' 945 946 def get_multicolumn_width(self, start, len_): 947 """Return sum of columnwidths for multicell.""" 948 try: 949 mc_width = sum([width 950 for width in ([self._col_width[start + co] 951 for co in range (len_)])]) 952 return 'p{%.2f\\DUtablewidth}' % mc_width 953 except IndexError: 954 return 'l' 955 956 def get_caption(self): 957 if not self.caption: 958 return '' 959 caption = ''.join(self.caption) 960 if 1 == self._translator.thead_depth(): 961 return r'\caption{%s}\\' '\n' % caption 962 return r'\caption[]{%s (... continued)}\\' '\n' % caption 963 964 def need_recurse(self): 965 if self._latex_type == 'longtable': 966 return 1 == self._translator.thead_depth() 967 return 0 968 969 def visit_thead(self): 970 self._in_thead += 1 971 if self.borders == 'standard': 972 return ['\\hline\n'] 973 elif self.borders == 'booktabs': 974 return ['\\toprule\n'] 975 return [] 976 977 def depart_thead(self): 978 a = [] 979 #if self.borders == 'standard': 980 # a.append('\\hline\n') 981 if self.borders == 'booktabs': 982 a.append('\\midrule\n') 983 if self._latex_type == 'longtable': 984 if 1 == self._translator.thead_depth(): 985 a.append('\\endfirsthead\n') 986 else: 987 a.append('\\endhead\n') 988 a.append(r'\multicolumn{%d}{c}' % len(self._col_specs) + 989 r'{\hfill ... continued on next page} \\') 990 a.append('\n\\endfoot\n\\endlastfoot\n') 991 # for longtable one could add firsthead, foot and lastfoot 992 self._in_thead -= 1 993 return a 994 995 def visit_row(self): 996 self._cell_in_row = 0 997 998 def depart_row(self): 999 res = [' \\\\\n'] 1000 self._cell_in_row = None # remove cell counter 1001 for i in range(len(self._rowspan)): 1002 if (self._rowspan[i]>0): 1003 self._rowspan[i] -= 1 1004 1005 if self.borders == 'standard': 1006 rowspans = [i+1 for i in range(len(self._rowspan)) 1007 if (self._rowspan[i]<=0)] 1008 if len(rowspans)==len(self._rowspan): 1009 res.append('\\hline\n') 1010 else: 1011 cline = '' 1012 rowspans.reverse() 1013 # TODO merge clines 1014 while True: 1015 try: 1016 c_start = rowspans.pop() 1017 except: 1018 break 1019 cline += '\\cline{%d-%d}\n' % (c_start, c_start) 1020 res.append(cline) 1021 return res 1022 1023 def set_rowspan(self, cell, value): 1024 try: 1025 self._rowspan[cell] = value 1026 except: 1027 pass 1028 1029 def get_rowspan(self, cell): 1030 try: 1031 return self._rowspan[cell] 1032 except: 1033 return 0 1034 1035 def get_entry_number(self): 1036 return self._cell_in_row 1037 1038 def visit_entry(self): 1039 self._cell_in_row += 1 1040 1041 def is_stub_column(self): 1042 if len(self.stubs) >= self._cell_in_row: 1043 return self.stubs[self._cell_in_row] 1044 return False 1045 1046 1047class LaTeXTranslator(nodes.NodeVisitor): 1048 """ 1049 Generate code for 8-bit LaTeX from a Docutils document tree. 1050 1051 See the docstring of docutils.writers._html_base.HTMLTranslator for 1052 notes on and examples of safe subclassing. 1053 """ 1054 1055 # When options are given to the documentclass, latex will pass them 1056 # to other packages, as done with babel. 1057 # Dummy settings might be taken from document settings 1058 1059 # Generate code for typesetting with 8-bit latex/pdflatex vs. 1060 # xelatex/lualatex engine. Overwritten by the XeTeX writer 1061 is_xetex = False 1062 1063 # Config setting defaults 1064 # ----------------------- 1065 1066 # TODO: use mixins for different implementations. 1067 # list environment for docinfo. else tabularx 1068 ## use_optionlist_for_docinfo = False # TODO: NOT YET IN USE 1069 1070 # Use compound enumerations (1.A.1.) 1071 compound_enumerators = False 1072 1073 # If using compound enumerations, include section information. 1074 section_prefix_for_enumerators = False 1075 1076 # This is the character that separates the section ("." subsection ...) 1077 # prefix from the regular list enumerator. 1078 section_enumerator_separator = '-' 1079 1080 # Auxiliary variables 1081 # ------------------- 1082 1083 has_latex_toc = False # is there a toc in the doc? (needed by minitoc) 1084 is_toc_list = False # is the current bullet_list a ToC? 1085 section_level = 0 1086 1087 # Flags to encode(): 1088 # inside citation reference labels underscores dont need to be escaped 1089 inside_citation_reference_label = False 1090 verbatim = False # do not encode 1091 insert_non_breaking_blanks = False # replace blanks by "~" 1092 insert_newline = False # add latex newline commands 1093 literal = False # literal text (block or inline) 1094 alltt = False # inside `alltt` environment 1095 1096 def __init__(self, document, babel_class=Babel): 1097 nodes.NodeVisitor.__init__(self, document) 1098 # Reporter 1099 # ~~~~~~~~ 1100 self.warn = self.document.reporter.warning 1101 self.error = self.document.reporter.error 1102 1103 # Settings 1104 # ~~~~~~~~ 1105 self.settings = settings = document.settings 1106 self.latex_encoding = self.to_latex_encoding(settings.output_encoding) 1107 self.use_latex_toc = settings.use_latex_toc 1108 self.use_latex_docinfo = settings.use_latex_docinfo 1109 self._use_latex_citations = settings.use_latex_citations 1110 self._reference_label = settings.reference_label 1111 self.hyperlink_color = settings.hyperlink_color 1112 self.compound_enumerators = settings.compound_enumerators 1113 self.font_encoding = getattr(settings, 'font_encoding', '') 1114 self.section_prefix_for_enumerators = ( 1115 settings.section_prefix_for_enumerators) 1116 self.section_enumerator_separator = ( 1117 settings.section_enumerator_separator.replace('_', r'\_')) 1118 # literal blocks: 1119 self.literal_block_env = '' 1120 self.literal_block_options = '' 1121 if settings.literal_block_env: 1122 (none, 1123 self.literal_block_env, 1124 self.literal_block_options, 1125 none) = re.split(r'(\w+)(.*)', settings.literal_block_env) 1126 elif settings.use_verbatim_when_possible: 1127 self.literal_block_env = 'verbatim' 1128 # 1129 if self.settings.use_bibtex: 1130 self.bibtex = self.settings.use_bibtex.split(',', 1) 1131 # TODO avoid errors on not declared citations. 1132 else: 1133 self.bibtex = None 1134 # language module for Docutils-generated text 1135 # (labels, bibliographic_fields, and author_separators) 1136 self.language_module = languages.get_language(settings.language_code, 1137 document.reporter) 1138 self.babel = babel_class(settings.language_code, document.reporter) 1139 self.author_separator = self.language_module.author_separators[0] 1140 d_options = [self.settings.documentoptions] 1141 if self.babel.language not in ('english', ''): 1142 d_options.append(self.babel.language) 1143 self.documentoptions = ','.join(filter(None, d_options)) 1144 self.d_class = DocumentClass(settings.documentclass, 1145 settings.use_part_section) 1146 # graphic package options: 1147 if self.settings.graphicx_option == '': 1148 self.graphicx_package = r'\usepackage{graphicx}' 1149 else: 1150 self.graphicx_package = (r'\usepackage[%s]{graphicx}' % 1151 self.settings.graphicx_option) 1152 # footnotes: TODO: implement LaTeX footnotes 1153 self.docutils_footnotes = settings.docutils_footnotes 1154 # @@ table_style: list of values from fixed set: warn? 1155 # for s in self.settings.table_style: 1156 # if s not in Writer.table_style_values: 1157 # self.warn('Ignoring value "%s" in "table-style" setting.' %s) 1158 1159 # Output collection stacks 1160 # ~~~~~~~~~~~~~~~~~~~~~~~~ 1161 1162 # Document parts 1163 self.head_prefix = [r'\documentclass[%s]{%s}' % 1164 (self.documentoptions, self.settings.documentclass)] 1165 self.requirements = SortableDict() # made a list in depart_document() 1166 self.requirements['__static'] = r'\usepackage{ifthen}' 1167 self.latex_preamble = [settings.latex_preamble] 1168 self.fallbacks = SortableDict() # made a list in depart_document() 1169 self.pdfsetup = [] # PDF properties (hyperref package) 1170 self.title = [] 1171 self.subtitle = [] 1172 self.titledata = [] # \title, \author, \date 1173 ## self.body_prefix = ['\\begin{document}\n'] 1174 self.body_pre_docinfo = [] # \maketitle 1175 self.docinfo = [] 1176 self.dedication = [] 1177 self.abstract = [] 1178 self.body = [] 1179 ## self.body_suffix = ['\\end{document}\n'] 1180 1181 self.context = [] 1182 """Heterogeneous stack. 1183 1184 Used by visit_* and depart_* functions in conjunction with the tree 1185 traversal. Make sure that the pops correspond to the pushes.""" 1186 1187 # Title metadata: 1188 self.title_labels = [] 1189 self.subtitle_labels = [] 1190 # (if use_latex_docinfo: collects lists of 1191 # author/organization/contact/address lines) 1192 self.author_stack = [] 1193 self.date = [] 1194 1195 # PDF properties: pdftitle, pdfauthor 1196 self.pdfauthor = [] 1197 self.pdfinfo = [] 1198 if settings.language_code != 'en': 1199 self.pdfinfo.append(' pdflang={%s},'%settings.language_code) 1200 1201 # Stack of section counters so that we don't have to use_latex_toc. 1202 # This will grow and shrink as processing occurs. 1203 # Initialized for potential first-level sections. 1204 self._section_number = [0] 1205 1206 # The current stack of enumerations so that we can expand 1207 # them into a compound enumeration. 1208 self._enumeration_counters = [] 1209 # The maximum number of enumeration counters we've used. 1210 # If we go beyond this number, we need to create a new 1211 # counter; otherwise, just reuse an old one. 1212 self._max_enumeration_counters = 0 1213 1214 self._bibitems = [] 1215 1216 # object for a table while proccessing. 1217 self.table_stack = [] 1218 self.active_table = Table(self, 'longtable') 1219 1220 # Where to collect the output of visitor methods (default: body) 1221 self.out = self.body 1222 self.out_stack = [] # stack of output collectors 1223 1224 # Process settings 1225 # ~~~~~~~~~~~~~~~~ 1226 # Encodings: 1227 # Docutils' output-encoding => TeX input encoding 1228 if self.latex_encoding != 'ascii': 1229 self.requirements['_inputenc'] = (r'\usepackage[%s]{inputenc}' 1230 % self.latex_encoding) 1231 # TeX font encoding 1232 if not self.is_xetex: 1233 if self.font_encoding: 1234 self.requirements['_fontenc'] = (r'\usepackage[%s]{fontenc}' % 1235 self.font_encoding) 1236 # ensure \textquotedbl is defined: 1237 for enc in self.font_encoding.split(','): 1238 enc = enc.strip() 1239 if enc == 'OT1': 1240 self.requirements['_textquotedblOT1'] = ( 1241 r'\DeclareTextSymbol{\textquotedbl}{OT1}{`\"}') 1242 elif enc not in ('T1', 'T2A', 'T2B', 'T2C', 'T4', 'T5'): 1243 self.requirements['_textquotedbl'] = ( 1244 r'\DeclareTextSymbolDefault{\textquotedbl}{T1}') 1245 # page layout with typearea (if there are relevant document options) 1246 if (settings.documentclass.find('scr') == -1 and 1247 (self.documentoptions.find('DIV') != -1 or 1248 self.documentoptions.find('BCOR') != -1)): 1249 self.requirements['typearea'] = r'\usepackage{typearea}' 1250 1251 # Stylesheets 1252 # (the name `self.stylesheet` is singular because only one 1253 # stylesheet was supported before Docutils 0.6). 1254 stylesheet_list = utils.get_stylesheet_list(settings) 1255 self.fallback_stylesheet = 'docutils' in stylesheet_list 1256 if self.fallback_stylesheet: 1257 stylesheet_list = [sheet for sheet in stylesheet_list 1258 if sheet != 'docutils'] 1259 if self.settings.legacy_class_functions: 1260 # docutils.sty is incompatible with legacy functions 1261 self.fallback_stylesheet = False 1262 else: 1263 # require a minimal version: 1264 self.fallbacks['docutils.sty' 1265 ] = r'\usepackage{docutils}[2020/08/28]' 1266 1267 self.stylesheet = [self.stylesheet_call(path) 1268 for path in stylesheet_list] 1269 1270 # PDF setup 1271 if self.hyperlink_color in ('0', 'false', 'False', ''): 1272 self.hyperref_options = '' 1273 else: 1274 self.hyperref_options = 'colorlinks=true,linkcolor=%s,urlcolor=%s' % ( 1275 self.hyperlink_color, self.hyperlink_color) 1276 if settings.hyperref_options: 1277 self.hyperref_options += ',' + settings.hyperref_options 1278 1279 # LaTeX Toc 1280 # include all supported sections in toc and PDF bookmarks 1281 # (or use documentclass-default (as currently))? 1282 ## if self.use_latex_toc: 1283 ## self.requirements['tocdepth'] = (r'\setcounter{tocdepth}{%d}' % 1284 ## len(self.d_class.sections)) 1285 1286 # Section numbering 1287 if settings.sectnum_xform: # section numbering by Docutils 1288 PreambleCmds.secnumdepth = r'\setcounter{secnumdepth}{0}' 1289 else: # section numbering by LaTeX: 1290 secnumdepth = settings.sectnum_depth 1291 # Possible values of settings.sectnum_depth: 1292 # None "sectnum" directive without depth arg -> LaTeX default 1293 # 0 no "sectnum" directive -> no section numbers 1294 # >0 value of "depth" argument -> translate to LaTeX levels: 1295 # -1 part (0 with "article" document class) 1296 # 0 chapter (missing in "article" document class) 1297 # 1 section 1298 # 2 subsection 1299 # 3 subsubsection 1300 # 4 paragraph 1301 # 5 subparagraph 1302 if secnumdepth is not None: 1303 # limit to supported levels 1304 secnumdepth = min(secnumdepth, len(self.d_class.sections)) 1305 # adjust to document class and use_part_section settings 1306 if 'chapter' in self.d_class.sections: 1307 secnumdepth -= 1 1308 if self.d_class.sections[0] == 'part': 1309 secnumdepth -= 1 1310 PreambleCmds.secnumdepth = \ 1311 r'\setcounter{secnumdepth}{%d}' % secnumdepth 1312 1313 # start with specified number: 1314 if (hasattr(settings, 'sectnum_start') and 1315 settings.sectnum_start != 1): 1316 self.requirements['sectnum_start'] = ( 1317 r'\setcounter{%s}{%d}' % (self.d_class.sections[0], 1318 settings.sectnum_start-1)) 1319 # TODO: currently ignored (configure in a stylesheet): 1320 ## settings.sectnum_prefix 1321 ## settings.sectnum_suffix 1322 1323 # Auxiliary Methods 1324 # ----------------- 1325 1326 def stylesheet_call(self, path): 1327 """Return code to reference or embed stylesheet file `path`""" 1328 # is it a package (no extension or *.sty) or "normal" tex code: 1329 (base, ext) = os.path.splitext(path) 1330 is_package = ext in ['.sty', ''] 1331 # Embed content of style file: 1332 if self.settings.embed_stylesheet: 1333 if is_package: 1334 path = base + '.sty' # ensure extension 1335 try: 1336 content = docutils.io.FileInput(source_path=path, 1337 encoding='utf-8').read() 1338 self.settings.record_dependencies.add(path) 1339 except IOError as err: 1340 msg = u'Cannot embed stylesheet %r:\n %s.' % ( 1341 path, SafeString(err.strerror)) 1342 self.document.reporter.error(msg) 1343 return '% ' + msg.replace('\n', '\n% ') 1344 if is_package: 1345 content = '\n'.join([r'\makeatletter', 1346 content, 1347 r'\makeatother']) 1348 return '%% embedded stylesheet: %s\n%s' % (path, content) 1349 # Link to style file: 1350 if is_package: 1351 path = base # drop extension 1352 cmd = r'\usepackage{%s}' 1353 else: 1354 cmd = r'\input{%s}' 1355 if self.settings.stylesheet_path: 1356 # adapt path relative to output (cf. config.html#stylesheet-path) 1357 path = utils.relative_path(self.settings._destination, path) 1358 return cmd % path 1359 1360 def to_latex_encoding(self, docutils_encoding): 1361 """Translate docutils encoding name into LaTeX's. 1362 1363 Default method is remove "-" and "_" chars from docutils_encoding. 1364 """ 1365 tr = { 'iso-8859-1': 'latin1', # west european 1366 'iso-8859-2': 'latin2', # east european 1367 'iso-8859-3': 'latin3', # esperanto, maltese 1368 'iso-8859-4': 'latin4', # north european, scandinavian, baltic 1369 'iso-8859-5': 'iso88595', # cyrillic (ISO) 1370 'iso-8859-9': 'latin5', # turkish 1371 'iso-8859-15': 'latin9', # latin9, update to latin1. 1372 'mac_cyrillic': 'maccyr', # cyrillic (on Mac) 1373 'windows-1251': 'cp1251', # cyrillic (on Windows) 1374 'koi8-r': 'koi8-r', # cyrillic (Russian) 1375 'koi8-u': 'koi8-u', # cyrillic (Ukrainian) 1376 'windows-1250': 'cp1250', # 1377 'windows-1252': 'cp1252', # 1378 'us-ascii': 'ascii', # ASCII (US) 1379 # unmatched encodings 1380 #'': 'applemac', 1381 #'': 'ansinew', # windows 3.1 ansi 1382 #'': 'ascii', # ASCII encoding for the range 32--127. 1383 #'': 'cp437', # dos latin us 1384 #'': 'cp850', # dos latin 1 1385 #'': 'cp852', # dos latin 2 1386 #'': 'decmulti', 1387 #'': 'latin10', 1388 #'iso-8859-6': '' # arabic 1389 #'iso-8859-7': '' # greek 1390 #'iso-8859-8': '' # hebrew 1391 #'iso-8859-10': '' # latin6, more complete iso-8859-4 1392 } 1393 encoding = docutils_encoding.lower() 1394 if encoding in tr: 1395 return tr[encoding] 1396 # drop hyphen or low-line from "latin-1", "latin_1", "utf-8" and similar 1397 encoding = encoding.replace('_', '').replace('-', '') 1398 # strip the error handler 1399 return encoding.split(':')[0] 1400 1401 def language_label(self, docutil_label): 1402 return self.language_module.labels[docutil_label] 1403 1404 def encode(self, text): 1405 """Return text with 'problematic' characters escaped. 1406 1407 * Escape the special printing characters ``# $ % & ~ _ ^ \\ { }``, 1408 square brackets ``[ ]``, double quotes and (in OT1) ``< | >``. 1409 * Translate non-supported Unicode characters. 1410 * Separate ``-`` (and more in literal text) to prevent input ligatures. 1411 """ 1412 if self.verbatim: 1413 return text 1414 # Set up the translation table: 1415 table = CharMaps.alltt.copy() 1416 if not self.alltt: 1417 table.update(CharMaps.special) 1418 # keep the underscore in citation references 1419 if self.inside_citation_reference_label and not self.alltt: 1420 del(table[ord('_')]) 1421 # Workarounds for OT1 font-encoding 1422 if self.font_encoding in ['OT1', ''] and not self.is_xetex: 1423 # * out-of-order characters in cmtt 1424 if self.literal: 1425 # replace underscore by underlined blank, 1426 # because this has correct width. 1427 table[ord('_')] = u'\\underline{~}' 1428 # the backslash doesn't work, so we use a mirrored slash. 1429 # \reflectbox is provided by graphicx: 1430 self.requirements['graphicx'] = self.graphicx_package 1431 table[ord('\\')] = u'\\reflectbox{/}' 1432 # * ``< | >`` come out as different chars (except for cmtt): 1433 else: 1434 table[ord('|')] = u'\\textbar{}' 1435 table[ord('<')] = u'\\textless{}' 1436 table[ord('>')] = u'\\textgreater{}' 1437 if self.insert_non_breaking_blanks: 1438 table[ord(' ')] = u'~' 1439 # tab chars may occur in included files (literal or code) 1440 # quick-and-dirty replacement with spaces 1441 # (for better results use `--literal-block-env=lstlisting`) 1442 table[ord('\t')] = u'~' * self.settings.tab_width 1443 # Unicode replacements for 8-bit tex engines (not required with XeTeX/LuaTeX): 1444 if not self.is_xetex: 1445 if not self.latex_encoding.startswith('utf8'): 1446 table.update(CharMaps.unsupported_unicode) 1447 table.update(CharMaps.utf8_supported_unicode) 1448 table.update(CharMaps.textcomp) 1449 table.update(CharMaps.pifont) 1450 # Characters that require a feature/package to render 1451 for ch in text: 1452 cp = ord(ch) 1453 if cp in CharMaps.textcomp and not self.fallback_stylesheet: 1454 self.requirements['textcomp'] = PreambleCmds.textcomp 1455 elif cp in CharMaps.pifont: 1456 self.requirements['pifont'] = '\\usepackage{pifont}' 1457 # preamble-definitions for unsupported Unicode characters 1458 elif (self.latex_encoding == 'utf8' 1459 and cp in CharMaps.unsupported_unicode): 1460 self.requirements['_inputenc'+str(cp)] = ( 1461 '\\DeclareUnicodeCharacter{%04X}{%s}' 1462 % (cp, CharMaps.unsupported_unicode[cp])) 1463 text = text.translate(table) 1464 1465 # Break up input ligatures e.g. '--' to '-{}-'. 1466 if not self.is_xetex: # Not required with xetex/luatex 1467 separate_chars = '-' 1468 # In monospace-font, we also separate ',,', '``' and "''" and some 1469 # other characters which can't occur in non-literal text. 1470 if self.literal: 1471 separate_chars += ',`\'"<>' 1472 for char in separate_chars * 2: 1473 # Do it twice ("* 2") because otherwise we would replace 1474 # '---' by '-{}--'. 1475 text = text.replace(char + char, char + '{}' + char) 1476 1477 # Literal line breaks (in address or literal blocks): 1478 if self.insert_newline: 1479 lines = text.split('\n') 1480 # Add a protected space to blank lines (except the last) 1481 # to avoid ``! LaTeX Error: There's no line here to end.`` 1482 for i, line in enumerate(lines[:-1]): 1483 if not line.lstrip(): 1484 lines[i] += '~' 1485 text = (r'\\' + '\n').join(lines) 1486 if self.literal and not self.insert_non_breaking_blanks: 1487 # preserve runs of spaces but allow wrapping 1488 text = text.replace(' ', ' ~') 1489 return text 1490 1491 def attval(self, text, 1492 whitespace=re.compile('[\n\r\t\v\f]')): 1493 """Cleanse, encode, and return attribute value text.""" 1494 return self.encode(whitespace.sub(' ', text)) 1495 1496 # TODO: is this used anywhere? -> update (use template) or delete 1497 ## def astext(self): 1498 ## """Assemble document parts and return as string.""" 1499 ## head = '\n'.join(self.head_prefix + self.stylesheet + self.head) 1500 ## body = ''.join(self.body_prefix + self.body + self.body_suffix) 1501 ## return head + '\n' + body 1502 1503 def is_inline(self, node): 1504 """Check whether a node represents an inline or block-level element""" 1505 return isinstance(node.parent, nodes.TextElement) 1506 1507 def append_hypertargets(self, node): 1508 """Append hypertargets for all ids of `node`""" 1509 # hypertarget places the anchor at the target's baseline, 1510 # so we raise it explicitely 1511 self.out.append('%\n'.join(['\\raisebox{1em}{\\hypertarget{%s}{}}' % 1512 id for id in node['ids']])) 1513 1514 def ids_to_labels(self, node, set_anchor=True, protect=False): 1515 """Return list of label definitions for all ids of `node` 1516 1517 If `set_anchor` is True, an anchor is set with \\phantomsection. 1518 If `protect` is True, the \\label cmd is made robust. 1519 """ 1520 labels = ['\\label{%s}' % id for id in node.get('ids', [])] 1521 if protect: 1522 labels = ['\\protect'+label for label in labels] 1523 if set_anchor and labels: 1524 labels.insert(0, '\\phantomsection') 1525 return labels 1526 1527 def set_align_from_classes(self, node): 1528 """Convert ``align-*`` class arguments into alignment args.""" 1529 # separate: 1530 align = [cls for cls in node['classes'] if cls.startswith('align-')] 1531 if align: 1532 node['align'] = align[-1].replace('align-', '') 1533 node['classes'] = [cls for cls in node['classes'] 1534 if not cls.startswith('align-')] 1535 1536 def insert_align_declaration(self, node, default=None): 1537 align = node.get('align', default) 1538 if align == 'left': 1539 self.out.append('\\raggedright\n') 1540 elif align == 'center': 1541 self.out.append('\\centering\n') 1542 elif align == 'right': 1543 self.out.append('\\raggedleft\n') 1544 1545 def duclass_open(self, node): 1546 """Open a group and insert declarations for class values.""" 1547 if not isinstance(node.parent, nodes.compound): 1548 self.out.append('\n') 1549 for cls in node['classes']: 1550 if cls.startswith('language-'): 1551 language = self.babel.language_name(cls[9:]) 1552 if language: 1553 self.babel.otherlanguages[language] = True 1554 self.out.append('\\begin{selectlanguage}{%s}\n' % language) 1555 elif isinstance(node, nodes.table) and cls in Writer.table_style_values: 1556 pass 1557 else: 1558 if not self.fallback_stylesheet: 1559 self.fallbacks['DUclass'] = PreambleCmds.duclass 1560 self.out.append('\\begin{DUclass}{%s}\n' % cls) 1561 1562 def duclass_close(self, node): 1563 """Close a group of class declarations.""" 1564 for cls in reversed(node['classes']): 1565 if cls.startswith('language-'): 1566 language = self.babel.language_name(cls[9:]) 1567 if language: 1568 self.out.append('\\end{selectlanguage}\n') 1569 elif isinstance(node, nodes.table) and cls in Writer.table_style_values: 1570 pass 1571 else: 1572 if not self.fallback_stylesheet: 1573 self.fallbacks['DUclass'] = PreambleCmds.duclass 1574 self.out.append('\\end{DUclass}\n') 1575 1576 def push_output_collector(self, new_out): 1577 self.out_stack.append(self.out) 1578 self.out = new_out 1579 1580 def pop_output_collector(self): 1581 self.out = self.out_stack.pop() 1582 1583 def term_postfix(self, node): 1584 """ 1585 Return LaTeX code required between term or field name and content. 1586 1587 In a LaTeX "description" environment (used for definition 1588 lists and non-docinfo field lists), a ``\\leavevmode`` 1589 between an item's label and content ensures the correct 1590 placement of certain block constructs. 1591 """ 1592 for child in node: 1593 if not isinstance(child, (nodes.Invisible, nodes.footnote, 1594 nodes.citation)): 1595 break 1596 else: 1597 return '' 1598 if isinstance(child, (nodes.image)): 1599 return '\\leavevmode\n' # Images get an additional newline. 1600 if isinstance(child, (nodes.container, nodes.compound)): 1601 return self.term_postfix(child) 1602 if not isinstance(child, 1603 (nodes.paragraph, nodes.math_block)): 1604 return r'\leavevmode' 1605 return '' 1606 1607 # Visitor methods 1608 # --------------- 1609 1610 def visit_Text(self, node): 1611 self.out.append(self.encode(node.astext())) 1612 1613 def depart_Text(self, node): 1614 pass 1615 1616 def visit_abbreviation(self, node): 1617 node['classes'].insert(0, 'abbreviation') 1618 self.visit_inline(node) 1619 1620 def depart_abbreviation(self, node): 1621 self.depart_inline(node) 1622 1623 def visit_acronym(self, node): 1624 node['classes'].insert(0, 'acronym') 1625 self.visit_inline(node) 1626 1627 def depart_acronym(self, node): 1628 self.depart_inline(node) 1629 1630 def visit_address(self, node): 1631 self.visit_docinfo_item(node, 'address') 1632 1633 def depart_address(self, node): 1634 self.depart_docinfo_item(node) 1635 1636 def visit_admonition(self, node): 1637 # strip the generic 'admonition' from the list of classes 1638 node['classes'] = [cls for cls in node['classes'] 1639 if cls != 'admonition'] 1640 if self.settings.legacy_class_functions: 1641 self.fallbacks['admonition'] = PreambleCmds.admonition_legacy 1642 if 'error' in node['classes']: 1643 self.fallbacks['error'] = PreambleCmds.error_legacy 1644 self.out.append('\n\\DUadmonition[%s]{' % ','.join(node['classes'])) 1645 return 1646 if not self.fallback_stylesheet: 1647 self.fallbacks['admonition'] = PreambleCmds.admonition 1648 if 'error' in node['classes'] and not self.fallback_stylesheet: 1649 self.fallbacks['error'] = PreambleCmds.error 1650 self.duclass_open(node) 1651 self.out.append('\\begin{DUadmonition}') 1652 1653 def depart_admonition(self, node): 1654 if self.settings.legacy_class_functions: 1655 self.out.append('}\n') 1656 return 1657 self.out.append('\\end{DUadmonition}\n') 1658 self.duclass_close(node) 1659 1660 def visit_author(self, node): 1661 self.pdfauthor.append(self.attval(node.astext())) 1662 self.visit_docinfo_item(node, 'author') 1663 1664 def depart_author(self, node): 1665 self.depart_docinfo_item(node) 1666 1667 def visit_authors(self, node): 1668 # not used: visit_author is called anyway for each author. 1669 pass 1670 1671 def depart_authors(self, node): 1672 pass 1673 1674 def visit_block_quote(self, node): 1675 self.duclass_open(node) 1676 self.out.append('\\begin{quote}') 1677 1678 def depart_block_quote(self, node): 1679 self.out.append('\\end{quote}\n') 1680 self.duclass_close(node) 1681 1682 def visit_bullet_list(self, node): 1683 self.duclass_open(node) 1684 if self.is_toc_list: 1685 self.out.append('\\begin{list}{}{}') 1686 else: 1687 self.out.append('\\begin{itemize}') 1688 1689 def depart_bullet_list(self, node): 1690 if self.is_toc_list: 1691 self.out.append('\\end{list}\n') 1692 else: 1693 self.out.append('\\end{itemize}\n') 1694 self.duclass_close(node) 1695 1696 def visit_superscript(self, node): 1697 self.out.append(r'\textsuperscript{') 1698 if node['classes']: 1699 self.visit_inline(node) 1700 1701 def depart_superscript(self, node): 1702 if node['classes']: 1703 self.depart_inline(node) 1704 self.out.append('}') 1705 1706 def visit_subscript(self, node): 1707 self.out.append(r'\textsubscript{') 1708 if node['classes']: 1709 self.visit_inline(node) 1710 1711 def depart_subscript(self, node): 1712 if node['classes']: 1713 self.depart_inline(node) 1714 self.out.append('}') 1715 1716 def visit_caption(self, node): 1717 self.out.append('\n\\caption{') 1718 1719 def depart_caption(self, node): 1720 self.out.append('}\n') 1721 1722 def visit_title_reference(self, node): 1723 if not self.fallback_stylesheet: 1724 self.fallbacks['titlereference'] = PreambleCmds.titlereference 1725 self.out.append(r'\DUroletitlereference{') 1726 if node['classes']: 1727 self.visit_inline(node) 1728 1729 def depart_title_reference(self, node): 1730 if node['classes']: 1731 self.depart_inline(node) 1732 self.out.append('}') 1733 1734 def visit_citation(self, node): 1735 if self._use_latex_citations: 1736 self.push_output_collector([]) 1737 else: 1738 ## self.requirements['~fnt_floats'] = PreambleCmds.footnote_floats 1739 self.out.append(r'\begin{figure}[b]') 1740 self.append_hypertargets(node) 1741 1742 def depart_citation(self, node): 1743 if self._use_latex_citations: 1744 # TODO: normalize label 1745 label = self.out[0] 1746 text = ''.join(self.out[1:]) 1747 self._bibitems.append([label, text]) 1748 self.pop_output_collector() 1749 else: 1750 self.out.append('\\end{figure}\n') 1751 1752 def visit_citation_reference(self, node): 1753 if self._use_latex_citations: 1754 if not self.inside_citation_reference_label: 1755 self.out.append(r'\cite{') 1756 self.inside_citation_reference_label = 1 1757 else: 1758 assert self.out[-1] in (' ', '\n'),\ 1759 'unexpected non-whitespace while in reference label' 1760 del self.out[-1] 1761 else: 1762 href = '' 1763 if 'refid' in node: 1764 href = node['refid'] 1765 elif 'refname' in node: 1766 href = self.document.nameids[node['refname']] 1767 self.out.append('\\hyperlink{%s}{[' % href) 1768 1769 def depart_citation_reference(self, node): 1770 # TODO: normalize labels 1771 if self._use_latex_citations: 1772 followup_citation = False 1773 # check for a following citation separated by a space or newline 1774 sibling = node.next_node(descend=False, siblings=True) 1775 if (isinstance(sibling, nodes.Text) 1776 and sibling.astext() in (' ', '\n')): 1777 sibling2 = sibling.next_node(descend=False, siblings=True) 1778 if isinstance(sibling2, nodes.citation_reference): 1779 followup_citation = True 1780 if followup_citation: 1781 self.out.append(',') 1782 else: 1783 self.out.append('}') 1784 self.inside_citation_reference_label = False 1785 else: 1786 self.out.append(']}') 1787 1788 def visit_classifier(self, node): 1789 self.out.append('(\\textbf{') 1790 1791 def depart_classifier(self, node): 1792 self.out.append('})') 1793 1794 def visit_colspec(self, node): 1795 self.active_table.visit_colspec(node) 1796 1797 def depart_colspec(self, node): 1798 pass 1799 1800 def visit_comment(self, node): 1801 if not isinstance(node.parent, nodes.compound): 1802 self.out.append('\n') 1803 # Precede every line with a comment sign, wrap in newlines 1804 self.out.append('%% %s\n' % node.astext().replace('\n', '\n% ')) 1805 raise nodes.SkipNode 1806 1807 def depart_comment(self, node): 1808 pass 1809 1810 def visit_compound(self, node): 1811 if isinstance(node.parent, nodes.compound): 1812 self.out.append('\n') 1813 node['classes'].insert(0, 'compound') 1814 self.duclass_open(node) 1815 1816 def depart_compound(self, node): 1817 self.duclass_close(node) 1818 1819 def visit_contact(self, node): 1820 self.visit_docinfo_item(node, 'contact') 1821 1822 def depart_contact(self, node): 1823 self.depart_docinfo_item(node) 1824 1825 def visit_container(self, node): 1826 self.duclass_open(node) 1827 1828 def depart_container(self, node): 1829 self.duclass_close(node) 1830 1831 def visit_copyright(self, node): 1832 self.visit_docinfo_item(node, 'copyright') 1833 1834 def depart_copyright(self, node): 1835 self.depart_docinfo_item(node) 1836 1837 def visit_date(self, node): 1838 self.visit_docinfo_item(node, 'date') 1839 1840 def depart_date(self, node): 1841 self.depart_docinfo_item(node) 1842 1843 def visit_decoration(self, node): 1844 # header and footer 1845 pass 1846 1847 def depart_decoration(self, node): 1848 pass 1849 1850 def visit_definition(self, node): 1851 pass 1852 1853 def depart_definition(self, node): 1854 self.out.append('\n') # TODO: just pass? 1855 1856 def visit_definition_list(self, node): 1857 self.duclass_open(node) 1858 self.out.append('\\begin{description}\n') 1859 1860 def depart_definition_list(self, node): 1861 self.out.append('\\end{description}\n') 1862 self.duclass_close(node) 1863 1864 def visit_definition_list_item(self, node): 1865 pass 1866 1867 def depart_definition_list_item(self, node): 1868 pass 1869 1870 def visit_description(self, node): 1871 self.out.append(' ') 1872 1873 def depart_description(self, node): 1874 pass 1875 1876 def visit_docinfo(self, node): 1877 self.push_output_collector(self.docinfo) 1878 1879 def depart_docinfo(self, node): 1880 self.pop_output_collector() 1881 # Some itmes (e.g. author) end up at other places 1882 if self.docinfo: 1883 # tabularx: automatic width of columns, no page breaks allowed. 1884 self.requirements['tabularx'] = r'\usepackage{tabularx}' 1885 if not self.fallback_stylesheet: 1886 self.fallbacks['_providelength'] = PreambleCmds.providelength 1887 self.fallbacks['docinfo'] = PreambleCmds.docinfo 1888 # 1889 self.docinfo.insert(0, '\n% Docinfo\n' 1890 '\\begin{center}\n' 1891 '\\begin{tabularx}{\\DUdocinfowidth}{lX}\n') 1892 self.docinfo.append('\\end{tabularx}\n' 1893 '\\end{center}\n') 1894 1895 def visit_docinfo_item(self, node, name): 1896 if self.use_latex_docinfo: 1897 if name in ('author', 'organization', 'contact', 'address'): 1898 # We attach these to the last author. If any of them precedes 1899 # the first author, put them in a separate "author" group 1900 # (in lack of better semantics). 1901 if name == 'author' or not self.author_stack: 1902 self.author_stack.append([]) 1903 if name == 'address': # newlines are meaningful 1904 self.insert_newline = True 1905 text = self.encode(node.astext()) 1906 self.insert_newline = False 1907 else: 1908 text = self.attval(node.astext()) 1909 self.author_stack[-1].append(text) 1910 raise nodes.SkipNode 1911 elif name == 'date': 1912 self.date.append(self.attval(node.astext())) 1913 raise nodes.SkipNode 1914 self.out.append('\\textbf{%s}: &\n\t' % self.language_label(name)) 1915 if name == 'address': 1916 self.insert_newline = True 1917 self.out.append('{\\raggedright\n') 1918 self.context.append(' } \\\\\n') 1919 else: 1920 self.context.append(' \\\\\n') 1921 1922 def depart_docinfo_item(self, node): 1923 self.out.append(self.context.pop()) 1924 # for address we did set insert_newline 1925 self.insert_newline = False 1926 1927 def visit_doctest_block(self, node): 1928 self.visit_literal_block(node) 1929 1930 def depart_doctest_block(self, node): 1931 self.depart_literal_block(node) 1932 1933 def visit_document(self, node): 1934 # titled document? 1935 if (self.use_latex_docinfo or len(node) and 1936 isinstance(node[0], nodes.title)): 1937 protect = (self.settings.documentclass == 'memoir') 1938 self.title_labels += self.ids_to_labels(node, set_anchor=False, 1939 protect=protect) 1940 1941 def depart_document(self, node): 1942 # Complete header with information gained from walkabout 1943 # * language setup 1944 if (self.babel.otherlanguages or 1945 self.babel.language not in ('', 'english')): 1946 self.requirements['babel'] = self.babel() 1947 # * conditional requirements (before style sheet) 1948 self.requirements = self.requirements.sortedvalues() 1949 # * coditional fallback definitions (after style sheet) 1950 self.fallbacks = self.fallbacks.sortedvalues() 1951 # * PDF properties 1952 self.pdfsetup.append(PreambleCmds.linking % self.hyperref_options) 1953 if self.pdfauthor: 1954 authors = self.author_separator.join(self.pdfauthor) 1955 self.pdfinfo.append(' pdfauthor={%s}' % authors) 1956 if self.pdfinfo: 1957 self.pdfsetup += [r'\hypersetup{'] + self.pdfinfo + ['}'] 1958 # Complete body 1959 # * document title (with "use_latex_docinfo" also 1960 # 'author', 'organization', 'contact', 'address' and 'date') 1961 if self.title or ( 1962 self.use_latex_docinfo and (self.author_stack or self.date)): 1963 # \title (empty \title prevents error with \maketitle) 1964 title = [''.join(self.title)] 1965 if self.title: 1966 title += self.title_labels 1967 if self.subtitle: 1968 title += [r'\\', 1969 r'\DUdocumentsubtitle{%s}' % ''.join(self.subtitle) 1970 ] + self.subtitle_labels 1971 self.titledata.append(r'\title{%s}' % '%\n '.join(title)) 1972 # \author (empty \author prevents warning with \maketitle) 1973 authors = ['\\\\\n'.join(author_entry) 1974 for author_entry in self.author_stack] 1975 self.titledata.append(r'\author{%s}' % 1976 ' \\and\n'.join(authors)) 1977 # \date (empty \date prevents defaulting to \today) 1978 self.titledata.append(r'\date{%s}' % ', '.join(self.date)) 1979 # \maketitle in the body formats title with LaTeX 1980 self.body_pre_docinfo.append('\\maketitle\n') 1981 1982 # * bibliography 1983 # TODO insertion point of bibliography should be configurable. 1984 if self._use_latex_citations and len(self._bibitems)>0: 1985 if not self.bibtex: 1986 widest_label = '' 1987 for bi in self._bibitems: 1988 if len(widest_label)<len(bi[0]): 1989 widest_label = bi[0] 1990 self.out.append('\n\\begin{thebibliography}{%s}\n' % 1991 widest_label) 1992 for bi in self._bibitems: 1993 # cite_key: underscores must not be escaped 1994 cite_key = bi[0].replace(r'\_', '_') 1995 self.out.append('\\bibitem[%s]{%s}{%s}\n' % 1996 (bi[0], cite_key, bi[1])) 1997 self.out.append('\\end{thebibliography}\n') 1998 else: 1999 self.out.append('\n\\bibliographystyle{%s}\n' % 2000 self.bibtex[0]) 2001 self.out.append('\\bibliography{%s}\n' % self.bibtex[1]) 2002 # * make sure to generate a toc file if needed for local contents: 2003 if 'minitoc' in self.requirements and not self.has_latex_toc: 2004 self.out.append('\n\\faketableofcontents % for local ToCs\n') 2005 2006 def visit_emphasis(self, node): 2007 self.out.append('\\emph{') 2008 if node['classes']: 2009 self.visit_inline(node) 2010 2011 def depart_emphasis(self, node): 2012 if node['classes']: 2013 self.depart_inline(node) 2014 self.out.append('}') 2015 2016 # Append column delimiters and advance column counter, 2017 # if the current cell is a multi-row continuation.""" 2018 def insert_additional_table_colum_delimiters(self): 2019 while self.active_table.get_rowspan( 2020 self.active_table.get_entry_number()): 2021 self.out.append(' & ') 2022 self.active_table.visit_entry() # increment cell count 2023 2024 def visit_entry(self, node): 2025 # cell separation 2026 if self.active_table.get_entry_number() == 0: 2027 self.insert_additional_table_colum_delimiters() 2028 else: 2029 self.out.append(' & ') 2030 2031 # multirow, multicolumn 2032 if 'morerows' in node and 'morecols' in node: 2033 raise NotImplementedError('Cells that ' 2034 'span multiple rows *and* columns currently not supported, sorry.') 2035 # TODO: should be possible with LaTeX, see e.g. 2036 # http://texblog.org/2012/12/21/multi-column-and-multi-row-cells-in-latex-tables/ 2037 # multirow in LaTeX simply will enlarge the cell over several rows 2038 # (the following n if n is positive, the former if negative). 2039 if 'morerows' in node: 2040 self.requirements['multirow'] = r'\usepackage{multirow}' 2041 mrows = node['morerows'] + 1 2042 self.active_table.set_rowspan( 2043 self.active_table.get_entry_number(), mrows) 2044 self.out.append('\\multirow{%d}{%s}{' % 2045 (mrows, self.active_table.get_column_width())) 2046 self.context.append('}') 2047 elif 'morecols' in node: 2048 # the vertical bar before column is missing if it is the first 2049 # column. the one after always. 2050 if self.active_table.get_entry_number() == 0: 2051 bar1 = self.active_table.get_vertical_bar() 2052 else: 2053 bar1 = '' 2054 mcols = node['morecols'] + 1 2055 self.out.append('\\multicolumn{%d}{%s%s%s}{' % 2056 (mcols, bar1, 2057 self.active_table.get_multicolumn_width( 2058 self.active_table.get_entry_number(), 2059 mcols), 2060 self.active_table.get_vertical_bar())) 2061 self.context.append('}') 2062 else: 2063 self.context.append('') 2064 2065 # bold header/stub-column 2066 if len(node) and (isinstance(node.parent.parent, nodes.thead) 2067 or self.active_table.is_stub_column()): 2068 self.out.append('\\textbf{') 2069 self.context.append('}') 2070 else: 2071 self.context.append('') 2072 2073 # if line ends with '{', mask line break to prevent spurious whitespace 2074 if (not self.active_table.colwidths_auto 2075 and self.out[-1].endswith("{") 2076 and node.astext()): 2077 self.out.append("%") 2078 2079 self.active_table.visit_entry() # increment cell count 2080 2081 def depart_entry(self, node): 2082 self.out.append(self.context.pop()) # header / not header 2083 self.out.append(self.context.pop()) # multirow/column 2084 # insert extra "&"s, if following rows are spanned from above: 2085 self.insert_additional_table_colum_delimiters() 2086 2087 def visit_row(self, node): 2088 self.active_table.visit_row() 2089 2090 def depart_row(self, node): 2091 self.out.extend(self.active_table.depart_row()) 2092 2093 def visit_enumerated_list(self, node): 2094 # enumeration styles: 2095 types = {'': '', 2096 'arabic':'arabic', 2097 'loweralpha':'alph', 2098 'upperalpha':'Alph', 2099 'lowerroman':'roman', 2100 'upperroman':'Roman'} 2101 # the 4 default LaTeX enumeration labels: präfix, enumtype, suffix, 2102 labels = [('', 'arabic', '.'), # 1. 2103 ('(', 'alph', ')'), # (a) 2104 ('', 'roman', '.'), # i. 2105 ('', 'Alph', '.')] # A. 2106 2107 prefix = '' 2108 if self.compound_enumerators: 2109 if (self.section_prefix_for_enumerators and self.section_level 2110 and not self._enumeration_counters): 2111 prefix = '.'.join([str(n) for n in 2112 self._section_number[:self.section_level]] 2113 ) + self.section_enumerator_separator 2114 if self._enumeration_counters: 2115 prefix += self._enumeration_counters[-1] 2116 # TODO: use LaTeX default for unspecified label-type? 2117 # (needs change of parser) 2118 prefix += node.get('prefix', '') 2119 enumtype = types[node.get('enumtype' '')] 2120 suffix = node.get('suffix', '') 2121 2122 enumeration_level = len(self._enumeration_counters)+1 2123 counter_name = 'enum' + roman.toRoman(enumeration_level).lower() 2124 label = r'%s\%s{%s}%s' % (prefix, enumtype, counter_name, suffix) 2125 self._enumeration_counters.append(label) 2126 2127 self.duclass_open(node) 2128 if enumeration_level <= 4: 2129 self.out.append('\\begin{enumerate}') 2130 if (prefix, enumtype, suffix 2131 ) != labels[enumeration_level-1]: 2132 self.out.append('\n\\renewcommand{\\label%s}{%s}' % 2133 (counter_name, label)) 2134 else: 2135 self.fallbacks[counter_name] = '\\newcounter{%s}' % counter_name 2136 self.out.append('\\begin{list}') 2137 self.out.append('{%s}' % label) 2138 self.out.append('{\\usecounter{%s}}' % counter_name) 2139 if 'start' in node: 2140 self.out.append('\n\\setcounter{%s}{%d}' % 2141 (counter_name, node['start']-1)) 2142 2143 2144 def depart_enumerated_list(self, node): 2145 if len(self._enumeration_counters) <= 4: 2146 self.out.append('\\end{enumerate}\n') 2147 else: 2148 self.out.append('\\end{list}\n') 2149 self.duclass_close(node) 2150 self._enumeration_counters.pop() 2151 2152 def visit_field(self, node): 2153 # output is done in field_body, field_name 2154 pass 2155 2156 def depart_field(self, node): 2157 pass 2158 2159 def visit_field_body(self, node): 2160 if not isinstance(node.parent.parent, nodes.docinfo): 2161 self.out.append(self.term_postfix(node)) 2162 2163 def depart_field_body(self, node): 2164 if self.out is self.docinfo: 2165 self.out.append(r'\\'+'\n') 2166 2167 def visit_field_list(self, node): 2168 self.duclass_open(node) 2169 if self.out is not self.docinfo: 2170 if not self.fallback_stylesheet: 2171 self.fallbacks['fieldlist'] = PreambleCmds.fieldlist 2172 self.out.append('\\begin{DUfieldlist}') 2173 2174 def depart_field_list(self, node): 2175 if self.out is not self.docinfo: 2176 self.out.append('\\end{DUfieldlist}\n') 2177 self.duclass_close(node) 2178 2179 def visit_field_name(self, node): 2180 if self.out is self.docinfo: 2181 self.out.append('\\textbf{') 2182 else: 2183 # Commands with optional args inside an optional arg must be put 2184 # in a group, e.g. ``\item[{\hyperref[label]{text}}]``. 2185 self.out.append('\n\\item[{') 2186 2187 def depart_field_name(self, node): 2188 if self.out is self.docinfo: 2189 self.out.append('}: &') 2190 else: 2191 self.out.append(':}]') 2192 2193 def visit_figure(self, node): 2194 self.requirements['float'] = PreambleCmds.float 2195 self.duclass_open(node) 2196 # The 'align' attribute sets the "outer alignment", 2197 # for "inner alignment" use LaTeX default alignment (similar to HTML) 2198 alignment = node.attributes.get('align', 'center') 2199 if alignment != 'center': 2200 # The LaTeX "figure" environment always uses the full linewidth, 2201 # so "outer alignment" is ignored. Just write a comment. 2202 # TODO: use the wrapfigure environment? 2203 self.out.append('\\begin{figure} %% align = "%s"\n' % alignment) 2204 else: 2205 self.out.append('\\begin{figure}\n') 2206 if node.get('ids'): 2207 self.out += self.ids_to_labels(node) + ['\n'] 2208 2209 def depart_figure(self, node): 2210 self.out.append('\\end{figure}\n') 2211 self.duclass_close(node) 2212 2213 def visit_footer(self, node): 2214 self.push_output_collector([]) 2215 self.out.append(r'\newcommand{\DUfooter}{') 2216 2217 def depart_footer(self, node): 2218 self.out.append('}') 2219 self.requirements['~footer'] = ''.join(self.out) 2220 self.pop_output_collector() 2221 2222 def visit_footnote(self, node): 2223 try: 2224 backref = node['backrefs'][0] 2225 except IndexError: 2226 backref = node['ids'][0] # no backref, use self-ref instead 2227 if self.docutils_footnotes: 2228 if not self.fallback_stylesheet: 2229 self.fallbacks['footnotes'] = PreambleCmds.footnotes 2230 num = node[0].astext() 2231 if self.settings.footnote_references == 'brackets': 2232 num = '[%s]' % num 2233 self.out.append('%%\n\\DUfootnotetext{%s}{%s}{%s}{' % 2234 (node['ids'][0], backref, self.encode(num))) 2235 if node['ids'] == node['names']: 2236 self.out += self.ids_to_labels(node) 2237 # mask newline to prevent spurious whitespace if paragraph follows: 2238 if node[1:] and isinstance(node[1], nodes.paragraph): 2239 self.out.append('%') 2240 # TODO: "real" LaTeX \footnote{}s (see visit_footnotes_reference()) 2241 2242 def depart_footnote(self, node): 2243 self.out.append('}\n') 2244 2245 def visit_footnote_reference(self, node): 2246 href = '' 2247 if 'refid' in node: 2248 href = node['refid'] 2249 elif 'refname' in node: 2250 href = self.document.nameids[node['refname']] 2251 # if not self.docutils_footnotes: 2252 # TODO: insert footnote content at (or near) this place 2253 # print("footnote-ref to", node['refid']) 2254 # footnotes = (self.document.footnotes + 2255 # self.document.autofootnotes + 2256 # self.document.symbol_footnotes) 2257 # for footnote in footnotes: 2258 # # print(footnote['ids']) 2259 # if node.get('refid', '') in footnote['ids']: 2260 # print('matches', footnote['ids']) 2261 format = self.settings.footnote_references 2262 if format == 'brackets': 2263 self.append_hypertargets(node) 2264 self.out.append('\\hyperlink{%s}{[' % href) 2265 self.context.append(']}') 2266 else: 2267 if not self.fallback_stylesheet: 2268 self.fallbacks['footnotes'] = PreambleCmds.footnotes 2269 self.out.append(r'\DUfootnotemark{%s}{%s}{' % 2270 (node['ids'][0], href)) 2271 self.context.append('}') 2272 2273 def depart_footnote_reference(self, node): 2274 self.out.append(self.context.pop()) 2275 2276 # footnote/citation label 2277 def label_delim(self, node, bracket, superscript): 2278 if isinstance(node.parent, nodes.footnote): 2279 raise nodes.SkipNode 2280 else: 2281 assert isinstance(node.parent, nodes.citation) 2282 if not self._use_latex_citations: 2283 self.out.append(bracket) 2284 2285 def visit_label(self, node): 2286 """footnote or citation label: in brackets or as superscript""" 2287 self.label_delim(node, '[', '\\textsuperscript{') 2288 2289 def depart_label(self, node): 2290 self.label_delim(node, ']', '}') 2291 2292 # elements generated by the framework e.g. section numbers. 2293 def visit_generated(self, node): 2294 pass 2295 2296 def depart_generated(self, node): 2297 pass 2298 2299 def visit_header(self, node): 2300 self.push_output_collector([]) 2301 self.out.append(r'\newcommand{\DUheader}{') 2302 2303 def depart_header(self, node): 2304 self.out.append('}') 2305 self.requirements['~header'] = ''.join(self.out) 2306 self.pop_output_collector() 2307 2308 def to_latex_length(self, length_str, pxunit=None): 2309 """Convert `length_str` with rst lenght to LaTeX length 2310 """ 2311 if pxunit is not None: 2312 sys.stderr.write('deprecation warning: LaTeXTranslator.to_latex_length()' 2313 ' option `pxunit` will be removed.') 2314 match = re.match(r'(\d*\.?\d*)\s*(\S*)', length_str) 2315 if not match: 2316 return length_str 2317 value, unit = match.groups()[:2] 2318 # no unit or "DTP" points (called 'bp' in TeX): 2319 if unit in ('', 'pt'): 2320 length_str = '%sbp' % value 2321 # percentage: relate to current line width 2322 elif unit == '%': 2323 length_str = '%.3f\\linewidth' % (float(value)/100.0) 2324 elif self.is_xetex and unit == 'px': 2325 # XeTeX does not know the length unit px. 2326 # Use \pdfpxdimen, the macro to set the value of 1 px in pdftex. 2327 # This way, configuring works the same for pdftex and xetex. 2328 if not self.fallback_stylesheet: 2329 self.fallbacks['_providelength'] = PreambleCmds.providelength 2330 self.fallbacks['px'] = '\n\\DUprovidelength{\\pdfpxdimen}{1bp}\n' 2331 length_str = r'%s\pdfpxdimen' % value 2332 return length_str 2333 2334 def visit_image(self, node): 2335 self.requirements['graphicx'] = self.graphicx_package 2336 attrs = node.attributes 2337 # Convert image URI to a local file path 2338 imagepath = url2pathname(attrs['uri']).replace('\\', '/') 2339 # alignment defaults: 2340 if not 'align' in attrs: 2341 # Set default align of image in a figure to 'center' 2342 if isinstance(node.parent, nodes.figure): 2343 attrs['align'] = 'center' 2344 self.set_align_from_classes(node) 2345 # pre- and postfix (prefix inserted in reverse order) 2346 pre = [] 2347 post = [] 2348 include_graphics_options = [] 2349 align_codes = { 2350 # inline images: by default latex aligns the bottom. 2351 'bottom': ('', ''), 2352 'middle': (r'\raisebox{-0.5\height}{', '}'), 2353 'top': (r'\raisebox{-\height}{', '}'), 2354 # block level images: 2355 'center': (r'\noindent\makebox[\linewidth][c]{', '}'), 2356 'left': (r'\noindent{', r'\hfill}'), 2357 'right': (r'\noindent{\hfill', '}'),} 2358 if 'align' in attrs: 2359 # TODO: warn or ignore non-applicable alignment settings? 2360 try: 2361 align_code = align_codes[attrs['align']] 2362 pre.append(align_code[0]) 2363 post.append(align_code[1]) 2364 except KeyError: 2365 pass # TODO: warn? 2366 if 'height' in attrs: 2367 include_graphics_options.append('height=%s' % 2368 self.to_latex_length(attrs['height'])) 2369 if 'scale' in attrs: 2370 include_graphics_options.append('scale=%f' % 2371 (attrs['scale'] / 100.0)) 2372 if 'width' in attrs: 2373 include_graphics_options.append('width=%s' % 2374 self.to_latex_length(attrs['width'])) 2375 if not (self.is_inline(node) or 2376 isinstance(node.parent, (nodes.figure, nodes.compound))): 2377 pre.append('\n') 2378 if not (self.is_inline(node) or 2379 isinstance(node.parent, nodes.figure)): 2380 post.append('\n') 2381 pre.reverse() 2382 self.out.extend(pre) 2383 options = '' 2384 if include_graphics_options: 2385 options = '[%s]' % (','.join(include_graphics_options)) 2386 self.out.append('\\includegraphics%s{%s}' % (options, imagepath)) 2387 self.out.extend(post) 2388 2389 def depart_image(self, node): 2390 if node.get('ids'): 2391 self.out += self.ids_to_labels(node) + ['\n'] 2392 2393 def visit_inline(self, node): # <span>, i.e. custom roles 2394 for cls in node['classes']: 2395 if cls.startswith('language-'): 2396 language = self.babel.language_name(cls[9:]) 2397 if language: 2398 self.babel.otherlanguages[language] = True 2399 self.out.append(r'\foreignlanguage{%s}{' % language) 2400 else: 2401 if not self.fallback_stylesheet: 2402 self.fallbacks['inline'] = PreambleCmds.inline 2403 self.out.append(r'\DUrole{%s}{' % cls) 2404 2405 def depart_inline(self, node): 2406 self.out.append('}' * len(node['classes'])) 2407 2408 def visit_legend(self, node): 2409 if not self.fallback_stylesheet: 2410 self.fallbacks['legend'] = PreambleCmds.legend 2411 self.out.append('\\begin{DUlegend}') 2412 2413 def depart_legend(self, node): 2414 self.out.append('\\end{DUlegend}\n') 2415 2416 def visit_line(self, node): 2417 self.out.append(r'\item[] ') 2418 2419 def depart_line(self, node): 2420 self.out.append('\n') 2421 2422 def visit_line_block(self, node): 2423 if not self.fallback_stylesheet: 2424 self.fallbacks['_providelength'] = PreambleCmds.providelength 2425 self.fallbacks['lineblock'] = PreambleCmds.lineblock 2426 self.set_align_from_classes(node) 2427 if isinstance(node.parent, nodes.line_block): 2428 self.out.append('\\item[]\n' 2429 '\\begin{DUlineblock}{\\DUlineblockindent}\n') 2430 # nested line-blocks cannot be given class arguments 2431 else: 2432 self.duclass_open(node) 2433 self.out.append('\\begin{DUlineblock}{0em}\n') 2434 self.insert_align_declaration(node) 2435 2436 def depart_line_block(self, node): 2437 self.out.append('\\end{DUlineblock}\n') 2438 self.duclass_close(node) 2439 2440 def visit_list_item(self, node): 2441 self.out.append('\n\\item ') 2442 2443 def depart_list_item(self, node): 2444 pass 2445 2446 def visit_literal(self, node): 2447 self.literal = True 2448 if ('code' in node['classes'] and 2449 self.settings.syntax_highlight != 'none'): 2450 self.requirements['color'] = PreambleCmds.color 2451 if not self.fallback_stylesheet: 2452 self.fallbacks['code'] = PreambleCmds.highlight_rules 2453 self.out.append('\\texttt{') 2454 if node['classes']: 2455 self.visit_inline(node) 2456 2457 def depart_literal(self, node): 2458 self.literal = False 2459 if node['classes']: 2460 self.depart_inline(node) 2461 self.out.append('}') 2462 2463 # Literal blocks are used for '::'-prefixed literal-indented 2464 # blocks of text, where the inline markup is not recognized, 2465 # but are also the product of the "parsed-literal" directive, 2466 # where the markup is respected. 2467 # 2468 # In both cases, we want to use a typewriter/monospaced typeface. 2469 # For "real" literal-blocks, we can use \verbatim, while for all 2470 # the others we must use \ttfamily and \raggedright. 2471 # 2472 # We can distinguish between the two kinds by the number of 2473 # siblings that compose this node: if it is composed by a 2474 # single element, it's either 2475 # * a real one, 2476 # * a parsed-literal that does not contain any markup, or 2477 # * a parsed-literal containing just one markup construct. 2478 def is_plaintext(self, node): 2479 """Check whether a node can be typeset verbatim""" 2480 return (len(node) == 1) and isinstance(node[0], nodes.Text) 2481 2482 def visit_literal_block(self, node): 2483 """Render a literal block. 2484 2485 Corresponding rST elements: literal block, parsed-literal, code. 2486 """ 2487 packages = {'lstlisting': r'\usepackage{listings}' '\n' 2488 r'\lstset{xleftmargin=\leftmargin}', 2489 'listing': r'\usepackage{moreverb}', 2490 'Verbatim': r'\usepackage{fancyvrb}', 2491 'verbatimtab': r'\usepackage{moreverb}'} 2492 2493 literal_env = self.literal_block_env 2494 2495 # Check, if it is possible to use a literal-block environment 2496 _plaintext = self.is_plaintext(node) 2497 _in_table = self.active_table.is_open() 2498 # TODO: fails if normal text precedes the literal block. 2499 # Check parent node instead? 2500 _autowidth_table = _in_table and self.active_table.colwidths_auto 2501 _use_env = _plaintext and not isinstance(node.parent, 2502 (nodes.footnote, nodes.admonition, nodes.system_message)) 2503 _use_listings = (literal_env == 'lstlisting') and _use_env 2504 2505 # Labels and classes: 2506 if node.get('ids'): 2507 self.out += ['\n'] + self.ids_to_labels(node) 2508 self.duclass_open(node) 2509 # Highlight code? 2510 if (not _plaintext 2511 and 'code' in node['classes'] 2512 and self.settings.syntax_highlight != 'none'): 2513 self.requirements['color'] = PreambleCmds.color 2514 if not self.fallback_stylesheet: 2515 self.fallbacks['code'] = PreambleCmds.highlight_rules 2516 # Wrap? 2517 if _in_table and _use_env and not _autowidth_table: 2518 # Wrap in minipage to prevent extra vertical space 2519 # with alltt and verbatim-like environments: 2520 self.fallbacks['ttem'] = '\n'.join(['', 2521 r'% character width in monospaced font', 2522 r'\newlength{\ttemwidth}', 2523 r'\settowidth{\ttemwidth}{\ttfamily M}']) 2524 self.out.append('\\begin{minipage}{%d\\ttemwidth}\n' % 2525 (max(len(line) for line in node.astext().split('\n')))) 2526 self.context.append('\n\\end{minipage}\n') 2527 elif not _in_table and not _use_listings: 2528 # Wrap in quote to set off vertically and indent 2529 self.out.append('\\begin{quote}\n') 2530 self.context.append('\n\\end{quote}\n') 2531 else: 2532 self.context.append('\n') 2533 2534 # Use verbatim-like environment, if defined and possible 2535 # (in an auto-width table, only listings works): 2536 if literal_env and _use_env and (not _autowidth_table 2537 or _use_listings): 2538 try: 2539 self.requirements['literal_block'] = packages[literal_env] 2540 except KeyError: 2541 pass 2542 self.verbatim = True 2543 if _in_table and _use_listings: 2544 self.out.append('\\lstset{xleftmargin=0pt}\n') 2545 self.out.append('\\begin{%s}%s\n' % 2546 (literal_env, self.literal_block_options)) 2547 self.context.append('\n\\end{%s}' % literal_env) 2548 elif _use_env and not _autowidth_table: 2549 self.alltt = True 2550 self.requirements['alltt'] = r'\usepackage{alltt}' 2551 self.out.append('\\begin{alltt}\n') 2552 self.context.append('\n\\end{alltt}') 2553 else: 2554 self.literal = True 2555 self.insert_newline = True 2556 self.insert_non_breaking_blanks = True 2557 # \raggedright ensures leading blanks are respected but 2558 # leads to additional leading vspace if the first line 2559 # of the block is overfull :-( 2560 self.out.append('\\ttfamily\\raggedright\n') 2561 self.context.append('') 2562 2563 def depart_literal_block(self, node): 2564 self.insert_non_breaking_blanks = False 2565 self.insert_newline = False 2566 self.literal = False 2567 self.verbatim = False 2568 self.alltt = False 2569 self.out.append(self.context.pop()) 2570 self.out.append(self.context.pop()) 2571 self.duclass_close(node) 2572 2573 def visit_meta(self, node): 2574 name = node.attributes.get('name') 2575 content = node.attributes.get('content') 2576 if not name or not content: 2577 return 2578 if name in ('author', 'creator', 'keywords', 'subject', 'title'): 2579 # fields with dedicated hyperref options: 2580 self.pdfinfo.append(' pdf%s={%s},'%(name, content)) 2581 elif name == 'producer': 2582 self.pdfinfo.append(' addtopdfproducer={%s},'%content) 2583 else: 2584 # generic interface (case sensitive!) 2585 self.pdfinfo.append(' pdfinfo={%s={%s}},'%(name, content)) 2586 2587 def depart_meta(self, node): 2588 pass 2589 2590 def visit_math(self, node, math_env='$'): 2591 """math role""" 2592 if node['classes']: 2593 self.visit_inline(node) 2594 self.requirements['amsmath'] = r'\usepackage{amsmath}' 2595 math_code = node.astext().translate(unichar2tex.uni2tex_table) 2596 if node.get('ids'): 2597 math_code = '\n'.join([math_code] + self.ids_to_labels(node)) 2598 if math_env == '$': 2599 if self.alltt: 2600 wrapper = u'\\(%s\\)' 2601 else: 2602 wrapper = u'$%s$' 2603 else: 2604 wrapper = u'\n'.join(['%%', 2605 r'\begin{%s}' % math_env, 2606 '%s', 2607 r'\end{%s}' % math_env]) 2608 self.out.append(wrapper % math_code) 2609 if node['classes']: 2610 self.depart_inline(node) 2611 # Content already processed: 2612 raise nodes.SkipNode 2613 2614 def depart_math(self, node): 2615 pass # never reached 2616 2617 def visit_math_block(self, node): 2618 math_env = pick_math_environment(node.astext()) 2619 self.visit_math(node, math_env=math_env) 2620 2621 def depart_math_block(self, node): 2622 pass # never reached 2623 2624 def visit_option(self, node): 2625 if self.context[-1]: 2626 # this is not the first option 2627 self.out.append(', ') 2628 2629 def depart_option(self, node): 2630 # flag that the first option is done. 2631 self.context[-1] += 1 2632 2633 def visit_option_argument(self, node): 2634 """Append the delimiter betweeen an option and its argument to body.""" 2635 self.out.append(node.get('delimiter', ' ')) 2636 2637 def depart_option_argument(self, node): 2638 pass 2639 2640 def visit_option_group(self, node): 2641 self.out.append('\n\\item[') 2642 # flag for first option 2643 self.context.append(0) 2644 2645 def depart_option_group(self, node): 2646 self.context.pop() # the flag 2647 self.out.append('] ') 2648 2649 def visit_option_list(self, node): 2650 if not self.fallback_stylesheet: 2651 self.fallbacks['_providelength'] = PreambleCmds.providelength 2652 self.fallbacks['optionlist'] = PreambleCmds.optionlist 2653 self.duclass_open(node) 2654 self.out.append('\\begin{DUoptionlist}') 2655 2656 def depart_option_list(self, node): 2657 self.out.append('\\end{DUoptionlist}\n') 2658 self.duclass_close(node) 2659 2660 def visit_option_list_item(self, node): 2661 pass 2662 2663 def depart_option_list_item(self, node): 2664 pass 2665 2666 def visit_option_string(self, node): 2667 ##self.out.append(self.starttag(node, 'span', '', CLASS='option')) 2668 pass 2669 2670 def depart_option_string(self, node): 2671 ##self.out.append('</span>') 2672 pass 2673 2674 def visit_organization(self, node): 2675 self.visit_docinfo_item(node, 'organization') 2676 2677 def depart_organization(self, node): 2678 self.depart_docinfo_item(node) 2679 2680 def visit_paragraph(self, node): 2681 # insert blank line, unless 2682 # * the paragraph is first in a list item, compound, or container 2683 # * follows a non-paragraph node in a compound, 2684 # * is in a table with auto-width columns 2685 index = node.parent.index(node) 2686 if index == 0 and isinstance(node.parent, 2687 (nodes.list_item, nodes.description, 2688 nodes.compound, nodes.container)): 2689 pass 2690 elif (index > 0 2691 and isinstance(node.parent, nodes.compound) 2692 and not isinstance(node.parent[index - 1], 2693 (nodes.paragraph, nodes.compound))): 2694 pass 2695 elif self.active_table.colwidths_auto: 2696 if index == 1: # second paragraph 2697 self.warn('LaTeX merges paragraphs in tables ' 2698 'with auto-sized columns!', base_node=node) 2699 if index > 0: 2700 self.out.append('\n') 2701 else: 2702 self.out.append('\n') 2703 if node.get('ids'): 2704 self.out += self.ids_to_labels(node) + ['\n'] 2705 if node['classes']: 2706 self.visit_inline(node) 2707 2708 def depart_paragraph(self, node): 2709 if node['classes']: 2710 self.depart_inline(node) 2711 if not self.active_table.colwidths_auto: 2712 self.out.append('\n') 2713 2714 def visit_problematic(self, node): 2715 self.requirements['color'] = PreambleCmds.color 2716 self.out.append('%\n') 2717 self.append_hypertargets(node) 2718 self.out.append(r'\hyperlink{%s}{\textbf{\color{red}' % node['refid']) 2719 2720 def depart_problematic(self, node): 2721 self.out.append('}}') 2722 2723 def visit_raw(self, node): 2724 if not 'latex' in node.get('format', '').split(): 2725 raise nodes.SkipNode 2726 if not self.is_inline(node): 2727 self.out.append('\n') 2728 if node['classes']: 2729 self.visit_inline(node) 2730 # append "as-is" skipping any LaTeX-encoding 2731 self.verbatim = True 2732 2733 def depart_raw(self, node): 2734 self.verbatim = False 2735 if node['classes']: 2736 self.depart_inline(node) 2737 if not self.is_inline(node): 2738 self.out.append('\n') 2739 2740 def has_unbalanced_braces(self, string): 2741 """Test whether there are unmatched '{' or '}' characters.""" 2742 level = 0 2743 for ch in string: 2744 if ch == '{': 2745 level += 1 2746 if ch == '}': 2747 level -= 1 2748 if level < 0: 2749 return True 2750 return level != 0 2751 2752 def visit_reference(self, node): 2753 # We need to escape #, \, and % if we use the URL in a command. 2754 special_chars = {ord('#'): u'\\#', 2755 ord('%'): u'\\%', 2756 ord('\\'): u'\\\\', 2757 } 2758 # external reference (URL) 2759 if 'refuri' in node: 2760 href = unicode(node['refuri']).translate(special_chars) 2761 # problematic chars double caret and unbalanced braces: 2762 if href.find('^^') != -1 or self.has_unbalanced_braces(href): 2763 self.error( 2764 'External link "%s" not supported by LaTeX.\n' 2765 ' (Must not contain "^^" or unbalanced braces.)' % href) 2766 if node['refuri'] == node.astext(): 2767 self.out.append(r'\url{%s}' % href) 2768 raise nodes.SkipNode 2769 self.out.append(r'\href{%s}{' % href) 2770 return 2771 # internal reference 2772 if 'refid' in node: 2773 href = node['refid'] 2774 elif 'refname' in node: 2775 href = self.document.nameids[node['refname']] 2776 else: 2777 raise AssertionError('Unknown reference.') 2778 if not self.is_inline(node): 2779 self.out.append('\n') 2780 self.out.append('\\hyperref[%s]{' % href) 2781 if self._reference_label: 2782 self.out.append('\\%s{%s}}' % 2783 (self._reference_label, href.replace('#', ''))) 2784 raise nodes.SkipNode 2785 2786 def depart_reference(self, node): 2787 self.out.append('}') 2788 if not self.is_inline(node): 2789 self.out.append('\n') 2790 2791 def visit_revision(self, node): 2792 self.visit_docinfo_item(node, 'revision') 2793 2794 def depart_revision(self, node): 2795 self.depart_docinfo_item(node) 2796 2797 def visit_rubric(self, node): 2798 if not self.fallback_stylesheet: 2799 self.fallbacks['rubric'] = PreambleCmds.rubric 2800 # class wrapper would interfere with ``\section*"`` type commands 2801 # (spacing/indent of first paragraph) 2802 self.out.append('\n\\DUrubric{') 2803 2804 def depart_rubric(self, node): 2805 self.out.append('}\n') 2806 2807 def visit_section(self, node): 2808 self.section_level += 1 2809 # Initialize counter for potential subsections: 2810 self._section_number.append(0) 2811 # Counter for this section's level (initialized by parent section): 2812 self._section_number[self.section_level - 1] += 1 2813 2814 def depart_section(self, node): 2815 # Remove counter for potential subsections: 2816 self._section_number.pop() 2817 self.section_level -= 1 2818 2819 def visit_sidebar(self, node): 2820 self.duclass_open(node) 2821 self.requirements['color'] = PreambleCmds.color 2822 if not self.fallback_stylesheet: 2823 self.fallbacks['sidebar'] = PreambleCmds.sidebar 2824 self.out.append('\\DUsidebar{') 2825 2826 def depart_sidebar(self, node): 2827 self.out.append('}\n') 2828 self.duclass_close(node) 2829 2830 attribution_formats = {'dash': (u'—', ''), # EM DASH 2831 'parentheses': ('(', ')'), 2832 'parens': ('(', ')'), 2833 'none': ('', '')} 2834 2835 def visit_attribution(self, node): 2836 prefix, suffix = self.attribution_formats[self.settings.attribution] 2837 self.out.append('\\nopagebreak\n\n\\raggedleft ') 2838 self.out.append(prefix) 2839 self.context.append(suffix) 2840 2841 def depart_attribution(self, node): 2842 self.out.append(self.context.pop() + '\n') 2843 2844 def visit_status(self, node): 2845 self.visit_docinfo_item(node, 'status') 2846 2847 def depart_status(self, node): 2848 self.depart_docinfo_item(node) 2849 2850 def visit_strong(self, node): 2851 self.out.append('\\textbf{') 2852 if node['classes']: 2853 self.visit_inline(node) 2854 2855 def depart_strong(self, node): 2856 if node['classes']: 2857 self.depart_inline(node) 2858 self.out.append('}') 2859 2860 def visit_substitution_definition(self, node): 2861 raise nodes.SkipNode 2862 2863 def visit_substitution_reference(self, node): 2864 self.unimplemented_visit(node) 2865 2866 def visit_subtitle(self, node): 2867 if isinstance(node.parent, nodes.document): 2868 self.push_output_collector(self.subtitle) 2869 if not self.fallback_stylesheet: 2870 self.fallbacks['documentsubtitle'] = PreambleCmds.documentsubtitle 2871 protect = (self.settings.documentclass == 'memoir') 2872 self.subtitle_labels += self.ids_to_labels(node, set_anchor=False, 2873 protect=protect) 2874 # section subtitle: "starred" (no number, not in ToC) 2875 elif isinstance(node.parent, nodes.section): 2876 self.out.append(r'\%s*{' % 2877 self.d_class.section(self.section_level + 1)) 2878 else: 2879 if not self.fallback_stylesheet: 2880 self.fallbacks['subtitle'] = PreambleCmds.subtitle 2881 self.out.append('\n\\DUsubtitle{') 2882 2883 def depart_subtitle(self, node): 2884 if isinstance(node.parent, nodes.document): 2885 self.pop_output_collector() 2886 else: 2887 self.out.append('}\n') 2888 2889 def visit_system_message(self, node): 2890 self.requirements['color'] = PreambleCmds.color 2891 if not self.fallback_stylesheet: 2892 self.fallbacks['title'] = PreambleCmds.title 2893 if self.settings.legacy_class_functions: 2894 self.fallbacks['title'] = PreambleCmds.title_legacy 2895 node['classes'] = ['system-message'] 2896 self.visit_admonition(node) 2897 if self.settings.legacy_class_functions: 2898 self.out.append('\n\\DUtitle[system-message]{system-message\n') 2899 else: 2900 self.out.append('\n\\DUtitle{system-message\n') 2901 self.append_hypertargets(node) 2902 try: 2903 line = ', line~%s' % node['line'] 2904 except KeyError: 2905 line = '' 2906 self.out.append('}\n\n{\\color{red}%s/%s} in \\texttt{%s}%s\n' % 2907 (node['type'], node['level'], 2908 self.encode(node['source']), line)) 2909 if len(node['backrefs']) == 1: 2910 self.out.append('\n\\hyperlink{%s}{' % node['backrefs'][0]) 2911 self.context.append('}') 2912 else: 2913 backrefs = ['\\hyperlink{%s}{%d}' % (href, i+1) 2914 for (i, href) in enumerate(node['backrefs'])] 2915 self.context.append('backrefs: ' + ' '.join(backrefs)) 2916 2917 def depart_system_message(self, node): 2918 self.out.append(self.context.pop()) 2919 self.depart_admonition(node) 2920 2921 def visit_table(self, node): 2922 self.duclass_open(node) 2923 self.requirements['table'] = PreambleCmds.table 2924 if self.active_table.is_open(): 2925 self.table_stack.append(self.active_table) 2926 # nesting longtable does not work (e.g. 2007-04-18) 2927 # TODO: don't use a longtable or add \noindent before 2928 # the next paragraph, when in a "compound paragraph". 2929 self.active_table = Table(self, 'tabular') 2930 # A longtable moves before \paragraph and \subparagraph 2931 # section titles if it immediately follows them: 2932 if (self.active_table._latex_type == 'longtable' and 2933 isinstance(node.parent, nodes.section) and 2934 node.parent.index(node) == 1 and 2935 self.d_class.section(self.section_level).find('paragraph') != -1): 2936 self.out.append('\\leavevmode') 2937 self.active_table.open() 2938 self.active_table.set_table_style(self.settings.table_style, 2939 node['classes']) 2940 if 'align' in node: 2941 self.active_table.set('align', node['align']) 2942 if self.active_table.borders == 'booktabs': 2943 self.requirements['booktabs'] = r'\usepackage{booktabs}' 2944 self.push_output_collector([]) 2945 2946 def depart_table(self, node): 2947 # wrap content in the right environment: 2948 content = self.out 2949 self.pop_output_collector() 2950 try: 2951 width = self.to_latex_length(node.attributes['width']) 2952 except KeyError: 2953 width = r'\linewidth' 2954 if isinstance(node.parent, nodes.compound): 2955 self.out.append('\n') 2956 self.out.append(self.active_table.get_opening(width)) 2957 self.out += content 2958 self.out.append(self.active_table.get_closing() + '\n') 2959 self.active_table.close() 2960 if len(self.table_stack)>0: 2961 self.active_table = self.table_stack.pop() 2962 # Insert hyperlabel after (long)table, as 2963 # other places (beginning, caption) result in LaTeX errors. 2964 if node.get('ids'): 2965 self.out += self.ids_to_labels(node, set_anchor=False) + ['\n'] 2966 self.duclass_close(node) 2967 2968 def visit_target(self, node): 2969 # Skip indirect targets: 2970 if ('refuri' in node # external hyperlink 2971 or 'refid' in node # resolved internal link 2972 or 'refname' in node): # unresolved internal link 2973 ## self.out.append('%% %s\n' % node) # for debugging 2974 return 2975 self.out.append('%\n') 2976 # do we need an anchor (\phantomsection)? 2977 set_anchor = not isinstance(node.parent, (nodes.caption, nodes.title)) 2978 # TODO: where else can/must we omit the \phantomsection? 2979 self.out += self.ids_to_labels(node, set_anchor) 2980 2981 def depart_target(self, node): 2982 pass 2983 2984 def visit_tbody(self, node): 2985 # BUG write preamble if not yet done (colspecs not []) 2986 # for tables without heads. 2987 if not self.active_table.get('preamble written'): 2988 self.visit_thead(node) 2989 self.depart_thead(None) 2990 2991 def depart_tbody(self, node): 2992 pass 2993 2994 def visit_term(self, node): 2995 """definition list term""" 2996 # Commands with optional args inside an optional arg must be put 2997 # in a group, e.g. ``\item[{\hyperref[label]{text}}]``. 2998 self.out.append('\\item[{') 2999 3000 def depart_term(self, node): 3001 self.out.append('}] ') 3002 # Do we need a \leavevmode (line break if the field body begins 3003 # with a list or environment)? 3004 next_node = node.next_node(descend=False, siblings=True) 3005 if not isinstance(next_node, nodes.classifier): 3006 self.out.append(self.term_postfix(next_node)) 3007 3008 def visit_tgroup(self, node): 3009 pass 3010 3011 def depart_tgroup(self, node): 3012 pass 3013 3014 _thead_depth = 0 3015 def thead_depth (self): 3016 return self._thead_depth 3017 3018 def visit_thead(self, node): 3019 self._thead_depth += 1 3020 if 1 == self.thead_depth(): 3021 self.out.append('{%s}\n' % self.active_table.get_colspecs(node)) 3022 self.active_table.set('preamble written', 1) 3023 self.out.append(self.active_table.get_caption()) 3024 self.out.extend(self.active_table.visit_thead()) 3025 3026 def depart_thead(self, node): 3027 if node is not None: 3028 self.out.extend(self.active_table.depart_thead()) 3029 if self.active_table.need_recurse(): 3030 node.walkabout(self) 3031 self._thead_depth -= 1 3032 3033 def visit_title(self, node): 3034 """Append section and other titles.""" 3035 # Document title 3036 if isinstance(node.parent, nodes.document): 3037 self.push_output_collector(self.title) 3038 self.context.append('') 3039 self.pdfinfo.append(' pdftitle={%s},' % 3040 self.encode(node.astext())) 3041 # Topic titles (topic, admonition, sidebar) 3042 elif (isinstance(node.parent, nodes.topic) or 3043 isinstance(node.parent, nodes.admonition) or 3044 isinstance(node.parent, nodes.sidebar)): 3045 classes = node.parent['classes'] or [node.parent.tagname] 3046 if self.settings.legacy_class_functions: 3047 self.fallbacks['title'] = PreambleCmds.title_legacy 3048 self.out.append('\n\\DUtitle[%s]{' % ','.join(classes)) 3049 else: 3050 if not self.fallback_stylesheet: 3051 self.fallbacks['title'] = PreambleCmds.title 3052 self.out.append('\n\\DUtitle{') 3053 self.context.append('}\n') 3054 # Table caption 3055 elif isinstance(node.parent, nodes.table): 3056 self.push_output_collector(self.active_table.caption) 3057 self.context.append('') 3058 # Section title 3059 else: 3060 if hasattr(PreambleCmds, 'secnumdepth'): 3061 self.requirements['secnumdepth'] = PreambleCmds.secnumdepth 3062 level = self.section_level 3063 section_name = self.d_class.section(level) 3064 self.out.append('\n\n') 3065 if level > len(self.d_class.sections): 3066 # section level not supported by LaTeX 3067 if self.settings.legacy_class_functions: 3068 self.fallbacks['title'] = PreambleCmds.title_legacy 3069 section_name += '[section%s]' % roman.toRoman(level) 3070 else: 3071 if not self.fallback_stylesheet: 3072 self.fallbacks['title'] = PreambleCmds.title 3073 self.fallbacks['DUclass'] = PreambleCmds.duclass 3074 self.out.append('\\begin{DUclass}{section%s}\n' 3075 % roman.toRoman(level)) 3076 3077 # System messages heading in red: 3078 if ('system-messages' in node.parent['classes']): 3079 self.requirements['color'] = PreambleCmds.color 3080 section_title = self.encode(node.astext()) 3081 self.out.append(r'\%s[%s]{\color{red}' % ( 3082 section_name, section_title)) 3083 else: 3084 self.out.append(r'\%s{' % section_name) 3085 3086 # label and ToC entry: 3087 bookmark = [''] 3088 # add sections with unsupported level to toc and pdfbookmarks? 3089 ## if level > len(self.d_class.sections): 3090 ## section_title = self.encode(node.astext()) 3091 ## bookmark.append(r'\addcontentsline{toc}{%s}{%s}' % 3092 ## (section_name, section_title)) 3093 bookmark += self.ids_to_labels(node.parent, set_anchor=False) 3094 self.context.append('%\n '.join(bookmark) + '%\n}\n') 3095 if (level > len(self.d_class.sections) 3096 and not self.settings.legacy_class_functions): 3097 self.context[-1] += '\\end{DUclass}\n' 3098 # MAYBE postfix paragraph and subparagraph with \leavemode to 3099 # ensure floats stay in the section and text starts on a new line. 3100 3101 def depart_title(self, node): 3102 self.out.append(self.context.pop()) 3103 if isinstance(node.parent, (nodes.table, nodes.document)): 3104 self.pop_output_collector() 3105 3106 def minitoc(self, node, title, depth): 3107 """Generate a local table of contents with LaTeX package minitoc""" 3108 section_name = self.d_class.section(self.section_level) 3109 # name-prefix for current section level 3110 minitoc_names = {'part': 'part', 'chapter': 'mini'} 3111 if 'chapter' not in self.d_class.sections: 3112 minitoc_names['section'] = 'sect' 3113 try: 3114 minitoc_name = minitoc_names[section_name] 3115 except KeyError: # minitoc only supports part- and toplevel 3116 self.warn('Skipping local ToC at %s level.\n' % section_name + 3117 ' Feature not supported with option "use-latex-toc"', 3118 base_node=node) 3119 return 3120 # Requirements/Setup 3121 self.requirements['minitoc'] = PreambleCmds.minitoc 3122 self.requirements['minitoc-'+minitoc_name] = (r'\do%stoc' % 3123 minitoc_name) 3124 # depth: (Docutils defaults to unlimited depth) 3125 maxdepth = len(self.d_class.sections) 3126 self.requirements['minitoc-%s-depth' % minitoc_name] = ( 3127 r'\mtcsetdepth{%stoc}{%d}' % (minitoc_name, maxdepth)) 3128 # Process 'depth' argument (!Docutils stores a relative depth while 3129 # minitoc expects an absolute depth!): 3130 offset = {'sect': 1, 'mini': 0, 'part': 0} 3131 if 'chapter' in self.d_class.sections: 3132 offset['part'] = -1 3133 if depth: 3134 self.out.append('\\setcounter{%stocdepth}{%d}' % 3135 (minitoc_name, depth + offset[minitoc_name])) 3136 # title: 3137 self.out.append('\\mtcsettitle{%stoc}{%s}\n' % (minitoc_name, title)) 3138 # the toc-generating command: 3139 self.out.append('\\%stoc\n' % minitoc_name) 3140 3141 def visit_topic(self, node): 3142 # Topic nodes can be generic topic, abstract, dedication, or ToC. 3143 # table of contents: 3144 if 'contents' in node['classes']: 3145 self.out.append('\n') 3146 self.out += self.ids_to_labels(node) 3147 # add contents to PDF bookmarks sidebar 3148 if (isinstance(node.next_node(), nodes.title) 3149 and self.settings.documentclass != 'memoir'): 3150 self.out.append('\n\\pdfbookmark[%d]{%s}{%s}' % 3151 (self.section_level+1, 3152 node.next_node().astext(), 3153 node.get('ids', ['contents'])[0] 3154 )) 3155 if self.use_latex_toc: 3156 title = '' 3157 if isinstance(node.next_node(), nodes.title): 3158 title = self.encode(node.pop(0).astext()) 3159 depth = node.get('depth', 0) 3160 if 'local' in node['classes']: 3161 self.minitoc(node, title, depth) 3162 return 3163 if depth: 3164 self.out.append('\\setcounter{tocdepth}{%d}\n' % depth) 3165 if title != 'Contents': 3166 self.out.append('\n\\renewcommand{\\contentsname}{%s}' % 3167 title) 3168 self.out.append('\n\\tableofcontents\n') 3169 self.has_latex_toc = True 3170 # ignore rest of node content 3171 raise nodes.SkipNode 3172 else: # Docutils generated contents list 3173 # set flag for visit_bullet_list() and visit_title() 3174 self.is_toc_list = True 3175 elif ('abstract' in node['classes'] and 3176 self.settings.use_latex_abstract): 3177 self.push_output_collector(self.abstract) 3178 self.out.append('\\begin{abstract}') 3179 if isinstance(node.next_node(), nodes.title): 3180 node.pop(0) # LaTeX provides its own title 3181 else: 3182 # special topics: 3183 if 'abstract' in node['classes']: 3184 if not self.fallback_stylesheet: 3185 self.fallbacks['abstract'] = PreambleCmds.abstract 3186 if self.settings.legacy_class_functions: 3187 self.fallbacks['abstract'] = PreambleCmds.abstract_legacy 3188 self.push_output_collector(self.abstract) 3189 elif 'dedication' in node['classes']: 3190 if not self.fallback_stylesheet: 3191 self.fallbacks['dedication'] = PreambleCmds.dedication 3192 self.push_output_collector(self.dedication) 3193 else: 3194 node['classes'].insert(0, 'topic') 3195 self.visit_block_quote(node) 3196 3197 def depart_topic(self, node): 3198 self.is_toc_list = False 3199 if ('abstract' in node['classes'] 3200 and self.settings.use_latex_abstract): 3201 self.out.append('\\end{abstract}\n') 3202 elif not 'contents' in node['classes']: 3203 self.depart_block_quote(node) 3204 if ('abstract' in node['classes'] or 3205 'dedication' in node['classes']): 3206 self.pop_output_collector() 3207 3208 def visit_transition(self, node): 3209 if not self.fallback_stylesheet: 3210 self.fallbacks['transition'] = PreambleCmds.transition 3211 self.out.append('\n%' + '_' * 75 + '\n') 3212 self.out.append('\\DUtransition\n') 3213 3214 def depart_transition(self, node): 3215 pass 3216 3217 def visit_version(self, node): 3218 self.visit_docinfo_item(node, 'version') 3219 3220 def depart_version(self, node): 3221 self.depart_docinfo_item(node) 3222 3223 def unimplemented_visit(self, node): 3224 raise NotImplementedError('visiting unimplemented node type: %s' % 3225 node.__class__.__name__) 3226 3227# def unknown_visit(self, node): 3228# def default_visit(self, node): 3229 3230# vim: set ts=4 et ai : 3231