forked from myint/rst2beamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrst2beamer.py
executable file
·1311 lines (1075 loc) · 41.8 KB
/
rst2beamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# encoding: utf-8
# Copyright (C) 2007-2009 Ryan Krauss, Paul-Michael Agapow
# Copyright (C) 2013-2016 Steven Myint
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""A docutils script converting restructured text into Beamer-flavoured LaTeX.
Beamer is a LaTeX document class for presentations. Via this script, ReST can
be used to prepare slides. It can be called::
rst2beamer.py infile.txt > outfile.tex
where ``infile.txt`` contains the rst and ``outfile.tex`` contains the
Beamer-flavoured LaTeX.
See <http:www.agapow.net/software/rst2beamer> for more details.
"""
# TODO: modifications for handout sections?
# TODO: sections and subsections?
# TODO: convert document metadata to front page fields?
# TODO: toc-conversion?
# TODO: fix descriptions
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import re
from docutils.core import publish_cmdline, default_description
from docutils.writers.latex2e import Writer as Latex2eWriter
from docutils.writers.latex2e import LaTeXTranslator, DocumentClass
from docutils import nodes
from docutils.nodes import fully_normalize_name as normalize_name
from docutils.parsers.rst import directives, Directive
from docutils import utils
from docutils.writers.latex2e import PreambleCmds
__docformat__ = 'restructuredtext en'
__author__ = ('Ryan Krauss <[email protected]> & '
'Paul-Michael Agapow <[email protected]>')
__version__ = '0.9.1'
SHOWNOTES_FALSE = 'false'
SHOWNOTES_TRUE = 'true'
SHOWNOTES_ONLY = 'only'
SHOWNOTES_LEFT = 'left'
SHOWNOTES_RIGHT = 'right'
SHOWNOTES_TOP = 'top'
SHOWNOTES_BOTTOM = 'bottom'
SHOWNOTES_OPTIONS = [
SHOWNOTES_FALSE,
SHOWNOTES_TRUE,
SHOWNOTES_ONLY,
SHOWNOTES_LEFT,
SHOWNOTES_RIGHT,
SHOWNOTES_TOP,
SHOWNOTES_BOTTOM,
]
HIGHLIGHT_OPTIONS = {
'python': 'python',
'guess': 'guess',
'c++': 'cpp',
}
BEAMER_SPEC = (
'Beamer options',
'These are derived almost entirely from the LaTeX2e options',
tuple(
[
(
'Specify theme.',
['--theme'],
{'default': 'Ilmenau', }
),
(
'Overlay bulleted items. Put [<+-| alert@+>] at the end of '
'\\begin{itemize} so that Beamer creates an overlay for each '
'bulleted item and the presentation reveals one bullet at a '
'time',
['--overlaybullets'],
{'default': False, }
),
(
'Default for whether or not to pass the fragile option to '
'the beamber frames (slides).',
['--fragile-default'],
{'default': True, }
),
(
'Center figures. All includegraphics statements will be put '
'inside center environments.',
['--centerfigs'],
{'default': True, }
),
(
'Shortauthor for use in header/footer of slide',
['--shortauthor'],
{'default': '', }
),
(
'Shorttitle for use in header/footer of slide',
['--shorttitle'],
{'default': '', }
),
(
# TODO: this doesn't seem to do anything ...
'Specify document options. Multiple options can be given, '
'separated by commas. Default is "10pt,a4paper".',
['--documentoptions'],
{'default': '', }
),
(
'Print embedded notes along with the slides. Possible '
"arguments include 'false' (don't show), 'only' (show "
"only notes), 'left', 'right', 'top', 'bottom' (show in "
'relation to the annotated slide).',
['--shownotes'],
{
'action': 'store',
'type': 'choice',
'dest': 'shownotes',
'choices': SHOWNOTES_OPTIONS,
'default': SHOWNOTES_FALSE,
}
),
# should the pygments highlighter be used for codeblocks?
(
'Use the Pygments syntax highlighter to color blocks of '
'code. Otherwise, they will be typeset as simple literal '
'text. Obviously Pygments must be installed or an error '
'will be raised. ',
['--no-codeblocks-use-pygments'],
{
'action': 'store_false',
'dest': 'cb_use_pygments',
'default': True,
}
),
# replace tabs inside codeblocks?
(
'Replace the leading tabs in codeblocks with spaces.',
['--codeblocks-replace-tabs'],
{
'action': 'store',
'type': int,
'dest': 'cb_replace_tabs',
'default': 0,
}
),
# what language the codeblock is if not specified
(
'The default language to highlight code blocks as. ',
['--codeblocks-default-language'],
{
'action': 'store',
'type': 'choice',
'dest': 'cb_default_lang',
'choices': list(HIGHLIGHT_OPTIONS.values()),
'default': 'guess',
}
),
] + list(Latex2eWriter.settings_spec[2][2:])
),
)
BEAMER_DEFAULTS = {
'use_latex_toc': True,
'output_encoding': 'utf-8',
'documentclass': 'beamer',
'documentoptions': 't',
# text is at the top of each slide rather than centered. Changing to
# 'c' centers the text on each slide (vertically)
}
BEAMER_DEFAULT_OVERRIDES = {'use_latex_docinfo': 1}
BOOL_DICT = {'false': False,
'true': True,
'0': False,
'1': True}
PreambleCmds.documenttitle = r"""
%% Document title
\title[%s]{%s}
\author[%s]{%s}
\date{%s}
\maketitle
"""
DOCINFO_W_INSTITUTE = r"""
%% Document title
\title[%s]{%s}
\author[%s]{%s}
\date{%s}
\institute{%s}
\maketitle
"""
LEADING_SPACE_RE = re.compile('^ +')
def adjust_indent_spaces(strn, orig_width=8, new_width=3):
"""Adjust the leading space on a string so as to change the indent width.
:Parameters:
strn
The source string to change.
orig_width
The expected width for an indent in the source string.
new_width
The new width to make an ident.
:Returns:
The original string re-indented.
That takes strings that may be indented by a set number of spaces (or its
multiple) and adjusts the indent for a new number of spaces. So if the
expected indent width is 8 and the desired ident width is 3, a string has
been indented by 16 spaces, will be changed to have a indent of 6.
For example::
>>> adjust_indent_spaces (' foo')
' foo'
>>> adjust_indent_spaces (' foo', orig_width=2, new_width=1)
' foo'
This is useful where meaningful indent must be preserved (i.e. passed
through) ReST, especially tabs when used in the literal environments. ReST
transforms tabs-as-indents to 8 spaces, which leads to excessively spaced
out text. This function can be used to adjust the indent step to a
reasonable size.
.. note::
Excess spaces (those above and beyond a multiple of the original
indent width) will be preserved. Only indenting spaces will be
handled. Mixing tabs and spaces is - as always - a bad idea.
"""
# Preconditions & preparation:
assert orig_width >= 1
assert new_width >= 0
if orig_width == new_width:
return strn
# Main:
match = LEADING_SPACE_RE.match(strn)
if match:
indent_len = match.end() - match.start()
indent_cnt = indent_len // orig_width
indent_depth = indent_cnt * orig_width
strn = ' ' * indent_cnt * new_width + strn[indent_depth:]
return strn
def node_has_class(node, classes):
"""Does the node have one of these classes?
:Parameters:
node
A docutils node
class
A class name or list of class names.
:Returns:
A boolean indicating membership.
A convenience function, largely for testing for the special class names
in containers.
"""
# Preconditions & preparation:
# wrap single name in list
if not issubclass(type(classes), list):
classes = [classes]
# Main:
for cname in classes:
if cname in node['classes']:
return True
return False
def node_lang_class(node):
"""Extract a language specification from a node class names.
:Parameters:
node
A docutils node
:Returns:
A string giving a language abbreviation (e.g. 'py') or None if no
langauge is found.
Some sourcecode containers can pass a (programming) language specification
by passing it via a classname like 'lang-py'. This function searches a
nodes classnames for those starting with 'lang-' and returns the trailing
portion. Note that if more than one classname matches, only the first is
seen.
"""
# Main:
for cname in node['classes']:
if cname.startswith('lang-'):
return cname[5:]
return None
def wrap_children_in_columns(par_node, children, width=None):
"""Replace this node's children with columns containing the passed
children.
:Parameters:
par_node
The node whose children are to be replaced.
children
The new child nodes, to be wrapped in columns and added to the
parent.
width
The width to be assigned to the columns.
In constructing columns for beamer using either 'simplecolumns' approach,
we have to wrap the original elements in column nodes, giving them an
appropriate width. Note that this mutates parent node.
"""
# Preconditions & preparation:
# TODO: check for children and raise error if not?
width = width or 0.90
# Main:
# calc width of child columns
child_cnt = len(children)
col_width = width / child_cnt
# set each element of content in a column and add to column set
new_children = []
for child in children:
col = column()
col.width = col_width
col.append(child)
new_children.append(col)
par_node.children = new_children
def has_sub_sections(node):
"""Test whether or not a section node has children with the tagname
section.
The function is going to be used to assess
whether or not a certain section is the lowest level. Sections
that have not sub-sections (i.e. no children with the tagname
section) are assumed to be Beamer slides
"""
for child in node.children:
if child.tagname == 'section':
return True
return False
def string_to_bool(text, default=True):
"""Turn a commandline argument string into a boolean value.
>>> string_to_bool('true')
True
>>> string_to_bool('false')
False
>>> string_to_bool('0')
False
>>> string_to_bool('1')
True
>>> string_to_bool('abc')
True
"""
if isinstance(text, bool):
return text
return BOOL_DICT.get(text.lower(), default)
def highlight_code(text, lang):
"""Syntax-highlight source code using Pygments.
:Parameters:
text
The code to be formatted.
lang
The language of the source code.
:Returns:
A LaTeX formatted representation of the source code.
"""
# Preconditions & preparation:
from pygments import highlight
from pygments.formatters import LatexFormatter
lexer = get_lexer(text, lang)
if not lexer:
return text
lexer.add_filter('whitespace', tabsize=3, tabs=' ')
return highlight(text, lexer, LatexFormatter(tabsize=3))
def get_lexer(text, lang):
"""Return the Pygments lexer for parsing this sourcecode.
:Parameters:
text
The sourcecode to be lexed for highlighting. This is analysed if
the language is 'guess'.
lang
An abbreviation for the programming langauge of the code. Can be
any 'name' accepted by Pygments, including 'none' (plain text) or
'guess' (analyse the passed code for clues).
:Returns:
A Pygments lexer.
"""
# TODO: what if source has errors?
# Preconditions & preparation:
from pygments.lexers import (get_lexer_by_name, TextLexer, guess_lexer)
# Main:
if lang == 'guess':
try:
return guess_lexer(text)
except ValueError:
return None
elif lang == 'none':
return TextLexer
else:
return get_lexer_by_name(lang)
# Special nodes for marking up beamer layout.
class columnset(nodes.container):
"""A group of columns to display on one slide.
Named as per docutils standards.
"""
# NOTE: a simple container, has no attributes.
class column(nodes.container):
"""A single column, grouping content.
Named as per docutils standards.
"""
width = 0
class beamer_note(nodes.container):
"""Annotations for a beamer presentation.
Named as per docutils standards and to distinguish it from core
docutils node type.
"""
class onlybeamer(nodes.container):
"""A block of text to appear in the presentation and not in the handouts or
article form.
Named as per docutils standards.
"""
handouttext = ''
class block(nodes.container):
"""A block of text to appear in a block environment.
Named as per docutils standards.
"""
title = ''
# DIRECTIVES
class CodeBlockDirective(Directive):
"""Directive for a code block with special highlighting or line numbering
settings.
Unabashedly borrowed from the Sphinx source.
"""
has_content = True
required_arguments = 0
optional_arguments = 1
final_argument_whitespace = False
option_spec = {
'linenos': directives.flag,
}
def run(self):
# extract langauge from block or commandline
# we allow the langauge specification to be optional
try:
language = self.arguments[0]
except IndexError:
language = 'guess'
code = '\n'.join(self.content)
literal = nodes.literal_block(code, code)
literal['classes'].append('code-block')
literal['language'] = language
literal['linenos'] = 'linenos' in self.options
return [literal]
for _name in ['code-block', 'sourcecode']:
directives.register_directive(_name, CodeBlockDirective)
class SimpleColsDirective(Directive):
"""A directive that wraps all contained nodes in beamer columns.
Accept 'width' as an optional argument for total width of contained
columns.
"""
required_arguments = 0
optional_arguments = 1
final_argument_whitespace = True
has_content = True
option_spec = {'width': float}
def run(self):
# Preconditions:
self.assert_has_content()
# get width
width = self.options.get('width', 0.9)
if (width <= 0.) or (width > 1.):
raise self.error(
"columnset width '%f' must be between 0.0 and 1.0" %
width)
# Main:
# parse content of columnset
dummy = nodes.Element()
self.state.nested_parse(self.content, self.content_offset,
dummy)
# make columnset
text = '\n'.join(self.content)
cset = columnset(text)
# wrap children in columns & set widths
wrap_children_in_columns(cset, dummy.children, width)
# Postconditions & return:
return [cset]
directives.register_directive('beamer-simplecolumns', SimpleColsDirective)
class ColumnSetDirective(Directive):
"""A directive that encloses explicit columns in a 'columns' environment.
Within this, columns are explcitly set with the column directive. There is
a single optional argument 'width' to determine the total width of
columns on the page, expressed as a fraction of textwidth. If no width is
given, it defaults to 0.90.
Contained columns may have an assigned width. If not, the remaining width
is divided amongst them. Contained columns can 'overassign' width,
provided all column widths are defined.
"""
required_arguments = 0
optional_arguments = 1
final_argument_whitespace = True
has_content = True
option_spec = {'width': float}
def run(self):
# Preconditions:
self.assert_has_content()
# get and check width of column set
width = self.options.get('width', 0.9)
if (width <= 0.) or (width > 1.):
raise self.error(
"columnset width '%f' must be between 0.0 and 1.0" % width)
# Main:
# make columnset
text = '\n'.join(self.content)
cset = columnset(text)
# parse content of columnset
self.state.nested_parse(self.content, self.content_offset, cset)
# survey widths
used_width = 0.0
unsized_cols = []
for child in cset:
child_width = getattr(child, 'width', None)
if child_width:
used_width += child_width
else:
unsized_cols.append(child)
if used_width > 1.:
raise self.error(
"cumulative column width '%f' exceeds 1.0" % used_width)
# set unsized widths
if unsized_cols:
excess_width = width - used_width
if excess_width <= 0.:
raise self.error(
"no room for unsized columns '%f'" % excess_width)
col_width = excess_width / len(unsized_cols)
for child in unsized_cols:
child.width = col_width
elif width < used_width:
# TODO: should post a warning?
pass
# Postconditions & return:
return [cset]
directives.register_directive('beamer-columnset', ColumnSetDirective)
class ColumnDirective(Directive):
"""A directive to explicitly create an individual column.
This can only be used within the columnset directive. It can takes a
single optional argument 'width' to determine the column width on
page. If no width is given, it is recorded as None and should be
later assigned by the enclosing columnset.
"""
required_arguments = 0
optional_arguments = 1
final_argument_whitespace = True
has_content = True
option_spec = {'width': float}
def run(self):
# Preconditions:
self.assert_has_content()
# get width
width = self.options.get('width')
if width is not None:
if (width <= 0.) or (width > 1.):
raise self.error(
"columnset width '%f' must be between 0.0 and 1.0" %
width)
# Main:
# make columnset
text = '\n'.join(self.content)
col = column(text)
col.width = width
# parse content of column
self.state.nested_parse(self.content, self.content_offset, col)
# adjust widths
# Postconditions & return:
return [col]
directives.register_directive('beamer-column', ColumnDirective)
class NoteDirective(Directive):
"""A directive to include notes within a beamer presentation."""
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = True
has_content = True
option_spec = {}
def run(self):
# Preconditions:
self.assert_has_content()
# Main:
# Preconditions:
# make columnset
text = '\n'.join(self.content)
note_node = beamer_note(text)
# parse content of note
self.state.nested_parse(self.content, self.content_offset, note_node)
# Postconditions & return:
return [note_node]
directives.register_directive('beamer-note', NoteDirective)
class BeamerSection(Directive):
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True
has_content = True
def run(self):
title = self.arguments[0]
text_node = nodes.Text(title)
text_nodes = [text_node]
title_node = nodes.title(title, '', *text_nodes)
name = normalize_name(title_node.astext())
section_node = nodes.section(rawsource=self.block_text)
section_node['names'].append(name)
section_node += title_node
messages = []
title_messages = []
section_node += messages
section_node += title_messages
section_node.tagname = 'beamer-section'
return [section_node]
directives.register_directive('beamer-section', BeamerSection)
class OnlyBeamerDirective(Directive):
r"""A directive that encloses its content in \only<beamer>{content} so that
the content shows up in the presentation and not in the handouts or article
version."""
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = True
has_content = True
option_spec = {'handouttext': str}
def run(self):
# Preconditions:
self.assert_has_content()
# get and check width of column set
text = '\n'.join(self.content)
only_beamer_set = onlybeamer(text)
handouttext = self.options.get('handouttext', '')
only_beamer_set.handouttext = handouttext
# parse content of columnset
self.state.nested_parse(self.content, self.content_offset,
only_beamer_set)
# survey widths
return [only_beamer_set]
directives.register_directive('onlybeamer', OnlyBeamerDirective)
class BlockDirective(Directive):
r"""A directive that encloses its content in.
\begin{block}{title}
content
\end{block}
"""
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True
has_content = True
option_spec = {}
def run(self):
# Preconditions:
self.assert_has_content()
title = self.arguments[0]
# get and check width of column set
text = '\n'.join(self.content)
body_set = block(text)
# parse content of columnset
self.state.nested_parse(self.content, self.content_offset,
body_set)
# survey widths
body_set.title = title
return [body_set]
directives.register_directive('block', BlockDirective)
class BeamerTranslator(LaTeXTranslator):
"""A converter for docutils elements to beamer-flavoured latex."""
def __init__(self, document):
LaTeXTranslator.__init__(self, document)
# Used for Beamer title and possibly header/footer. Set from docinfo
# record the settings for codeblocks.
self.organization = None
self.cb_use_pygments = document.settings.cb_use_pygments
self.cb_replace_tabs = document.settings.cb_replace_tabs
self.cb_default_lang = document.settings.cb_default_lang
self.head_prefix = [x for x in self.head_prefix
if '{typearea}' not in x]
self.head_prefix.extend([
'\\definecolor{rrblitbackground}{rgb}{0.55, 0.3, 0.1}\n',
'\\newenvironment{rtbliteral}{\n',
'\\begin{ttfamily}\n',
'\\color{rrblitbackground}\n',
'}{\n',
'\\end{ttfamily}\n',
'}\n',
])
if self.cb_use_pygments:
self.head_prefix.extend([
'\\usepackage{fancyvrb}\n',
'\\usepackage{color}\n',
])
# set appropriate header options for theming
theme = document.settings.theme
if theme:
self.head_prefix.append('\\usetheme{%s}\n' % theme)
set_header_options(self.head_prefix, document.settings.shownotes)
if self.cb_use_pygments:
from pygments.formatters import LatexFormatter
self.head_prefix.extend([
LatexFormatter().get_style_defs(),
])
self.overlay_bullets = string_to_bool(
document.settings.overlaybullets, False)
self.fragile_default = string_to_bool(
document.settings.fragile_default,
True)
self.shortauthor = document.settings.shortauthor
self.shorttitle = document.settings.shorttitle
# using a False default because
# True is the actual default. If you are trying to pass in a value
# and I can't determine what you really meant, I am assuming you
# want something other than the actual default.
self.centerfigs = string_to_bool(
document.settings.centerfigs,
False) # same reasoning as above
self.in_columnset = False
self.in_column = False
self.in_note = False
self.frame_level = 0
# this fixes the hardcoded section titles in docutils 0.4
self.d_class = DocumentClass('article')
self.admonition_alert_type = None
def depart_document(self, node):
# Complete header with information gained from walkabout
# a) conditional requirements (before style sheet)
self.requirements = self.requirements.sortedvalues()
# b) coditional fallback definitions (after style sheet)
self.fallbacks = self.fallbacks.sortedvalues()
# c) PDF properties
self.pdfsetup.append(PreambleCmds.linking % self.hyperref_options)
if self.pdfauthor:
authors = self.author_separator.join(self.pdfauthor)
self.pdfinfo.append(' pdfauthor={%s}' % authors)
if self.pdfinfo:
self.pdfsetup += [r'\hypersetup{'] + self.pdfinfo + ['}']
# Complete body
# a) document title (part 'body_prefix')
# NOTE: Docutils puts author/date into docinfo, so normally
# we do not want LaTeX author/date handling (via \maketitle).
# To deactivate it, we add \title, \author, \date,
# even if the arguments are empty strings.
if self.title or self.author_stack or self.date:
authors = ['\\\\\n'.join(author_entry)
for author_entry in self.author_stack]
title = [''.join(self.title)] + self.title_labels
if self.shorttitle:
shorttitle = self.shorttitle
else:
shorttitle = ''.join(self.title)
if self.shortauthor:
shortauthor = self.shortauthor
else:
shortauthor = ', '.join(self.pdfauthor)
if self.subtitle:
title += [r'\\ % subtitle',
r'\large{%s}' % ''.join(self.subtitle)
] + self.subtitle_labels
docinfo_list = [shorttitle,
'%\n '.join(title),
shortauthor,
' \\and\n'.join(authors),
', '.join(self.date)]
if self.organization is None:
docinfo_str = PreambleCmds.documenttitle % tuple(docinfo_list)
else:
docinfo_list.append(self.organization)
docinfo_str = DOCINFO_W_INSTITUTE % tuple(docinfo_list)
self.body_pre_docinfo.append(docinfo_str)
# b) bibliography
# TODO insertion point of bibliography should be configurable.
if self._use_latex_citations and self._bibitems:
if not self.bibtex:
widest_label = ''
for bib_item in self._bibitems:
if len(widest_label) < len(bib_item[0]):
widest_label = bib_item[0]
self.out.append('\n\\begin{thebibliography}{%s}\n' %
widest_label)
for bib_item in self._bibitems:
# cite_key: underscores must not be escaped
cite_key = bib_item[0].replace(r'\_', '_')
self.out.append('\\bibitem[%s]{%s}{%s}\n' %
(bib_item[0], cite_key, bib_item[1]))
self.out.append('\\end{thebibliography}\n')
else:
self.out.append('\n\\bibliographystyle{%s}\n' %
self.bibtex[0])
self.out.append('\\bibliography{%s}\n' % self.bibtex[1])
# c) make sure to generate a toc file if needed for local contents:
if 'minitoc' in self.requirements and not self.has_latex_toc:
self.out.append('\n\\faketableofcontents % for local ToCs\n')
def visit_docinfo_item(self, node, name):
if name == 'author':
self.pdfauthor.append(self.attval(node.astext()))
if self.use_latex_docinfo:
if name in ('author', 'contact', 'address'):
# We attach these to the last author. If any of them precedes
# the first author, put them in a separate "author" group
# (in lack of better semantics).
if name == 'author' or not self.author_stack:
self.author_stack.append([])
if name == 'address': # newlines are meaningful
self.insert_newline = True
text = self.encode(node.astext())
self.insert_newline = False
else:
text = self.attval(node.astext())
self.author_stack[-1].append(text)
raise nodes.SkipNode
elif name == 'date':
self.date.append(self.attval(node.astext()))
raise nodes.SkipNode
elif name == 'organization':
self.organization = node.astext()
raise nodes.SkipNode
self.out.append('\\textbf{%s}: &\n\t' % self.language_label(name))
if name == 'address':
self.insert_newline = True
self.out.append('{\\raggedright\n')
self.context.append(' } \\\\\n')
else:
self.context.append(' \\\\\n')
def visit_image(self, node):
attrs = node.attributes
if 'align' not in attrs and self.centerfigs:
attrs['align'] = 'center'
if (
('height' not in attrs) and
('width' not in attrs) and
('scale' not in attrs)
):
attrs['height'] = '0.75\\textheight'
LaTeXTranslator.visit_image(self, node)
def depart_Text(self, node):
pass
def visit_section(self, node):