Show More
@@ -1,111 +1,111 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | Exporter that allows Latex Jinja templates to work. Contains logic to |
|
3 | 3 | appropriately prepare IPYNB files for export to LaTeX. Including but |
|
4 | 4 | not limited to escaping LaTeX, fixing math region tags, using special |
|
5 | 5 | tags to circumvent Jinja/Latex syntax conflicts. |
|
6 | 6 | """ |
|
7 | 7 | #----------------------------------------------------------------------------- |
|
8 | 8 | # Copyright (c) 2013, the IPython Development Team. |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the Modified BSD License. |
|
11 | 11 | # |
|
12 | 12 | # The full license is in the file COPYING.txt, distributed with this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | # Stdlib imports |
|
20 | 20 | import os |
|
21 | 21 | |
|
22 | 22 | # IPython imports |
|
23 | 23 | from IPython.utils.traitlets import Unicode |
|
24 | 24 | from IPython.config import Config |
|
25 | 25 | |
|
26 | 26 | from IPython.nbconvert import filters, transformers |
|
27 | 27 | from .exporter import Exporter |
|
28 | 28 | |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | # Classes and functions |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | class LatexExporter(Exporter): |
|
34 | 34 | """ |
|
35 | 35 | Exports to a Latex template. Inherit from this class if your template is |
|
36 | 36 | LaTeX based and you need custom tranformers/filters. Inherit from it if |
|
37 | 37 | you are writing your own HTML template and need custom tranformers/filters. |
|
38 | 38 | If you don't need custom tranformers/filters, just change the |
|
39 | 39 | 'template_file' config option. Place your template in the special "/latex" |
|
40 | 40 | subfolder of the "../templates" folder. |
|
41 | 41 | """ |
|
42 | 42 | |
|
43 | 43 | file_extension = Unicode( |
|
44 | 44 | 'tex', config=True, |
|
45 | 45 | help="Extension of the file that should be written to disk") |
|
46 | 46 | |
|
47 | 47 | template_file = Unicode( |
|
48 | 48 | 'base', config=True, |
|
49 | 49 | help="Name of the template file to use") |
|
50 | 50 | |
|
51 | 51 | #Latex constants |
|
52 | 52 | template_path = Unicode( |
|
53 | 53 | os.path.join("..", "templates", "latex"), config=True, |
|
54 | 54 | help="Path where the template files are located.") |
|
55 | 55 | |
|
56 | 56 | template_skeleton_path = Unicode( |
|
57 | 57 | os.path.join("..", "templates", "latex", "skeleton"), config=True, |
|
58 | 58 | help="Path where the template skeleton files are located.") |
|
59 | 59 | |
|
60 | 60 | #Special Jinja2 syntax that will not conflict when exporting latex. |
|
61 | 61 | jinja_comment_block_start = Unicode("((=", config=True) |
|
62 | 62 | jinja_comment_block_end = Unicode("=))", config=True) |
|
63 | 63 | jinja_variable_block_start = Unicode("(((", config=True) |
|
64 | 64 | jinja_variable_block_end = Unicode(")))", config=True) |
|
65 | 65 | jinja_logic_block_start = Unicode("((*", config=True) |
|
66 | 66 | jinja_logic_block_end = Unicode("*))", config=True) |
|
67 | 67 | |
|
68 | 68 | #Extension that the template files use. |
|
69 | 69 | template_extension = Unicode(".tplx", config=True) |
|
70 | 70 | |
|
71 | 71 | def _register_filters(self): |
|
72 | 72 | """ |
|
73 | 73 | Register all of the filters required for the exporter. |
|
74 | 74 | """ |
|
75 | 75 | |
|
76 | 76 | #Register the filters of the base class. |
|
77 | 77 | super(LatexExporter, self)._register_filters() |
|
78 | 78 | |
|
79 | 79 | #Add latex filters to the Jinja2 environment |
|
80 | 80 | self.register_filter('escape_tex', filters.escape_latex) |
|
81 | 81 | self.register_filter('highlight', filters.highlight2latex) |
|
82 | 82 | |
|
83 | 83 | |
|
84 | 84 | def _register_transformers(self): |
|
85 | 85 | """ |
|
86 | 86 | Register all of the transformers needed for this exporter. |
|
87 | 87 | """ |
|
88 | 88 | |
|
89 | 89 | #Register ConvertSvgTransformer before any other transformers! |
|
90 | 90 | #Important because it allows the conversion of svg->png BEFORE the |
|
91 | 91 | #extract figure transformer acts on the data. |
|
92 | 92 | self.register_transformer(transformers.ConvertSvgTransformer, True) |
|
93 | 93 | |
|
94 | 94 | #Register transformers |
|
95 | 95 | super(LatexExporter, self)._register_transformers() |
|
96 | 96 | self.register_transformer(transformers.LatexTransformer, True) |
|
97 | 97 | |
|
98 | 98 | @property |
|
99 | 99 | def default_config(self): |
|
100 | 100 | c = Config({ |
|
101 | 101 | 'GlobalConfigurable': { |
|
102 | 'display_data_priority' : ['latex', 'png', 'jpg', 'jpeg'] | |
|
102 | 'display_data_priority' : ['latex', 'png', 'jpg', 'jpeg', 'text'] | |
|
103 | 103 | }, |
|
104 | 104 | 'ExtractFigureTransformer': { |
|
105 | 105 | 'enabled':True |
|
106 | 106 | } |
|
107 | 107 | |
|
108 | 108 | }) |
|
109 | 109 | c.merge(super(LatexExporter,self).default_config) |
|
110 | 110 | return c |
|
111 | 111 |
General Comments 0
You need to be logged in to leave comments.
Login now