Show More
@@ -1,59 +1,53 b'' | |||
|
1 | 1 | """HTML Exporter class""" |
|
2 | 2 | |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2013, the IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | from IPython.utils.traitlets import Unicode, List |
|
16 | 16 | |
|
17 | 17 | from IPython.nbconvert import preprocessors |
|
18 | 18 | from IPython.config import Config |
|
19 | 19 | |
|
20 | 20 | from .templateexporter import TemplateExporter |
|
21 | 21 | |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | # Classes |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | |
|
26 | 26 | class HTMLExporter(TemplateExporter): |
|
27 | 27 | """ |
|
28 | 28 | Exports a basic HTML document. This exporter assists with the export of |
|
29 | 29 | HTML. Inherit from it if you are writing your own HTML template and need |
|
30 | 30 | custom preprocessors/filters. If you don't need custom preprocessors/ |
|
31 | 31 | filters, just change the 'template_file' config option. |
|
32 | 32 | """ |
|
33 | 33 | |
|
34 |
|
|
|
35 | 'html', config=True, | |
|
36 | help="Extension of the file that should be written to disk" | |
|
37 | ) | |
|
34 | def _file_extension_default(self): | |
|
35 | return 'html' | |
|
38 | 36 | |
|
39 | mime_type = Unicode('text/html', config=True, | |
|
40 | help="MIME type of the result file, for HTTP response headers." | |
|
41 | ) | |
|
42 | ||
|
43 | default_template = Unicode('full', config=True, help="""Flavor of the data | |
|
44 | format to use. I.E. 'full' or 'basic'""") | |
|
37 | def _template_file_default(self): | |
|
38 | return 'html_full' | |
|
45 | 39 | |
|
46 | 40 | output_mimetype = 'text/html' |
|
47 | 41 | |
|
48 | 42 | @property |
|
49 | 43 | def default_config(self): |
|
50 | 44 | c = Config({ |
|
51 | 45 | 'CSSHTMLHeaderPreprocessor':{ |
|
52 | 46 | 'enabled':True |
|
53 | 47 | }, |
|
54 | 48 | 'HighlightMagicsPreprocessor': { |
|
55 | 49 | 'enabled':True |
|
56 | 50 | } |
|
57 | 51 | }) |
|
58 | 52 | c.merge(super(HTMLExporter,self).default_config) |
|
59 | 53 | return c |
@@ -1,92 +1,95 b'' | |||
|
1 | 1 | """LaTeX Exporter class""" |
|
2 | 2 | |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2013, the IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | # Stdlib imports |
|
16 | 16 | import os |
|
17 | 17 | |
|
18 | 18 | # IPython imports |
|
19 | 19 | from IPython.utils.traitlets import Unicode, List |
|
20 | 20 | from IPython.config import Config |
|
21 | 21 | |
|
22 | 22 | from IPython.nbconvert import filters, preprocessors |
|
23 | 23 | from .templateexporter import TemplateExporter |
|
24 | 24 | |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | # Classes and functions |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | |
|
29 | 29 | class LatexExporter(TemplateExporter): |
|
30 | 30 | """ |
|
31 | 31 | Exports to a Latex template. Inherit from this class if your template is |
|
32 | 32 | LaTeX based and you need custom tranformers/filters. Inherit from it if |
|
33 | 33 | you are writing your own HTML template and need custom tranformers/filters. |
|
34 | 34 | If you don't need custom tranformers/filters, just change the |
|
35 | 35 | 'template_file' config option. Place your template in the special "/latex" |
|
36 | 36 | subfolder of the "../templates" folder. |
|
37 | 37 | """ |
|
38 | 38 | |
|
39 | def _file_extension_default(self): | |
|
40 | return 'tex' | |
|
41 | ||
|
42 | def _template_file_default(self): | |
|
43 | return 'article' | |
|
44 | ||
|
39 | 45 | file_extension = Unicode( |
|
40 | 46 | 'tex', config=True, |
|
41 | 47 | help="Extension of the file that should be written to disk") |
|
42 | 48 | |
|
43 | default_template = Unicode('article', config=True, help="""Template of the | |
|
44 | data format to use. I.E. 'article' or 'report'""") | |
|
45 | ||
|
46 | 49 | #Latex constants |
|
47 | 50 | default_template_path = Unicode( |
|
48 | 51 | os.path.join("..", "templates", "latex"), config=True, |
|
49 | 52 | help="Path where the template files are located.") |
|
50 | 53 | |
|
51 | 54 | template_skeleton_path = Unicode( |
|
52 | 55 | os.path.join("..", "templates", "latex", "skeleton"), config=True, |
|
53 | 56 | help="Path where the template skeleton files are located.") |
|
54 | 57 | |
|
55 | 58 | #Special Jinja2 syntax that will not conflict when exporting latex. |
|
56 | 59 | jinja_comment_block_start = Unicode("((=", config=True) |
|
57 | 60 | jinja_comment_block_end = Unicode("=))", config=True) |
|
58 | 61 | jinja_variable_block_start = Unicode("(((", config=True) |
|
59 | 62 | jinja_variable_block_end = Unicode(")))", config=True) |
|
60 | 63 | jinja_logic_block_start = Unicode("((*", config=True) |
|
61 | 64 | jinja_logic_block_end = Unicode("*))", config=True) |
|
62 | 65 | |
|
63 | 66 | #Extension that the template files use. |
|
64 | 67 | template_extension = Unicode(".tplx", config=True) |
|
65 | 68 | |
|
66 | 69 | output_mimetype = 'text/latex' |
|
67 | 70 | |
|
68 | 71 | |
|
69 | 72 | @property |
|
70 | 73 | def default_config(self): |
|
71 | 74 | c = Config({ |
|
72 | 75 | 'NbConvertBase': { |
|
73 | 76 | 'display_data_priority' : ['latex', 'pdf', 'png', 'jpg', 'svg', 'jpeg', 'text'] |
|
74 | 77 | }, |
|
75 | 78 | 'ExtractOutputPreprocessor': { |
|
76 | 79 | 'enabled':True |
|
77 | 80 | }, |
|
78 | 81 | 'SVG2PDFPreprocessor': { |
|
79 | 82 | 'enabled':True |
|
80 | 83 | }, |
|
81 | 84 | 'LatexPreprocessor': { |
|
82 | 85 | 'enabled':True |
|
83 | 86 | }, |
|
84 | 87 | 'SphinxPreprocessor': { |
|
85 | 88 | 'enabled':True |
|
86 | 89 | }, |
|
87 | 90 | 'HighlightMagicsPreprocessor': { |
|
88 | 91 | 'enabled':True |
|
89 | 92 | } |
|
90 | 93 | }) |
|
91 | 94 | c.merge(super(LatexExporter,self).default_config) |
|
92 | 95 | return c |
@@ -1,42 +1,44 b'' | |||
|
1 | 1 | """Markdown Exporter class""" |
|
2 | 2 | |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2013, the IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | from IPython.config import Config |
|
16 | 16 | from IPython.utils.traitlets import Unicode |
|
17 | 17 | |
|
18 | 18 | from .templateexporter import TemplateExporter |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Classes |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | |
|
24 | 24 | class MarkdownExporter(TemplateExporter): |
|
25 | 25 | """ |
|
26 | 26 | Exports to a markdown document (.md) |
|
27 | 27 | """ |
|
28 | 28 | |
|
29 |
|
|
|
30 | 'md', config=True, | |
|
31 | help="Extension of the file that should be written to disk") | |
|
29 | def _file_extension_default(self): | |
|
30 | return 'md' | |
|
31 | ||
|
32 | def _template_file_default(self): | |
|
33 | return 'markdown' | |
|
32 | 34 | |
|
33 | 35 | output_mimetype = 'text/markdown' |
|
34 | 36 | |
|
35 | 37 | def _raw_mimetypes_default(self): |
|
36 | 38 | return ['text/markdown', 'text/html', ''] |
|
37 | 39 | |
|
38 | 40 | @property |
|
39 | 41 | def default_config(self): |
|
40 | 42 | c = Config({'ExtractOutputPreprocessor':{'enabled':True}}) |
|
41 | 43 | c.merge(super(MarkdownExporter,self).default_config) |
|
42 | 44 | return c |
@@ -1,32 +1,33 b'' | |||
|
1 | 1 | """Python script Exporter class""" |
|
2 | 2 | |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2013, the IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | from IPython.utils.traitlets import Unicode |
|
16 | 16 | |
|
17 | 17 | from .templateexporter import TemplateExporter |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Classes |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | |
|
23 | 23 | class PythonExporter(TemplateExporter): |
|
24 | 24 | """ |
|
25 | 25 | Exports a Python code file. |
|
26 | 26 | """ |
|
27 | def _file_extension_default(self): | |
|
28 | return 'py' | |
|
27 | 29 | |
|
28 | file_extension = Unicode( | |
|
29 | 'py', config=True, | |
|
30 | help="Extension of the file that should be written to disk") | |
|
30 | def _template_file_default(self): | |
|
31 | return 'python' | |
|
31 | 32 | |
|
32 | 33 | output_mimetype = 'text/x-python' |
@@ -1,39 +1,41 b'' | |||
|
1 | 1 | """restructuredText Exporter class""" |
|
2 | 2 | |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2013, the IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | from IPython.utils.traitlets import Unicode |
|
16 | 16 | from IPython.config import Config |
|
17 | 17 | |
|
18 | 18 | from .templateexporter import TemplateExporter |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Classes |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | |
|
24 | 24 | class RSTExporter(TemplateExporter): |
|
25 | 25 | """ |
|
26 | 26 | Exports restructured text documents. |
|
27 | 27 | """ |
|
28 | 28 | |
|
29 |
|
|
|
30 | 'rst', config=True, | |
|
31 | help="Extension of the file that should be written to disk") | |
|
29 | def _file_extension_default(self): | |
|
30 | return 'rst' | |
|
31 | ||
|
32 | def _template_file_default(self): | |
|
33 | return 'rst' | |
|
32 | 34 | |
|
33 | 35 | output_mimetype = 'text/restructuredtext' |
|
34 | 36 | |
|
35 | 37 | @property |
|
36 | 38 | def default_config(self): |
|
37 | 39 | c = Config({'ExtractOutputPreprocessor':{'enabled':True}}) |
|
38 | 40 | c.merge(super(RSTExporter,self).default_config) |
|
39 | 41 | return c |
@@ -1,47 +1,45 b'' | |||
|
1 | 1 | """HTML slide show Exporter class""" |
|
2 | 2 | |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2013, the IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | from IPython.utils.traitlets import Unicode |
|
16 | 16 | |
|
17 | 17 | from IPython.nbconvert import preprocessors |
|
18 | 18 | from IPython.config import Config |
|
19 | 19 | |
|
20 | 20 | from .html import HTMLExporter |
|
21 | 21 | |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | # Classes |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | |
|
26 | 26 | class SlidesExporter(HTMLExporter): |
|
27 | 27 | """Exports HTML slides with reveal.js""" |
|
28 | 28 | |
|
29 |
|
|
|
30 |
'slides.html' |
|
|
31 | help="Extension of the file that should be written to disk" | |
|
32 | ) | |
|
29 | def _file_extension_default(self): | |
|
30 | return 'slides.html' | |
|
33 | 31 | |
|
34 | output_mimetype = 'text/html' | |
|
32 | def _template_file_default(self): | |
|
33 | return 'slides_reveal' | |
|
35 | 34 | |
|
36 | default_template = Unicode('reveal', config=True, help="""Template of the | |
|
37 | data format to use. I.E. 'reveal'""") | |
|
35 | output_mimetype = 'text/html' | |
|
38 | 36 | |
|
39 | 37 | @property |
|
40 | 38 | def default_config(self): |
|
41 | 39 | c = Config({ |
|
42 | 40 | 'RevealHelpPreprocessor': { |
|
43 | 41 | 'enabled': True, |
|
44 | 42 | }, |
|
45 | 43 | }) |
|
46 | 44 | c.merge(super(SlidesExporter,self).default_config) |
|
47 | 45 | return c |
@@ -1,322 +1,319 b'' | |||
|
1 | 1 | """This module defines TemplateExporter, a highly configurable converter |
|
2 | 2 | that uses Jinja2 to export notebook files into different formats. |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | # |
|
10 | 10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | from __future__ import print_function, absolute_import |
|
18 | 18 | |
|
19 | 19 | # Stdlib imports |
|
20 | 20 | import os |
|
21 | 21 | |
|
22 | 22 | # other libs/dependencies |
|
23 | 23 | from jinja2 import Environment, FileSystemLoader, ChoiceLoader, TemplateNotFound |
|
24 | 24 | |
|
25 | 25 | # IPython imports |
|
26 | 26 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List, Dict, Any |
|
27 | 27 | from IPython.utils.importstring import import_item |
|
28 | 28 | from IPython.utils import py3compat, text |
|
29 | 29 | |
|
30 | 30 | from IPython.nbformat.current import docstring_nbformat_mod |
|
31 | 31 | from IPython.nbconvert import filters |
|
32 | 32 | from .exporter import Exporter |
|
33 | 33 | |
|
34 | 34 | #----------------------------------------------------------------------------- |
|
35 | 35 | # Globals and constants |
|
36 | 36 | #----------------------------------------------------------------------------- |
|
37 | 37 | |
|
38 | 38 | #Jinja2 extensions to load. |
|
39 | 39 | JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] |
|
40 | 40 | |
|
41 | 41 | default_filters = { |
|
42 | 42 | 'indent': text.indent, |
|
43 | 43 | 'markdown2html': filters.markdown2html, |
|
44 | 44 | 'ansi2html': filters.ansi2html, |
|
45 | 45 | 'filter_data_type': filters.DataTypeFilter, |
|
46 | 46 | 'get_lines': filters.get_lines, |
|
47 | 47 | 'highlight2html': filters.Highlight2Html, |
|
48 | 48 | 'highlight2latex': filters.Highlight2Latex, |
|
49 | 49 | 'ipython2python': filters.ipython2python, |
|
50 | 50 | 'posix_path': filters.posix_path, |
|
51 | 51 | 'markdown2latex': filters.markdown2latex, |
|
52 | 52 | 'markdown2rst': filters.markdown2rst, |
|
53 | 53 | 'comment_lines': filters.comment_lines, |
|
54 | 54 | 'strip_ansi': filters.strip_ansi, |
|
55 | 55 | 'strip_dollars': filters.strip_dollars, |
|
56 | 56 | 'strip_files_prefix': filters.strip_files_prefix, |
|
57 | 57 | 'html2text' : filters.html2text, |
|
58 | 58 | 'add_anchor': filters.add_anchor, |
|
59 | 59 | 'ansi2latex': filters.ansi2latex, |
|
60 | 60 | 'wrap_text': filters.wrap_text, |
|
61 | 61 | 'escape_latex': filters.escape_latex, |
|
62 | 62 | 'citation2latex': filters.citation2latex, |
|
63 | 63 | 'path2url': filters.path2url, |
|
64 | 64 | 'add_prompts': filters.add_prompts, |
|
65 | 65 | } |
|
66 | 66 | |
|
67 | 67 | #----------------------------------------------------------------------------- |
|
68 | 68 | # Class |
|
69 | 69 | #----------------------------------------------------------------------------- |
|
70 | 70 | |
|
71 | 71 | class TemplateExporter(Exporter): |
|
72 | 72 | """ |
|
73 | 73 | Exports notebooks into other file formats. Uses Jinja 2 templating engine |
|
74 | 74 | to output new formats. Inherit from this class if you are creating a new |
|
75 | 75 | template type along with new filters/preprocessors. If the filters/ |
|
76 | 76 | preprocessors provided by default suffice, there is no need to inherit from |
|
77 | 77 | this class. Instead, override the template_file and file_extension |
|
78 | 78 | traits via a config file. |
|
79 | 79 | |
|
80 | 80 | {filters} |
|
81 | 81 | """ |
|
82 | 82 | |
|
83 | 83 | # finish the docstring |
|
84 | 84 | __doc__ = __doc__.format(filters = '- '+'\n - '.join(default_filters.keys())) |
|
85 | 85 | |
|
86 | 86 | |
|
87 | 87 | template_file = Unicode(u'default', |
|
88 | 88 | config=True, |
|
89 | 89 | help="Name of the template file to use") |
|
90 | 90 | def _template_file_changed(self, name, old, new): |
|
91 | 91 | if new == 'default': |
|
92 | 92 | self.template_file = self.default_template |
|
93 | 93 | else: |
|
94 | 94 | self.template_file = new |
|
95 | 95 | self.template = None |
|
96 | 96 | self._load_template() |
|
97 | 97 | |
|
98 | 98 | default_template = Unicode(u'') |
|
99 | 99 | template = Any() |
|
100 | 100 | environment = Any() |
|
101 | 101 | |
|
102 | 102 | template_path = List(['.'], config=True) |
|
103 | 103 | def _template_path_changed(self, name, old, new): |
|
104 | 104 | self._load_template() |
|
105 | 105 | |
|
106 | 106 | default_template_path = Unicode( |
|
107 | 107 | os.path.join("..", "templates"), |
|
108 | 108 | help="Path where the template files are located.") |
|
109 | 109 | |
|
110 | 110 | template_skeleton_path = Unicode( |
|
111 | 111 | os.path.join("..", "templates", "skeleton"), |
|
112 | 112 | help="Path where the template skeleton files are located.") |
|
113 | 113 | |
|
114 | 114 | #Jinja block definitions |
|
115 | 115 | jinja_comment_block_start = Unicode("", config=True) |
|
116 | 116 | jinja_comment_block_end = Unicode("", config=True) |
|
117 | 117 | jinja_variable_block_start = Unicode("", config=True) |
|
118 | 118 | jinja_variable_block_end = Unicode("", config=True) |
|
119 | 119 | jinja_logic_block_start = Unicode("", config=True) |
|
120 | 120 | jinja_logic_block_end = Unicode("", config=True) |
|
121 | 121 | |
|
122 | 122 | #Extension that the template files use. |
|
123 | 123 | template_extension = Unicode(".tpl", config=True) |
|
124 | 124 | |
|
125 | 125 | filters = Dict(config=True, |
|
126 | 126 | help="""Dictionary of filters, by name and namespace, to add to the Jinja |
|
127 | 127 | environment.""") |
|
128 | 128 | |
|
129 | 129 | raw_mimetypes = List(config=True, |
|
130 | 130 | help="""formats of raw cells to be included in this Exporter's output.""" |
|
131 | 131 | ) |
|
132 | 132 | def _raw_mimetypes_default(self): |
|
133 | 133 | return [self.output_mimetype, ''] |
|
134 | 134 | |
|
135 | 135 | |
|
136 | 136 | def __init__(self, config=None, extra_loaders=None, **kw): |
|
137 | 137 | """ |
|
138 | 138 | Public constructor |
|
139 | 139 | |
|
140 | 140 | Parameters |
|
141 | 141 | ---------- |
|
142 | 142 | config : config |
|
143 | 143 | User configuration instance. |
|
144 | 144 | extra_loaders : list[of Jinja Loaders] |
|
145 | 145 | ordered list of Jinja loader to find templates. Will be tried in order |
|
146 | 146 | before the default FileSystem ones. |
|
147 | 147 | template : str (optional, kw arg) |
|
148 | 148 | Template to use when exporting. |
|
149 | 149 | """ |
|
150 | 150 | super(TemplateExporter, self).__init__(config=config, **kw) |
|
151 | 151 | |
|
152 | 152 | #Init |
|
153 | 153 | self._init_template() |
|
154 | 154 | self._init_environment(extra_loaders=extra_loaders) |
|
155 | 155 | self._init_preprocessors() |
|
156 | 156 | self._init_filters() |
|
157 | 157 | |
|
158 | 158 | |
|
159 | 159 | def _load_template(self): |
|
160 | 160 | """Load the Jinja template object from the template file |
|
161 | 161 | |
|
162 | 162 | This is a no-op if the template attribute is already defined, |
|
163 | 163 | or the Jinja environment is not setup yet. |
|
164 | 164 | |
|
165 | 165 | This is triggered by various trait changes that would change the template. |
|
166 | 166 | """ |
|
167 | 167 | if self.template is not None: |
|
168 | 168 | return |
|
169 | 169 | # called too early, do nothing |
|
170 | 170 | if self.environment is None: |
|
171 | 171 | return |
|
172 | 172 | # Try different template names during conversion. First try to load the |
|
173 | 173 | # template by name with extension added, then try loading the template |
|
174 | 174 | # as if the name is explicitly specified, then try the name as a |
|
175 | 175 | # 'flavor', and lastly just try to load the template by module name. |
|
176 | module_name = self.__module__.rsplit('.', 1)[-1] | |
|
177 | 176 | try_names = [] |
|
178 | 177 | if self.template_file: |
|
179 | 178 | try_names.extend([ |
|
180 | 179 | self.template_file + self.template_extension, |
|
181 | 180 | self.template_file, |
|
182 | module_name + '_' + self.template_file + self.template_extension, | |
|
183 | 181 | ]) |
|
184 | try_names.append(module_name + self.template_extension) | |
|
185 | 182 | for try_name in try_names: |
|
186 | 183 | self.log.debug("Attempting to load template %s", try_name) |
|
187 | 184 | try: |
|
188 | 185 | self.template = self.environment.get_template(try_name) |
|
189 | 186 | except (TemplateNotFound, IOError): |
|
190 | 187 | pass |
|
191 | 188 | except Exception as e: |
|
192 | 189 | self.log.warn("Unexpected exception loading template: %s", try_name, exc_info=True) |
|
193 | 190 | else: |
|
194 | 191 | self.log.info("Loaded template %s", try_name) |
|
195 | 192 | break |
|
196 | 193 | |
|
197 | 194 | @docstring_nbformat_mod |
|
198 | 195 | def from_notebook_node(self, nb, resources=None, **kw): |
|
199 | 196 | """ |
|
200 | 197 | Convert a notebook from a notebook node instance. |
|
201 | 198 | |
|
202 | 199 | Parameters |
|
203 | 200 | ---------- |
|
204 | 201 | nb : :class:`~{nbformat_mod}.nbbase.NotebookNode` |
|
205 | 202 | Notebook node |
|
206 | 203 | resources : dict |
|
207 | 204 | Additional resources that can be accessed read/write by |
|
208 | 205 | preprocessors and filters. |
|
209 | 206 | """ |
|
210 | 207 | nb_copy, resources = super(TemplateExporter, self).from_notebook_node(nb, resources, **kw) |
|
211 | 208 | resources.setdefault('raw_mimetypes', self.raw_mimetypes) |
|
212 | 209 | |
|
213 | 210 | self._load_template() |
|
214 | 211 | |
|
215 | 212 | if self.template is not None: |
|
216 | 213 | output = self.template.render(nb=nb_copy, resources=resources) |
|
217 | 214 | else: |
|
218 | 215 | raise IOError('template file "%s" could not be found' % self.template_file) |
|
219 | 216 | return output, resources |
|
220 | 217 | |
|
221 | 218 | |
|
222 | 219 | def register_filter(self, name, jinja_filter): |
|
223 | 220 | """ |
|
224 | 221 | Register a filter. |
|
225 | 222 | A filter is a function that accepts and acts on one string. |
|
226 | 223 | The filters are accesible within the Jinja templating engine. |
|
227 | 224 | |
|
228 | 225 | Parameters |
|
229 | 226 | ---------- |
|
230 | 227 | name : str |
|
231 | 228 | name to give the filter in the Jinja engine |
|
232 | 229 | filter : filter |
|
233 | 230 | """ |
|
234 | 231 | if jinja_filter is None: |
|
235 | 232 | raise TypeError('filter') |
|
236 | 233 | isclass = isinstance(jinja_filter, type) |
|
237 | 234 | constructed = not isclass |
|
238 | 235 | |
|
239 | 236 | #Handle filter's registration based on it's type |
|
240 | 237 | if constructed and isinstance(jinja_filter, py3compat.string_types): |
|
241 | 238 | #filter is a string, import the namespace and recursively call |
|
242 | 239 | #this register_filter method |
|
243 | 240 | filter_cls = import_item(jinja_filter) |
|
244 | 241 | return self.register_filter(name, filter_cls) |
|
245 | 242 | |
|
246 | 243 | if constructed and hasattr(jinja_filter, '__call__'): |
|
247 | 244 | #filter is a function, no need to construct it. |
|
248 | 245 | self.environment.filters[name] = jinja_filter |
|
249 | 246 | return jinja_filter |
|
250 | 247 | |
|
251 | 248 | elif isclass and isinstance(jinja_filter, MetaHasTraits): |
|
252 | 249 | #filter is configurable. Make sure to pass in new default for |
|
253 | 250 | #the enabled flag if one was specified. |
|
254 | 251 | filter_instance = jinja_filter(parent=self) |
|
255 | 252 | self.register_filter(name, filter_instance ) |
|
256 | 253 | |
|
257 | 254 | elif isclass: |
|
258 | 255 | #filter is not configurable, construct it |
|
259 | 256 | filter_instance = jinja_filter() |
|
260 | 257 | self.register_filter(name, filter_instance) |
|
261 | 258 | |
|
262 | 259 | else: |
|
263 | 260 | #filter is an instance of something without a __call__ |
|
264 | 261 | #attribute. |
|
265 | 262 | raise TypeError('filter') |
|
266 | 263 | |
|
267 | 264 | |
|
268 | 265 | def _init_template(self): |
|
269 | 266 | """ |
|
270 | 267 | Make sure a template name is specified. If one isn't specified, try to |
|
271 | 268 | build one from the information we know. |
|
272 | 269 | """ |
|
273 | 270 | self._template_file_changed('template_file', self.template_file, self.template_file) |
|
274 | 271 | |
|
275 | 272 | |
|
276 | 273 | def _init_environment(self, extra_loaders=None): |
|
277 | 274 | """ |
|
278 | 275 | Create the Jinja templating environment. |
|
279 | 276 | """ |
|
280 | 277 | here = os.path.dirname(os.path.realpath(__file__)) |
|
281 | 278 | loaders = [] |
|
282 | 279 | if extra_loaders: |
|
283 | 280 | loaders.extend(extra_loaders) |
|
284 | 281 | |
|
285 | 282 | paths = self.template_path |
|
286 | 283 | paths.extend([os.path.join(here, self.default_template_path), |
|
287 | 284 | os.path.join(here, self.template_skeleton_path)]) |
|
288 | 285 | loaders.append(FileSystemLoader(paths)) |
|
289 | 286 | |
|
290 | 287 | self.environment = Environment( |
|
291 | 288 | loader= ChoiceLoader(loaders), |
|
292 | 289 | extensions=JINJA_EXTENSIONS |
|
293 | 290 | ) |
|
294 | 291 | |
|
295 | 292 | #Set special Jinja2 syntax that will not conflict with latex. |
|
296 | 293 | if self.jinja_logic_block_start: |
|
297 | 294 | self.environment.block_start_string = self.jinja_logic_block_start |
|
298 | 295 | if self.jinja_logic_block_end: |
|
299 | 296 | self.environment.block_end_string = self.jinja_logic_block_end |
|
300 | 297 | if self.jinja_variable_block_start: |
|
301 | 298 | self.environment.variable_start_string = self.jinja_variable_block_start |
|
302 | 299 | if self.jinja_variable_block_end: |
|
303 | 300 | self.environment.variable_end_string = self.jinja_variable_block_end |
|
304 | 301 | if self.jinja_comment_block_start: |
|
305 | 302 | self.environment.comment_start_string = self.jinja_comment_block_start |
|
306 | 303 | if self.jinja_comment_block_end: |
|
307 | 304 | self.environment.comment_end_string = self.jinja_comment_block_end |
|
308 | 305 | |
|
309 | 306 | |
|
310 | 307 | def _init_filters(self): |
|
311 | 308 | """ |
|
312 | 309 | Register all of the filters required for the exporter. |
|
313 | 310 | """ |
|
314 | 311 | |
|
315 | 312 | #Add default filters to the Jinja2 environment |
|
316 | 313 | for key, value in default_filters.items(): |
|
317 | 314 | self.register_filter(key, value) |
|
318 | 315 | |
|
319 | 316 | #Load user filters. Overwrite existing filters if need be. |
|
320 | 317 | if self.filters: |
|
321 | 318 | for key, user_filter in self.filters.items(): |
|
322 | 319 | self.register_filter(key, user_filter) |
|
1 | NO CONTENT: file renamed from IPython/nbconvert/templates/latex/latex_article.tplx to IPython/nbconvert/templates/latex/article.tplx |
|
1 | NO CONTENT: file renamed from IPython/nbconvert/templates/latex/latex_base.tplx to IPython/nbconvert/templates/latex/base.tplx |
|
1 | NO CONTENT: file renamed from IPython/nbconvert/templates/latex/latex_report.tplx to IPython/nbconvert/templates/latex/report.tplx |
@@ -1,41 +1,41 b'' | |||
|
1 | 1 | ((= Black&white ipython input/output style =)) |
|
2 | 2 | |
|
3 |
((*- extends ' |
|
|
3 | ((*- extends 'base.tplx' -*)) | |
|
4 | 4 | |
|
5 | 5 | %=============================================================================== |
|
6 | 6 | % Input |
|
7 | 7 | %=============================================================================== |
|
8 | 8 | |
|
9 | 9 | ((* block input scoped *)) |
|
10 | 10 | ((( add_prompt(cell.input, cell, 'In ') ))) |
|
11 | 11 | ((* endblock input *)) |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | %=============================================================================== |
|
15 | 15 | % Output |
|
16 | 16 | %=============================================================================== |
|
17 | 17 | |
|
18 | 18 | ((* block pyout scoped *)) |
|
19 | 19 | ((*- for type in output | filter_data_type -*)) |
|
20 | 20 | ((*- if type in ['text']*)) |
|
21 | 21 | ((( add_prompt(output.text, cell, 'Out') ))) |
|
22 | 22 | ((*- else -*)) |
|
23 | 23 | \verb+Out[((( cell.prompt_number )))]:+((( super() ))) |
|
24 | 24 | ((*- endif -*)) |
|
25 | 25 | ((*- endfor -*)) |
|
26 | 26 | ((* endblock pyout *)) |
|
27 | 27 | |
|
28 | 28 | |
|
29 | 29 | %============================================================================== |
|
30 | 30 | % Support Macros |
|
31 | 31 | %============================================================================== |
|
32 | 32 | |
|
33 | 33 | % Name: draw_prompt |
|
34 | 34 | % Purpose: Renders an output/input prompt |
|
35 | 35 | ((* macro add_prompt(text, cell, prompt) -*)) |
|
36 | 36 | ((*- set prompt_number = "" ~ cell.prompt_number -*)) |
|
37 | 37 | ((*- set indentation = " " * (prompt_number | length + 7) -*)) |
|
38 | 38 | \begin{verbatim} |
|
39 | 39 | (((- text | add_prompts(first=prompt ~ '[' ~ prompt_number ~ ']: ', cont=indentation) -))) |
|
40 | 40 | \end{verbatim} |
|
41 | 41 | ((*- endmacro *)) |
@@ -1,13 +1,13 b'' | |||
|
1 | 1 | ((= Black&white Python input/output style =)) |
|
2 | 2 | |
|
3 |
((*- extends ' |
|
|
3 | ((*- extends 'base.tplx' -*)) | |
|
4 | 4 | |
|
5 | 5 | %=============================================================================== |
|
6 | 6 | % Input |
|
7 | 7 | %=============================================================================== |
|
8 | 8 | |
|
9 | 9 | ((* block input scoped *)) |
|
10 | 10 | \begin{verbatim} |
|
11 | 11 | ((( cell.input | add_prompts ))) |
|
12 | 12 | \end{verbatim} |
|
13 | 13 | ((* endblock input *)) |
@@ -1,54 +1,54 b'' | |||
|
1 | 1 | ((= IPython input/output style =)) |
|
2 | 2 | |
|
3 |
((*- extends ' |
|
|
3 | ((*- extends 'base.tplx' -*)) | |
|
4 | 4 | |
|
5 | 5 | % Custom definitions |
|
6 | 6 | ((* block definitions *)) |
|
7 | 7 | ((( super() ))) |
|
8 | 8 | |
|
9 | 9 | % Pygments definitions |
|
10 | 10 | ((( resources.latex.pygments_definitions ))) |
|
11 | 11 | |
|
12 | 12 | % Exact colors from NB |
|
13 | 13 | \definecolor{incolor}{rgb}{0.0, 0.0, 0.5} |
|
14 | 14 | \definecolor{outcolor}{rgb}{0.545, 0.0, 0.0} |
|
15 | 15 | |
|
16 | 16 | ((* endblock definitions *)) |
|
17 | 17 | |
|
18 | 18 | %=============================================================================== |
|
19 | 19 | % Input |
|
20 | 20 | %=============================================================================== |
|
21 | 21 | |
|
22 | 22 | ((* block input scoped *)) |
|
23 | 23 | ((( add_prompt(cell.input | highlight2latex(strip_verbatim=True), cell, 'In ', 'incolor') ))) |
|
24 | 24 | ((* endblock input *)) |
|
25 | 25 | |
|
26 | 26 | |
|
27 | 27 | %=============================================================================== |
|
28 | 28 | % Output |
|
29 | 29 | %=============================================================================== |
|
30 | 30 | |
|
31 | 31 | ((* block pyout scoped *)) |
|
32 | 32 | ((*- for type in output | filter_data_type -*)) |
|
33 | 33 | ((*- if type in ['text']*)) |
|
34 | 34 | ((( add_prompt(output.text | escape_latex, cell, 'Out', 'outcolor') ))) |
|
35 | 35 | ((* else -*)) |
|
36 | 36 | \texttt{\color{outcolor}Out[{\color{outcolor}((( cell.prompt_number )))}]:}((( super() ))) |
|
37 | 37 | ((*- endif -*)) |
|
38 | 38 | ((*- endfor -*)) |
|
39 | 39 | ((* endblock pyout *)) |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | %============================================================================== |
|
43 | 43 | % Support Macros |
|
44 | 44 | %============================================================================== |
|
45 | 45 | |
|
46 | 46 | % Name: draw_prompt |
|
47 | 47 | % Purpose: Renders an output/input prompt |
|
48 | 48 | ((* macro add_prompt(text, cell, prompt, prompt_color) -*)) |
|
49 | 49 | ((*- set prompt_number = "" ~ cell.prompt_number -*)) |
|
50 | 50 | ((*- set indention = " " * (prompt_number | length + 7) -*)) |
|
51 | 51 | \begin{Verbatim}[commandchars=\\\{\}] |
|
52 | 52 | ((( text | add_prompts(first='{\color{' ~ prompt_color ~ '}' ~ prompt ~ '[{\\color{' ~ prompt_color ~ '}' ~ prompt_number ~ '}]:} ', cont=indention) ))) |
|
53 | 53 | \end{Verbatim} |
|
54 | 54 | ((*- endmacro *)) |
@@ -1,21 +1,21 b'' | |||
|
1 | 1 | ((= Python input/output style =)) |
|
2 | 2 | |
|
3 |
((*- extends ' |
|
|
3 | ((*- extends 'base.tplx' -*)) | |
|
4 | 4 | |
|
5 | 5 | % Custom definitions |
|
6 | 6 | ((* block definitions *)) |
|
7 | 7 | ((( super() ))) |
|
8 | 8 | |
|
9 | 9 | % Pygments definitions |
|
10 | 10 | ((( resources.latex.pygments_definitions ))) |
|
11 | 11 | ((* endblock definitions *)) |
|
12 | 12 | |
|
13 | 13 | %=============================================================================== |
|
14 | 14 | % Input |
|
15 | 15 | %=============================================================================== |
|
16 | 16 | |
|
17 | 17 | ((* block input scoped *)) |
|
18 | 18 | \begin{Verbatim}[commandchars=\\\{\}] |
|
19 | 19 | ((( cell.input | highlight2latex(strip_verbatim=True) | add_prompts ))) |
|
20 | 20 | \end{Verbatim} |
|
21 | 21 | ((* endblock input *)) |
General Comments 0
You need to be logged in to leave comments.
Login now