Show More
@@ -1,106 +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 | # IPython imports |
|
19 | # IPython imports | |
20 | from IPython.utils.traitlets import Unicode |
|
20 | from IPython.utils.traitlets import Unicode | |
21 | from IPython.config import Config |
|
21 | from IPython.config import Config | |
22 |
|
22 | |||
23 | # other libs/dependencies |
|
23 | # other libs/dependencies | |
24 | import nbconvert.filters.latex |
|
24 | import nbconvert.filters.latex | |
25 | import nbconvert.filters.highlight |
|
25 | import nbconvert.filters.highlight | |
26 | from nbconvert.transformers.latex import LatexTransformer |
|
26 | from nbconvert.transformers.latex import LatexTransformer | |
27 |
|
27 | |||
28 | # local import |
|
28 | # local import | |
29 | import exporter |
|
29 | import exporter | |
30 |
|
30 | |||
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 | # Classes and functions |
|
32 | # Classes and functions | |
33 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
34 |
|
34 | |||
35 | class LatexExporter(exporter.Exporter): |
|
35 | class LatexExporter(exporter.Exporter): | |
36 | """ |
|
36 | """ | |
37 | Exports to a Latex template. Inherit from this class if your template is |
|
37 | Exports to a Latex template. Inherit from this class if your template is | |
38 | LaTeX based and you need custom tranformers/filters. Inherit from it if |
|
38 | LaTeX based and you need custom tranformers/filters. Inherit from it if | |
39 | you are writing your own HTML template and need custom tranformers/filters. |
|
39 | you are writing your own HTML template and need custom tranformers/filters. | |
40 | If you don't need custom tranformers/filters, just change the |
|
40 | If you don't need custom tranformers/filters, just change the | |
41 | 'template_file' config option. Place your template in the special "/latex" |
|
41 | 'template_file' config option. Place your template in the special "/latex" | |
42 | subfolder of the "../templates" folder. |
|
42 | subfolder of the "../templates" folder. | |
43 | """ |
|
43 | """ | |
44 |
|
44 | |||
45 | file_extension = Unicode( |
|
45 | file_extension = Unicode( | |
46 | 'tex', config=True, |
|
46 | 'tex', config=True, | |
47 | help="Extension of the file that should be written to disk") |
|
47 | help="Extension of the file that should be written to disk") | |
48 |
|
48 | |||
49 | template_file = Unicode( |
|
49 | template_file = Unicode( | |
50 | 'base', config=True, |
|
50 | 'base', config=True, | |
51 | help="Name of the template file to use") |
|
51 | help="Name of the template file to use") | |
52 |
|
52 | |||
53 | #Latex constants |
|
53 | #Latex constants | |
54 | template_path = Unicode( |
|
54 | template_path = Unicode( | |
55 | "/../templates/latex/", config=True, |
|
55 | "/../templates/latex/", config=True, | |
56 | help="Path where the template files are located.") |
|
56 | help="Path where the template files are located.") | |
57 |
|
57 | |||
58 | template_skeleton_path = Unicode( |
|
58 | template_skeleton_path = Unicode( | |
59 | "/../templates/latex/skeleton/", config=True, |
|
59 | "/../templates/latex/skeleton/", config=True, | |
60 | help="Path where the template skeleton files are located.") |
|
60 | help="Path where the template skeleton files are located.") | |
61 |
|
61 | |||
62 | #Special Jinja2 syntax that will not conflict when exporting latex. |
|
62 | #Special Jinja2 syntax that will not conflict when exporting latex. | |
63 | jinja_comment_block_start = Unicode("((=", config=True) |
|
63 | jinja_comment_block_start = Unicode("((=", config=True) | |
64 | jinja_comment_block_end = Unicode("=))", config=True) |
|
64 | jinja_comment_block_end = Unicode("=))", config=True) | |
65 | jinja_variable_block_start = Unicode("(((", config=True) |
|
65 | jinja_variable_block_start = Unicode("(((", config=True) | |
66 | jinja_variable_block_end = Unicode(")))", config=True) |
|
66 | jinja_variable_block_end = Unicode(")))", config=True) | |
67 | jinja_logic_block_start = Unicode("((*", config=True) |
|
67 | jinja_logic_block_start = Unicode("((*", config=True) | |
68 | jinja_logic_block_end = Unicode("*))", config=True) |
|
68 | jinja_logic_block_end = Unicode("*))", config=True) | |
69 |
|
69 | |||
70 | #Extension that the template files use. |
|
70 | #Extension that the template files use. | |
71 | template_extension = Unicode(".tplx", config=True) |
|
71 | template_extension = Unicode(".tplx", config=True) | |
72 |
|
72 | |||
73 | def _register_filters(self): |
|
73 | def _register_filters(self): | |
74 | """ |
|
74 | """ | |
75 | Register all of the filters required for the exporter. |
|
75 | Register all of the filters required for the exporter. | |
76 | """ |
|
76 | """ | |
77 |
|
77 | |||
78 | #Register the filters of the base class. |
|
78 | #Register the filters of the base class. | |
79 | super(LatexExporter, self)._register_filters() |
|
79 | super(LatexExporter, self)._register_filters() | |
80 |
|
80 | |||
81 | #Add latex filters to the Jinja2 environment |
|
81 | #Add latex filters to the Jinja2 environment | |
82 | self.register_filter('escape_tex', nbconvert.filters.latex.escape_latex) |
|
82 | self.register_filter('escape_tex', nbconvert.filters.latex.escape_latex) | |
83 | self.register_filter('highlight', nbconvert.filters.highlight.highlight2latex) |
|
83 | self.register_filter('highlight', nbconvert.filters.highlight.highlight2latex) | |
84 |
|
84 | |||
85 |
|
85 | |||
86 | def _register_transformers(self): |
|
86 | def _register_transformers(self): | |
87 | """ |
|
87 | """ | |
88 | Register all of the transformers needed for this exporter. |
|
88 | Register all of the transformers needed for this exporter. | |
89 | """ |
|
89 | """ | |
90 |
|
90 | |||
91 | #Register the transformers of the base class. |
|
91 | #Register the transformers of the base class. | |
92 | super(LatexExporter, self)._register_transformers() |
|
92 | super(LatexExporter, self)._register_transformers() | |
93 |
|
93 | |||
94 | #Register latex transformer |
|
94 | #Register latex transformer | |
95 | self.register_transformer(LatexTransformer) |
|
95 | self.register_transformer(LatexTransformer) | |
96 |
|
96 | |||
97 | @property |
|
97 | @property | |
98 | def default_config(self): |
|
98 | def default_config(self): | |
99 | c = Config({ |
|
99 | c = Config({ | |
100 | 'display_data_priority' : ['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text'], |
|
100 | 'GlobalConfigurable': { | |
|
101 | 'display_data_priority' : ['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text'] | |||
|
102 | }, | |||
|
103 | 'ExtractFigureTransformer': { | |||
|
104 | 'enabled':True, | |||
101 | 'extra_ext_map':{'svg':'pdf'}, |
|
105 | 'extra_ext_map':{'svg':'pdf'}, | |
102 | 'ExtractFigureTransformer' : Config({'enabled':True}) |
|
106 | ||
|
107 | } | |||
103 | }) |
|
108 | }) | |
104 | c.merge(super(LatexExporter,self).default_config) |
|
109 | c.merge(super(LatexExporter,self).default_config) | |
105 | return c |
|
110 | return c | |
106 |
|
111 |
@@ -1,145 +1,145 b'' | |||||
1 | """Module containing a transformer that extracts all of the figures from the |
|
1 | """Module containing a transformer that extracts all of the figures from the | |
2 | notebook file. The extracted figures are returned in the 'resources' dictionary. |
|
2 | notebook file. The extracted figures are returned in the 'resources' dictionary. | |
3 | """ |
|
3 | """ | |
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (c) 2013, the IPython Development Team. |
|
5 | # Copyright (c) 2013, the IPython Development Team. | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the Modified BSD License. |
|
7 | # Distributed under the terms of the Modified BSD License. | |
8 | # |
|
8 | # | |
9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Imports |
|
13 | # Imports | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | import itertools |
|
15 | import itertools | |
16 |
|
16 | |||
17 | from IPython.utils.traitlets import (Dict, List, Unicode) |
|
17 | from IPython.utils.traitlets import (Dict, List, Unicode) | |
18 | from .activatable import ActivatableTransformer |
|
18 | from .activatable import ActivatableTransformer | |
19 |
|
19 | |||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | # Constants |
|
21 | # Constants | |
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 |
|
23 | |||
24 | FIGURES_KEY = "figures" |
|
24 | FIGURES_KEY = "figures" | |
25 | BINARY_KEY = "binary" |
|
25 | BINARY_KEY = "binary" | |
26 | TEXT_KEY = "text" |
|
26 | TEXT_KEY = "text" | |
27 |
|
27 | |||
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 | # Classes |
|
29 | # Classes | |
30 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
31 |
|
31 | |||
32 | class ExtractFigureTransformer(ActivatableTransformer): |
|
32 | class ExtractFigureTransformer(ActivatableTransformer): | |
33 | """ |
|
33 | """ | |
34 | Extracts all of the figures from the notebook file. The extracted |
|
34 | Extracts all of the figures from the notebook file. The extracted | |
35 | figures are returned in the 'resources' dictionary. |
|
35 | figures are returned in the 'resources' dictionary. | |
36 | """ |
|
36 | """ | |
37 |
|
37 | |||
38 | extra_extension_map = Dict({}, |
|
38 | extra_extension_map = Dict({}, | |
39 | config=True, |
|
39 | config=True, | |
40 | help="""Extra map to override extension based on type. |
|
40 | help="""Extra map to override extension based on type. | |
41 | Useful for latex where SVG will be converted to PDF before inclusion |
|
41 | Useful for latex where SVG will be converted to PDF before inclusion | |
42 | """) |
|
42 | """) | |
43 |
|
43 | |||
44 | key_format_map = Dict({}, config=True,) |
|
44 | key_format_map = Dict({}, config=True,) | |
45 | figure_name_format_map = Dict({}, config=True) |
|
45 | figure_name_format_map = Dict({}, config=True) | |
46 |
|
46 | |||
47 | display_data_priority = List(['svg', 'png', 'latex', 'jpg', 'jpeg','text']) |
|
47 | #display_data_priority = List(['svg', 'png', 'latex', 'jpg', 'jpeg','text']) | |
48 |
|
48 | |||
49 | #TODO: Change this to .format {} syntax |
|
49 | #TODO: Change this to .format {} syntax | |
50 | default_key_template = Unicode('_fig_{index:02d}.{ext}', config=True) |
|
50 | default_key_template = Unicode('_fig_{index:02d}.{ext}', config=True) | |
51 |
|
51 | |||
52 | def __init__(self, config=None, **kw): |
|
52 | def __init__(self, config=None, **kw): | |
53 | """ |
|
53 | """ | |
54 | Public constructor |
|
54 | Public constructor | |
55 |
|
55 | |||
56 | Parameters |
|
56 | Parameters | |
57 | ---------- |
|
57 | ---------- | |
58 | config : Config |
|
58 | config : Config | |
59 | Configuration file structure |
|
59 | Configuration file structure | |
60 | **kw : misc |
|
60 | **kw : misc | |
61 | Additional arguments |
|
61 | Additional arguments | |
62 | """ |
|
62 | """ | |
63 |
|
63 | |||
64 | super(ExtractFigureTransformer, self).__init__(config=config, **kw) |
|
64 | super(ExtractFigureTransformer, self).__init__(config=config, **kw) | |
65 |
|
65 | |||
66 | # A unique index for association with extracted figures |
|
66 | # A unique index for association with extracted figures | |
67 | self.index_generator = itertools.count(1) |
|
67 | self.index_generator = itertools.count(1) | |
68 |
|
68 | |||
69 | def cell_transform(self, cell, resources, index): |
|
69 | def cell_transform(self, cell, resources, index): | |
70 | """ |
|
70 | """ | |
71 | Apply a transformation on each cell, |
|
71 | Apply a transformation on each cell, | |
72 |
|
72 | |||
73 | Parameters |
|
73 | Parameters | |
74 | ---------- |
|
74 | ---------- | |
75 | cell : NotebookNode cell |
|
75 | cell : NotebookNode cell | |
76 | Notebook cell being processed |
|
76 | Notebook cell being processed | |
77 | resources : dictionary |
|
77 | resources : dictionary | |
78 | Additional resources used in the conversion process. Allows |
|
78 | Additional resources used in the conversion process. Allows | |
79 | transformers to pass variables into the Jinja engine. |
|
79 | transformers to pass variables into the Jinja engine. | |
80 | index : int |
|
80 | index : int | |
81 | Index of the cell being processed (see base.py) |
|
81 | Index of the cell being processed (see base.py) | |
82 | """ |
|
82 | """ | |
83 |
|
83 | |||
84 | if resources.get(FIGURES_KEY, None) is None : |
|
84 | if resources.get(FIGURES_KEY, None) is None : | |
85 | resources[FIGURES_KEY] = {TEXT_KEY:{},BINARY_KEY:{}} |
|
85 | resources[FIGURES_KEY] = {TEXT_KEY:{},BINARY_KEY:{}} | |
86 |
|
86 | |||
87 | for out in cell.get('outputs', []): |
|
87 | for out in cell.get('outputs', []): | |
88 | for out_type in self.display_data_priority: |
|
88 | for out_type in self.display_data_priority: | |
89 |
|
89 | |||
90 | if out.hasattr(out_type): |
|
90 | if out.hasattr(out_type): | |
91 | figname, key, data, binary = self._new_figure(out[out_type], out_type) |
|
91 | figname, key, data, binary = self._new_figure(out[out_type], out_type) | |
92 | out['key_'+out_type] = figname |
|
92 | out['key_'+out_type] = figname | |
93 |
|
93 | |||
94 | if binary : |
|
94 | if binary : | |
95 | resources[FIGURES_KEY][BINARY_KEY][key] = data |
|
95 | resources[FIGURES_KEY][BINARY_KEY][key] = data | |
96 | else : |
|
96 | else : | |
97 | resources[FIGURES_KEY][TEXT_KEY][key] = data |
|
97 | resources[FIGURES_KEY][TEXT_KEY][key] = data | |
98 |
|
98 | |||
99 | index += 1 |
|
99 | index += 1 | |
100 | return cell, resources |
|
100 | return cell, resources | |
101 |
|
101 | |||
102 |
|
102 | |||
103 | def _get_override_extension(self, extension): |
|
103 | def _get_override_extension(self, extension): | |
104 | """Gets the overriden extension if it exists, else returns extension. |
|
104 | """Gets the overriden extension if it exists, else returns extension. | |
105 |
|
105 | |||
106 | Parameters |
|
106 | Parameters | |
107 | ---------- |
|
107 | ---------- | |
108 | extension : str |
|
108 | extension : str | |
109 | File extension. |
|
109 | File extension. | |
110 | """ |
|
110 | """ | |
111 |
|
111 | |||
112 | if extension in self.extra_extension_map : |
|
112 | if extension in self.extra_extension_map : | |
113 | return self.extra_extension_map[extension] |
|
113 | return self.extra_extension_map[extension] | |
114 |
|
114 | |||
115 | return extension |
|
115 | return extension | |
116 |
|
116 | |||
117 |
|
117 | |||
118 | def _new_figure(self, data, format): |
|
118 | def _new_figure(self, data, format): | |
119 | """Create a new figure file in the given format. |
|
119 | """Create a new figure file in the given format. | |
120 |
|
120 | |||
121 | Parameters |
|
121 | Parameters | |
122 | ---------- |
|
122 | ---------- | |
123 | data : str |
|
123 | data : str | |
124 | Cell data (from Notebook node cell) |
|
124 | Cell data (from Notebook node cell) | |
125 | format : str |
|
125 | format : str | |
126 | Figure format |
|
126 | Figure format | |
127 | index : int |
|
127 | index : int | |
128 | Index of the figure being extracted |
|
128 | Index of the figure being extracted | |
129 | """ |
|
129 | """ | |
130 |
|
130 | |||
131 | figure_name_template = self.figure_name_format_map.get(format, self.default_key_template) |
|
131 | figure_name_template = self.figure_name_format_map.get(format, self.default_key_template) | |
132 | key_template = self.key_format_map.get(format, self.default_key_template) |
|
132 | key_template = self.key_format_map.get(format, self.default_key_template) | |
133 |
|
133 | |||
134 | #TODO: option to pass the hash as data? |
|
134 | #TODO: option to pass the hash as data? | |
135 | index = next(self.index_generator) |
|
135 | index = next(self.index_generator) | |
136 | figure_name = figure_name_template.format(index=index, ext=self._get_override_extension(format)) |
|
136 | figure_name = figure_name_template.format(index=index, ext=self._get_override_extension(format)) | |
137 | key = key_template.format(index=index, ext=self._get_override_extension(format)) |
|
137 | key = key_template.format(index=index, ext=self._get_override_extension(format)) | |
138 |
|
138 | |||
139 | #Binary files are base64-encoded, SVG is already XML |
|
139 | #Binary files are base64-encoded, SVG is already XML | |
140 | binary = False |
|
140 | binary = False | |
141 | if format in ('png', 'jpg', 'pdf'): |
|
141 | if format in ('png', 'jpg', 'pdf'): | |
142 | data = data.decode('base64') |
|
142 | data = data.decode('base64') | |
143 | binary = True |
|
143 | binary = True | |
144 |
|
144 | |||
145 | return figure_name, key, data, binary |
|
145 | return figure_name, key, data, binary |
General Comments 0
You need to be logged in to leave comments.
Login now