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