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