Show More
@@ -1,105 +1,108 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 | # Stdlib imports | |
|
20 | import os | |
|
21 | ||
|
19 | 22 | # IPython imports |
|
20 | 23 | from IPython.utils.traitlets import Unicode |
|
21 | 24 | from IPython.config import Config |
|
22 | 25 | |
|
23 | 26 | from IPython.nbconvert import filters, transformers |
|
24 | 27 | from .exporter import Exporter |
|
25 | 28 | |
|
26 | 29 | #----------------------------------------------------------------------------- |
|
27 | 30 | # Classes and functions |
|
28 | 31 | #----------------------------------------------------------------------------- |
|
29 | 32 | |
|
30 | 33 | class LatexExporter(Exporter): |
|
31 | 34 | """ |
|
32 | 35 | Exports to a Latex template. Inherit from this class if your template is |
|
33 | 36 | LaTeX based and you need custom tranformers/filters. Inherit from it if |
|
34 | 37 | you are writing your own HTML template and need custom tranformers/filters. |
|
35 | 38 | If you don't need custom tranformers/filters, just change the |
|
36 | 39 | 'template_file' config option. Place your template in the special "/latex" |
|
37 | 40 | subfolder of the "../templates" folder. |
|
38 | 41 | """ |
|
39 | 42 | |
|
40 | 43 | file_extension = Unicode( |
|
41 | 44 | 'tex', config=True, |
|
42 | 45 | help="Extension of the file that should be written to disk") |
|
43 | 46 | |
|
44 | 47 | template_file = Unicode( |
|
45 | 48 | 'base', config=True, |
|
46 | 49 | help="Name of the template file to use") |
|
47 | 50 | |
|
48 | 51 | #Latex constants |
|
49 | 52 | template_path = Unicode( |
|
50 | 53 | os.path.join("..", "templates", "latex"), config=True, |
|
51 | 54 | help="Path where the template files are located.") |
|
52 | 55 | |
|
53 | 56 | template_skeleton_path = Unicode( |
|
54 | 57 | os.path.join("..", "templates", "latex", "skeleton"), config=True, |
|
55 | 58 | help="Path where the template skeleton files are located.") |
|
56 | 59 | |
|
57 | 60 | #Special Jinja2 syntax that will not conflict when exporting latex. |
|
58 | 61 | jinja_comment_block_start = Unicode("((=", config=True) |
|
59 | 62 | jinja_comment_block_end = Unicode("=))", config=True) |
|
60 | 63 | jinja_variable_block_start = Unicode("(((", config=True) |
|
61 | 64 | jinja_variable_block_end = Unicode(")))", config=True) |
|
62 | 65 | jinja_logic_block_start = Unicode("((*", config=True) |
|
63 | 66 | jinja_logic_block_end = Unicode("*))", config=True) |
|
64 | 67 | |
|
65 | 68 | #Extension that the template files use. |
|
66 | 69 | template_extension = Unicode(".tplx", config=True) |
|
67 | 70 | |
|
68 | 71 | def _register_filters(self): |
|
69 | 72 | """ |
|
70 | 73 | Register all of the filters required for the exporter. |
|
71 | 74 | """ |
|
72 | 75 | |
|
73 | 76 | #Register the filters of the base class. |
|
74 | 77 | super(LatexExporter, self)._register_filters() |
|
75 | 78 | |
|
76 | 79 | #Add latex filters to the Jinja2 environment |
|
77 | 80 | self.register_filter('escape_tex', filters.escape_latex) |
|
78 | 81 | self.register_filter('highlight', filters.highlight2latex) |
|
79 | 82 | |
|
80 | 83 | |
|
81 | 84 | def _register_transformers(self): |
|
82 | 85 | """ |
|
83 | 86 | Register all of the transformers needed for this exporter. |
|
84 | 87 | """ |
|
85 | 88 | |
|
86 | 89 | #Register the transformers of the base class. |
|
87 | 90 | super(LatexExporter, self)._register_transformers() |
|
88 | 91 | |
|
89 | 92 | #Register latex transformer |
|
90 | 93 | self.register_transformer(transformers.LatexTransformer) |
|
91 | 94 | |
|
92 | 95 | @property |
|
93 | 96 | def default_config(self): |
|
94 | 97 | c = Config({ |
|
95 | 98 | 'GlobalConfigurable': { |
|
96 | 99 | 'display_data_priority' : ['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text'] |
|
97 | 100 | }, |
|
98 | 101 | 'ExtractFigureTransformer': { |
|
99 | 102 | 'enabled':True, |
|
100 | 103 | 'extra_ext_map':{'svg':'pdf'}, |
|
101 | 104 | } |
|
102 | 105 | }) |
|
103 | 106 | c.merge(super(LatexExporter,self).default_config) |
|
104 | 107 | return c |
|
105 | 108 |
General Comments 0
You need to be logged in to leave comments.
Login now