Show More
@@ -1,127 +1,140 b'' | |||||
1 | """Latex exporter for the notebook conversion pipeline. |
|
1 | """Latex exporter for the notebook conversion pipeline. | |
2 |
|
2 | |||
3 | This module defines Exporter, a highly configurable converter |
|
3 | This module defines Exporter, a highly configurable converter | |
4 | that uses Jinja2 to export notebook files into different format. |
|
4 | that uses Jinja2 to export notebook files into different format. | |
5 |
|
5 | |||
6 | You can register both pre-transformers that will act on the notebook format |
|
6 | You can register both pre-transformers that will act on the notebook format | |
7 | before conversion and jinja filter that would then be available in the templates |
|
7 | before conversion and jinja filter that would then be available in the templates | |
8 | """ |
|
8 | """ | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Copyright (c) 2013, the IPython Development Team. |
|
11 | # Copyright (c) 2013, the IPython Development Team. | |
12 | # |
|
12 | # | |
13 | # Distributed under the terms of the Modified BSD License. |
|
13 | # Distributed under the terms of the Modified BSD License. | |
14 | # |
|
14 | # | |
15 | # The full license is in the file COPYING.txt, distributed with this software. |
|
15 | # The full license is in the file COPYING.txt, distributed with this software. | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Imports |
|
19 | # Imports | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
|
21 | ||||
|
22 | # Stdlib imports | |||
|
23 | import io | |||
|
24 | import os | |||
|
25 | ||||
|
26 | # IPython imports | |||
|
27 | from IPython.config.configurable import Configurable | |||
|
28 | from IPython.nbformat import current as nbformat | |||
|
29 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List, Bool | |||
|
30 | from IPython.utils.text import indent | |||
|
31 | ||||
|
32 | # other libs/dependencies | |||
|
33 | from jinja2 import Environment, FileSystemLoader | |||
|
34 | from markdown import markdown | |||
|
35 | ||||
21 | import base.Exporter as Exporter |
|
36 | import base.Exporter as Exporter | |
|
37 | import filters.latex | |||
|
38 | import filters.pygments | |||
22 |
|
39 | |||
23 | #Try to import the Sphinx exporter. If the user doesn't have Sphinx isntalled |
|
40 | #Try to import the Sphinx exporter. If the user doesn't have Sphinx isntalled | |
24 | #on his/her machine, fail silently. |
|
41 | #on his/her machine, fail silently. | |
25 | try: |
|
42 | try: | |
26 | from .sphinx_transformer import (SphinxTransformer) #TODO |
|
43 | from .sphinx_transformer import (SphinxTransformer) #TODO | |
27 | except ImportError: |
|
44 | except ImportError: | |
28 | SphinxTransformer = None |
|
45 | SphinxTransformer = None | |
29 |
|
46 | |||
30 | #----------------------------------------------------------------------------- |
|
47 | #----------------------------------------------------------------------------- | |
31 | # Globals and constants |
|
48 | # Globals and constants | |
32 | #----------------------------------------------------------------------------- |
|
49 | #----------------------------------------------------------------------------- | |
33 |
|
50 | |||
34 | #Latex Jinja2 constants |
|
51 | #Latex Jinja2 constants | |
35 | LATEX_TEMPLATE_PATH = "/../templates/tex/" |
|
52 | LATEX_TEMPLATE_PATH = "/../templates/tex/" | |
36 | LATEX_TEMPLATE_SKELETON_PATH = "/../templates/tex/skeleton/" |
|
53 | LATEX_TEMPLATE_SKELETON_PATH = "/../templates/tex/skeleton/" | |
37 | LATEX_TEMPLATE_EXTENSION = ".tplx" |
|
54 | LATEX_TEMPLATE_EXTENSION = ".tplx" | |
38 |
|
55 | |||
39 | #Special Jinja2 syntax that will not conflict when exporting latex. |
|
56 | #Special Jinja2 syntax that will not conflict when exporting latex. | |
40 | LATEX_JINJA_COMMENT_BLOCK = ["((=", "=))"] |
|
57 | LATEX_JINJA_COMMENT_BLOCK = ["((=", "=))"] | |
41 | LATEX_JINJA_VARIABLE_BLOCK = ["(((", ")))"] |
|
58 | LATEX_JINJA_VARIABLE_BLOCK = ["(((", ")))"] | |
42 | LATEX_JINJA_LOGIC_BLOCK = ["((*", "*))"] |
|
59 | LATEX_JINJA_LOGIC_BLOCK = ["((*", "*))"] | |
43 |
|
60 | |||
44 | #----------------------------------------------------------------------------- |
|
61 | #----------------------------------------------------------------------------- | |
45 | # Classes and functions |
|
62 | # Classes and functions | |
46 | #----------------------------------------------------------------------------- |
|
63 | #----------------------------------------------------------------------------- | |
47 | class LatexExporter(Exporter): |
|
64 | class LatexExporter(Exporter): | |
48 | """ A Jinja2 base converter templates |
|
65 | """ A Jinja2 base converter templates | |
49 |
|
66 | |||
50 | Preprocess the ipynb files, feed it throug jinja templates, |
|
67 | Preprocess the ipynb files, feed it throug jinja templates, | |
51 | and spit an converted files and a data object with other data |
|
68 | and spit an converted files and a data object with other data | |
52 | should be mostly configurable |
|
69 | should be mostly configurable | |
53 | """ |
|
70 | """ | |
54 |
|
71 | |||
55 | #Processors that process the input data prior to the export, set in the |
|
72 | #Processors that process the input data prior to the export, set in the | |
56 | #constructor for this class. |
|
73 | #constructor for this class. | |
57 | preprocessors = [] |
|
74 | preprocessors = [] | |
58 |
|
75 | |||
59 | def __init__(self, preprocessors={}, jinja_filters={}, config=None, export_format, **kw): |
|
76 | def __init__(self, preprocessors={}, jinja_filters={}, config=None, export_format, **kw): | |
60 | """ Init a new converter. |
|
77 | """ Init a new converter. | |
61 |
|
78 | |||
62 | config: the Configurable config object to pass around. |
|
79 | config: the Configurable config object to pass around. | |
63 |
|
80 | |||
64 | preprocessors: dict of **available** key/value function to run on |
|
81 | preprocessors: dict of **available** key/value function to run on | |
65 | ipynb json data before conversion to extract/inline file. |
|
82 | ipynb json data before conversion to extract/inline file. | |
66 | See `transformer.py` and `ConfigurableTransformers` |
|
83 | See `transformer.py` and `ConfigurableTransformers` | |
67 |
|
84 | |||
68 | set the order in which the transformers should apply |
|
85 | set the order in which the transformers should apply | |
69 | with the `pre_transformer_order` trait of this class |
|
86 | with the `pre_transformer_order` trait of this class | |
70 |
|
87 | |||
71 | transformers registerd by this key will take precedence on |
|
88 | transformers registerd by this key will take precedence on | |
72 | default one. |
|
89 | default one. | |
73 |
|
90 | |||
74 | jinja_filters: dict of supplementary jinja filter that should be made |
|
91 | jinja_filters: dict of supplementary jinja filter that should be made | |
75 | available in template. If those are of Configurable Class type, |
|
92 | available in template. If those are of Configurable Class type, | |
76 | they will be instanciated with the config object as argument. |
|
93 | they will be instanciated with the config object as argument. | |
77 |
|
94 | |||
78 | user defined filter will overwrite the one available by default. |
|
95 | user defined filter will overwrite the one available by default. | |
79 | """ |
|
96 | """ | |
80 |
|
97 | |||
81 | #Call the base class constructor |
|
98 | #Call the base class constructor | |
82 | super(Exporter, self).__init__(config=config, **kw) |
|
99 | super(Exporter, self).__init__(config=config, **kw) | |
83 |
|
100 | |||
84 | #For compatibility, TODO: remove later. |
|
101 | #For compatibility, TODO: remove later. | |
85 | self.preprocessors.append(trans.coalesce_streams) |
|
|||
86 | self.preprocessors.append(trans.ExtractFigureTransformer(config=config)) |
|
|||
87 | self.preprocessors.append(trans.RevealHelpTransformer(config=config)) |
|
|||
88 | self.preprocessors.append(trans.CSSHtmlHeaderTransformer(config=config)) |
|
|||
89 | self.preprocessors.append(LatexTransformer(config=config)) |
|
102 | self.preprocessors.append(LatexTransformer(config=config)) | |
90 |
|
103 | |||
91 | #Only load the sphinx transformer if the file reference worked |
|
104 | #Only load the sphinx transformer if the file reference worked | |
92 | #(Sphinx dependencies exist on the user's machine.) |
|
105 | #(Sphinx dependencies exist on the user's machine.) | |
93 | if SphinxTransformer: |
|
106 | if SphinxTransformer: | |
94 | self.preprocessors.append(SphinxTransformer(config=config)) |
|
107 | self.preprocessors.append(SphinxTransformer(config=config)) | |
95 |
|
108 | |||
96 | #Add filters to the Jinja2 environment |
|
109 | #Add filters to the Jinja2 environment | |
97 |
self. |
|
110 | self.register_filter('escape_tex', filters.latex.escape_tex) | |
98 |
self. |
|
111 | self.register_filter('highlight', filters.pygments.highlight2latex) | |
99 |
|
112 | |||
100 | #Load user filters. Overwrite existing filters if need be. |
|
113 | #Load user filters. Overwrite existing filters if need be. | |
101 | for key, user_filter in jinja_filters.iteritems(): |
|
114 | for key, user_filter in jinja_filters.iteritems(): | |
102 | if isinstance(user_filter, MetaHasTraits): |
|
115 | if isinstance(user_filter, MetaHasTraits): | |
103 | self.env.filters[key] = user_filter(config=config) |
|
116 | self.env.filters[key] = user_filter(config=config) | |
104 | else: |
|
117 | else: | |
105 | self.env.filters[key] = user_filter |
|
118 | self.env.filters[key] = user_filter | |
106 |
|
119 | |||
107 | #Load the template file. |
|
120 | #Load the template file. | |
108 | self.template = self.env.get_template(self.template_file+self.ext) |
|
121 | self.template = self.env.get_template(self.template_file+self.ext) | |
109 |
|
122 | |||
110 |
|
123 | |||
111 | def _init_environment(self): |
|
124 | def _init_environment(self): | |
112 | self.ext = LATEX_TEMPLATE_EXTENSION |
|
125 | self.ext = LATEX_TEMPLATE_EXTENSION | |
113 | self.env = Environment( |
|
126 | self.env = Environment( | |
114 | loader=FileSystemLoader([ |
|
127 | loader=FileSystemLoader([ | |
115 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_PATH, |
|
128 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_PATH, | |
116 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_SKELETON_PATH, |
|
129 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_SKELETON_PATH, | |
117 | ]), |
|
130 | ]), | |
118 | extensions=JINJA_EXTENSIONS |
|
131 | extensions=JINJA_EXTENSIONS | |
119 | ) |
|
132 | ) | |
120 |
|
133 | |||
121 | #Set special Jinja2 syntax that will not conflict with latex. |
|
134 | #Set special Jinja2 syntax that will not conflict with latex. | |
122 | self.env.block_start_string = LATEX_JINJA_LOGIC_BLOCK[0] |
|
135 | self.env.block_start_string = LATEX_JINJA_LOGIC_BLOCK[0] | |
123 | self.env.block_end_string = LATEX_JINJA_LOGIC_BLOCK[1] |
|
136 | self.env.block_end_string = LATEX_JINJA_LOGIC_BLOCK[1] | |
124 | self.env.variable_start_string = LATEX_JINJA_VARIABLE_BLOCK[0] |
|
137 | self.env.variable_start_string = LATEX_JINJA_VARIABLE_BLOCK[0] | |
125 | self.env.variable_end_string = LATEX_JINJA_VARIABLE_BLOCK[1] |
|
138 | self.env.variable_end_string = LATEX_JINJA_VARIABLE_BLOCK[1] | |
126 | self.env.comment_start_string = LATEX_JINJA_COMMENT_BLOCK[0] |
|
139 | self.env.comment_start_string = LATEX_JINJA_COMMENT_BLOCK[0] | |
127 | self.env.comment_end_string = LATEX_JINJA_COMMENT_BLOCK[1] No newline at end of file |
|
140 | self.env.comment_end_string = LATEX_JINJA_COMMENT_BLOCK[1] |
General Comments 0
You need to be logged in to leave comments.
Login now