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