##// END OF EJS Templates
Merge pull request #3784 from jdfreder/export_flavors...
Jonathan Frederic -
r11769:a9c0f491 merge
parent child Browse files
Show More
@@ -0,0 +1,2 b''
1 from .base import PostProcessorBase
2 from .pdf import PDFPostProcessor
@@ -0,0 +1,51 b''
1 """
2 Contains writer for writing nbconvert output to PDF.
3 """
4 #-----------------------------------------------------------------------------
5 #Copyright (c) 2013, the IPython Development Team.
6 #
7 #Distributed under the terms of the Modified BSD License.
8 #
9 #The full license is in the file COPYING.txt, distributed with this software.
10 #-----------------------------------------------------------------------------
11
12 #-----------------------------------------------------------------------------
13 # Imports
14 #-----------------------------------------------------------------------------
15
16 import subprocess
17 import os
18
19 from IPython.utils.traitlets import Integer, Unicode, Bool
20
21 from .base import PostProcessorBase
22
23 #-----------------------------------------------------------------------------
24 # Classes
25 #-----------------------------------------------------------------------------
26 class PDFPostProcessor(PostProcessorBase):
27 """Writer designed to write to PDF files"""
28
29 iteration_count = Integer(3, config=True, help="""
30 How many times pdflatex will be called.
31 """)
32
33 compiler = Unicode(u'pdflatex {0}', config=True, help="""
34 Shell command used to compile PDF.""")
35
36 verbose = Bool(False, config=True, help="""
37 Whether or not to display the output of the compile call.
38 """)
39
40 def call(self, input):
41 """
42 Consume and write Jinja output a PDF.
43 See files.py for more...
44 """
45 command = self.compiler.format(input)
46 for index in range(self.iteration_count):
47 if self.verbose:
48 subprocess.Popen(command, shell=True)
49 else:
50 with open(os.devnull, 'wb') as null:
51 subprocess.Popen(command, shell=True, stdout=null)
@@ -1,6 +1,7 b''
1 """Utilities for converting notebooks to and from different formats."""
1 """Utilities for converting notebooks to and from different formats."""
2
2
3 from .exporters import *
3 from .exporters import *
4 import filters
4 import filters
5 import transformers
5 import transformers
6 import post_processors
6 import writers
7 import writers
@@ -1,11 +1,8 b''
1 from .basichtml import BasicHTMLExporter
2 from .export import *
1 from .export import *
2 from .html import HTMLExporter
3 from .slides import SlidesExporter
3 from .exporter import Exporter
4 from .exporter import Exporter
4 from .fullhtml import FullHTMLExporter
5 from .reveal import RevealExporter
6 from .latex import LatexExporter
5 from .latex import LatexExporter
7 from .markdown import MarkdownExporter
6 from .markdown import MarkdownExporter
8 from .python import PythonExporter
7 from .python import PythonExporter
9 from .rst import RSTExporter
8 from .rst import RSTExporter
10 from .sphinx_howto import SphinxHowtoExporter
11 from .sphinx_manual import SphinxManualExporter
@@ -1,226 +1,211 b''
1 """
1 """
2 Module containing single call export functions.
2 Module containing single call export functions.
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
15
16 from functools import wraps
16 from functools import wraps
17
17
18 from IPython.nbformat.v3.nbbase import NotebookNode
18 from IPython.nbformat.v3.nbbase import NotebookNode
19 from IPython.config import Config
19 from IPython.config import Config
20
20
21 from .exporter import Exporter
21 from .exporter import Exporter
22 from .basichtml import BasicHTMLExporter
22 from .html import HTMLExporter
23 from .fullhtml import FullHTMLExporter
23 from .slides import SlidesExporter
24 from .latex import LatexExporter
24 from .latex import LatexExporter
25 from .markdown import MarkdownExporter
25 from .markdown import MarkdownExporter
26 from .python import PythonExporter
26 from .python import PythonExporter
27 from .reveal import RevealExporter
28 from .rst import RSTExporter
27 from .rst import RSTExporter
29 from .sphinx_howto import SphinxHowtoExporter
30 from .sphinx_manual import SphinxManualExporter
31
28
32 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
33 # Classes
30 # Classes
34 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
35
32
36 def DocDecorator(f):
33 def DocDecorator(f):
37
34
38 #Set docstring of function
35 #Set docstring of function
39 f.__doc__ = f.__doc__ + """
36 f.__doc__ = f.__doc__ + """
40 nb : Notebook node
37 nb : Notebook node
41 config : config (optional, keyword arg)
38 config : config (optional, keyword arg)
42 User configuration instance.
39 User configuration instance.
43 resources : dict (optional, keyword arg)
40 resources : dict (optional, keyword arg)
44 Resources used in the conversion process.
41 Resources used in the conversion process.
45
42
46 Returns
43 Returns
47 ----------
44 ----------
48 tuple- output, resources, exporter_instance
45 tuple- output, resources, exporter_instance
49 output : str
46 output : str
50 Jinja 2 output. This is the resulting converted notebook.
47 Jinja 2 output. This is the resulting converted notebook.
51 resources : dictionary
48 resources : dictionary
52 Dictionary of resources used prior to and during the conversion
49 Dictionary of resources used prior to and during the conversion
53 process.
50 process.
54 exporter_instance : Exporter
51 exporter_instance : Exporter
55 Instance of the Exporter class used to export the document. Useful
52 Instance of the Exporter class used to export the document. Useful
56 to caller because it provides a 'file_extension' property which
53 to caller because it provides a 'file_extension' property which
57 specifies what extension the output should be saved as."""
54 specifies what extension the output should be saved as.
55
56 WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT
57 """
58
58
59 @wraps(f)
59 @wraps(f)
60 def decorator(*args, **kwargs):
60 def decorator(*args, **kwargs):
61 return f(*args, **kwargs)
61 return f(*args, **kwargs)
62
62
63 return decorator
63 return decorator
64
64
65
65
66 #-----------------------------------------------------------------------------
66 #-----------------------------------------------------------------------------
67 # Functions
67 # Functions
68 #-----------------------------------------------------------------------------
68 #-----------------------------------------------------------------------------
69
69
70 __all__ = [
70 __all__ = [
71 'export',
71 'export',
72 'export_sphinx_manual',
72 'export_html',
73 'export_sphinx_howto',
73 'export_custom',
74 'export_basic_html',
74 'export_slides',
75 'export_full_html',
76 'export_latex',
75 'export_latex',
77 'export_markdown',
76 'export_markdown',
78 'export_python',
77 'export_python',
79 'export_reveal',
80 'export_rst',
78 'export_rst',
81 'export_by_name',
79 'export_by_name',
82 'get_export_names',
80 'get_export_names',
83 'ExporterNameError'
81 'ExporterNameError'
84 ]
82 ]
85
83
86
84
87 class ExporterNameError(NameError):
85 class ExporterNameError(NameError):
88 pass
86 pass
89
87
90
88
91 @DocDecorator
89 @DocDecorator
92 def export(exporter, nb, **kw):
90 def export(exporter, nb, **kw):
93 """
91 """
94 Export a notebook object using specific exporter class.
92 Export a notebook object using specific exporter class.
95
93
96 exporter : Exporter class type or instance
94 exporter : Exporter class type or instance
97 Class type or instance of the exporter that should be used. If the
95 Class type or instance of the exporter that should be used. If the
98 method initializes it's own instance of the class, it is ASSUMED that
96 method initializes it's own instance of the class, it is ASSUMED that
99 the class type provided exposes a constructor (__init__) with the same
97 the class type provided exposes a constructor (__init__) with the same
100 signature as the base Exporter class.
98 signature as the base Exporter class.
101 """
99 """
102
100
103 #Check arguments
101 #Check arguments
104 if exporter is None:
102 if exporter is None:
105 raise TypeError("Exporter is None")
103 raise TypeError("Exporter is None")
106 elif not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter):
104 elif not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter):
107 raise TypeError("exporter does not inherit from Exporter (base)")
105 raise TypeError("exporter does not inherit from Exporter (base)")
108 if nb is None:
106 if nb is None:
109 raise TypeError("nb is None")
107 raise TypeError("nb is None")
110
108
111 #Create the exporter
109 #Create the exporter
112 resources = kw.pop('resources', None)
110 resources = kw.pop('resources', None)
113 if isinstance(exporter, Exporter):
111 if isinstance(exporter, Exporter):
114 exporter_instance = exporter
112 exporter_instance = exporter
115 else:
113 else:
116 exporter_instance = exporter(**kw)
114 exporter_instance = exporter(**kw)
117
115
118 #Try to convert the notebook using the appropriate conversion function.
116 #Try to convert the notebook using the appropriate conversion function.
119 if isinstance(nb, NotebookNode):
117 if isinstance(nb, NotebookNode):
120 output, resources = exporter_instance.from_notebook_node(nb, resources)
118 output, resources = exporter_instance.from_notebook_node(nb, resources)
121 elif isinstance(nb, basestring):
119 elif isinstance(nb, basestring):
122 output, resources = exporter_instance.from_filename(nb, resources)
120 output, resources = exporter_instance.from_filename(nb, resources)
123 else:
121 else:
124 output, resources = exporter_instance.from_file(nb, resources)
122 output, resources = exporter_instance.from_file(nb, resources)
125 return output, resources
123 return output, resources
126
124
127
125
128 @DocDecorator
126 @DocDecorator
129 def export_sphinx_manual(nb, **kw):
127 def export_custom(nb, **kw):
130 """
131 Export a notebook object to Sphinx Manual LaTeX
132 """
133 return export(SphinxManualExporter, nb, **kw)
134
135
136 @DocDecorator
137 def export_sphinx_howto(nb, **kw):
138 """
128 """
139 Export a notebook object to Sphinx HowTo LaTeX
129 Export a notebook object to a custom format
140 """
130 """
141 return export(SphinxHowtoExporter, nb, **kw)
131 return export(Exporter, nb, **kw)
142
132
143
133
144 @DocDecorator
134 @DocDecorator
145 def export_basic_html(nb, **kw):
135 def export_html(nb, **kw):
146 """
136 """
147 Export a notebook object to Basic HTML
137 Export a notebook object to HTML
148 """
138 """
149 return export(BasicHTMLExporter, nb, **kw)
139 return export(HTMLExporter, nb, **kw)
150
140
151
141
152 @DocDecorator
142 @DocDecorator
153 def export_full_html(nb, **kw):
143 def export_slides(nb, **kw):
154 """
144 """
155 Export a notebook object to Full HTML
145 Export a notebook object to Slides
156 """
146 """
157 return export(FullHTMLExporter, nb, **kw)
147 return export(SlidesExporter, nb, **kw)
158
148
159
149
160 @DocDecorator
150 @DocDecorator
161 def export_latex(nb, **kw):
151 def export_latex(nb, **kw):
162 """
152 """
163 Export a notebook object to LaTeX
153 Export a notebook object to LaTeX
164 """
154 """
165 return export(LatexExporter, nb, **kw)
155 return export(LatexExporter, nb, **kw)
166
156
167
157
168 @DocDecorator
158 @DocDecorator
169 def export_markdown(nb, **kw):
159 def export_markdown(nb, **kw):
170 """
160 """
171 Export a notebook object to Markdown
161 Export a notebook object to Markdown
172 """
162 """
173 return export(MarkdownExporter, nb, **kw)
163 return export(MarkdownExporter, nb, **kw)
174
164
175
165
176 @DocDecorator
166 @DocDecorator
177 def export_python(nb, **kw):
167 def export_python(nb, **kw):
178 """
168 """
179 Export a notebook object to Python
169 Export a notebook object to Python
180 """
170 """
181 return export(PythonExporter, nb, **kw)
171 return export(PythonExporter, nb, **kw)
182
172
183
173
184 @DocDecorator
174 @DocDecorator
185 def export_reveal(nb, **kw):
186 """
187 Export a notebook object to a Reveal.js presentation
188 """
189 return export(RevealExporter, nb, **kw)
190
191
192 @DocDecorator
193 def export_rst(nb, **kw):
175 def export_rst(nb, **kw):
194 """
176 """
195 Export a notebook object to reStructuredText
177 Export a notebook object to reStructuredText
196 """
178 """
197 return export(RSTExporter, nb, **kw)
179 return export(RSTExporter, nb, **kw)
198
180
199
181
200 @DocDecorator
182 @DocDecorator
201 def export_by_name(format_name, nb, **kw):
183 def export_by_name(format_name, nb, **kw):
202 """
184 """
203 Export a notebook object to a template type by its name. Reflection
185 Export a notebook object to a template type by its name. Reflection
204 (Inspect) is used to find the template's corresponding explicit export
186 (Inspect) is used to find the template's corresponding explicit export
205 method defined in this module. That method is then called directly.
187 method defined in this module. That method is then called directly.
206
188
207 format_name : str
189 format_name : str
208 Name of the template style to export to.
190 Name of the template style to export to.
209 """
191 """
210
192
211 function_name = "export_" + format_name.lower()
193 function_name = "export_" + format_name.lower()
212
194
213 if function_name in globals():
195 if function_name in globals():
214 return globals()[function_name](nb, **kw)
196 return globals()[function_name](nb, **kw)
215 else:
197 else:
216 raise ExporterNameError("template for `%s` not found" % function_name)
198 raise ExporterNameError("template for `%s` not found" % function_name)
217
199
218
200
219 def get_export_names():
201 def get_export_names():
220 "Return a list of the currently supported export targets"
202 """Return a list of the currently supported export targets
203
204 WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT"""
205
221 # grab everything after 'export_'
206 # grab everything after 'export_'
222 l = [x[len('export_'):] for x in __all__ if x.startswith('export_')]
207 l = [x[len('export_'):] for x in __all__ if x.startswith('export_')]
223
208
224 # filter out the one method that is not a template
209 # filter out the one method that is not a template
225 l = [x for x in l if 'by_name' not in x]
210 l = [x for x in l if 'by_name' not in x]
226 return sorted(l)
211 return sorted(l)
@@ -1,446 +1,482 b''
1 """This module defines Exporter, a highly configurable converter
1 """This module defines Exporter, a highly configurable converter
2 that uses Jinja2 to export notebook files into different formats.
2 that uses Jinja2 to export notebook files into different formats.
3 """
3 """
4
4
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from __future__ import print_function, absolute_import
17 from __future__ import print_function, absolute_import
18
18
19 # Stdlib imports
19 # Stdlib imports
20 import io
20 import io
21 import os
21 import os
22 import inspect
22 import inspect
23 import copy
23 import copy
24 import collections
24 import collections
25 import datetime
25 import datetime
26
26
27 # other libs/dependencies
27 # other libs/dependencies
28 from jinja2 import Environment, FileSystemLoader, ChoiceLoader
28 from jinja2 import Environment, FileSystemLoader, ChoiceLoader, TemplateNotFound
29
29
30 # IPython imports
30 # IPython imports
31 from IPython.config.configurable import Configurable
31 from IPython.config.configurable import Configurable
32 from IPython.config import Config
32 from IPython.config import Config
33 from IPython.nbformat import current as nbformat
33 from IPython.nbformat import current as nbformat
34 from IPython.utils.traitlets import MetaHasTraits, DottedObjectName, Unicode, List, Dict
34 from IPython.utils.traitlets import MetaHasTraits, DottedObjectName, Unicode, List, Dict
35 from IPython.utils.importstring import import_item
35 from IPython.utils.importstring import import_item
36 from IPython.utils.text import indent
36 from IPython.utils.text import indent
37 from IPython.utils import py3compat
37 from IPython.utils import py3compat
38
38
39 from IPython.nbconvert import transformers as nbtransformers
39 from IPython.nbconvert import transformers as nbtransformers
40 from IPython.nbconvert import filters
40 from IPython.nbconvert import filters
41
41
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43 # Globals and constants
43 # Globals and constants
44 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
45
45
46 #Jinja2 extensions to load.
46 #Jinja2 extensions to load.
47 JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols']
47 JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols']
48
48
49 default_filters = {
49 default_filters = {
50 'indent': indent,
50 'indent': indent,
51 'markdown2html': filters.markdown2html,
51 'markdown2html': filters.markdown2html,
52 'ansi2html': filters.ansi2html,
52 'ansi2html': filters.ansi2html,
53 'filter_data_type': filters.DataTypeFilter,
53 'filter_data_type': filters.DataTypeFilter,
54 'get_lines': filters.get_lines,
54 'get_lines': filters.get_lines,
55 'highlight2html': filters.highlight2html,
55 'highlight2html': filters.highlight2html,
56 'highlight2latex': filters.highlight2latex,
56 'highlight2latex': filters.highlight2latex,
57 'ipython2python': filters.ipython2python,
57 'ipython2python': filters.ipython2python,
58 'markdown2latex': filters.markdown2latex,
58 'markdown2latex': filters.markdown2latex,
59 'markdown2rst': filters.markdown2rst,
59 'markdown2rst': filters.markdown2rst,
60 'comment_lines': filters.comment_lines,
60 'comment_lines': filters.comment_lines,
61 'strip_ansi': filters.strip_ansi,
61 'strip_ansi': filters.strip_ansi,
62 'strip_dollars': filters.strip_dollars,
62 'strip_dollars': filters.strip_dollars,
63 'strip_files_prefix': filters.strip_files_prefix,
63 'strip_files_prefix': filters.strip_files_prefix,
64 'html2text' : filters.html2text,
64 'html2text' : filters.html2text,
65 'add_anchor': filters.add_anchor,
65 'add_anchor': filters.add_anchor,
66 'ansi2latex': filters.ansi2latex,
66 'ansi2latex': filters.ansi2latex,
67 'strip_math_space': filters.strip_math_space,
67 'strip_math_space': filters.strip_math_space,
68 'wrap_text': filters.wrap_text,
68 'wrap_text': filters.wrap_text,
69 'escape_latex': filters.escape_latex
69 'escape_latex': filters.escape_latex
70 }
70 }
71
71
72 #-----------------------------------------------------------------------------
72 #-----------------------------------------------------------------------------
73 # Class
73 # Class
74 #-----------------------------------------------------------------------------
74 #-----------------------------------------------------------------------------
75
75
76 class ResourcesDict(collections.defaultdict):
76 class ResourcesDict(collections.defaultdict):
77 def __missing__(self, key):
77 def __missing__(self, key):
78 return ''
78 return ''
79
79
80
80
81 class Exporter(Configurable):
81 class Exporter(Configurable):
82 """
82 """
83 Exports notebooks into other file formats. Uses Jinja 2 templating engine
83 Exports notebooks into other file formats. Uses Jinja 2 templating engine
84 to output new formats. Inherit from this class if you are creating a new
84 to output new formats. Inherit from this class if you are creating a new
85 template type along with new filters/transformers. If the filters/
85 template type along with new filters/transformers. If the filters/
86 transformers provided by default suffice, there is no need to inherit from
86 transformers provided by default suffice, there is no need to inherit from
87 this class. Instead, override the template_file and file_extension
87 this class. Instead, override the template_file and file_extension
88 traits via a config file.
88 traits via a config file.
89
89
90 {filters}
90 {filters}
91 """
91 """
92
92
93 # finish the docstring
93 # finish the docstring
94 __doc__ = __doc__.format(filters = '- '+'\n - '.join(default_filters.keys()))
94 __doc__ = __doc__.format(filters = '- '+'\n - '.join(default_filters.keys()))
95
95
96
96
97 template_file = Unicode(
97 template_file = Unicode(u'default',
98 '', config=True,
98 config=True,
99 help="Name of the template file to use")
99 help="Name of the template file to use")
100 def _template_file_changed(self, name, old, new):
101 if new=='default':
102 self.template_file = self.default_template
103 else:
104 self.template_file = new
105 default_template = Unicode(u'')
100
106
101 file_extension = Unicode(
107 file_extension = Unicode(
102 'txt', config=True,
108 'txt', config=True,
103 help="Extension of the file that should be written to disk"
109 help="Extension of the file that should be written to disk"
104 )
110 )
105
111
106 template_path = List(['.'], config=True)
112 template_path = List(['.'], config=True)
107
113
108 default_template_path = Unicode(
114 default_template_path = Unicode(
109 os.path.join("..", "templates"),
115 os.path.join("..", "templates"),
110 help="Path where the template files are located.")
116 help="Path where the template files are located.")
111
117
112 template_skeleton_path = Unicode(
118 template_skeleton_path = Unicode(
113 os.path.join("..", "templates", "skeleton"),
119 os.path.join("..", "templates", "skeleton"),
114 help="Path where the template skeleton files are located.")
120 help="Path where the template skeleton files are located.")
115
121
116 #Jinja block definitions
122 #Jinja block definitions
117 jinja_comment_block_start = Unicode("", config=True)
123 jinja_comment_block_start = Unicode("", config=True)
118 jinja_comment_block_end = Unicode("", config=True)
124 jinja_comment_block_end = Unicode("", config=True)
119 jinja_variable_block_start = Unicode("", config=True)
125 jinja_variable_block_start = Unicode("", config=True)
120 jinja_variable_block_end = Unicode("", config=True)
126 jinja_variable_block_end = Unicode("", config=True)
121 jinja_logic_block_start = Unicode("", config=True)
127 jinja_logic_block_start = Unicode("", config=True)
122 jinja_logic_block_end = Unicode("", config=True)
128 jinja_logic_block_end = Unicode("", config=True)
123
129
124 #Extension that the template files use.
130 #Extension that the template files use.
125 template_extension = Unicode(".tpl", config=True)
131 template_extension = Unicode(".tpl", config=True)
126
132
127 #Configurability, allows the user to easily add filters and transformers.
133 #Configurability, allows the user to easily add filters and transformers.
128 transformers = List(config=True,
134 transformers = List(config=True,
129 help="""List of transformers, by name or namespace, to enable.""")
135 help="""List of transformers, by name or namespace, to enable.""")
130
136
131 filters = Dict(config=True,
137 filters = Dict(config=True,
132 help="""Dictionary of filters, by name and namespace, to add to the Jinja
138 help="""Dictionary of filters, by name and namespace, to add to the Jinja
133 environment.""")
139 environment.""")
134
140
135 default_transformers = List([nbtransformers.coalesce_streams,
141 default_transformers = List([nbtransformers.coalesce_streams,
136 nbtransformers.SVG2PDFTransformer,
142 nbtransformers.SVG2PDFTransformer,
137 nbtransformers.ExtractOutputTransformer,
143 nbtransformers.ExtractOutputTransformer,
138 nbtransformers.CSSHTMLHeaderTransformer,
144 nbtransformers.CSSHTMLHeaderTransformer,
139 nbtransformers.RevealHelpTransformer,
145 nbtransformers.RevealHelpTransformer,
140 nbtransformers.LatexTransformer,
146 nbtransformers.LatexTransformer,
141 nbtransformers.SphinxTransformer],
147 nbtransformers.SphinxTransformer],
142 config=True,
148 config=True,
143 help="""List of transformers available by default, by name, namespace,
149 help="""List of transformers available by default, by name, namespace,
144 instance, or type.""")
150 instance, or type.""")
145
151
146
152
147 def __init__(self, config=None, extra_loaders=None, **kw):
153 def __init__(self, config=None, extra_loaders=None, **kw):
148 """
154 """
149 Public constructor
155 Public constructor
150
156
151 Parameters
157 Parameters
152 ----------
158 ----------
153 config : config
159 config : config
154 User configuration instance.
160 User configuration instance.
155 extra_loaders : list[of Jinja Loaders]
161 extra_loaders : list[of Jinja Loaders]
156 ordered list of Jinja loder to find templates. Will be tried in order
162 ordered list of Jinja loder to find templates. Will be tried in order
157 before the default FileSysteme ones.
163 before the default FileSysteme ones.
164 template : str (optional, kw arg)
165 Template to use when exporting.
158 """
166 """
159
167
160 #Call the base class constructor
168 #Call the base class constructor
161 c = self.default_config
169 c = self.default_config
162 if config:
170 if config:
163 c.merge(config)
171 c.merge(config)
164
172
165 super(Exporter, self).__init__(config=c, **kw)
173 super(Exporter, self).__init__(config=c, **kw)
166
174
167 #Init
175 #Init
176 self._init_template(**kw)
168 self._init_environment(extra_loaders=extra_loaders)
177 self._init_environment(extra_loaders=extra_loaders)
169 self._init_transformers()
178 self._init_transformers()
170 self._init_filters()
179 self._init_filters()
171
180
172
181
173 @property
182 @property
174 def default_config(self):
183 def default_config(self):
175 return Config()
184 return Config()
176
185
177
186
178 def from_notebook_node(self, nb, resources=None, **kw):
187 def from_notebook_node(self, nb, resources=None, **kw):
179 """
188 """
180 Convert a notebook from a notebook node instance.
189 Convert a notebook from a notebook node instance.
181
190
182 Parameters
191 Parameters
183 ----------
192 ----------
184 nb : Notebook node
193 nb : Notebook node
185 resources : dict (**kw)
194 resources : dict (**kw)
186 of additional resources that can be accessed read/write by
195 of additional resources that can be accessed read/write by
187 transformers and filters.
196 transformers and filters.
188 """
197 """
189 nb_copy = copy.deepcopy(nb)
198 nb_copy = copy.deepcopy(nb)
190 resources = self._init_resources(resources)
199 resources = self._init_resources(resources)
191
200
192 #Preprocess
201 # Preprocess
193 nb_copy, resources = self._transform(nb_copy, resources)
202 nb_copy, resources = self._transform(nb_copy, resources)
194
203
195 #Convert
204 # Try different template names during conversion. First try to load the
196 self.template = self.environment.get_template(self.template_file + self.template_extension)
205 # template by name with extension added, then try loading the template
197 output = self.template.render(nb=nb_copy, resources=resources)
206 # as if the name is explicitly specified, then try the name as a
207 # 'flavor', and lastly just try to load the template by module name.
208 module_name = self.__module__.split('.')[-1]
209 try_names = [self.template_file + self.template_extension,
210 self.template_file,
211 module_name + '_' + self.template_file + self.template_extension,
212 module_name + self.template_extension]
213 for try_name in try_names:
214 try:
215 self.template = self.environment.get_template(try_name)
216 break
217 except TemplateNotFound:
218 pass
219
220 if hasattr(self, 'template'):
221 output = self.template.render(nb=nb_copy, resources=resources)
222 else:
223 raise IOError('template file "%s" could not be found' % self.template_file)
198 return output, resources
224 return output, resources
199
225
200
226
201 def from_filename(self, filename, resources=None, **kw):
227 def from_filename(self, filename, resources=None, **kw):
202 """
228 """
203 Convert a notebook from a notebook file.
229 Convert a notebook from a notebook file.
204
230
205 Parameters
231 Parameters
206 ----------
232 ----------
207 filename : str
233 filename : str
208 Full filename of the notebook file to open and convert.
234 Full filename of the notebook file to open and convert.
209 """
235 """
210
236
211 #Pull the metadata from the filesystem.
237 #Pull the metadata from the filesystem.
212 if resources is None:
238 if resources is None:
213 resources = ResourcesDict()
239 resources = ResourcesDict()
214 if not 'metadata' in resources or resources['metadata'] == '':
240 if not 'metadata' in resources or resources['metadata'] == '':
215 resources['metadata'] = ResourcesDict()
241 resources['metadata'] = ResourcesDict()
216 basename = os.path.basename(filename)
242 basename = os.path.basename(filename)
217 notebook_name = basename[:basename.rfind('.')]
243 notebook_name = basename[:basename.rfind('.')]
218 resources['metadata']['name'] = notebook_name
244 resources['metadata']['name'] = notebook_name
219
245
220 modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
246 modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
221 resources['metadata']['modified_date'] = modified_date.strftime("%B %d, %Y")
247 resources['metadata']['modified_date'] = modified_date.strftime("%B %d, %Y")
222
248
223 with io.open(filename) as f:
249 with io.open(filename) as f:
224 return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources,**kw)
250 return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources,**kw)
225
251
226
252
227 def from_file(self, file_stream, resources=None, **kw):
253 def from_file(self, file_stream, resources=None, **kw):
228 """
254 """
229 Convert a notebook from a notebook file.
255 Convert a notebook from a notebook file.
230
256
231 Parameters
257 Parameters
232 ----------
258 ----------
233 file_stream : file-like object
259 file_stream : file-like object
234 Notebook file-like object to convert.
260 Notebook file-like object to convert.
235 """
261 """
236 return self.from_notebook_node(nbformat.read(file_stream, 'json'), resources=resources, **kw)
262 return self.from_notebook_node(nbformat.read(file_stream, 'json'), resources=resources, **kw)
237
263
238
264
239 def register_transformer(self, transformer, enabled=False):
265 def register_transformer(self, transformer, enabled=False):
240 """
266 """
241 Register a transformer.
267 Register a transformer.
242 Transformers are classes that act upon the notebook before it is
268 Transformers are classes that act upon the notebook before it is
243 passed into the Jinja templating engine. Transformers are also
269 passed into the Jinja templating engine. Transformers are also
244 capable of passing additional information to the Jinja
270 capable of passing additional information to the Jinja
245 templating engine.
271 templating engine.
246
272
247 Parameters
273 Parameters
248 ----------
274 ----------
249 transformer : transformer
275 transformer : transformer
250 """
276 """
251 if transformer is None:
277 if transformer is None:
252 raise TypeError('transformer')
278 raise TypeError('transformer')
253 isclass = isinstance(transformer, type)
279 isclass = isinstance(transformer, type)
254 constructed = not isclass
280 constructed = not isclass
255
281
256 #Handle transformer's registration based on it's type
282 #Handle transformer's registration based on it's type
257 if constructed and isinstance(transformer, py3compat.string_types):
283 if constructed and isinstance(transformer, py3compat.string_types):
258 #Transformer is a string, import the namespace and recursively call
284 #Transformer is a string, import the namespace and recursively call
259 #this register_transformer method
285 #this register_transformer method
260 transformer_cls = import_item(transformer)
286 transformer_cls = import_item(transformer)
261 return self.register_transformer(transformer_cls, enabled)
287 return self.register_transformer(transformer_cls, enabled)
262
288
263 if constructed and hasattr(transformer, '__call__'):
289 if constructed and hasattr(transformer, '__call__'):
264 #Transformer is a function, no need to construct it.
290 #Transformer is a function, no need to construct it.
265 #Register and return the transformer.
291 #Register and return the transformer.
266 if enabled:
292 if enabled:
267 transformer.enabled = True
293 transformer.enabled = True
268 self._transformers.append(transformer)
294 self._transformers.append(transformer)
269 return transformer
295 return transformer
270
296
271 elif isclass and isinstance(transformer, MetaHasTraits):
297 elif isclass and isinstance(transformer, MetaHasTraits):
272 #Transformer is configurable. Make sure to pass in new default for
298 #Transformer is configurable. Make sure to pass in new default for
273 #the enabled flag if one was specified.
299 #the enabled flag if one was specified.
274 self.register_transformer(transformer(parent=self), enabled)
300 self.register_transformer(transformer(parent=self), enabled)
275
301
276 elif isclass:
302 elif isclass:
277 #Transformer is not configurable, construct it
303 #Transformer is not configurable, construct it
278 self.register_transformer(transformer(), enabled)
304 self.register_transformer(transformer(), enabled)
279
305
280 else:
306 else:
281 #Transformer is an instance of something without a __call__
307 #Transformer is an instance of something without a __call__
282 #attribute.
308 #attribute.
283 raise TypeError('transformer')
309 raise TypeError('transformer')
284
310
285
311
286 def register_filter(self, name, jinja_filter):
312 def register_filter(self, name, jinja_filter):
287 """
313 """
288 Register a filter.
314 Register a filter.
289 A filter is a function that accepts and acts on one string.
315 A filter is a function that accepts and acts on one string.
290 The filters are accesible within the Jinja templating engine.
316 The filters are accesible within the Jinja templating engine.
291
317
292 Parameters
318 Parameters
293 ----------
319 ----------
294 name : str
320 name : str
295 name to give the filter in the Jinja engine
321 name to give the filter in the Jinja engine
296 filter : filter
322 filter : filter
297 """
323 """
298 if jinja_filter is None:
324 if jinja_filter is None:
299 raise TypeError('filter')
325 raise TypeError('filter')
300 isclass = isinstance(jinja_filter, type)
326 isclass = isinstance(jinja_filter, type)
301 constructed = not isclass
327 constructed = not isclass
302
328
303 #Handle filter's registration based on it's type
329 #Handle filter's registration based on it's type
304 if constructed and isinstance(jinja_filter, py3compat.string_types):
330 if constructed and isinstance(jinja_filter, py3compat.string_types):
305 #filter is a string, import the namespace and recursively call
331 #filter is a string, import the namespace and recursively call
306 #this register_filter method
332 #this register_filter method
307 filter_cls = import_item(jinja_filter)
333 filter_cls = import_item(jinja_filter)
308 return self.register_filter(name, filter_cls)
334 return self.register_filter(name, filter_cls)
309
335
310 if constructed and hasattr(jinja_filter, '__call__'):
336 if constructed and hasattr(jinja_filter, '__call__'):
311 #filter is a function, no need to construct it.
337 #filter is a function, no need to construct it.
312 self.environment.filters[name] = jinja_filter
338 self.environment.filters[name] = jinja_filter
313 return jinja_filter
339 return jinja_filter
314
340
315 elif isclass and isinstance(jinja_filter, MetaHasTraits):
341 elif isclass and isinstance(jinja_filter, MetaHasTraits):
316 #filter is configurable. Make sure to pass in new default for
342 #filter is configurable. Make sure to pass in new default for
317 #the enabled flag if one was specified.
343 #the enabled flag if one was specified.
318 filter_instance = jinja_filter(parent=self)
344 filter_instance = jinja_filter(parent=self)
319 self.register_filter(name, filter_instance )
345 self.register_filter(name, filter_instance )
320
346
321 elif isclass:
347 elif isclass:
322 #filter is not configurable, construct it
348 #filter is not configurable, construct it
323 filter_instance = jinja_filter()
349 filter_instance = jinja_filter()
324 self.register_filter(name, filter_instance)
350 self.register_filter(name, filter_instance)
325
351
326 else:
352 else:
327 #filter is an instance of something without a __call__
353 #filter is an instance of something without a __call__
328 #attribute.
354 #attribute.
329 raise TypeError('filter')
355 raise TypeError('filter')
330
356
331
357
358 def _init_template(self, **kw):
359 """
360 Make sure a template name is specified. If one isn't specified, try to
361 build one from the information we know.
362 """
363 self._template_file_changed('template_file', self.template_file, self.template_file)
364 if 'template' in kw:
365 self.template_file = kw['template']
366
367
332 def _init_environment(self, extra_loaders=None):
368 def _init_environment(self, extra_loaders=None):
333 """
369 """
334 Create the Jinja templating environment.
370 Create the Jinja templating environment.
335 """
371 """
336 here = os.path.dirname(os.path.realpath(__file__))
372 here = os.path.dirname(os.path.realpath(__file__))
337 loaders = []
373 loaders = []
338 if extra_loaders:
374 if extra_loaders:
339 loaders.extend(extra_loaders)
375 loaders.extend(extra_loaders)
340
376
341 paths = self.template_path
377 paths = self.template_path
342 paths.extend([os.path.join(here, self.default_template_path),
378 paths.extend([os.path.join(here, self.default_template_path),
343 os.path.join(here, self.template_skeleton_path)])
379 os.path.join(here, self.template_skeleton_path)])
344 loaders.append(FileSystemLoader(paths))
380 loaders.append(FileSystemLoader(paths))
345
381
346 self.environment = Environment(
382 self.environment = Environment(
347 loader= ChoiceLoader(loaders),
383 loader= ChoiceLoader(loaders),
348 extensions=JINJA_EXTENSIONS
384 extensions=JINJA_EXTENSIONS
349 )
385 )
350
386
351 #Set special Jinja2 syntax that will not conflict with latex.
387 #Set special Jinja2 syntax that will not conflict with latex.
352 if self.jinja_logic_block_start:
388 if self.jinja_logic_block_start:
353 self.environment.block_start_string = self.jinja_logic_block_start
389 self.environment.block_start_string = self.jinja_logic_block_start
354 if self.jinja_logic_block_end:
390 if self.jinja_logic_block_end:
355 self.environment.block_end_string = self.jinja_logic_block_end
391 self.environment.block_end_string = self.jinja_logic_block_end
356 if self.jinja_variable_block_start:
392 if self.jinja_variable_block_start:
357 self.environment.variable_start_string = self.jinja_variable_block_start
393 self.environment.variable_start_string = self.jinja_variable_block_start
358 if self.jinja_variable_block_end:
394 if self.jinja_variable_block_end:
359 self.environment.variable_end_string = self.jinja_variable_block_end
395 self.environment.variable_end_string = self.jinja_variable_block_end
360 if self.jinja_comment_block_start:
396 if self.jinja_comment_block_start:
361 self.environment.comment_start_string = self.jinja_comment_block_start
397 self.environment.comment_start_string = self.jinja_comment_block_start
362 if self.jinja_comment_block_end:
398 if self.jinja_comment_block_end:
363 self.environment.comment_end_string = self.jinja_comment_block_end
399 self.environment.comment_end_string = self.jinja_comment_block_end
364
400
365
401
366 def _init_transformers(self):
402 def _init_transformers(self):
367 """
403 """
368 Register all of the transformers needed for this exporter, disabled
404 Register all of the transformers needed for this exporter, disabled
369 unless specified explicitly.
405 unless specified explicitly.
370 """
406 """
371 self._transformers = []
407 self._transformers = []
372
408
373 #Load default transformers (not necessarly enabled by default).
409 #Load default transformers (not necessarly enabled by default).
374 if self.default_transformers:
410 if self.default_transformers:
375 for transformer in self.default_transformers:
411 for transformer in self.default_transformers:
376 self.register_transformer(transformer)
412 self.register_transformer(transformer)
377
413
378 #Load user transformers. Enable by default.
414 #Load user transformers. Enable by default.
379 if self.transformers:
415 if self.transformers:
380 for transformer in self.transformers:
416 for transformer in self.transformers:
381 self.register_transformer(transformer, enabled=True)
417 self.register_transformer(transformer, enabled=True)
382
418
383
419
384 def _init_filters(self):
420 def _init_filters(self):
385 """
421 """
386 Register all of the filters required for the exporter.
422 Register all of the filters required for the exporter.
387 """
423 """
388
424
389 #Add default filters to the Jinja2 environment
425 #Add default filters to the Jinja2 environment
390 for key, value in default_filters.items():
426 for key, value in default_filters.items():
391 self.register_filter(key, value)
427 self.register_filter(key, value)
392
428
393 #Load user filters. Overwrite existing filters if need be.
429 #Load user filters. Overwrite existing filters if need be.
394 if self.filters:
430 if self.filters:
395 for key, user_filter in self.filters.items():
431 for key, user_filter in self.filters.items():
396 self.register_filter(key, user_filter)
432 self.register_filter(key, user_filter)
397
433
398
434
399 def _init_resources(self, resources):
435 def _init_resources(self, resources):
400
436
401 #Make sure the resources dict is of ResourcesDict type.
437 #Make sure the resources dict is of ResourcesDict type.
402 if resources is None:
438 if resources is None:
403 resources = ResourcesDict()
439 resources = ResourcesDict()
404 if not isinstance(resources, ResourcesDict):
440 if not isinstance(resources, ResourcesDict):
405 new_resources = ResourcesDict()
441 new_resources = ResourcesDict()
406 new_resources.update(resources)
442 new_resources.update(resources)
407 resources = new_resources
443 resources = new_resources
408
444
409 #Make sure the metadata extension exists in resources
445 #Make sure the metadata extension exists in resources
410 if 'metadata' in resources:
446 if 'metadata' in resources:
411 if not isinstance(resources['metadata'], ResourcesDict):
447 if not isinstance(resources['metadata'], ResourcesDict):
412 resources['metadata'] = ResourcesDict(resources['metadata'])
448 resources['metadata'] = ResourcesDict(resources['metadata'])
413 else:
449 else:
414 resources['metadata'] = ResourcesDict()
450 resources['metadata'] = ResourcesDict()
415 if not resources['metadata']['name']:
451 if not resources['metadata']['name']:
416 resources['metadata']['name'] = 'Notebook'
452 resources['metadata']['name'] = 'Notebook'
417
453
418 #Set the output extension
454 #Set the output extension
419 resources['output_extension'] = self.file_extension
455 resources['output_extension'] = self.file_extension
420 return resources
456 return resources
421
457
422
458
423 def _transform(self, nb, resources):
459 def _transform(self, nb, resources):
424 """
460 """
425 Preprocess the notebook before passing it into the Jinja engine.
461 Preprocess the notebook before passing it into the Jinja engine.
426 To preprocess the notebook is to apply all of the
462 To preprocess the notebook is to apply all of the
427
463
428 Parameters
464 Parameters
429 ----------
465 ----------
430 nb : notebook node
466 nb : notebook node
431 notebook that is being exported.
467 notebook that is being exported.
432 resources : a dict of additional resources that
468 resources : a dict of additional resources that
433 can be accessed read/write by transformers
469 can be accessed read/write by transformers
434 and filters.
470 and filters.
435 """
471 """
436
472
437 # Do a copy.deepcopy first,
473 # Do a copy.deepcopy first,
438 # we are never safe enough with what the transformers could do.
474 # we are never safe enough with what the transformers could do.
439 nbc = copy.deepcopy(nb)
475 nbc = copy.deepcopy(nb)
440 resc = copy.deepcopy(resources)
476 resc = copy.deepcopy(resources)
441
477
442 #Run each transformer on the notebook. Carry the output along
478 #Run each transformer on the notebook. Carry the output along
443 #to each transformer
479 #to each transformer
444 for transformer in self._transformers:
480 for transformer in self._transformers:
445 nbc, resc = transformer(nbc, resc)
481 nbc, resc = transformer(nbc, resc)
446 return nbc, resc
482 return nbc, resc
@@ -1,42 +1,52 b''
1 """
1 """
2 Exporter that exports Basic HTML.
2 Exporter that exports Basic HTML.
3 """
3 """
4
4
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from IPython.utils.traitlets import Unicode, List
17 from IPython.utils.traitlets import Unicode, List
18
18
19 from IPython.nbconvert import transformers
19 from IPython.nbconvert import transformers
20 from IPython.config import Config
20
21
21 from .exporter import Exporter
22 from .exporter import Exporter
22
23
23 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
24 # Classes
25 # Classes
25 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
26
27
27 class BasicHTMLExporter(Exporter):
28 class HTMLExporter(Exporter):
28 """
29 """
29 Exports a basic HTML document. This exporter assists with the export of
30 Exports a basic HTML document. This exporter assists with the export of
30 HTML. Inherit from it if you are writing your own HTML template and need
31 HTML. Inherit from it if you are writing your own HTML template and need
31 custom transformers/filters. If you don't need custom transformers/
32 custom transformers/filters. If you don't need custom transformers/
32 filters, just change the 'template_file' config option.
33 filters, just change the 'template_file' config option.
33 """
34 """
34
35
35 file_extension = Unicode(
36 file_extension = Unicode(
36 'html', config=True,
37 'html', config=True,
37 help="Extension of the file that should be written to disk"
38 help="Extension of the file that should be written to disk"
38 )
39 )
39
40
40 template_file = Unicode(
41 default_template = Unicode('full', config=True, help="""Flavor of the data
41 'basichtml', config=True,
42 format to use. I.E. 'full' or 'basic'""")
42 help="Name of the template file to use")
43
44 @property
45 def default_config(self):
46 c = Config({
47 'CSSHTMLHeaderTransformer':{
48 'enabled':True
49 }
50 })
51 c.merge(super(HTMLExporter,self).default_config)
52 return c
@@ -1,88 +1,91 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 # Stdlib imports
19 # Stdlib imports
20 import os
20 import os
21
21
22 # IPython imports
22 # IPython imports
23 from IPython.utils.traitlets import Unicode, List
23 from IPython.utils.traitlets import Unicode, List
24 from IPython.config import Config
24 from IPython.config import Config
25
25
26 from IPython.nbconvert import filters, transformers
26 from IPython.nbconvert import filters, transformers
27 from .exporter import Exporter
27 from .exporter import Exporter
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Classes and functions
30 # Classes and functions
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 class LatexExporter(Exporter):
33 class LatexExporter(Exporter):
34 """
34 """
35 Exports to a Latex template. Inherit from this class if your template is
35 Exports to a Latex template. Inherit from this class if your template is
36 LaTeX based and you need custom tranformers/filters. Inherit from it if
36 LaTeX based and you need custom tranformers/filters. Inherit from it if
37 you are writing your own HTML template and need custom tranformers/filters.
37 you are writing your own HTML template and need custom tranformers/filters.
38 If you don't need custom tranformers/filters, just change the
38 If you don't need custom tranformers/filters, just change the
39 'template_file' config option. Place your template in the special "/latex"
39 'template_file' config option. Place your template in the special "/latex"
40 subfolder of the "../templates" folder.
40 subfolder of the "../templates" folder.
41 """
41 """
42
42
43 file_extension = Unicode(
43 file_extension = Unicode(
44 'tex', config=True,
44 'tex', config=True,
45 help="Extension of the file that should be written to disk")
45 help="Extension of the file that should be written to disk")
46
46
47 template_file = Unicode(
47 default_template = Unicode('article', config=True, help="""Template of the
48 'base', config=True,
48 data format to use. I.E. 'full' or 'basic'""")
49 help="Name of the template file to use")
50
49
51 #Latex constants
50 #Latex constants
52 default_template_path = Unicode(
51 default_template_path = Unicode(
53 os.path.join("..", "templates", "latex"), config=True,
52 os.path.join("..", "templates", "latex"), config=True,
54 help="Path where the template files are located.")
53 help="Path where the template files are located.")
55
54
56 template_skeleton_path = Unicode(
55 template_skeleton_path = Unicode(
57 os.path.join("..", "templates", "latex", "skeleton"), config=True,
56 os.path.join("..", "templates", "latex", "skeleton"), config=True,
58 help="Path where the template skeleton files are located.")
57 help="Path where the template skeleton files are located.")
59
58
60 #Special Jinja2 syntax that will not conflict when exporting latex.
59 #Special Jinja2 syntax that will not conflict when exporting latex.
61 jinja_comment_block_start = Unicode("((=", config=True)
60 jinja_comment_block_start = Unicode("((=", config=True)
62 jinja_comment_block_end = Unicode("=))", config=True)
61 jinja_comment_block_end = Unicode("=))", config=True)
63 jinja_variable_block_start = Unicode("(((", config=True)
62 jinja_variable_block_start = Unicode("(((", config=True)
64 jinja_variable_block_end = Unicode(")))", config=True)
63 jinja_variable_block_end = Unicode(")))", config=True)
65 jinja_logic_block_start = Unicode("((*", config=True)
64 jinja_logic_block_start = Unicode("((*", config=True)
66 jinja_logic_block_end = Unicode("*))", config=True)
65 jinja_logic_block_end = Unicode("*))", config=True)
67
66
68 #Extension that the template files use.
67 #Extension that the template files use.
69 template_extension = Unicode(".tplx", config=True)
68 template_extension = Unicode(".tplx", config=True)
70
69
70
71 @property
71 @property
72 def default_config(self):
72 def default_config(self):
73 c = Config({
73 c = Config({
74 'NbConvertBase': {
74 'NbConvertBase': {
75 'display_data_priority' : ['latex', 'pdf', 'png', 'jpg', 'svg', 'jpeg', 'text']
75 'display_data_priority' : ['latex', 'pdf', 'png', 'jpg', 'svg', 'jpeg', 'text']
76 },
76 },
77 'ExtractOutputTransformer': {
77 'ExtractOutputTransformer': {
78 'enabled':True
78 'enabled':True
79 },
79 },
80 'SVG2PDFTransformer': {
80 'SVG2PDFTransformer': {
81 'enabled':True
81 'enabled':True
82 },
82 },
83 'LatexTransformer': {
83 'LatexTransformer': {
84 'enabled':True
84 'enabled':True
85 },
86 'SphinxTransformer': {
87 'enabled':True
85 }
88 }
86 })
89 })
87 c.merge(super(LatexExporter,self).default_config)
90 c.merge(super(LatexExporter,self).default_config)
88 return c
91 return c
@@ -1,35 +1,31 b''
1 """
1 """
2 Exporter that will export your ipynb to Markdown.
2 Exporter that will export your ipynb to Markdown.
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
15
16 from IPython.utils.traitlets import Unicode
16 from IPython.utils.traitlets import Unicode
17
17
18 from .exporter import Exporter
18 from .exporter import Exporter
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Classes
21 # Classes
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23
23
24 class MarkdownExporter(Exporter):
24 class MarkdownExporter(Exporter):
25 """
25 """
26 Exports to a markdown document (.md)
26 Exports to a markdown document (.md)
27 """
27 """
28
28
29 file_extension = Unicode(
29 file_extension = Unicode(
30 'md', config=True,
30 'md', config=True,
31 help="Extension of the file that should be written to disk")
31 help="Extension of the file that should be written to disk")
32
33 template_file = Unicode(
34 'markdown', config=True,
35 help="Name of the template file to use")
@@ -1,35 +1,31 b''
1 """
1 """
2 Python exporter which exports Notebook code into a PY file.
2 Python exporter which exports Notebook code into a PY file.
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
15
16 from IPython.utils.traitlets import Unicode
16 from IPython.utils.traitlets import Unicode
17
17
18 from .exporter import Exporter
18 from .exporter import Exporter
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Classes
21 # Classes
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23
23
24 class PythonExporter(Exporter):
24 class PythonExporter(Exporter):
25 """
25 """
26 Exports a Python code file.
26 Exports a Python code file.
27 """
27 """
28
28
29 file_extension = Unicode(
29 file_extension = Unicode(
30 'py', config=True,
30 'py', config=True,
31 help="Extension of the file that should be written to disk")
31 help="Extension of the file that should be written to disk")
32
33 template_file = Unicode(
34 'python', config=True,
35 help="Name of the template file to use")
@@ -1,42 +1,38 b''
1 """
1 """
2 Exporter for exporting notebooks to restructured text.
2 Exporter for exporting notebooks to restructured text.
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
15
16 from IPython.utils.traitlets import Unicode
16 from IPython.utils.traitlets import Unicode
17 from IPython.config import Config
17 from IPython.config import Config
18
18
19 from .exporter import Exporter
19 from .exporter import Exporter
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Classes
22 # Classes
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 class RSTExporter(Exporter):
25 class RSTExporter(Exporter):
26 """
26 """
27 Exports restructured text documents.
27 Exports restructured text documents.
28 """
28 """
29
29
30 file_extension = Unicode(
30 file_extension = Unicode(
31 'rst', config=True,
31 'rst', config=True,
32 help="Extension of the file that should be written to disk")
32 help="Extension of the file that should be written to disk")
33
33
34 template_file = Unicode(
35 'rst', config=True,
36 help="Name of the template file to use")
37
38 @property
34 @property
39 def default_config(self):
35 def default_config(self):
40 c = Config({'ExtractOutputTransformer':{'enabled':True}})
36 c = Config({'ExtractOutputTransformer':{'enabled':True}})
41 c.merge(super(RSTExporter,self).default_config)
37 c.merge(super(RSTExporter,self).default_config)
42 return c
38 return c
@@ -1,51 +1,52 b''
1 """
1 """
2 Reveal slide show exporter.
2 Contains slide show exporter
3 """
3 """
4
4 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
5 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
6 #
7 #
7 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
8 #
9 #
9 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
10 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
11
12
12 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
13 # Imports
14 # Imports
14 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
15
16
16 from IPython.utils.traitlets import Unicode, List
17 from IPython.utils.traitlets import Unicode
17 from IPython.config import Config
18
18
19 from .basichtml import BasicHTMLExporter
20 from IPython.nbconvert import transformers
19 from IPython.nbconvert import transformers
20 from IPython.config import Config
21
22 from .exporter import Exporter
21
23
22 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
23 # Classes
25 # Classes
24 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
25
27
26 class RevealExporter(BasicHTMLExporter):
28 class SlidesExporter(Exporter):
27 """
29 """
28 Exports a Reveal slide show (.HTML) which may be rendered in a web browser.
30 Exports slides
29 """
31 """
30
32
31 file_extension = Unicode(
33 file_extension = Unicode(
32 'reveal.html', config=True,
34 'slides.html', config=True,
33 help="Extension of the file that should be written to disk")
35 help="Extension of the file that should be written to disk"
34
36 )
35 template_file = Unicode(
36 'reveal', config=True,
37 help="Name of the template file to use")
38
37
38 default_template = Unicode('reveal', config=True, help="""Template of the
39 data format to use. I.E. 'reveal'""")
39
40
40 @property
41 @property
41 def default_config(self):
42 def default_config(self):
42 c = Config({
43 c = Config({
43 'CSSHTMLHeaderTransformer':{
44 'CSSHTMLHeaderTransformer':{
44 'enabled':True
45 'enabled':True
45 },
46 },
46 'RevealHelpTransformer':{
47 'RevealHelpTransformer':{
47 'enabled':True,
48 'enabled':True,
48 },
49 },
49 })
50 })
50 c.merge(super(RevealExporter,self).default_config)
51 c.merge(super(SlidesExporter,self).default_config)
51 return c
52 return c
@@ -1,40 +1,59 b''
1 """
1 """
2 Module with tests for fullhtml.py
2 Module with tests for html.py
3 """
3 """
4
4
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from .base import ExportersTestsBase
17 from .base import ExportersTestsBase
18 from ..fullhtml import FullHTMLExporter
18 from ..html import HTMLExporter
19 from IPython.testing.decorators import onlyif_cmds_exist
19 from IPython.testing.decorators import onlyif_cmds_exist
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Class
22 # Class
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 class TestFullHTMLExporter(ExportersTestsBase):
25 class TestHTMLExporter(ExportersTestsBase):
26 """Contains test functions for fullhtml.py"""
26 """Contains test functions for html.py"""
27
27
28 def test_constructor(self):
28 def test_constructor(self):
29 """
29 """
30 Can a FullHTMLExporter be constructed?
30 Can a HTMLExporter be constructed?
31 """
31 """
32 FullHTMLExporter()
32 HTMLExporter()
33
33
34
34 @onlyif_cmds_exist('pandoc')
35 @onlyif_cmds_exist('pandoc')
35 def test_export(self):
36 def test_export(self):
36 """
37 """
37 Can a FullHTMLExporter export something?
38 Can a HTMLExporter export something?
39 """
40 (output, resources) = HTMLExporter().from_filename(self._get_notebook())
41 assert len(output) > 0
42
43
44 @onlyif_cmds_exist('pandoc')
45 def test_export_basic(self):
46 """
47 Can a HTMLExporter export using the 'basic' template?
48 """
49 (output, resources) = HTMLExporter(template='basic').from_filename(self._get_notebook())
50 assert len(output) > 0
51
52
53 @onlyif_cmds_exist('pandoc')
54 def test_export_full(self):
55 """
56 Can a HTMLExporter export using the 'full' template?
38 """
57 """
39 (output, resources) = FullHTMLExporter().from_filename(self._get_notebook())
58 (output, resources) = HTMLExporter(template='full').from_filename(self._get_notebook())
40 assert len(output) > 0
59 assert len(output) > 0
@@ -1,41 +1,68 b''
1 """
1 """
2 Module with tests for latex.py
2 Module with tests for latex.py
3 """
3 """
4
4
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from .base import ExportersTestsBase
17 from .base import ExportersTestsBase
18 from ..latex import LatexExporter
18 from ..latex import LatexExporter
19 from IPython.testing.decorators import onlyif_cmds_exist
19 from IPython.testing.decorators import onlyif_cmds_exist
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Class
22 # Class
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 class TestLatexExporter(ExportersTestsBase):
25 class TestLatexExporter(ExportersTestsBase):
26 """Contains test functions for latex.py"""
26 """Contains test functions for latex.py"""
27
27
28 def test_constructor(self):
28 def test_constructor(self):
29 """
29 """
30 Can a LatexExporter be constructed?
30 Can a LatexExporter be constructed?
31 """
31 """
32 LatexExporter()
32 LatexExporter()
33
33
34
34
35 @onlyif_cmds_exist('pandoc')
35 @onlyif_cmds_exist('pandoc')
36 def test_export(self):
36 def test_export(self):
37 """
37 """
38 Can a LatexExporter export something?
38 Can a LatexExporter export something?
39 """
39 """
40 (output, resources) = LatexExporter().from_filename(self._get_notebook())
40 (output, resources) = LatexExporter().from_filename(self._get_notebook())
41 assert len(output) > 0
41 assert len(output) > 0
42
43
44 @onlyif_cmds_exist('pandoc')
45 def test_export_book(self):
46 """
47 Can a LatexExporter export using 'book' template?
48 """
49 (output, resources) = LatexExporter(template='book').from_filename(self._get_notebook())
50 assert len(output) > 0
51
52
53 @onlyif_cmds_exist('pandoc')
54 def test_export_basic(self):
55 """
56 Can a LatexExporter export using 'basic' template?
57 """
58 (output, resources) = LatexExporter(template='basic').from_filename(self._get_notebook())
59 assert len(output) > 0
60
61
62 @onlyif_cmds_exist('pandoc')
63 def test_export_article(self):
64 """
65 Can a LatexExporter export using 'article' template?
66 """
67 (output, resources) = LatexExporter(template='article').from_filename(self._get_notebook())
68 assert len(output) > 0 No newline at end of file
@@ -1,40 +1,47 b''
1 """
1 """
2 Module with tests for basichtml.py
2 Module with tests for slides.py
3 """
3 """
4
4
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from .base import ExportersTestsBase
17 from .base import ExportersTestsBase
18 from ..basichtml import BasicHTMLExporter
18 from ..slides import SlidesExporter
19 from IPython.testing.decorators import onlyif_cmds_exist
20
19
21 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
22 # Class
21 # Class
23 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
24
23
25 class TestBasicHTMLExporter(ExportersTestsBase):
24 class TestSlidesExporter(ExportersTestsBase):
26 """Contains test functions for basichtml.py"""
25 """Contains test functions for slides.py"""
27
26
28 def test_constructor(self):
27 def test_constructor(self):
29 """
28 """
30 Can a BasicHTMLExporter be constructed?
29 Can a SlidesExporter be constructed?
31 """
30 """
32 BasicHTMLExporter()
31 SlidesExporter()
32
33
33
34 @onlyif_cmds_exist('pandoc')
35 def test_export(self):
34 def test_export(self):
36 """
35 """
37 Can a BasicHTMLExporter export something?
36 Can a SlidesExporter export something?
37 """
38 (output, resources) = SlidesExporter().from_filename(self._get_notebook())
39 assert len(output) > 0
40
41
42 def test_export_reveal(self):
43 """
44 Can a SlidesExporter export using the 'reveal' template?
38 """
45 """
39 (output, resources) = BasicHTMLExporter().from_filename(self._get_notebook())
46 (output, resources) = SlidesExporter(template='reveal').from_filename(self._get_notebook())
40 assert len(output) > 0
47 assert len(output) > 0
@@ -1,241 +1,301 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 """NBConvert is a utility for conversion of .ipynb files.
2 """NBConvert is a utility for conversion of .ipynb files.
3
3
4 Command-line interface for the NbConvert conversion utility.
4 Command-line interface for the NbConvert conversion utility.
5 """
5 """
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 #Copyright (c) 2013, the IPython Development Team.
7 #Copyright (c) 2013, the IPython Development Team.
8 #
8 #
9 #Distributed under the terms of the Modified BSD License.
9 #Distributed under the terms of the Modified BSD License.
10 #
10 #
11 #The full license is in the file COPYING.txt, distributed with this software.
11 #The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 #Imports
15 #Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 # Stdlib imports
18 # Stdlib imports
19 from __future__ import print_function
19 from __future__ import print_function
20 import sys
20 import sys
21 import os
21 import os
22 import glob
22 import glob
23
23
24 # From IPython
24 # From IPython
25 from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags
25 from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags
26 from IPython.config import catch_config_error, Configurable
26 from IPython.config import catch_config_error, Configurable
27 from IPython.utils.traitlets import (
27 from IPython.utils.traitlets import (
28 Unicode, List, Instance, DottedObjectName, Type, CaselessStrEnum,
28 Unicode, List, Instance, DottedObjectName, Type, CaselessStrEnum,
29 )
29 )
30 from IPython.utils.importstring import import_item
30 from IPython.utils.importstring import import_item
31
31
32 from .exporters.export import export_by_name, get_export_names, ExporterNameError
32 from .exporters.export import export_by_name, get_export_names, ExporterNameError
33 from IPython.nbconvert import exporters, transformers, writers
33 from IPython.nbconvert import exporters, transformers, writers, post_processors
34 from .utils.base import NbConvertBase
34 from .utils.base import NbConvertBase
35 from .utils.exceptions import ConversionException
35 from .utils.exceptions import ConversionException
36
36
37 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
38 #Classes and functions
38 #Classes and functions
39 #-----------------------------------------------------------------------------
39 #-----------------------------------------------------------------------------
40
40
41 class DottedOrNone(DottedObjectName):
42 """
43 A string holding a valid dotted object name in Python, such as A.b3._c
44 Also allows for None type."""
45
46 default_value = u''
47
48 def validate(self, obj, value):
49 if value is not None and len(value) > 0:
50 return super(DottedOrNone, self).validate(obj, value)
51 else:
52 return value
53
41 nbconvert_aliases = {}
54 nbconvert_aliases = {}
42 nbconvert_aliases.update(base_aliases)
55 nbconvert_aliases.update(base_aliases)
43 nbconvert_aliases.update({
56 nbconvert_aliases.update({
44 'format' : 'NbConvertApp.export_format',
57 'to' : 'NbConvertApp.export_format',
58 'template' : 'Exporter.template_file',
45 'notebooks' : 'NbConvertApp.notebooks',
59 'notebooks' : 'NbConvertApp.notebooks',
46 'writer' : 'NbConvertApp.writer_class',
60 'writer' : 'NbConvertApp.writer_class',
61 'post': 'NbConvertApp.post_processor_class'
47 })
62 })
48
63
49 nbconvert_flags = {}
64 nbconvert_flags = {}
50 nbconvert_flags.update(base_flags)
65 nbconvert_flags.update(base_flags)
51 nbconvert_flags.update({
66 nbconvert_flags.update({
52 'stdout' : (
67 'stdout' : (
53 {'NbConvertApp' : {'writer_class' : "StdoutWriter"}},
68 {'NbConvertApp' : {'writer_class' : "StdoutWriter"}},
54 "Write notebook output to stdout instead of files."
69 "Write notebook output to stdout instead of files."
55 )
70 )
56 })
71 })
57
72
58
73
59 class NbConvertApp(BaseIPythonApplication):
74 class NbConvertApp(BaseIPythonApplication):
60 """Application used to convert to and from notebook file type (*.ipynb)"""
75 """Application used to convert to and from notebook file type (*.ipynb)"""
61
76
62 name = 'ipython-nbconvert'
77 name = 'ipython-nbconvert'
63 aliases = nbconvert_aliases
78 aliases = nbconvert_aliases
64 flags = nbconvert_flags
79 flags = nbconvert_flags
65
80
66 def _classes_default(self):
81 def _classes_default(self):
67 classes = [NbConvertBase]
82 classes = [NbConvertBase]
68 for pkg in (exporters, transformers, writers):
83 for pkg in (exporters, transformers, writers):
69 for name in dir(pkg):
84 for name in dir(pkg):
70 cls = getattr(pkg, name)
85 cls = getattr(pkg, name)
71 if isinstance(cls, type) and issubclass(cls, Configurable):
86 if isinstance(cls, type) and issubclass(cls, Configurable):
72 classes.append(cls)
87 classes.append(cls)
73 return classes
88 return classes
74
89
75 description = Unicode(
90 description = Unicode(
76 u"""This application is used to convert notebook files (*.ipynb)
91 u"""This application is used to convert notebook files (*.ipynb)
77 to various other formats.""")
92 to various other formats.
93
94 WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.""")
78
95
79 examples = Unicode(u"""
96 examples = Unicode(u"""
80 The simplest way to use nbconvert is
97 The simplest way to use nbconvert is
81
98
82 > ipython nbconvert mynotebook.ipynb
99 > ipython nbconvert mynotebook.ipynb
83
100
84 which will convert mynotebook.ipynb to the default format (probably HTML).
101 which will convert mynotebook.ipynb to the default format (probably HTML).
85
102
86 You can specify the export format with `--format`.
103 You can specify the export format with `--to`.
87 Options include {0}
104 Options include {0}
88
105
89 > ipython nbconvert --format latex mynotebook.ipnynb
106 > ipython nbconvert --to latex mynotebook.ipnynb
107
108 Both HTML and LaTeX support multiple output templates. LaTeX includes
109 'basic', 'book', and 'article'. HTML includes 'basic' and 'full'. You
110 can specify the flavor of the format used.
111
112 > ipython nbconvert --to html --template basic mynotebook.ipynb
90
113
91 You can also pipe the output to stdout, rather than a file
114 You can also pipe the output to stdout, rather than a file
92
115
93 > ipython nbconvert mynotebook.ipynb --stdout
116 > ipython nbconvert mynotebook.ipynb --stdout
117
118 A post-processor can be used to compile a PDF
119
120 > ipython nbconvert mynotebook.ipynb --to latex --post PDF
94
121
95 Multiple notebooks can be given at the command line in a couple of
122 Multiple notebooks can be given at the command line in a couple of
96 different ways:
123 different ways:
97
124
98 > ipython nbconvert notebook*.ipynb
125 > ipython nbconvert notebook*.ipynb
99 > ipython nbconvert notebook1.ipynb notebook2.ipynb
126 > ipython nbconvert notebook1.ipynb notebook2.ipynb
100
127
101 or you can specify the notebooks list in a config file, containing::
128 or you can specify the notebooks list in a config file, containing::
102
129
103 c.NbConvertApp.notebooks = ["my_notebook.ipynb"]
130 c.NbConvertApp.notebooks = ["my_notebook.ipynb"]
104
131
105 > ipython nbconvert --config mycfg.py
132 > ipython nbconvert --config mycfg.py
106 """.format(get_export_names()))
133 """.format(get_export_names()))
134
107 # Writer specific variables
135 # Writer specific variables
108 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
136 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
109 help="""Instance of the writer class used to write the
137 help="""Instance of the writer class used to write the
110 results of the conversion.""")
138 results of the conversion.""")
111 writer_class = DottedObjectName('FilesWriter', config=True,
139 writer_class = DottedObjectName('FilesWriter', config=True,
112 help="""Writer class used to write the
140 help="""Writer class used to write the
113 results of the conversion""")
141 results of the conversion""")
114 writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter',
142 writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter',
115 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter',
143 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter',
116 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'}
144 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'}
117 writer_factory = Type()
145 writer_factory = Type()
118
146
119 def _writer_class_changed(self, name, old, new):
147 def _writer_class_changed(self, name, old, new):
120 if new in self.writer_aliases:
148 if new in self.writer_aliases:
121 new = self.writer_aliases[new]
149 new = self.writer_aliases[new]
122 self.writer_factory = import_item(new)
150 self.writer_factory = import_item(new)
123
151
152 # Post-processor specific variables
153 post_processor = Instance('IPython.nbconvert.post_processors.base.PostProcessorBase',
154 help="""Instance of the PostProcessor class used to write the
155 results of the conversion.""")
156
157 post_processor_class = DottedOrNone(config=True,
158 help="""PostProcessor class used to write the
159 results of the conversion""")
160 post_processor_aliases = {'PDF': 'IPython.nbconvert.post_processors.pdf.PDFPostProcessor'}
161 post_processor_factory = Type()
162
163 def _post_processor_class_changed(self, name, old, new):
164 if new in self.post_processor_aliases:
165 new = self.post_processor_aliases[new]
166 if new:
167 self.post_processor_factory = import_item(new)
168
124
169
125 # Other configurable variables
170 # Other configurable variables
126 export_format = CaselessStrEnum(get_export_names(),
171 export_format = CaselessStrEnum(get_export_names(),
127 default_value="full_html",
172 default_value="html",
128 config=True,
173 config=True,
129 help="""The export format to be used."""
174 help="""The export format to be used."""
130 )
175 )
131
176
132 notebooks = List([], config=True, help="""List of notebooks to convert.
177 notebooks = List([], config=True, help="""List of notebooks to convert.
133 Wildcards are supported.
178 Wildcards are supported.
134 Filenames passed positionally will be added to the list.
179 Filenames passed positionally will be added to the list.
135 """)
180 """)
136
181
137 @catch_config_error
182 @catch_config_error
138 def initialize(self, argv=None):
183 def initialize(self, argv=None):
139 super(NbConvertApp, self).initialize(argv)
184 super(NbConvertApp, self).initialize(argv)
140 self.init_syspath()
185 self.init_syspath()
141 self.init_notebooks()
186 self.init_notebooks()
142 self.init_writer()
187 self.init_writer()
188 self.init_post_processor()
189
143
190
144
191
145 def init_syspath(self):
192 def init_syspath(self):
146 """
193 """
147 Add the cwd to the sys.path ($PYTHONPATH)
194 Add the cwd to the sys.path ($PYTHONPATH)
148 """
195 """
149 sys.path.insert(0, os.getcwd())
196 sys.path.insert(0, os.getcwd())
150
197
151
198
152 def init_notebooks(self):
199 def init_notebooks(self):
153 """Construct the list of notebooks.
200 """Construct the list of notebooks.
154 If notebooks are passed on the command-line,
201 If notebooks are passed on the command-line,
155 they override notebooks specified in config files.
202 they override notebooks specified in config files.
156 Glob each notebook to replace notebook patterns with filenames.
203 Glob each notebook to replace notebook patterns with filenames.
157 """
204 """
158
205
159 # Specifying notebooks on the command-line overrides (rather than adds)
206 # Specifying notebooks on the command-line overrides (rather than adds)
160 # the notebook list
207 # the notebook list
161 if self.extra_args:
208 if self.extra_args:
162 patterns = self.extra_args
209 patterns = self.extra_args
163 else:
210 else:
164 patterns = self.notebooks
211 patterns = self.notebooks
165
212
166 # Use glob to replace all the notebook patterns with filenames.
213 # Use glob to replace all the notebook patterns with filenames.
167 filenames = []
214 filenames = []
168 for pattern in patterns:
215 for pattern in patterns:
169
216
170 # Use glob to find matching filenames. Allow the user to convert
217 # Use glob to find matching filenames. Allow the user to convert
171 # notebooks without having to type the extension.
218 # notebooks without having to type the extension.
172 globbed_files = glob.glob(pattern)
219 globbed_files = glob.glob(pattern)
173 globbed_files.extend(glob.glob(pattern + '.ipynb'))
220 globbed_files.extend(glob.glob(pattern + '.ipynb'))
174
221
175 for filename in globbed_files:
222 for filename in globbed_files:
176 if not filename in filenames:
223 if not filename in filenames:
177 filenames.append(filename)
224 filenames.append(filename)
178 self.notebooks = filenames
225 self.notebooks = filenames
179
226
180 def init_writer(self):
227 def init_writer(self):
181 """
228 """
182 Initialize the writer (which is stateless)
229 Initialize the writer (which is stateless)
183 """
230 """
184 self._writer_class_changed(None, self.writer_class, self.writer_class)
231 self._writer_class_changed(None, self.writer_class, self.writer_class)
185 self.writer = self.writer_factory(parent=self)
232 self.writer = self.writer_factory(parent=self)
186
233
234 def init_post_processor(self):
235 """
236 Initialize the post_processor (which is stateless)
237 """
238 self._post_processor_class_changed(None, self.post_processor_class,
239 self.post_processor_class)
240 if self.post_processor_factory:
241 self.post_processor = self.post_processor_factory(parent=self)
242
187 def start(self):
243 def start(self):
188 """
244 """
189 Ran after initialization completed
245 Ran after initialization completed
190 """
246 """
191 super(NbConvertApp, self).start()
247 super(NbConvertApp, self).start()
192 self.convert_notebooks()
248 self.convert_notebooks()
193
249
194 def convert_notebooks(self):
250 def convert_notebooks(self):
195 """
251 """
196 Convert the notebooks in the self.notebook traitlet
252 Convert the notebooks in the self.notebook traitlet
197 """
253 """
198 # Export each notebook
254 # Export each notebook
199 conversion_success = 0
255 conversion_success = 0
200 for notebook_filename in self.notebooks:
256 for notebook_filename in self.notebooks:
201
257
202 # Get a unique key for the notebook and set it in the resources object.
258 # Get a unique key for the notebook and set it in the resources object.
203 basename = os.path.basename(notebook_filename)
259 basename = os.path.basename(notebook_filename)
204 notebook_name = basename[:basename.rfind('.')]
260 notebook_name = basename[:basename.rfind('.')]
205 resources = {}
261 resources = {}
206 resources['unique_key'] = notebook_name
262 resources['unique_key'] = notebook_name
207 resources['output_files_dir'] = '%s_files' % notebook_name
263 resources['output_files_dir'] = '%s_files' % notebook_name
208
264
209 # Try to export
265 # Try to export
210 try:
266 try:
211 output, resources = export_by_name(self.export_format,
267 output, resources = export_by_name(self.export_format,
212 notebook_filename,
268 notebook_filename,
213 resources=resources,
269 resources=resources,
214 config=self.config)
270 config=self.config)
215 except ExporterNameError as e:
271 except ExporterNameError as e:
216 print("Error while converting '%s': '%s' exporter not found."
272 print("Error while converting '%s': '%s' exporter not found."
217 %(notebook_filename, self.export_format),
273 %(notebook_filename, self.export_format),
218 file=sys.stderr)
274 file=sys.stderr)
219 print("Known exporters are:",
275 print("Known exporters are:",
220 "\n\t" + "\n\t".join(get_export_names()),
276 "\n\t" + "\n\t".join(get_export_names()),
221 file=sys.stderr)
277 file=sys.stderr)
222 self.exit(1)
278 self.exit(1)
223 except ConversionException as e:
279 except ConversionException as e:
224 print("Error while converting '%s': %s" %(notebook_filename, e),
280 print("Error while converting '%s': %s" %(notebook_filename, e),
225 file=sys.stderr)
281 file=sys.stderr)
226 self.exit(1)
282 self.exit(1)
227 else:
283 else:
228 self.writer.write(output, resources, notebook_name=notebook_name)
284 write_resultes = self.writer.write(output, resources, notebook_name=notebook_name)
285
286 #Post-process if post processor has been defined.
287 if hasattr(self, 'post_processor') and self.post_processor:
288 self.post_processor(write_resultes)
229 conversion_success += 1
289 conversion_success += 1
230
290
231 # If nothing was converted successfully, help the user.
291 # If nothing was converted successfully, help the user.
232 if conversion_success == 0:
292 if conversion_success == 0:
233 self.print_help()
293 self.print_help()
234 sys.exit(-1)
294 sys.exit(-1)
235
295
236
296
237 #-----------------------------------------------------------------------------
297 #-----------------------------------------------------------------------------
238 # Main entry point
298 # Main entry point
239 #-----------------------------------------------------------------------------
299 #-----------------------------------------------------------------------------
240
300
241 launch_new_instance = NbConvertApp.launch_instance
301 launch_new_instance = NbConvertApp.launch_instance
@@ -1,34 +1,35 b''
1 """
1 """
2 Exporter for exporting notebooks to Sphinx 'Manual' style latex. Latex
2 Basic post processor
3 formatted for use with PDFLatex.
4 """
3 """
5 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
5 #Copyright (c) 2013, the IPython Development Team.
7 #
6 #
8 # Distributed under the terms of the Modified BSD License.
7 #Distributed under the terms of the Modified BSD License.
9 #
8 #
10 # 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.
11 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
12
11
13 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
14 # Imports
13 # Imports
15 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
16
15
17 from IPython.utils.traitlets import Unicode
16 from ..utils.base import NbConvertBase
18
17
19 from .sphinx_howto import SphinxHowtoExporter
20
18
21 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
22 # Classes
20 # Classes
23 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 class PostProcessorBase(NbConvertBase):
24
23
25 class SphinxManualExporter(SphinxHowtoExporter):
24 def __call__(self, input):
26 """
25 """
27 Exports Sphinx "Manual" LaTeX documents. The Sphinx "Manual" exporter
26 See def call() ...
28 produces book like latex output for use with PDFLatex.
27 """
29 """
28 self.call(input)
30
29
31 template_file = Unicode(
30
32 'sphinx_manual', config=True,
31 def call(self, input):
33 help="Name of the template file to use")
32 """
34 No newline at end of file
33 Post-process output from a writer.
34 """
35 raise NotImplementedError('call')
1 NO CONTENT: file renamed from IPython/nbconvert/templates/basichtml.tpl to IPython/nbconvert/templates/html_basic.tpl
NO CONTENT: file renamed from IPython/nbconvert/templates/basichtml.tpl to IPython/nbconvert/templates/html_basic.tpl
@@ -1,68 +1,68 b''
1 {%- extends 'basichtml.tpl' -%}
1 {%- extends 'html_basic.tpl' -%}
2
2
3 {%- block header -%}<!DOCTYPE html>
3 {%- block header -%}<!DOCTYPE html>
4 <html>
4 <html>
5 <head>
5 <head>
6 <meta charset="UTF-8">
6 <meta charset="UTF-8">
7 <title>[{{nb.metadata.name}}]</title>
7 <title>[{{nb.metadata.name}}]</title>
8 {% for css in resources.inlining.css -%}
8 {% for css in resources.inlining.css -%}
9 <style type="text/css">
9 <style type="text/css">
10 {{css}}
10 {{css}}
11 </style>
11 </style>
12 {% endfor %}
12 {% endfor %}
13
13
14 <style type="text/css">
14 <style type="text/css">
15 /* Overrides of notebook CSS for static HTML export */
15 /* Overrides of notebook CSS for static HTML export */
16 body {
16 body {
17 overflow: visible;
17 overflow: visible;
18 padding: 8px;
18 padding: 8px;
19 }
19 }
20 .input_area {
20 .input_area {
21 padding: 0.2em;
21 padding: 0.2em;
22 }
22 }
23
23
24 pre {
24 pre {
25 padding: 0.2em;
25 padding: 0.2em;
26 border: none;
26 border: none;
27 margin: 0px;
27 margin: 0px;
28 font-size: 13px;
28 font-size: 13px;
29 }
29 }
30 </style>
30 </style>
31
31
32 <!-- Custom stylesheet, it must be in the same directory as the html file -->
32 <!-- Custom stylesheet, it must be in the same directory as the html file -->
33 <link rel="stylesheet" href="custom.css">
33 <link rel="stylesheet" href="custom.css">
34
34
35 <script src="https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS_HTML" type="text/javascript">
35 <script src="https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS_HTML" type="text/javascript">
36
36
37 </script>
37 </script>
38 <script type="text/javascript">
38 <script type="text/javascript">
39 init_mathjax = function() {
39 init_mathjax = function() {
40 if (window.MathJax) {
40 if (window.MathJax) {
41 // MathJax loaded
41 // MathJax loaded
42 MathJax.Hub.Config({
42 MathJax.Hub.Config({
43 tex2jax: {
43 tex2jax: {
44 inlineMath: [ ['$','$'], ["\\(","\\)"] ],
44 inlineMath: [ ['$','$'], ["\\(","\\)"] ],
45 displayMath: [ ['$$','$$'], ["\\[","\\]"] ]
45 displayMath: [ ['$$','$$'], ["\\[","\\]"] ]
46 },
46 },
47 displayAlign: 'left', // Change this to 'center' to center equations.
47 displayAlign: 'left', // Change this to 'center' to center equations.
48 "HTML-CSS": {
48 "HTML-CSS": {
49 styles: {'.MathJax_Display': {"margin": 0}}
49 styles: {'.MathJax_Display': {"margin": 0}}
50 }
50 }
51 });
51 });
52 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
52 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
53 }
53 }
54 }
54 }
55 init_mathjax();
55 init_mathjax();
56 </script>
56 </script>
57 </head>
57 </head>
58 {%- endblock header -%}
58 {%- endblock header -%}
59
59
60
60
61 {% block body %}
61 {% block body %}
62 <body>{{ super() }}
62 <body>{{ super() }}
63 </body>
63 </body>
64 {%- endblock body %}
64 {%- endblock body %}
65
65
66
66
67 {% block footer %}
67 {% block footer %}
68 </html>{% endblock footer %}
68 </html>{% endblock footer %}
@@ -1,25 +1,25 b''
1 ((============================================================================
1 ((============================================================================
2 NBConvert Sphinx-Latex HowTo Template
2 NBConvert Sphinx-Latex HowTo Template
3
3
4 Purpose: Allow export of PDF friendly Latex inspired by Sphinx HowTo
4 Purpose: Allow export of PDF friendly Latex inspired by Sphinx HowTo
5 document style. Most of the is derived directly from Sphinx source.
5 document style. Most of the is derived directly from Sphinx source.
6
6
7 Inheritance: null>display_priority>latex_base->latex_sphinx_base
7 Inheritance: null>display_priority>sphinx
8
8
9 ==========================================================================))
9 ==========================================================================))
10
10
11 ((*- extends 'sphinx_base.tplx' -*))
11 ((*- extends 'sphinx.tplx' -*))
12
12
13 ((* set parentdocumentclass = 'article' *))
13 ((* set parentdocumentclass = 'article' *))
14 ((* set documentclass = 'howto' *))
14 ((* set documentclass = 'howto' *))
15
15
16 ((* block h1 -*))part((* endblock h1 -*))
16 ((* block h1 -*))part((* endblock h1 -*))
17 ((* block h2 -*))section((* endblock h2 -*))
17 ((* block h2 -*))section((* endblock h2 -*))
18 ((* block h3 -*))subsection((* endblock h3 -*))
18 ((* block h3 -*))subsection((* endblock h3 -*))
19 ((* block h4 -*))subsubsection((* endblock h4 -*))
19 ((* block h4 -*))subsubsection((* endblock h4 -*))
20 ((* block h5 -*))paragraph((* endblock h5 -*))
20 ((* block h5 -*))paragraph((* endblock h5 -*))
21 ((* block h6 -*))subparagraph((* endblock h6 -*))
21 ((* block h6 -*))subparagraph((* endblock h6 -*))
22
22
23 % Diasble table of contents for howto
23 % Diasble table of contents for howto
24 ((* block toc *))
24 ((* block toc *))
25 ((* endblock toc *))
25 ((* endblock toc *))
@@ -1,258 +1,260 b''
1 ((*- extends 'display_priority.tplx' -*))
1 ((*- extends 'display_priority.tplx' -*))
2
2
3 \nonstopmode
4
3 ((* block in_prompt *))((* endblock in_prompt *))
5 ((* block in_prompt *))((* endblock in_prompt *))
4
6
5 ((* block output_prompt *))((* endblock output_prompt *))
7 ((* block output_prompt *))((* endblock output_prompt *))
6
8
7 ((* block codecell *))\begin{codecell}((( super() )))
9 ((* block codecell *))\begin{codecell}((( super() )))
8 \end{codecell}
10 \end{codecell}
9 ((* endblock *))
11 ((* endblock *))
10
12
11 ((* block input *))
13 ((* block input *))
12 \begin{codeinput}
14 \begin{codeinput}
13 \begin{lstlisting}
15 \begin{lstlisting}
14 ((( cell.input )))
16 ((( cell.input )))
15 \end{lstlisting}
17 \end{lstlisting}
16 \end{codeinput}
18 \end{codeinput}
17 ((* endblock input *))
19 ((* endblock input *))
18
20
19
21
20 ((= Those Two are for error displaying
22 ((= Those Two are for error displaying
21 even if the first one seem to do nothing,
23 even if the first one seem to do nothing,
22 it introduces a new line
24 it introduces a new line
23
25
24 =))
26 =))
25 ((* block pyerr *))
27 ((* block pyerr *))
26 \begin{traceback}
28 \begin{traceback}
27 \begin{verbatim}((( super() )))
29 \begin{verbatim}((( super() )))
28 \end{verbatim}
30 \end{verbatim}
29 \end{traceback}
31 \end{traceback}
30 ((* endblock pyerr *))
32 ((* endblock pyerr *))
31
33
32 ((* block traceback_line *))
34 ((* block traceback_line *))
33 ((( line |indent| strip_ansi )))((* endblock traceback_line *))
35 ((( line |indent| strip_ansi )))((* endblock traceback_line *))
34 ((= .... =))
36 ((= .... =))
35
37
36
38
37 ((*- block output_group -*))
39 ((*- block output_group -*))
38 \begin{codeoutput}
40 \begin{codeoutput}
39 ((( super() )))
41 ((( super() )))
40 \end{codeoutput}((* endblock *))
42 \end{codeoutput}((* endblock *))
41
43
42 ((*- block data_png -*))
44 ((*- block data_png -*))
43 \begin{center}
45 \begin{center}
44 \includegraphics[max size={0.7\textwidth}{0.9\textheight}]{(((output.png_filename)))}
46 \includegraphics[max size={0.7\textwidth}{0.9\textheight}]{(((output.png_filename)))}
45 \par
47 \par
46 \end{center}
48 \end{center}
47 ((*- endblock -*))
49 ((*- endblock -*))
48
50
49 ((*- block data_jpg -*))
51 ((*- block data_jpg -*))
50 \begin{center}
52 \begin{center}
51 \includegraphics[max size={0.7\textwidth}{0.9\textheight}]{(((output.jpeg_filename)))}
53 \includegraphics[max size={0.7\textwidth}{0.9\textheight}]{(((output.jpeg_filename)))}
52 \par
54 \par
53 \end{center}
55 \end{center}
54 ((*- endblock -*))
56 ((*- endblock -*))
55
57
56 ((*- block data_svg -*))
58 ((*- block data_svg -*))
57 \begin{center}
59 \begin{center}
58 \includegraphics[width=0.7\textwidth]{(((output.svg_filename)))}
60 \includegraphics[width=0.7\textwidth]{(((output.svg_filename)))}
59 \par
61 \par
60 \end{center}
62 \end{center}
61 ((*- endblock -*))
63 ((*- endblock -*))
62
64
63 ((*- block data_pdf -*))
65 ((*- block data_pdf -*))
64 \begin{center}
66 \begin{center}
65 \includegraphics[width=0.7\textwidth]{(((output.pdf_filename)))}
67 \includegraphics[width=0.7\textwidth]{(((output.pdf_filename)))}
66 \par
68 \par
67 \end{center}
69 \end{center}
68 ((*- endblock -*))
70 ((*- endblock -*))
69
71
70 ((* block pyout *))
72 ((* block pyout *))
71 ((* block data_priority scoped *))((( super() )))((* endblock *))
73 ((* block data_priority scoped *))((( super() )))((* endblock *))
72 ((* endblock pyout *))
74 ((* endblock pyout *))
73
75
74 ((* block data_text *))
76 ((* block data_text *))
75 \begin{verbatim}
77 \begin{verbatim}
76 ((( output.text )))
78 ((( output.text )))
77 \end{verbatim}
79 \end{verbatim}
78 ((* endblock *))
80 ((* endblock *))
79
81
80 ((* block data_latex -*))
82 ((* block data_latex -*))
81 ((*- if output.latex.startswith('$'): -*)) \begin{equation*}
83 ((*- if output.latex.startswith('$'): -*)) \begin{equation*}
82 ((( output.latex | strip_dollars)))
84 ((( output.latex | strip_dollars)))
83 \end{equation*}
85 \end{equation*}
84 ((*- else -*)) ((( output.latex ))) ((*- endif *))
86 ((*- else -*)) ((( output.latex ))) ((*- endif *))
85 ((* endblock *))
87 ((* endblock *))
86
88
87 ((* block stream *))
89 ((* block stream *))
88 \begin{Verbatim}[commandchars=\\\{\}]
90 \begin{Verbatim}[commandchars=\\\{\}]
89 ((( output.text | ansi2latex)))
91 ((( output.text | ansi2latex)))
90 \end{Verbatim}
92 \end{Verbatim}
91 ((* endblock stream *))
93 ((* endblock stream *))
92
94
93 ((* block markdowncell scoped *))((( cell.source | markdown2latex )))
95 ((* block markdowncell scoped *))((( cell.source | markdown2latex )))
94 ((* endblock markdowncell *))
96 ((* endblock markdowncell *))
95
97
96 ((* block headingcell scoped -*))
98 ((* block headingcell scoped -*))
97 ((( ('#' * cell.level + cell.source) | replace('\n', ' ') | markdown2latex )))
99 ((( ('#' * cell.level + cell.source) | replace('\n', ' ') | markdown2latex )))
98 ((* endblock headingcell *))
100 ((* endblock headingcell *))
99
101
100 ((* block rawcell scoped *))
102 ((* block rawcell scoped *))
101 ((( cell.source | comment_lines )))
103 ((( cell.source | comment_lines )))
102 ((* endblock rawcell *))
104 ((* endblock rawcell *))
103
105
104 ((* block unknowncell scoped *))
106 ((* block unknowncell scoped *))
105 unknown type (((cell.type)))
107 unknown type (((cell.type)))
106 ((* endblock unknowncell *))
108 ((* endblock unknowncell *))
107
109
108
110
109
111
110 ((* block body *))
112 ((* block body *))
111
113
112 ((* block bodyBegin *))
114 ((* block bodyBegin *))
113 \begin{document}
115 \begin{document}
114 ((* endblock bodyBegin *))
116 ((* endblock bodyBegin *))
115
117
116 ((( super() )))
118 ((( super() )))
117
119
118 ((* block bodyEnd *))
120 ((* block bodyEnd *))
119 \end{document}
121 \end{document}
120 ((* endblock bodyEnd *))
122 ((* endblock bodyEnd *))
121 ((* endblock body *))
123 ((* endblock body *))
122
124
123 ((* block header *))
125 ((* block header *))
124 %% This file was auto-generated by IPython.
126 %% This file was auto-generated by IPython.
125 %% Conversion from the original notebook file:
127 %% Conversion from the original notebook file:
126 %%
128 %%
127 \documentclass[11pt,english]{article}
129 \documentclass[11pt,english]{article}
128
130
129 %% This is the automatic preamble used by IPython. Note that it does *not*
131 %% This is the automatic preamble used by IPython. Note that it does *not*
130 %% include a documentclass declaration, that is added at runtime to the overall
132 %% include a documentclass declaration, that is added at runtime to the overall
131 %% document.
133 %% document.
132
134
133 \usepackage{amsmath}
135 \usepackage{amsmath}
134 \usepackage{amssymb}
136 \usepackage{amssymb}
135 \usepackage{graphicx}
137 \usepackage{graphicx}
136 \usepackage{ucs}
138 \usepackage{ucs}
137 \usepackage[utf8x]{inputenc}
139 \usepackage[utf8x]{inputenc}
138
140
139 % Scale down larger images
141 % Scale down larger images
140 \usepackage[export]{adjustbox}
142 \usepackage[export]{adjustbox}
141
143
142 %fancy verbatim
144 %fancy verbatim
143 \usepackage{fancyvrb}
145 \usepackage{fancyvrb}
144 % needed for markdown enumerations to work
146 % needed for markdown enumerations to work
145 \usepackage{enumerate}
147 \usepackage{enumerate}
146
148
147 % Slightly bigger margins than the latex defaults
149 % Slightly bigger margins than the latex defaults
148 \usepackage{geometry}
150 \usepackage{geometry}
149 \geometry{verbose,tmargin=3cm,bmargin=3cm,lmargin=2.5cm,rmargin=2.5cm}
151 \geometry{verbose,tmargin=3cm,bmargin=3cm,lmargin=2.5cm,rmargin=2.5cm}
150
152
151 % Define a few colors for use in code, links and cell shading
153 % Define a few colors for use in code, links and cell shading
152 \usepackage{color}
154 \usepackage{color}
153 \definecolor{orange}{cmyk}{0,0.4,0.8,0.2}
155 \definecolor{orange}{cmyk}{0,0.4,0.8,0.2}
154 \definecolor{darkorange}{rgb}{.71,0.21,0.01}
156 \definecolor{darkorange}{rgb}{.71,0.21,0.01}
155 \definecolor{darkgreen}{rgb}{.12,.54,.11}
157 \definecolor{darkgreen}{rgb}{.12,.54,.11}
156 \definecolor{myteal}{rgb}{.26, .44, .56}
158 \definecolor{myteal}{rgb}{.26, .44, .56}
157 \definecolor{gray}{gray}{0.45}
159 \definecolor{gray}{gray}{0.45}
158 \definecolor{lightgray}{gray}{.95}
160 \definecolor{lightgray}{gray}{.95}
159 \definecolor{mediumgray}{gray}{.8}
161 \definecolor{mediumgray}{gray}{.8}
160 \definecolor{inputbackground}{rgb}{.95, .95, .85}
162 \definecolor{inputbackground}{rgb}{.95, .95, .85}
161 \definecolor{outputbackground}{rgb}{.95, .95, .95}
163 \definecolor{outputbackground}{rgb}{.95, .95, .95}
162 \definecolor{traceback}{rgb}{1, .95, .95}
164 \definecolor{traceback}{rgb}{1, .95, .95}
163
165
164 % new ansi colors
166 % new ansi colors
165 \definecolor{brown}{rgb}{0.54,0.27,0.07}
167 \definecolor{brown}{rgb}{0.54,0.27,0.07}
166 \definecolor{purple}{rgb}{0.5,0.0,0.5}
168 \definecolor{purple}{rgb}{0.5,0.0,0.5}
167 \definecolor{darkgray}{gray}{0.25}
169 \definecolor{darkgray}{gray}{0.25}
168 \definecolor{lightred}{rgb}{1.0,0.39,0.28}
170 \definecolor{lightred}{rgb}{1.0,0.39,0.28}
169 \definecolor{lightgreen}{rgb}{0.48,0.99,0.0}
171 \definecolor{lightgreen}{rgb}{0.48,0.99,0.0}
170 \definecolor{lightblue}{rgb}{0.53,0.81,0.92}
172 \definecolor{lightblue}{rgb}{0.53,0.81,0.92}
171 \definecolor{lightpurple}{rgb}{0.87,0.63,0.87}
173 \definecolor{lightpurple}{rgb}{0.87,0.63,0.87}
172 \definecolor{lightcyan}{rgb}{0.5,1.0,0.83}
174 \definecolor{lightcyan}{rgb}{0.5,1.0,0.83}
173
175
174 % Framed environments for code cells (inputs, outputs, errors, ...). The
176 % Framed environments for code cells (inputs, outputs, errors, ...). The
175 % various uses of \unskip (or not) at the end were fine-tuned by hand, so don't
177 % various uses of \unskip (or not) at the end were fine-tuned by hand, so don't
176 % randomly change them unless you're sure of the effect it will have.
178 % randomly change them unless you're sure of the effect it will have.
177 \usepackage{framed}
179 \usepackage{framed}
178
180
179 % remove extraneous vertical space in boxes
181 % remove extraneous vertical space in boxes
180 \setlength\fboxsep{0pt}
182 \setlength\fboxsep{0pt}
181
183
182 % codecell is the whole input+output set of blocks that a Code cell can
184 % codecell is the whole input+output set of blocks that a Code cell can
183 % generate.
185 % generate.
184
186
185 % TODO: unfortunately, it seems that using a framed codecell environment breaks
187 % TODO: unfortunately, it seems that using a framed codecell environment breaks
186 % the ability of the frames inside of it to be broken across pages. This
188 % the ability of the frames inside of it to be broken across pages. This
187 % causes at least the problem of having lots of empty space at the bottom of
189 % causes at least the problem of having lots of empty space at the bottom of
188 % pages as new frames are moved to the next page, and if a single frame is too
190 % pages as new frames are moved to the next page, and if a single frame is too
189 % long to fit on a page, will completely stop latex from compiling the
191 % long to fit on a page, will completely stop latex from compiling the
190 % document. So unless we figure out a solution to this, we'll have to instead
192 % document. So unless we figure out a solution to this, we'll have to instead
191 % leave the codecell env. as empty. I'm keeping the original codecell
193 % leave the codecell env. as empty. I'm keeping the original codecell
192 % definition here (a thin vertical bar) for reference, in case we find a
194 % definition here (a thin vertical bar) for reference, in case we find a
193 % solution to the page break issue.
195 % solution to the page break issue.
194
196
195 %% \newenvironment{codecell}{%
197 %% \newenvironment{codecell}{%
196 %% \def\FrameCommand{\color{mediumgray} \vrule width 1pt \hspace{5pt}}%
198 %% \def\FrameCommand{\color{mediumgray} \vrule width 1pt \hspace{5pt}}%
197 %% \MakeFramed{\vspace{-0.5em}}}
199 %% \MakeFramed{\vspace{-0.5em}}}
198 %% {\unskip\endMakeFramed}
200 %% {\unskip\endMakeFramed}
199
201
200 % For now, make this a no-op...
202 % For now, make this a no-op...
201 \newenvironment{codecell}{}
203 \newenvironment{codecell}{}
202
204
203 \newenvironment{codeinput}{%
205 \newenvironment{codeinput}{%
204 \def\FrameCommand{\colorbox{inputbackground}}%
206 \def\FrameCommand{\colorbox{inputbackground}}%
205 \MakeFramed{\advance\hsize-\width \FrameRestore}}
207 \MakeFramed{\advance\hsize-\width \FrameRestore}}
206 {\unskip\endMakeFramed}
208 {\unskip\endMakeFramed}
207
209
208 \newenvironment{codeoutput}{%
210 \newenvironment{codeoutput}{%
209 \def\FrameCommand{\colorbox{outputbackground}}%
211 \def\FrameCommand{\colorbox{outputbackground}}%
210 \vspace{-1.4em}
212 \vspace{-1.4em}
211 \MakeFramed{\advance\hsize-\width \FrameRestore}}
213 \MakeFramed{\advance\hsize-\width \FrameRestore}}
212 {\unskip\medskip\endMakeFramed}
214 {\unskip\medskip\endMakeFramed}
213
215
214 \newenvironment{traceback}{%
216 \newenvironment{traceback}{%
215 \def\FrameCommand{\colorbox{traceback}}%
217 \def\FrameCommand{\colorbox{traceback}}%
216 \MakeFramed{\advance\hsize-\width \FrameRestore}}
218 \MakeFramed{\advance\hsize-\width \FrameRestore}}
217 {\endMakeFramed}
219 {\endMakeFramed}
218
220
219 % Use and configure listings package for nicely formatted code
221 % Use and configure listings package for nicely formatted code
220 \usepackage{listingsutf8}
222 \usepackage{listingsutf8}
221 \lstset{
223 \lstset{
222 language=python,
224 language=python,
223 inputencoding=utf8x,
225 inputencoding=utf8x,
224 extendedchars=\true,
226 extendedchars=\true,
225 aboveskip=\smallskipamount,
227 aboveskip=\smallskipamount,
226 belowskip=\smallskipamount,
228 belowskip=\smallskipamount,
227 xleftmargin=2mm,
229 xleftmargin=2mm,
228 breaklines=true,
230 breaklines=true,
229 basicstyle=\small \ttfamily,
231 basicstyle=\small \ttfamily,
230 showstringspaces=false,
232 showstringspaces=false,
231 keywordstyle=\color{blue}\bfseries,
233 keywordstyle=\color{blue}\bfseries,
232 commentstyle=\color{myteal},
234 commentstyle=\color{myteal},
233 stringstyle=\color{darkgreen},
235 stringstyle=\color{darkgreen},
234 identifierstyle=\color{darkorange},
236 identifierstyle=\color{darkorange},
235 columns=fullflexible, % tighter character kerning, like verb
237 columns=fullflexible, % tighter character kerning, like verb
236 }
238 }
237
239
238 % The hyperref package gives us a pdf with properly built
240 % The hyperref package gives us a pdf with properly built
239 % internal navigation ('pdf bookmarks' for the table of contents,
241 % internal navigation ('pdf bookmarks' for the table of contents,
240 % internal cross-reference links, web links for URLs, etc.)
242 % internal cross-reference links, web links for URLs, etc.)
241 \usepackage{hyperref}
243 \usepackage{hyperref}
242 \hypersetup{
244 \hypersetup{
243 breaklinks=true, % so long urls are correctly broken across lines
245 breaklinks=true, % so long urls are correctly broken across lines
244 colorlinks=true,
246 colorlinks=true,
245 urlcolor=blue,
247 urlcolor=blue,
246 linkcolor=darkorange,
248 linkcolor=darkorange,
247 citecolor=darkgreen,
249 citecolor=darkgreen,
248 }
250 }
249
251
250 % hardcode size of all verbatim environments to be a bit smaller
252 % hardcode size of all verbatim environments to be a bit smaller
251 \makeatletter
253 \makeatletter
252 \g@addto@macro\@verbatim\small\topsep=0.5em\partopsep=0pt
254 \g@addto@macro\@verbatim\small\topsep=0.5em\partopsep=0pt
253 \makeatother
255 \makeatother
254
256
255 % Prevent overflowing lines due to urls and other hard-to-break entities.
257 % Prevent overflowing lines due to urls and other hard-to-break entities.
256 \sloppy
258 \sloppy
257
259
258 ((* endblock *))
260 ((* endblock *))
@@ -1,21 +1,21 b''
1 ((============================================================================
1 ((============================================================================
2 NBConvert Sphinx-Latex Manual Template
2 NBConvert Sphinx-Latex Manual Template
3
3
4 Purpose: Allow export of PDF friendly Latex inspired by Sphinx Manual
4 Purpose: Allow export of PDF friendly Latex inspired by Sphinx Manual
5 document style. Most of the is derived directly from Sphinx source.
5 document style. Most of the is derived directly from Sphinx source.
6
6
7 Inheritance: null>display_priority>latex_base->latex_sphinx_base
7 Inheritance: null>display_priority>sphinx
8
8
9 ==========================================================================))
9 ==========================================================================))
10
10
11 ((*- extends 'sphinx_base.tplx' -*))
11 ((*- extends 'sphinx.tplx' -*))
12
12
13 ((* set parentdocumentclass = 'report' *))
13 ((* set parentdocumentclass = 'report' *))
14 ((* set documentclass = 'manual' *))
14 ((* set documentclass = 'manual' *))
15
15
16 ((* block h1 -*))part((* endblock h1 -*))
16 ((* block h1 -*))part((* endblock h1 -*))
17 ((* block h2 -*))chapter((* endblock h2 -*))
17 ((* block h2 -*))chapter((* endblock h2 -*))
18 ((* block h3 -*))section((* endblock h3 -*))
18 ((* block h3 -*))section((* endblock h3 -*))
19 ((* block h4 -*))subsection((* endblock h4 -*))
19 ((* block h4 -*))subsection((* endblock h4 -*))
20 ((* block h5 -*))subsubsection((* endblock h5 -*))
20 ((* block h5 -*))subsubsection((* endblock h5 -*))
21 ((* block h6 -*))paragraph((* endblock h6 -*))
21 ((* block h6 -*))paragraph((* endblock h6 -*))
@@ -1,450 +1,452 b''
1 ((= NBConvert Sphinx-Latex Template
1 ((= NBConvert Sphinx-Latex Template
2
2
3 Purpose: Allow export of PDF friendly Latex inspired by Sphinx. Most of the
3 Purpose: Allow export of PDF friendly Latex inspired by Sphinx. Most of the
4 template is derived directly from Sphinx source.
4 template is derived directly from Sphinx source.
5
5
6 Inheritance: null>display_priority
6 Inheritance: null>display_priority
7
7
8 Note: For best display, use latex syntax highlighting. =))
8 Note: For best display, use latex syntax highlighting. =))
9
9
10 ((*- extends 'display_priority.tplx' -*))
10 ((*- extends 'display_priority.tplx' -*))
11
11
12 \nonstopmode
13
12 %==============================================================================
14 %==============================================================================
13 % Declarations
15 % Declarations
14 %==============================================================================
16 %==============================================================================
15
17
16 % In order to make sure that the input/output header follows the code it
18 % In order to make sure that the input/output header follows the code it
17 % preceeds, the needspace package is used to request that a certain
19 % preceeds, the needspace package is used to request that a certain
18 % amount of lines (specified by this variable) are reserved. If those
20 % amount of lines (specified by this variable) are reserved. If those
19 % lines aren't available on the current page, the documenter will break
21 % lines aren't available on the current page, the documenter will break
20 % to the next page and the header along with accomanying lines will be
22 % to the next page and the header along with accomanying lines will be
21 % rendered together. This value specifies the number of lines that
23 % rendered together. This value specifies the number of lines that
22 % the header will be forced to group with without a page break.
24 % the header will be forced to group with without a page break.
23 ((*- set min_header_lines = 4 -*))
25 ((*- set min_header_lines = 4 -*))
24
26
25 % This is the number of characters that are permitted per line. It's
27 % This is the number of characters that are permitted per line. It's
26 % important that this limit is set so characters do not run off the
28 % important that this limit is set so characters do not run off the
27 % edges of latex pages (since latex does not always seem smart enough
29 % edges of latex pages (since latex does not always seem smart enough
28 % to prevent this in some cases.) This is only applied to textual output
30 % to prevent this in some cases.) This is only applied to textual output
29 ((* if resources.sphinx.outputstyle == 'simple' *))
31 ((* if resources.sphinx.outputstyle == 'simple' *))
30 ((*- set wrap_size = 85 -*))
32 ((*- set wrap_size = 85 -*))
31 ((* elif resources.sphinx.outputstyle == 'notebook' *))
33 ((* elif resources.sphinx.outputstyle == 'notebook' *))
32 ((*- set wrap_size = 70 -*))
34 ((*- set wrap_size = 70 -*))
33 ((* endif *))
35 ((* endif *))
34
36
35 %==============================================================================
37 %==============================================================================
36 % Header
38 % Header
37 %==============================================================================
39 %==============================================================================
38 ((* block header *))
40 ((* block header *))
39
41
40 % Header, overrides base
42 % Header, overrides base
41
43
42 % Make sure that the sphinx doc style knows who it inherits from.
44 % Make sure that the sphinx doc style knows who it inherits from.
43 \def\sphinxdocclass{(((parentdocumentclass)))}
45 \def\sphinxdocclass{(((parentdocumentclass)))}
44
46
45 % Declare the document class
47 % Declare the document class
46 \documentclass[letterpaper,10pt,english]{((( resources.sphinx.texinputs )))/sphinx(((documentclass)))}
48 \documentclass[letterpaper,10pt,english]{((( resources.sphinx.texinputs )))/sphinx(((documentclass)))}
47
49
48 % Imports
50 % Imports
49 \usepackage[utf8]{inputenc}
51 \usepackage[utf8]{inputenc}
50 \DeclareUnicodeCharacter{00A0}{\\nobreakspace}
52 \DeclareUnicodeCharacter{00A0}{\\nobreakspace}
51 \usepackage[T1]{fontenc}
53 \usepackage[T1]{fontenc}
52 \usepackage{babel}
54 \usepackage{babel}
53 \usepackage{times}
55 \usepackage{times}
54 \usepackage{import}
56 \usepackage{import}
55 \usepackage[((( resources.sphinx.chapterstyle )))]{((( resources.sphinx.texinputs )))/fncychap}
57 \usepackage[((( resources.sphinx.chapterstyle )))]{((( resources.sphinx.texinputs )))/fncychap}
56 \usepackage{longtable}
58 \usepackage{longtable}
57 \usepackage{((( resources.sphinx.texinputs )))/sphinx}
59 \usepackage{((( resources.sphinx.texinputs )))/sphinx}
58 \usepackage{multirow}
60 \usepackage{multirow}
59
61
60 \usepackage{amsmath}
62 \usepackage{amsmath}
61 \usepackage{amssymb}
63 \usepackage{amssymb}
62 \usepackage{ucs}
64 \usepackage{ucs}
63 \usepackage{enumerate}
65 \usepackage{enumerate}
64
66
65 % Used to make the Input/Output rules follow around the contents.
67 % Used to make the Input/Output rules follow around the contents.
66 \usepackage{needspace}
68 \usepackage{needspace}
67
69
68 % Pygments requirements
70 % Pygments requirements
69 \usepackage{fancyvrb}
71 \usepackage{fancyvrb}
70 \usepackage{color}
72 \usepackage{color}
71 % ansi colors additions
73 % ansi colors additions
72 \definecolor{darkgreen}{rgb}{.12,.54,.11}
74 \definecolor{darkgreen}{rgb}{.12,.54,.11}
73 \definecolor{lightgray}{gray}{.95}
75 \definecolor{lightgray}{gray}{.95}
74 \definecolor{brown}{rgb}{0.54,0.27,0.07}
76 \definecolor{brown}{rgb}{0.54,0.27,0.07}
75 \definecolor{purple}{rgb}{0.5,0.0,0.5}
77 \definecolor{purple}{rgb}{0.5,0.0,0.5}
76 \definecolor{darkgray}{gray}{0.25}
78 \definecolor{darkgray}{gray}{0.25}
77 \definecolor{lightred}{rgb}{1.0,0.39,0.28}
79 \definecolor{lightred}{rgb}{1.0,0.39,0.28}
78 \definecolor{lightgreen}{rgb}{0.48,0.99,0.0}
80 \definecolor{lightgreen}{rgb}{0.48,0.99,0.0}
79 \definecolor{lightblue}{rgb}{0.53,0.81,0.92}
81 \definecolor{lightblue}{rgb}{0.53,0.81,0.92}
80 \definecolor{lightpurple}{rgb}{0.87,0.63,0.87}
82 \definecolor{lightpurple}{rgb}{0.87,0.63,0.87}
81 \definecolor{lightcyan}{rgb}{0.5,1.0,0.83}
83 \definecolor{lightcyan}{rgb}{0.5,1.0,0.83}
82
84
83 % Needed to box output/input
85 % Needed to box output/input
84 \usepackage{tikz}
86 \usepackage{tikz}
85 \usetikzlibrary{calc,arrows,shadows}
87 \usetikzlibrary{calc,arrows,shadows}
86 \usepackage[framemethod=tikz]{mdframed}
88 \usepackage[framemethod=tikz]{mdframed}
87
89
88 \usepackage{alltt}
90 \usepackage{alltt}
89
91
90 % Used to load and display graphics
92 % Used to load and display graphics
91 \usepackage{graphicx}
93 \usepackage{graphicx}
92 \graphicspath{ {figs/} }
94 \graphicspath{ {figs/} }
93 \usepackage[Export]{adjustbox} % To resize
95 \usepackage[Export]{adjustbox} % To resize
94
96
95
97
96 % For formatting output while also word wrapping.
98 % For formatting output while also word wrapping.
97 \usepackage{listings}
99 \usepackage{listings}
98 \lstset{breaklines=true}
100 \lstset{breaklines=true}
99 \lstset{basicstyle=\small\ttfamily}
101 \lstset{basicstyle=\small\ttfamily}
100 \def\smaller{\fontsize{9.5pt}{9.5pt}\selectfont}
102 \def\smaller{\fontsize{9.5pt}{9.5pt}\selectfont}
101
103
102 %Pygments definitions
104 %Pygments definitions
103 ((( resources.sphinx.pygment_definitions )))
105 ((( resources.sphinx.pygment_definitions )))
104
106
105 %Set pygments styles if needed...
107 %Set pygments styles if needed...
106 ((* if resources.sphinx.outputstyle == 'notebook' *))
108 ((* if resources.sphinx.outputstyle == 'notebook' *))
107 \definecolor{nbframe-border}{rgb}{0.867,0.867,0.867}
109 \definecolor{nbframe-border}{rgb}{0.867,0.867,0.867}
108 \definecolor{nbframe-bg}{rgb}{0.969,0.969,0.969}
110 \definecolor{nbframe-bg}{rgb}{0.969,0.969,0.969}
109 \definecolor{nbframe-in-prompt}{rgb}{0.0,0.0,0.502}
111 \definecolor{nbframe-in-prompt}{rgb}{0.0,0.0,0.502}
110 \definecolor{nbframe-out-prompt}{rgb}{0.545,0.0,0.0}
112 \definecolor{nbframe-out-prompt}{rgb}{0.545,0.0,0.0}
111
113
112 \newenvironment{ColorVerbatim}
114 \newenvironment{ColorVerbatim}
113 {\begin{mdframed}[%
115 {\begin{mdframed}[%
114 roundcorner=1.0pt, %
116 roundcorner=1.0pt, %
115 backgroundcolor=nbframe-bg, %
117 backgroundcolor=nbframe-bg, %
116 userdefinedwidth=1\linewidth, %
118 userdefinedwidth=1\linewidth, %
117 leftmargin=0.1\linewidth, %
119 leftmargin=0.1\linewidth, %
118 innerleftmargin=0pt, %
120 innerleftmargin=0pt, %
119 innerrightmargin=0pt, %
121 innerrightmargin=0pt, %
120 linecolor=nbframe-border, %
122 linecolor=nbframe-border, %
121 linewidth=1pt, %
123 linewidth=1pt, %
122 usetwoside=false, %
124 usetwoside=false, %
123 everyline=true, %
125 everyline=true, %
124 innerlinewidth=3pt, %
126 innerlinewidth=3pt, %
125 innerlinecolor=nbframe-bg, %
127 innerlinecolor=nbframe-bg, %
126 middlelinewidth=1pt, %
128 middlelinewidth=1pt, %
127 middlelinecolor=nbframe-bg, %
129 middlelinecolor=nbframe-bg, %
128 outerlinewidth=0.5pt, %
130 outerlinewidth=0.5pt, %
129 outerlinecolor=nbframe-border, %
131 outerlinecolor=nbframe-border, %
130 needspace=0pt
132 needspace=0pt
131 ]}
133 ]}
132 {\end{mdframed}}
134 {\end{mdframed}}
133
135
134 \newenvironment{InvisibleVerbatim}
136 \newenvironment{InvisibleVerbatim}
135 {\begin{mdframed}[leftmargin=0.1\linewidth,innerleftmargin=3pt,innerrightmargin=3pt, userdefinedwidth=1\linewidth, linewidth=0pt, linecolor=white, usetwoside=false]}
137 {\begin{mdframed}[leftmargin=0.1\linewidth,innerleftmargin=3pt,innerrightmargin=3pt, userdefinedwidth=1\linewidth, linewidth=0pt, linecolor=white, usetwoside=false]}
136 {\end{mdframed}}
138 {\end{mdframed}}
137
139
138 \renewenvironment{Verbatim}[1][\unskip]
140 \renewenvironment{Verbatim}[1][\unskip]
139 {\begin{alltt}\smaller}
141 {\begin{alltt}\smaller}
140 {\end{alltt}}
142 {\end{alltt}}
141 ((* endif *))
143 ((* endif *))
142
144
143 % Help prevent overflowing lines due to urls and other hard-to-break
145 % Help prevent overflowing lines due to urls and other hard-to-break
144 % entities. This doesn't catch everything...
146 % entities. This doesn't catch everything...
145 \sloppy
147 \sloppy
146
148
147 % Document level variables
149 % Document level variables
148 \title{((( resources.metadata.name | escape_latex )))}
150 \title{((( resources.metadata.name | escape_latex )))}
149 \date{((( resources.sphinx.date | escape_latex )))}
151 \date{((( resources.sphinx.date | escape_latex )))}
150 \release{((( resources.sphinx.version | escape_latex )))}
152 \release{((( resources.sphinx.version | escape_latex )))}
151 \author{((( resources.sphinx.author | escape_latex )))}
153 \author{((( resources.sphinx.author | escape_latex )))}
152 \renewcommand{\releasename}{((( resources.sphinx.release | escape_latex )))}
154 \renewcommand{\releasename}{((( resources.sphinx.release | escape_latex )))}
153
155
154 % TODO: Add option for the user to specify a logo for his/her export.
156 % TODO: Add option for the user to specify a logo for his/her export.
155 \newcommand{\sphinxlogo}{}
157 \newcommand{\sphinxlogo}{}
156
158
157 % Make the index page of the document.
159 % Make the index page of the document.
158 \makeindex
160 \makeindex
159
161
160 % Import sphinx document type specifics.
162 % Import sphinx document type specifics.
161 ((* block sphinxheader *))((* endblock sphinxheader *))
163 ((* block sphinxheader *))((* endblock sphinxheader *))
162 ((* endblock header *))
164 ((* endblock header *))
163
165
164 %==============================================================================
166 %==============================================================================
165 % Body
167 % Body
166 %==============================================================================
168 %==============================================================================
167 ((* block body *))
169 ((* block body *))
168 ((* block bodyBegin *))
170 ((* block bodyBegin *))
169 % Body
171 % Body
170
172
171 % Start of the document
173 % Start of the document
172 \begin{document}
174 \begin{document}
173
175
174 ((* if resources.sphinx.header *))
176 ((* if resources.sphinx.header *))
175 \maketitle
177 \maketitle
176 ((* endif *))
178 ((* endif *))
177
179
178 ((* block toc *))
180 ((* block toc *))
179 \tableofcontents
181 \tableofcontents
180 ((* endblock toc *))
182 ((* endblock toc *))
181
183
182 ((* endblock bodyBegin *))((( super() )))((* block bodyEnd *))
184 ((* endblock bodyBegin *))((( super() )))((* block bodyEnd *))
183
185
184 \renewcommand{\indexname}{Index}
186 \renewcommand{\indexname}{Index}
185 \printindex
187 \printindex
186
188
187 % End of document
189 % End of document
188 \end{document}
190 \end{document}
189 ((* endblock bodyEnd *))
191 ((* endblock bodyEnd *))
190 ((* endblock body *))
192 ((* endblock body *))
191
193
192 %==============================================================================
194 %==============================================================================
193 % Footer
195 % Footer
194 %==============================================================================
196 %==============================================================================
195 ((* block footer *))
197 ((* block footer *))
196 ((* endblock footer *))
198 ((* endblock footer *))
197
199
198 %==============================================================================
200 %==============================================================================
199 % Headings
201 % Headings
200 %
202 %
201 % Purpose: Format pynb headers as sphinx headers. Depending on the Sphinx
203 % Purpose: Format pynb headers as sphinx headers. Depending on the Sphinx
202 % style that is active, this will change. Thus sphinx styles will
204 % style that is active, this will change. Thus sphinx styles will
203 % override the values here.
205 % override the values here.
204 %==============================================================================
206 %==============================================================================
205 ((* block headingcell -*))
207 ((* block headingcell -*))
206 \
208 \
207 ((*- if cell.level == 1 -*))
209 ((*- if cell.level == 1 -*))
208 ((* block h1 -*))part((* endblock h1 -*))
210 ((* block h1 -*))part((* endblock h1 -*))
209 ((*- elif cell.level == 2 -*))
211 ((*- elif cell.level == 2 -*))
210 ((* block h2 -*))chapter((* endblock h2 -*))
212 ((* block h2 -*))chapter((* endblock h2 -*))
211 ((*- elif cell.level == 3 -*))
213 ((*- elif cell.level == 3 -*))
212 ((* block h3 -*))section((* endblock h3 -*))
214 ((* block h3 -*))section((* endblock h3 -*))
213 ((*- elif cell.level == 4 -*))
215 ((*- elif cell.level == 4 -*))
214 ((* block h4 -*))subsection((* endblock h4 -*))
216 ((* block h4 -*))subsection((* endblock h4 -*))
215 ((*- elif cell.level == 5 -*))
217 ((*- elif cell.level == 5 -*))
216 ((* block h5 -*))subsubsection((* endblock h5 -*))
218 ((* block h5 -*))subsubsection((* endblock h5 -*))
217 ((*- elif cell.level == 6 -*))
219 ((*- elif cell.level == 6 -*))
218 ((* block h6 -*))paragraph((* endblock h6 -*))
220 ((* block h6 -*))paragraph((* endblock h6 -*))
219
221
220 ((= It's important to make sure that underscores (which tend to be common
222 ((= It's important to make sure that underscores (which tend to be common
221 in IPYNB file titles) do not make their way into latex. Sometimes this
223 in IPYNB file titles) do not make their way into latex. Sometimes this
222 causes latex to barf. =))
224 causes latex to barf. =))
223 ((*- endif -*)){((( cell.source | markdown2latex )))}
225 ((*- endif -*)){((( cell.source | markdown2latex )))}
224 ((*- endblock headingcell *))
226 ((*- endblock headingcell *))
225
227
226 %==============================================================================
228 %==============================================================================
227 % Markdown
229 % Markdown
228 %
230 %
229 % Purpose: Convert markdown to latex. Here markdown2latex is explicitly
231 % Purpose: Convert markdown to latex. Here markdown2latex is explicitly
230 % called since we know we want latex output.
232 % called since we know we want latex output.
231 %==============================================================================
233 %==============================================================================
232 ((*- block markdowncell scoped-*))
234 ((*- block markdowncell scoped-*))
233 ((( cell.source | markdown2latex )))
235 ((( cell.source | markdown2latex )))
234 ((*- endblock markdowncell -*))
236 ((*- endblock markdowncell -*))
235
237
236 %==============================================================================
238 %==============================================================================
237 % Rawcell
239 % Rawcell
238 %
240 %
239 % Purpose: Raw text cells allow the user to manually inject document code that
241 % Purpose: Raw text cells allow the user to manually inject document code that
240 % will not get touched by the templating system.
242 % will not get touched by the templating system.
241 %==============================================================================
243 %==============================================================================
242 ((*- block rawcell *))
244 ((*- block rawcell *))
243 ((( cell.source | wrap_text(wrap_size) )))
245 ((( cell.source | wrap_text(wrap_size) )))
244 ((* endblock rawcell -*))
246 ((* endblock rawcell -*))
245
247
246 %==============================================================================
248 %==============================================================================
247 % Unknowncell
249 % Unknowncell
248 %
250 %
249 % Purpose: This is the catch anything unhandled. To display this data, we
251 % Purpose: This is the catch anything unhandled. To display this data, we
250 % remove all possible latex conflicts and wrap the characters so they
252 % remove all possible latex conflicts and wrap the characters so they
251 % can't flow off of the page.
253 % can't flow off of the page.
252 %==============================================================================
254 %==============================================================================
253 ((* block unknowncell scoped*))
255 ((* block unknowncell scoped*))
254
256
255 % Unsupported cell type, no formatting
257 % Unsupported cell type, no formatting
256 ((( cell.source | wrap_text | escape_latex )))
258 ((( cell.source | wrap_text | escape_latex )))
257 ((* endblock unknowncell *))
259 ((* endblock unknowncell *))
258
260
259 %==============================================================================
261 %==============================================================================
260 % Input
262 % Input
261 %==============================================================================
263 %==============================================================================
262 ((* block input *))
264 ((* block input *))
263
265
264 % Make sure that atleast 4 lines are below the HR
266 % Make sure that atleast 4 lines are below the HR
265 \needspace{((( min_header_lines )))\baselineskip}
267 \needspace{((( min_header_lines )))\baselineskip}
266
268
267 ((* if resources.sphinx.outputstyle == 'simple' *))
269 ((* if resources.sphinx.outputstyle == 'simple' *))
268
270
269 % Add a horizantal break, along with break title.
271 % Add a horizantal break, along with break title.
270 \vspace{10pt}
272 \vspace{10pt}
271 {\scriptsize Input}\\*
273 {\scriptsize Input}\\*
272 \rule[10pt]{\linewidth}{0.5pt}
274 \rule[10pt]{\linewidth}{0.5pt}
273 \vspace{-25pt}
275 \vspace{-25pt}
274
276
275 % Add contents below.
277 % Add contents below.
276 ((( cell.input | highlight2latex )))
278 ((( cell.input | highlight2latex )))
277
279
278 ((* elif resources.sphinx.outputstyle == 'notebook' *))
280 ((* elif resources.sphinx.outputstyle == 'notebook' *))
279 \vspace{6pt}
281 \vspace{6pt}
280 ((( write_prompt("In", cell.prompt_number, "nbframe-in-prompt") )))
282 ((( write_prompt("In", cell.prompt_number, "nbframe-in-prompt") )))
281 \vspace{-2.65\baselineskip}
283 \vspace{-2.65\baselineskip}
282 \begin{ColorVerbatim}
284 \begin{ColorVerbatim}
283 \vspace{-0.7\baselineskip}
285 \vspace{-0.7\baselineskip}
284 ((( cell.input | highlight2latex )))
286 ((( cell.input | highlight2latex )))
285 ((* if cell.input == None or cell.input == '' *))
287 ((* if cell.input == None or cell.input == '' *))
286 \vspace{0.3\baselineskip}
288 \vspace{0.3\baselineskip}
287 ((* else *))
289 ((* else *))
288 \vspace{-0.2\baselineskip}
290 \vspace{-0.2\baselineskip}
289 ((* endif *))
291 ((* endif *))
290 \end{ColorVerbatim}
292 \end{ColorVerbatim}
291 ((* endif *))
293 ((* endif *))
292 ((* endblock input *))
294 ((* endblock input *))
293
295
294 %==============================================================================
296 %==============================================================================
295 % Output_Group
297 % Output_Group
296 %
298 %
297 % Purpose: Make sure that only one header bar only attaches to the output
299 % Purpose: Make sure that only one header bar only attaches to the output
298 % once. By keeping track of when an input group is started
300 % once. By keeping track of when an input group is started
299 %==============================================================================
301 %==============================================================================
300 ((* block output_group *))
302 ((* block output_group *))
301 ((* if cell.outputs.__len__() > 0 *))
303 ((* if cell.outputs.__len__() > 0 *))
302
304
303 % If the first block is an image, minipage the image. Else
305 % If the first block is an image, minipage the image. Else
304 % request a certain amount of space for the input text.
306 % request a certain amount of space for the input text.
305 ((( iff_figure(cell.outputs[0], "\\begin{minipage}{1.0\\textwidth}", "\\needspace{" ~ min_header_lines ~ "\\baselineskip}") )))
307 ((( iff_figure(cell.outputs[0], "\\begin{minipage}{1.0\\textwidth}", "\\needspace{" ~ min_header_lines ~ "\\baselineskip}") )))
306
308
307 ((* if resources.sphinx.outputstyle == 'simple' *))
309 ((* if resources.sphinx.outputstyle == 'simple' *))
308
310
309 % Add a horizantal break, along with break title.
311 % Add a horizantal break, along with break title.
310 \vspace{10pt}
312 \vspace{10pt}
311 {\scriptsize Output}\\*
313 {\scriptsize Output}\\*
312 \rule[10pt]{\linewidth}{0.5pt}
314 \rule[10pt]{\linewidth}{0.5pt}
313 \vspace{-20pt}
315 \vspace{-20pt}
314
316
315 % Add the contents of the first block.
317 % Add the contents of the first block.
316 ((( render_output(cell.outputs[0]) )))
318 ((( render_output(cell.outputs[0]) )))
317
319
318 % Close the minipage.
320 % Close the minipage.
319 ((( iff_figure(cell.outputs[0], "\\end{minipage}", "") )))
321 ((( iff_figure(cell.outputs[0], "\\end{minipage}", "") )))
320
322
321 % Add remainer of the document contents below.
323 % Add remainer of the document contents below.
322 ((* for output in cell.outputs[1:] *))
324 ((* for output in cell.outputs[1:] *))
323 ((( render_output(output, cell.prompt_number) )))
325 ((( render_output(output, cell.prompt_number) )))
324 ((* endfor *))
326 ((* endfor *))
325 ((* elif resources.sphinx.outputstyle == 'notebook' *))
327 ((* elif resources.sphinx.outputstyle == 'notebook' *))
326
328
327 % Add document contents.
329 % Add document contents.
328 ((* for output in cell.outputs *))
330 ((* for output in cell.outputs *))
329 ((( render_output(output, cell.prompt_number) )))
331 ((( render_output(output, cell.prompt_number) )))
330 ((* endfor *))
332 ((* endfor *))
331 ((* endif *))
333 ((* endif *))
332 ((* endif *))
334 ((* endif *))
333 ((* endblock *))
335 ((* endblock *))
334
336
335 %==============================================================================
337 %==============================================================================
336 % Additional formating
338 % Additional formating
337 %==============================================================================
339 %==============================================================================
338 ((* block data_text *))
340 ((* block data_text *))
339 ((( custom_verbatim(output.text) | ansi2latex)))
341 ((( custom_verbatim(output.text) | ansi2latex)))
340 ((* endblock *))
342 ((* endblock *))
341
343
342 ((* block traceback_line *))
344 ((* block traceback_line *))
343 ((( conditionally_center_output(line | indent| strip_ansi) )))
345 ((( conditionally_center_output(line | indent| strip_ansi) )))
344 ((* endblock traceback_line *))
346 ((* endblock traceback_line *))
345
347
346 %==============================================================================
348 %==============================================================================
347 % Supported image formats
349 % Supported image formats
348 %==============================================================================
350 %==============================================================================
349 ((*- block data_png -*))
351 ((*- block data_png -*))
350 ((( conditionally_center_output(insert_graphics(output.png_filename)) )))
352 ((( conditionally_center_output(insert_graphics(output.png_filename)) )))
351 ((*- endblock -*))
353 ((*- endblock -*))
352
354
353 ((*- block data_jpg -*))
355 ((*- block data_jpg -*))
354 ((( conditionally_center_output(insert_graphics(output.jpg_filename)) )))
356 ((( conditionally_center_output(insert_graphics(output.jpg_filename)) )))
355 ((*- endblock -*))
357 ((*- endblock -*))
356
358
357 ((*- block data_svg -*))
359 ((*- block data_svg -*))
358 ((( conditionally_center_output(insert_graphics(output.svg_filename)) )))
360 ((( conditionally_center_output(insert_graphics(output.svg_filename)) )))
359 ((*- endblock -*))
361 ((*- endblock -*))
360
362
361 ((*- block data_pdf -*))
363 ((*- block data_pdf -*))
362 ((( conditionally_center_output(insert_graphics(output.pdf_filename)) )))
364 ((( conditionally_center_output(insert_graphics(output.pdf_filename)) )))
363 ((*- endblock -*))
365 ((*- endblock -*))
364
366
365 ((*- block data_latex *))
367 ((*- block data_latex *))
366 ((* if resources.sphinx.centeroutput *))\begin{center}((* endif -*))((( output.latex | strip_math_space )))((*- if resources.sphinx.centeroutput *))\end{center} ((* endif -*))
368 ((* if resources.sphinx.centeroutput *))\begin{center}((* endif -*))((( output.latex | strip_math_space )))((*- if resources.sphinx.centeroutput *))\end{center} ((* endif -*))
367 ((*- endblock -*))
369 ((*- endblock -*))
368
370
369 %==============================================================================
371 %==============================================================================
370 % Support Macros
372 % Support Macros
371 %==============================================================================
373 %==============================================================================
372
374
373 % Name: write_prompt
375 % Name: write_prompt
374 % Purpose: Renders an output/input prompt for notebook style pdfs
376 % Purpose: Renders an output/input prompt for notebook style pdfs
375 ((* macro write_prompt(prompt, number, color) -*))
377 ((* macro write_prompt(prompt, number, color) -*))
376 \makebox[0.1\linewidth]{\smaller\hfill\tt\color{((( color )))}((( prompt )))\hspace{4pt}{[}((( number ))){]}:\hspace{4pt}}\\*
378 \makebox[0.1\linewidth]{\smaller\hfill\tt\color{((( color )))}((( prompt )))\hspace{4pt}{[}((( number ))){]}:\hspace{4pt}}\\*
377 ((*- endmacro *))
379 ((*- endmacro *))
378
380
379 % Name: render_output
381 % Name: render_output
380 % Purpose: Renders an output block appropriately.
382 % Purpose: Renders an output block appropriately.
381 ((* macro render_output(output, prompt_number) -*))
383 ((* macro render_output(output, prompt_number) -*))
382 ((*- if output.output_type == 'pyerr' -*))
384 ((*- if output.output_type == 'pyerr' -*))
383 ((*- block pyerr scoped *))
385 ((*- block pyerr scoped *))
384 ((( custom_verbatim(super()) )))
386 ((( custom_verbatim(super()) )))
385 ((* endblock pyerr -*))
387 ((* endblock pyerr -*))
386 ((*- else -*))
388 ((*- else -*))
387
389
388 ((* if resources.sphinx.outputstyle == 'notebook' *))
390 ((* if resources.sphinx.outputstyle == 'notebook' *))
389 ((*- if output.output_type == 'pyout' -*))
391 ((*- if output.output_type == 'pyout' -*))
390 ((( write_prompt("Out", prompt_number, "nbframe-out-prompt") )))
392 ((( write_prompt("Out", prompt_number, "nbframe-out-prompt") )))
391 \vspace{-2.55\baselineskip}
393 \vspace{-2.55\baselineskip}
392 ((*- endif -*))
394 ((*- endif -*))
393
395
394 \begin{InvisibleVerbatim}
396 \begin{InvisibleVerbatim}
395 \vspace{-0.5\baselineskip}
397 \vspace{-0.5\baselineskip}
396 ((*- endif -*))
398 ((*- endif -*))
397
399
398 ((*- block display_data scoped -*))
400 ((*- block display_data scoped -*))
399 ((( super() )))
401 ((( super() )))
400 ((*- endblock display_data -*))
402 ((*- endblock display_data -*))
401
403
402 ((* if resources.sphinx.outputstyle == 'notebook' *))
404 ((* if resources.sphinx.outputstyle == 'notebook' *))
403 \end{InvisibleVerbatim}
405 \end{InvisibleVerbatim}
404 ((*- endif -*))
406 ((*- endif -*))
405 ((*- endif -*))
407 ((*- endif -*))
406 ((*- endmacro *))
408 ((*- endmacro *))
407
409
408 % Name: iff_figure
410 % Name: iff_figure
409 % Purpose: If the output block provided is a figure type, the 'true_content'
411 % Purpose: If the output block provided is a figure type, the 'true_content'
410 % parameter will be returned. Else, the 'false_content'.
412 % parameter will be returned. Else, the 'false_content'.
411 ((* macro iff_figure(output, true_content, false_content) -*))
413 ((* macro iff_figure(output, true_content, false_content) -*))
412 ((*- set is_figure = false -*))
414 ((*- set is_figure = false -*))
413 ((*- for type in output | filter_data_type -*))
415 ((*- for type in output | filter_data_type -*))
414 ((*- if type in ['pdf', 'svg', 'png', 'jpeg','html']*))
416 ((*- if type in ['pdf', 'svg', 'png', 'jpeg','html']*))
415 ((*- set is_figure = true -*))
417 ((*- set is_figure = true -*))
416 ((*- endif -*))
418 ((*- endif -*))
417 ((*- endfor -*))
419 ((*- endfor -*))
418
420
419 ((* if is_figure -*))
421 ((* if is_figure -*))
420 ((( true_content )))
422 ((( true_content )))
421 ((*- else -*))
423 ((*- else -*))
422 ((( false_content )))
424 ((( false_content )))
423 ((*- endif *))
425 ((*- endif *))
424 ((*- endmacro *))
426 ((*- endmacro *))
425
427
426 % Name: custom_verbatim
428 % Name: custom_verbatim
427 % Purpose: This macro creates a verbatim style block that fits the existing
429 % Purpose: This macro creates a verbatim style block that fits the existing
428 % sphinx style more readily than standard verbatim blocks.
430 % sphinx style more readily than standard verbatim blocks.
429 ((* macro custom_verbatim(text) -*))
431 ((* macro custom_verbatim(text) -*))
430 \begin{alltt}
432 \begin{alltt}
431 ((*- if resources.sphinx.centeroutput *))\begin{center} ((* endif -*))
433 ((*- if resources.sphinx.centeroutput *))\begin{center} ((* endif -*))
432 ((( text | wrap_text(wrap_size) )))
434 ((( text | wrap_text(wrap_size) )))
433 ((*- if resources.sphinx.centeroutput *))\end{center}((* endif -*))
435 ((*- if resources.sphinx.centeroutput *))\end{center}((* endif -*))
434 \end{alltt}
436 \end{alltt}
435 ((*- endmacro *))
437 ((*- endmacro *))
436
438
437 % Name: conditionally_center_output
439 % Name: conditionally_center_output
438 % Purpose: This macro centers the output if the output centering is enabled.
440 % Purpose: This macro centers the output if the output centering is enabled.
439 ((* macro conditionally_center_output(text) -*))
441 ((* macro conditionally_center_output(text) -*))
440 ((* if resources.sphinx.centeroutput *)){\centering ((* endif *))((( text )))((* if resources.sphinx.centeroutput *))}((* endif *))
442 ((* if resources.sphinx.centeroutput *)){\centering ((* endif *))((( text )))((* if resources.sphinx.centeroutput *))}((* endif *))
441 ((*- endmacro *))
443 ((*- endmacro *))
442
444
443 % Name: insert_graphics
445 % Name: insert_graphics
444 % Purpose: This macro will insert an image in the latex document given a path.
446 % Purpose: This macro will insert an image in the latex document given a path.
445 ((* macro insert_graphics(path) -*))
447 ((* macro insert_graphics(path) -*))
446 \begin{center}
448 \begin{center}
447 \includegraphics[max size={\textwidth}{\textheight}]{(((path)))}
449 \includegraphics[max size={\textwidth}{\textheight}]{(((path)))}
448 \par
450 \par
449 \end{center}
451 \end{center}
450 ((*- endmacro *))
452 ((*- endmacro *))
@@ -1,21 +1,21 b''
1 {%- extends 'basichtml.tpl' -%}
1 {%- extends 'html_basic.tpl' -%}
2
2
3
3
4
4
5 {%- block any_cell scoped -%}
5 {%- block any_cell scoped -%}
6 {%- if cell.metadata.slide_type in ['-', 'slide', 'subslide'] -%}
6 {%- if cell.metadata.slide_type in ['-', 'slide', 'subslide'] -%}
7 {{ super() }}
7 {{ super() }}
8 {%- elif cell.metadata.slide_type in ['skip'] -%}
8 {%- elif cell.metadata.slide_type in ['skip'] -%}
9 <div style=display:none>
9 <div style=display:none>
10 {{ super() }}
10 {{ super() }}
11 </div>
11 </div>
12 {%- elif cell.metadata.slide_type in ['notes'] -%}
12 {%- elif cell.metadata.slide_type in ['notes'] -%}
13 <aside class="notes">
13 <aside class="notes">
14 {{ super() }}
14 {{ super() }}
15 </aside>
15 </aside>
16 {%- elif cell.metadata.slide_type in ['fragment'] -%}
16 {%- elif cell.metadata.slide_type in ['fragment'] -%}
17 <div class="fragment">
17 <div class="fragment">
18 {{ super() }}
18 {{ super() }}
19 </div>
19 </div>
20 {%- endif -%}
20 {%- endif -%}
21 {%- endblock any_cell -%}
21 {%- endblock any_cell -%}
1 NO CONTENT: file renamed from IPython/nbconvert/templates/reveal.tpl to IPython/nbconvert/templates/slides_reveal.tpl
NO CONTENT: file renamed from IPython/nbconvert/templates/reveal.tpl to IPython/nbconvert/templates/slides_reveal.tpl
@@ -1,178 +1,177 b''
1 #!/usr/bin/env python
2 """
1 """
3 Contains base test class for nbconvert
2 Contains base test class for nbconvert
4 """
3 """
5 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
6 #Copyright (c) 2013, the IPython Development Team.
5 #Copyright (c) 2013, the IPython Development Team.
7 #
6 #
8 #Distributed under the terms of the Modified BSD License.
7 #Distributed under the terms of the Modified BSD License.
9 #
8 #
10 #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.
11 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
12
11
13 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
14 # Imports
13 # Imports
15 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
16
15
17 import subprocess
16 import subprocess
18 import os
17 import os
19 import glob
18 import glob
20 import shutil
19 import shutil
21 import sys
20 import sys
22
21
23 import IPython
22 import IPython
24 from IPython.utils.tempdir import TemporaryDirectory
23 from IPython.utils.tempdir import TemporaryDirectory
25 from IPython.utils import py3compat
24 from IPython.utils import py3compat
26
25
27 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
28 # Classes and functions
27 # Classes and functions
29 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
30
29
31 class TemporaryWorkingDirectory(TemporaryDirectory):
30 class TemporaryWorkingDirectory(TemporaryDirectory):
32 """
31 """
33 Creates a temporary directory and sets the cwd to that directory.
32 Creates a temporary directory and sets the cwd to that directory.
34 Automatically reverts to previous cwd upon cleanup.
33 Automatically reverts to previous cwd upon cleanup.
35 Usage example:
34 Usage example:
36
35
37 with TemporaryWorakingDirectory() as tmpdir:
36 with TemporaryWorakingDirectory() as tmpdir:
38 ...
37 ...
39 """
38 """
40
39
41 def __init__(self, **kw):
40 def __init__(self, **kw):
42 """
41 """
43 Constructor
42 Constructor
44 """
43 """
45 super(TemporaryWorkingDirectory, self).__init__(**kw)
44 super(TemporaryWorkingDirectory, self).__init__(**kw)
46
45
47 #Change cwd to new temp dir. Remember old cwd.
46 #Change cwd to new temp dir. Remember old cwd.
48 self.old_wd = os.getcwd()
47 self.old_wd = os.getcwd()
49 os.chdir(self.name)
48 os.chdir(self.name)
50
49
51
50
52 def cleanup(self):
51 def cleanup(self):
53 """
52 """
54 Destructor
53 Destructor
55 """
54 """
56
55
57 #Revert to old cwd.
56 #Revert to old cwd.
58 os.chdir(self.old_wd)
57 os.chdir(self.old_wd)
59
58
60 #Cleanup
59 #Cleanup
61 super(TemporaryWorkingDirectory, self).cleanup()
60 super(TemporaryWorkingDirectory, self).cleanup()
62
61
63
62
64 class TestsBase(object):
63 class TestsBase(object):
65 """Base tests class. Contains usefull fuzzy comparison and nbconvert
64 """Base tests class. Contains usefull fuzzy comparison and nbconvert
66 functions."""
65 functions."""
67
66
68
67
69 def fuzzy_compare(self, a, b, newlines_are_spaces=True, tabs_are_spaces=True,
68 def fuzzy_compare(self, a, b, newlines_are_spaces=True, tabs_are_spaces=True,
70 fuzzy_spacing=True, ignore_spaces=False,
69 fuzzy_spacing=True, ignore_spaces=False,
71 ignore_newlines=False, case_sensitive=False):
70 ignore_newlines=False, case_sensitive=False):
72 """
71 """
73 Performs a fuzzy comparison of two strings. A fuzzy comparison is a
72 Performs a fuzzy comparison of two strings. A fuzzy comparison is a
74 comparison that ignores insignificant differences in the two comparands.
73 comparison that ignores insignificant differences in the two comparands.
75 The significance of certain differences can be specified via the keyword
74 The significance of certain differences can be specified via the keyword
76 parameters of this method.
75 parameters of this method.
77 """
76 """
78
77
79 if newlines_are_spaces:
78 if newlines_are_spaces:
80 a = a.replace('\n', ' ')
79 a = a.replace('\n', ' ')
81 b = b.replace('\n', ' ')
80 b = b.replace('\n', ' ')
82
81
83 if tabs_are_spaces:
82 if tabs_are_spaces:
84 a = a.replace('\t', ' ')
83 a = a.replace('\t', ' ')
85 b = b.replace('\t', ' ')
84 b = b.replace('\t', ' ')
86
85
87 if ignore_spaces:
86 if ignore_spaces:
88 a = a.replace(' ', '')
87 a = a.replace(' ', '')
89 b = b.replace(' ', '')
88 b = b.replace(' ', '')
90
89
91 if fuzzy_spacing:
90 if fuzzy_spacing:
92 a = self.recursive_replace(a, ' ', ' ')
91 a = self.recursive_replace(a, ' ', ' ')
93 b = self.recursive_replace(b, ' ', ' ')
92 b = self.recursive_replace(b, ' ', ' ')
94
93
95 if ignore_newlines:
94 if ignore_newlines:
96 a = a.replace('\n', '')
95 a = a.replace('\n', '')
97 b = b.replace('\n', '')
96 b = b.replace('\n', '')
98
97
99 if not case_sensitive:
98 if not case_sensitive:
100 a = a.lower()
99 a = a.lower()
101 b = b.lower()
100 b = b.lower()
102
101
103 return a == b
102 return a == b
104
103
105
104
106 def recursive_replace(self, text, search, replacement):
105 def recursive_replace(self, text, search, replacement):
107 """
106 """
108 Performs a recursive replacement operation. Replaces all instances
107 Performs a recursive replacement operation. Replaces all instances
109 of a search string in a text string with a replacement string until
108 of a search string in a text string with a replacement string until
110 the search string no longer exists. Recursion is needed because the
109 the search string no longer exists. Recursion is needed because the
111 replacement string may generate additional search strings.
110 replacement string may generate additional search strings.
112
111
113 For example:
112 For example:
114 Replace "ii" with "i" in the string "Hiiii" yields "Hii"
113 Replace "ii" with "i" in the string "Hiiii" yields "Hii"
115 Another replacement yields "Hi" (the desired output)
114 Another replacement yields "Hi" (the desired output)
116
115
117 Parameters:
116 Parameters:
118 -----------
117 -----------
119 text : string
118 text : string
120 Text to replace in.
119 Text to replace in.
121 search : string
120 search : string
122 String to search for within "text"
121 String to search for within "text"
123 replacement : string
122 replacement : string
124 String to replace "search" with
123 String to replace "search" with
125 """
124 """
126 while search in text:
125 while search in text:
127 text = text.replace(search, replacement)
126 text = text.replace(search, replacement)
128 return text
127 return text
129
128
130
129
131 def create_temp_cwd(self, copy_filenames=None):
130 def create_temp_cwd(self, copy_filenames=None):
132 temp_dir = TemporaryWorkingDirectory()
131 temp_dir = TemporaryWorkingDirectory()
133
132
134 #Copy the files if requested.
133 #Copy the files if requested.
135 if not copy_filenames is None:
134 if not copy_filenames is None:
136 self.copy_files_to(copy_filenames)
135 self.copy_files_to(copy_filenames)
137
136
138 #Return directory handler
137 #Return directory handler
139 return temp_dir
138 return temp_dir
140
139
141
140
142 def copy_files_to(self, copy_filenames=None, destination=None):
141 def copy_files_to(self, copy_filenames=None, destination=None):
143
142
144 #Copy test files into the destination directory.
143 #Copy test files into the destination directory.
145 if copy_filenames:
144 if copy_filenames:
146 for pattern in copy_filenames:
145 for pattern in copy_filenames:
147 for match in glob.glob(os.path.join(self._get_files_path(), pattern)):
146 for match in glob.glob(os.path.join(self._get_files_path(), pattern)):
148 if destination is None:
147 if destination is None:
149 shutil.copyfile(match, os.path.basename(match))
148 shutil.copyfile(match, os.path.basename(match))
150 else:
149 else:
151 if not os.path.isdir(destination):
150 if not os.path.isdir(destination):
152 os.makedirs(destination)
151 os.makedirs(destination)
153 shutil.copyfile(match, os.path.join(destination, os.path.basename(match)))
152 shutil.copyfile(match, os.path.join(destination, os.path.basename(match)))
154
153
155
154
156 def _get_files_path(self):
155 def _get_files_path(self):
157
156
158 #Get the relative path to this module in the IPython directory.
157 #Get the relative path to this module in the IPython directory.
159 names = self.__module__.split('.')[1:-1]
158 names = self.__module__.split('.')[1:-1]
160 names.append('files')
159 names.append('files')
161
160
162 #Build a path using the IPython directory and the relative path we just
161 #Build a path using the IPython directory and the relative path we just
163 #found.
162 #found.
164 path = IPython.__path__[0]
163 path = IPython.__path__[0]
165 for name in names:
164 for name in names:
166 path = os.path.join(path, name)
165 path = os.path.join(path, name)
167 return path
166 return path
168
167
169
168
170 def call(self, parameters):
169 def call(self, parameters):
171 output = subprocess.Popen(parameters, stdout=subprocess.PIPE).communicate()[0]
170 output = subprocess.Popen(parameters, stdout=subprocess.PIPE).communicate()[0]
172
171
173 #Convert the output to a string if running Python3
172 #Convert the output to a string if running Python3
174 if py3compat.PY3:
173 if py3compat.PY3:
175 return output.decode('utf-8')
174 return output.decode('utf-8')
176 else:
175 else:
177 return output
176 return output
178 No newline at end of file
177
@@ -1,123 +1,149 b''
1 """
1 """
2 Contains tests for the nbconvertapp
2 Contains tests for the nbconvertapp
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
15
16 import os
16 import os
17 from .base import TestsBase
17 from .base import TestsBase
18
18
19 from IPython.utils import py3compat
19 from IPython.utils import py3compat
20 from IPython.testing import decorators as dec
20
21
21
22
22 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
23 # Constants
24 # Constants
24 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
25
26
26 # Define ipython commandline name
27 # Define ipython commandline name
27 if py3compat.PY3:
28 if py3compat.PY3:
28 IPYTHON = 'ipython3'
29 IPYTHON = 'ipython3'
29 else:
30 else:
30 IPYTHON = 'ipython'
31 IPYTHON = 'ipython'
31
32
32
33
33 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
34 # Classes and functions
35 # Classes and functions
35 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
36
37
37 class TestNbConvertApp(TestsBase):
38 class TestNbConvertApp(TestsBase):
38 """Collection of NbConvertApp tests"""
39 """Collection of NbConvertApp tests"""
39
40
40
41
41 def test_notebook_help(self):
42 def test_notebook_help(self):
42 """
43 """
43 Will help show if no notebooks are specified?
44 Will help show if no notebooks are specified?
44 """
45 """
45 with self.create_temp_cwd():
46 with self.create_temp_cwd():
46 assert "see '--help-all'" in self.call([IPYTHON, 'nbconvert'])
47 assert "see '--help-all'" in self.call([IPYTHON, 'nbconvert'])
47
48
48
49
49 def test_glob(self):
50 def test_glob(self):
50 """
51 """
51 Do search patterns work for notebook names?
52 Do search patterns work for notebook names?
52 """
53 """
53 with self.create_temp_cwd(['notebook*.ipynb']):
54 with self.create_temp_cwd(['notebook*.ipynb']):
54 assert not 'error' in self.call([IPYTHON, 'nbconvert',
55 assert not 'error' in self.call([IPYTHON, 'nbconvert',
55 '--format="python"', '--notebooks=["*.ipynb"]']).lower()
56 '--to="python"', '--notebooks=["*.ipynb"]']).lower()
56 assert os.path.isfile('notebook1.py')
57 assert os.path.isfile('notebook1.py')
57 assert os.path.isfile('notebook2.py')
58 assert os.path.isfile('notebook2.py')
58
59
59
60
60 def test_glob_subdir(self):
61 def test_glob_subdir(self):
61 """
62 """
62 Do search patterns work for subdirectory notebook names?
63 Do search patterns work for subdirectory notebook names?
63 """
64 """
64 with self.create_temp_cwd() as cwd:
65 with self.create_temp_cwd() as cwd:
65 self.copy_files_to(['notebook*.ipynb'], 'subdir/')
66 self.copy_files_to(['notebook*.ipynb'], 'subdir/')
66 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--format="python"',
67 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="python"',
67 '--notebooks=["%s"]' % os.path.join('subdir', '*.ipynb')]).lower()
68 '--notebooks=["%s"]' % os.path.join('subdir', '*.ipynb')]).lower()
68 assert os.path.isfile('notebook1.py')
69 assert os.path.isfile('notebook1.py')
69 assert os.path.isfile('notebook2.py')
70 assert os.path.isfile('notebook2.py')
70
71
71
72
72 def test_explicit(self):
73 def test_explicit(self):
73 """
74 """
74 Do explicit notebook names work?
75 Do explicit notebook names work?
75 """
76 """
76 with self.create_temp_cwd(['notebook*.ipynb']):
77 with self.create_temp_cwd(['notebook*.ipynb']):
77 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--format="python"',
78 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="python"',
78 '--notebooks=["notebook2.ipynb"]']).lower()
79 '--notebooks=["notebook2.ipynb"]']).lower()
79 assert not os.path.isfile('notebook1.py')
80 assert not os.path.isfile('notebook1.py')
80 assert os.path.isfile('notebook2.py')
81 assert os.path.isfile('notebook2.py')
81
82
82
83
84 @dec.onlyif_cmds_exist('pdflatex')
85 def test_post_processor(self):
86 """
87 Do post processors work?
88 """
89 with self.create_temp_cwd(['notebook1.ipynb']):
90 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="latex"',
91 'notebook1', '--post="PDF"', '--PDFPostProcessor.verbose=True']).lower()
92 assert os.path.isfile('notebook1.tex')
93 print("\n\n\t" + "\n\t".join([f for f in os.listdir('.') if os.path.isfile(f)]) + "\n\n")
94 assert os.path.isfile('notebook1.pdf')
95
96
97 def test_template(self):
98 """
99 Do export templates work?
100 """
101 with self.create_temp_cwd(['notebook2.ipynb']):
102 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to=slides',
103 '--notebooks=["notebook2.ipynb"]', '--template=reveal']).lower()
104 assert os.path.isfile('notebook2.slides.html')
105 with open('notebook2.slides.html') as f:
106 assert '/reveal.css' in f.read()
107
108
83 def test_glob_explicit(self):
109 def test_glob_explicit(self):
84 """
110 """
85 Can a search pattern be used along with matching explicit notebook names?
111 Can a search pattern be used along with matching explicit notebook names?
86 """
112 """
87 with self.create_temp_cwd(['notebook*.ipynb']):
113 with self.create_temp_cwd(['notebook*.ipynb']):
88 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--format="python"',
114 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="python"',
89 '--notebooks=["*.ipynb", "notebook1.ipynb", "notebook2.ipynb"]']).lower()
115 '--notebooks=["*.ipynb", "notebook1.ipynb", "notebook2.ipynb"]']).lower()
90 assert os.path.isfile('notebook1.py')
116 assert os.path.isfile('notebook1.py')
91 assert os.path.isfile('notebook2.py')
117 assert os.path.isfile('notebook2.py')
92
118
93
119
94 def test_explicit_glob(self):
120 def test_explicit_glob(self):
95 """
121 """
96 Can explicit notebook names be used and then a matching search pattern?
122 Can explicit notebook names be used and then a matching search pattern?
97 """
123 """
98 with self.create_temp_cwd(['notebook*.ipynb']):
124 with self.create_temp_cwd(['notebook*.ipynb']):
99 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--format="python"',
125 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="python"',
100 '--notebooks=["notebook1.ipynb", "notebook2.ipynb", "*.ipynb"]']).lower()
126 '--notebooks=["notebook1.ipynb", "notebook2.ipynb", "*.ipynb"]']).lower()
101 assert os.path.isfile('notebook1.py')
127 assert os.path.isfile('notebook1.py')
102 assert os.path.isfile('notebook2.py')
128 assert os.path.isfile('notebook2.py')
103
129
104
130
105 def test_default_config(self):
131 def test_default_config(self):
106 """
132 """
107 Does the default config work?
133 Does the default config work?
108 """
134 """
109 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py']):
135 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py']):
110 assert not 'error' in self.call([IPYTHON, 'nbconvert']).lower()
136 assert not 'error' in self.call([IPYTHON, 'nbconvert']).lower()
111 assert os.path.isfile('notebook1.py')
137 assert os.path.isfile('notebook1.py')
112 assert not os.path.isfile('notebook2.py')
138 assert not os.path.isfile('notebook2.py')
113
139
114
140
115 def test_override_config(self):
141 def test_override_config(self):
116 """
142 """
117 Can the default config be overriden?
143 Can the default config be overriden?
118 """
144 """
119 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py',
145 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py',
120 'override.py']):
146 'override.py']):
121 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--config="override.py"']).lower()
147 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--config="override.py"']).lower()
122 assert not os.path.isfile('notebook1.py')
148 assert not os.path.isfile('notebook1.py')
123 assert os.path.isfile('notebook2.py')
149 assert os.path.isfile('notebook2.py')
@@ -1,2 +1,3 b''
1 from .files import FilesWriter
1 from .files import FilesWriter
2 from .stdout import StdoutWriter
2 from .stdout import StdoutWriter
3 from .base import WriterBase
@@ -1,57 +1,56 b''
1 #!/usr/bin/env python
2 """
1 """
3 Contains writer base class.
2 Contains writer base class.
4 """
3 """
5 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
6 #Copyright (c) 2013, the IPython Development Team.
5 #Copyright (c) 2013, the IPython Development Team.
7 #
6 #
8 #Distributed under the terms of the Modified BSD License.
7 #Distributed under the terms of the Modified BSD License.
9 #
8 #
10 #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.
11 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
12
11
13 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
14 # Imports
13 # Imports
15 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
16
15
17 from IPython.utils.traitlets import List
16 from IPython.utils.traitlets import List
18
17
19 from ..utils.base import NbConvertBase
18 from ..utils.base import NbConvertBase
20
19
21 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
22 # Classes
21 # Classes
23 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
24
23
25 class WriterBase(NbConvertBase):
24 class WriterBase(NbConvertBase):
26 """Consumes output from nbconvert export...() methods and writes to a
25 """Consumes output from nbconvert export...() methods and writes to a
27 useful location. """
26 useful location. """
28
27
29
28
30 files = List([], config=True, help="""
29 files = List([], config=True, help="""
31 List of the files that the notebook references. Files will be
30 List of the files that the notebook references. Files will be
32 included with written output.""")
31 included with written output.""")
33
32
34
33
35 def __init__(self, config=None, **kw):
34 def __init__(self, config=None, **kw):
36 """
35 """
37 Constructor
36 Constructor
38 """
37 """
39 super(WriterBase, self).__init__(config=config, **kw)
38 super(WriterBase, self).__init__(config=config, **kw)
40
39
41
40
42 def write(self, output, resources, **kw):
41 def write(self, output, resources, **kw):
43 """
42 """
44 Consume and write Jinja output.
43 Consume and write Jinja output.
45
44
46 Parameters
45 Parameters
47 ----------
46 ----------
48 output : string
47 output : string
49 Conversion results. This string contains the file contents of the
48 Conversion results. This string contains the file contents of the
50 converted file.
49 converted file.
51 resources : dict
50 resources : dict
52 Resources created and filled by the nbconvert conversion process.
51 Resources created and filled by the nbconvert conversion process.
53 Includes output from transformers, such as the extract figure
52 Includes output from transformers, such as the extract figure
54 transformer.
53 transformer.
55 """
54 """
56
55
57 raise NotImplementedError()
56 raise NotImplementedError()
@@ -1,43 +1,42 b''
1 #!/usr/bin/env python
2 """
1 """
3 Contains debug writer.
2 Contains debug writer.
4 """
3 """
5 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
6 #Copyright (c) 2013, the IPython Development Team.
5 #Copyright (c) 2013, the IPython Development Team.
7 #
6 #
8 #Distributed under the terms of the Modified BSD License.
7 #Distributed under the terms of the Modified BSD License.
9 #
8 #
10 #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.
11 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
12
11
13 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
14 # Imports
13 # Imports
15 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
16
15
17 from .base import WriterBase
16 from .base import WriterBase
18 from pprint import pprint
17 from pprint import pprint
19
18
20 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
21 # Classes
20 # Classes
22 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
23
22
24 class DebugWriter(WriterBase):
23 class DebugWriter(WriterBase):
25 """Consumes output from nbconvert export...() methods and writes usefull
24 """Consumes output from nbconvert export...() methods and writes usefull
26 debugging information to the stdout. The information includes a list of
25 debugging information to the stdout. The information includes a list of
27 resources that were extracted from the notebook(s) during export."""
26 resources that were extracted from the notebook(s) during export."""
28
27
29
28
30 def write(self, output, resources, notebook_name='notebook', **kw):
29 def write(self, output, resources, notebook_name='notebook', **kw):
31 """
30 """
32 Consume and write Jinja output.
31 Consume and write Jinja output.
33
32
34 See base for more...
33 See base for more...
35 """
34 """
36
35
37 if 'outputs' in resources:
36 if 'outputs' in resources:
38 print("outputs extracted from %s" % notebook_name)
37 print("outputs extracted from %s" % notebook_name)
39 print('-' * 80)
38 print('-' * 80)
40 pprint.pprint(resources['outputs'], indent=2, width=70)
39 pprint.pprint(resources['outputs'], indent=2, width=70)
41 else:
40 else:
42 print("No outputs extracted from %s" % notebook_name)
41 print("No outputs extracted from %s" % notebook_name)
43 print('=' * 80)
42 print('=' * 80)
@@ -1,102 +1,102 b''
1 #!/usr/bin/env python
2 """
1 """
3 Contains writer for writing nbconvert output to filesystem.
2 Contains writer for writing nbconvert output to filesystem.
4 """
3 """
5 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
6 #Copyright (c) 2013, the IPython Development Team.
5 #Copyright (c) 2013, the IPython Development Team.
7 #
6 #
8 #Distributed under the terms of the Modified BSD License.
7 #Distributed under the terms of the Modified BSD License.
9 #
8 #
10 #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.
11 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
12
11
13 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
14 # Imports
13 # Imports
15 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
16
15
17 import io
16 import io
18 import os
17 import os
19 import glob
18 import glob
20
19
21 from IPython.utils.traitlets import Unicode
20 from IPython.utils.traitlets import Unicode
22 from IPython.utils.path import link_or_copy
21 from IPython.utils.path import link_or_copy
23
22
24 from .base import WriterBase
23 from .base import WriterBase
25
24
26 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
27 # Classes
26 # Classes
28 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
29
28
30 class FilesWriter(WriterBase):
29 class FilesWriter(WriterBase):
31 """Consumes nbconvert output and produces files."""
30 """Consumes nbconvert output and produces files."""
32
31
33
32
34 build_directory = Unicode(".", config=True,
33 build_directory = Unicode(".", config=True,
35 help="""Directory to write output to. Leave blank
34 help="""Directory to write output to. Leave blank
36 to output to the current directory""")
35 to output to the current directory""")
37
36
38
37
39 # Make sure that the output directory exists.
38 # Make sure that the output directory exists.
40 def _build_directory_changed(self, name, old, new):
39 def _build_directory_changed(self, name, old, new):
41 if new and not os.path.isdir(new):
40 if new and not os.path.isdir(new):
42 os.makedirs(new)
41 os.makedirs(new)
43
42
44
43
45 def __init__(self, **kw):
44 def __init__(self, **kw):
46 super(FilesWriter, self).__init__(**kw)
45 super(FilesWriter, self).__init__(**kw)
47 self._build_directory_changed('build_directory', self.build_directory,
46 self._build_directory_changed('build_directory', self.build_directory,
48 self.build_directory)
47 self.build_directory)
49
48
50
49
51 def write(self, output, resources, notebook_name=None, **kw):
50 def write(self, output, resources, notebook_name=None, **kw):
52 """
51 """
53 Consume and write Jinja output to the file system. Output directory
52 Consume and write Jinja output to the file system. Output directory
54 is set via the 'build_directory' variable of this instance (a
53 is set via the 'build_directory' variable of this instance (a
55 configurable).
54 configurable).
56
55
57 See base for more...
56 See base for more...
58 """
57 """
59
58
60 # Pull the extension and subdir from the resources dict.
59 # Pull the extension and subdir from the resources dict.
61 output_extension = resources['output_extension']
60 output_extension = resources['output_extension']
62
61
63 # Write all of the extracted resources to the destination directory.
62 # Write all of the extracted resources to the destination directory.
64 # NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG
63 # NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG
65 # TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS...
64 # TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS...
66 for filename, data in resources.get('outputs', {}).items():
65 for filename, data in resources.get('outputs', {}).items():
67
66
68 # Determine where to write the file to
67 # Determine where to write the file to
69 dest = os.path.join(self.build_directory, filename)
68 dest = os.path.join(self.build_directory, filename)
70 path = os.path.dirname(dest)
69 path = os.path.dirname(dest)
71 if not os.path.isdir(path):
70 if not os.path.isdir(path):
72 os.makedirs(path)
71 os.makedirs(path)
73
72
74 # Write file
73 # Write file
75 with io.open(dest, 'wb') as f:
74 with io.open(dest, 'wb') as f:
76 f.write(data)
75 f.write(data)
77
76
78 # Copy referenced files to output directory
77 # Copy referenced files to output directory
79 if self.build_directory:
78 if self.build_directory:
80 for filename in self.files:
79 for filename in self.files:
81
80
82 # Copy files that match search pattern
81 # Copy files that match search pattern
83 for matching_filename in glob.glob(filename):
82 for matching_filename in glob.glob(filename):
84
83
85 # Make sure folder exists.
84 # Make sure folder exists.
86 dest = os.path.join(self.build_directory, filename)
85 dest = os.path.join(self.build_directory, filename)
87 path = os.path.dirname(dest)
86 path = os.path.dirname(dest)
88 if not os.path.isdir(path):
87 if not os.path.isdir(path):
89 os.makedirs(path)
88 os.makedirs(path)
90
89
91 # Copy if destination is different.
90 # Copy if destination is different.
92 if not os.path.normpath(dest) == os.path.normpath(matching_filename):
91 if not os.path.normpath(dest) == os.path.normpath(matching_filename):
93 link_or_copy(matching_filename, dest)
92 link_or_copy(matching_filename, dest)
94
93
95 # Determine where to write conversion results.
94 # Determine where to write conversion results.
96 dest = notebook_name + '.' + output_extension
95 dest = notebook_name + '.' + output_extension
97 if self.build_directory:
96 if self.build_directory:
98 dest = os.path.join(self.build_directory, dest)
97 dest = os.path.join(self.build_directory, dest)
99
98
100 # Write conversion results.
99 # Write conversion results.
101 with io.open(dest, 'w') as f:
100 with io.open(dest, 'w') as f:
102 f.write(output)
101 f.write(output)
102 return dest No newline at end of file
@@ -1,35 +1,34 b''
1 #!/usr/bin/env python
2 """
1 """
3 Contains Stdout writer
2 Contains Stdout writer
4 """
3 """
5 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
6 #Copyright (c) 2013, the IPython Development Team.
5 #Copyright (c) 2013, the IPython Development Team.
7 #
6 #
8 #Distributed under the terms of the Modified BSD License.
7 #Distributed under the terms of the Modified BSD License.
9 #
8 #
10 #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.
11 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
12
11
13 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
14 # Imports
13 # Imports
15 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
16
15
17 from .base import WriterBase
16 from .base import WriterBase
18
17
19 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
20 # Classes
19 # Classes
21 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
22
21
23 class StdoutWriter(WriterBase):
22 class StdoutWriter(WriterBase):
24 """Consumes output from nbconvert export...() methods and writes to the
23 """Consumes output from nbconvert export...() methods and writes to the
25 stdout stream."""
24 stdout stream."""
26
25
27
26
28 def write(self, output, resources, **kw):
27 def write(self, output, resources, **kw):
29 """
28 """
30 Consume and write Jinja output.
29 Consume and write Jinja output.
31
30
32 See base for more...
31 See base for more...
33 """
32 """
34
33
35 print(output)
34 print(output)
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now