##// END OF EJS Templates
Merge pull request #1 from minrk/nbconvert...
Jonathan Frederic -
r11470:6bbbdb48 merge
parent child Browse files
Show More
@@ -1,11 +1,11 b''
1 1 from .basichtml import BasicHTMLExporter
2 2 from .export import *
3 3 from .exporter import Exporter
4 4 from .fullhtml import FullHTMLExporter
5 5 from .reveal import RevealExporter
6 6 from .latex import LatexExporter
7 7 from .markdown import MarkdownExporter
8 8 from .python import PythonExporter
9 from .rst import RstExporter
9 from .rst import RSTExporter
10 10 from .sphinx_howto import SphinxHowtoExporter
11 11 from .sphinx_manual import SphinxManualExporter
@@ -1,226 +1,226 b''
1 1 """
2 2 Module containing single call export functions.
3 3 """
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (c) 2013, the IPython Development Team.
6 6 #
7 7 # Distributed under the terms of the Modified BSD License.
8 8 #
9 9 # The full license is in the file COPYING.txt, distributed with this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 16 from functools import wraps
17 17
18 18 from IPython.nbformat.v3.nbbase import NotebookNode
19 19 from IPython.config import Config
20 20
21 21 from .exporter import Exporter
22 22 from .basichtml import BasicHTMLExporter
23 23 from .fullhtml import FullHTMLExporter
24 24 from .latex import LatexExporter
25 25 from .markdown import MarkdownExporter
26 26 from .python import PythonExporter
27 27 from .reveal import RevealExporter
28 from .rst import RstExporter
28 from .rst import RSTExporter
29 29 from .sphinx_howto import SphinxHowtoExporter
30 30 from .sphinx_manual import SphinxManualExporter
31 31
32 32 #-----------------------------------------------------------------------------
33 33 # Classes
34 34 #-----------------------------------------------------------------------------
35 35
36 def DocDecorator(f):
36 def DocDecorator(f):
37 37
38 38 #Set docstring of function
39 39 f.__doc__ = f.__doc__ + """
40 40 nb : Notebook node
41 41 config : config (optional, keyword arg)
42 42 User configuration instance.
43 43 resources : dict (optional, keyword arg)
44 44 Resources used in the conversion process.
45 45
46 46 Returns
47 47 ----------
48 48 tuple- output, resources, exporter_instance
49 49 output : str
50 50 Jinja 2 output. This is the resulting converted notebook.
51 51 resources : dictionary
52 52 Dictionary of resources used prior to and during the conversion
53 53 process.
54 54 exporter_instance : Exporter
55 55 Instance of the Exporter class used to export the document. Useful
56 56 to caller because it provides a 'file_extension' property which
57 57 specifies what extension the output should be saved as."""
58 58
59 59 @wraps(f)
60 60 def decorator(*args, **kwargs):
61 61 return f(*args, **kwargs)
62 62
63 63 return decorator
64 64
65 65
66 66 #-----------------------------------------------------------------------------
67 67 # Functions
68 68 #-----------------------------------------------------------------------------
69 69
70 70 __all__ = [
71 71 'export',
72 72 'export_sphinx_manual',
73 73 'export_sphinx_howto',
74 74 'export_basic_html',
75 75 'export_full_html',
76 76 'export_latex',
77 77 'export_markdown',
78 78 'export_python',
79 79 'export_reveal',
80 80 'export_rst',
81 81 'export_by_name',
82 82 'get_export_names',
83 83 'ExporterNameError'
84 84 ]
85 85
86 86
87 87 class ExporterNameError(NameError):
88 88 pass
89 89
90 90
91 91 @DocDecorator
92 92 def export(exporter, nb, **kw):
93 93 """
94 94 Export a notebook object using specific exporter class.
95 95
96 96 exporter : Exporter class type or instance
97 97 Class type or instance of the exporter that should be used. If the
98 98 method initializes it's own instance of the class, it is ASSUMED that
99 99 the class type provided exposes a constructor (__init__) with the same
100 100 signature as the base Exporter class.
101 101 """
102 102
103 103 #Check arguments
104 104 if exporter is None:
105 105 raise TypeError("Exporter is None")
106 106 elif not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter):
107 107 raise TypeError("exporter does not inherit from Exporter (base)")
108 108 if nb is None:
109 109 raise TypeError("nb is None")
110 110
111 111 #Create the exporter
112 112 resources = kw.pop('resources', None)
113 113 if isinstance(exporter, Exporter):
114 114 exporter_instance = exporter
115 115 else:
116 116 exporter_instance = exporter(**kw)
117 117
118 118 #Try to convert the notebook using the appropriate conversion function.
119 119 if isinstance(nb, NotebookNode):
120 120 output, resources = exporter_instance.from_notebook_node(nb, resources)
121 121 elif isinstance(nb, basestring):
122 122 output, resources = exporter_instance.from_filename(nb, resources)
123 123 else:
124 124 output, resources = exporter_instance.from_file(nb, resources)
125 125 return output, resources
126 126
127 127
128 128 @DocDecorator
129 129 def export_sphinx_manual(nb, **kw):
130 130 """
131 131 Export a notebook object to Sphinx Manual LaTeX
132 132 """
133 133 return export(SphinxManualExporter, nb, **kw)
134 134
135 135
136 136 @DocDecorator
137 137 def export_sphinx_howto(nb, **kw):
138 138 """
139 139 Export a notebook object to Sphinx HowTo LaTeX
140 140 """
141 141 return export(SphinxHowtoExporter, nb, **kw)
142 142
143 143
144 144 @DocDecorator
145 145 def export_basic_html(nb, **kw):
146 146 """
147 147 Export a notebook object to Basic HTML
148 148 """
149 149 return export(BasicHTMLExporter, nb, **kw)
150 150
151 151
152 152 @DocDecorator
153 153 def export_full_html(nb, **kw):
154 154 """
155 155 Export a notebook object to Full HTML
156 156 """
157 157 return export(FullHTMLExporter, nb, **kw)
158 158
159 159
160 160 @DocDecorator
161 161 def export_latex(nb, **kw):
162 162 """
163 163 Export a notebook object to LaTeX
164 164 """
165 165 return export(LatexExporter, nb, **kw)
166 166
167 167
168 168 @DocDecorator
169 169 def export_markdown(nb, **kw):
170 170 """
171 171 Export a notebook object to Markdown
172 172 """
173 173 return export(MarkdownExporter, nb, **kw)
174 174
175 175
176 176 @DocDecorator
177 177 def export_python(nb, **kw):
178 178 """
179 179 Export a notebook object to Python
180 180 """
181 181 return export(PythonExporter, nb, **kw)
182 182
183 183
184 184 @DocDecorator
185 185 def export_reveal(nb, **kw):
186 186 """
187 Export a notebook object to Reveal
187 Export a notebook object to a Reveal.js presentation
188 188 """
189 189 return export(RevealExporter, nb, **kw)
190 190
191 191
192 192 @DocDecorator
193 193 def export_rst(nb, **kw):
194 194 """
195 Export a notebook object to RST
195 Export a notebook object to reStructuredText
196 196 """
197 return export(RstExporter, nb, **kw)
197 return export(RSTExporter, nb, **kw)
198 198
199 199
200 200 @DocDecorator
201 201 def export_by_name(format_name, nb, **kw):
202 202 """
203 203 Export a notebook object to a template type by its name. Reflection
204 204 (Inspect) is used to find the template's corresponding explicit export
205 205 method defined in this module. That method is then called directly.
206 206
207 207 format_name : str
208 208 Name of the template style to export to.
209 209 """
210 210
211 211 function_name = "export_" + format_name.lower()
212 212
213 213 if function_name in globals():
214 214 return globals()[function_name](nb, **kw)
215 215 else:
216 216 raise ExporterNameError("template for `%s` not found" % function_name)
217 217
218 218
219 219 def get_export_names():
220 220 "Return a list of the currently supported export targets"
221 221 # grab everything after 'export_'
222 222 l = [x[len('export_'):] for x in __all__ if x.startswith('export_')]
223 223
224 224 # filter out the one method that is not a template
225 225 l = [x for x in l if 'by_name' not in x]
226 226 return sorted(l)
@@ -1,42 +1,42 b''
1 1 """
2 2 Exporter for exporting notebooks to restructured text.
3 3 """
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (c) 2013, the IPython Development Team.
6 6 #
7 7 # Distributed under the terms of the Modified BSD License.
8 8 #
9 9 # The full license is in the file COPYING.txt, distributed with this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 16 from IPython.utils.traitlets import Unicode
17 17 from IPython.config import Config
18 18
19 19 from .exporter import Exporter
20 20
21 21 #-----------------------------------------------------------------------------
22 22 # Classes
23 23 #-----------------------------------------------------------------------------
24 24
25 class RstExporter(Exporter):
25 class RSTExporter(Exporter):
26 26 """
27 27 Exports restructured text documents.
28 28 """
29 29
30 30 file_extension = Unicode(
31 31 'rst', config=True,
32 32 help="Extension of the file that should be written to disk")
33 33
34 34 template_file = Unicode(
35 35 'rst', config=True,
36 36 help="Name of the template file to use")
37 37
38 38 @property
39 39 def default_config(self):
40 40 c = Config({'ExtractFigureTransformer':{'enabled':True}})
41 c.merge(super(RstExporter,self).default_config)
41 c.merge(super(RSTExporter,self).default_config)
42 42 return c
@@ -1,197 +1,229 b''
1 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 Commandline interface for the NBConvert conversion utility. Read the
5 readme.rst for usage information
4 Command-line interface for the NbConvert conversion utility.
6 5 """
7 6 #-----------------------------------------------------------------------------
8 7 #Copyright (c) 2013, the IPython Development Team.
9 8 #
10 9 #Distributed under the terms of the Modified BSD License.
11 10 #
12 11 #The full license is in the file COPYING.txt, distributed with this software.
13 12 #-----------------------------------------------------------------------------
14 13
15 14 #-----------------------------------------------------------------------------
16 15 #Imports
17 16 #-----------------------------------------------------------------------------
18 17
19 18 #Stdlib imports
20 19 from __future__ import print_function
21 20 import sys
22 21 import os
23 22 import glob
24 23
25 24 #From IPython
26 from IPython.core.application import BaseIPythonApplication
27 from IPython.config.application import catch_config_error
28 from IPython.utils.traitlets import Unicode, List, Instance, DottedObjectName, Type
25 from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags
26 from IPython.config import catch_config_error, Configurable
27 from IPython.utils.traitlets import (
28 Unicode, List, Instance, DottedObjectName, Type, CaselessStrEnum,
29 )
29 30 from IPython.utils.importstring import import_item
30 31
31 32 from .exporters.export import export_by_name, get_export_names, ExporterNameError
32 from .exporters.exporter import Exporter
33 from .writers.base import WriterBase
33 from IPython.nbconvert import exporters, transformers, writers
34 34 from .utils.base import NbConvertBase
35 35
36 36 #-----------------------------------------------------------------------------
37 37 #Classes and functions
38 38 #-----------------------------------------------------------------------------
39 39
40 nbconvert_aliases = {}
41 nbconvert_aliases.update(base_aliases)
42 nbconvert_aliases.update({
43 'format' : 'NbConvertApp.export_format',
44 'notebooks' : 'NbConvertApp.notebooks',
45 'writer' : 'NbConvertApp.writer_class',
46 })
47
48 nbconvert_flags = {}
49 nbconvert_flags.update(base_flags)
50 nbconvert_flags.update({
51 'stdout' : (
52 {'NbConvertApp' : {'writer_class' : "StdoutWriter"}},
53 "Write notebook output to stdout instead of files."
54 )
55 })
56
57
40 58 class NbConvertApp(BaseIPythonApplication):
41 59 """Application used to convert to and from notebook file type (*.ipynb)"""
42 60
43 61 name = 'ipython-nbconvert'
62 aliases = nbconvert_aliases
63 flags = nbconvert_flags
64
65 def _classes_default(self):
66 classes = [NbConvertBase]
67 for pkg in (exporters, transformers, writers):
68 for name in dir(pkg):
69 cls = getattr(pkg, name)
70 if isinstance(cls, type) and issubclass(cls, Configurable):
71 classes.append(cls)
72 return classes
44 73
45 74 description = Unicode(
46 u"""This application is used to convert notebook files (*.ipynb).
47 An ipython config file can be used to batch convert notebooks in the
48 current directory.""")
75 u"""This application is used to convert notebook files (*.ipynb)
76 to various other formats.""")
49 77
50 78 examples = Unicode(u"""
51 Running `ipython nbconvert` will read the directory config file and then
52 apply it to one or more notebooks.
53
79 The simplest way to use nbconvert is
80
81 > ipython nbconvert mynotebook.ipynb
82
83 which will convert mynotebook.ipynb to the default format (probably HTML).
84
85 You can specify the export format with `--format`.
86 Options include {0}
87
88 > ipython nbconvert --format latex mynotebook.ipnynb
89
90 You can also pipe the output to stdout, rather than a file
91
92 > ipython nbconvert mynotebook.ipynb --stdout
93
54 94 Multiple notebooks can be given at the command line in a couple of
55 95 different ways:
56 96
57 97 > ipython nbconvert notebook*.ipynb
58 98 > ipython nbconvert notebook1.ipynb notebook2.ipynb
59 > ipython nbconvert # this will use the config file to fill in the notebooks
60 """)
61
99
100 or you can specify the notebooks list in a config file, containing::
101
102 c.NbConvertApp.notebooks = ["my_notebook.ipynb"]
103
104 > ipython nbconvert --config mycfg.py
105 """.format(get_export_names()))
62 106 #Writer specific variables
63 107 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
64 108 help="""Instance of the writer class used to write the
65 109 results of the conversion.""")
66 110 writer_class = DottedObjectName('FilesWriter', config=True,
67 111 help="""Writer class used to write the
68 112 results of the conversion""")
69 113 writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter',
70 114 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter',
71 115 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'}
72 116 writer_factory = Type()
73 117
74 118 def _writer_class_changed(self, name, old, new):
75 119 if new in self.writer_aliases:
76 120 new = self.writer_aliases[new]
77 121 self.writer_factory = import_item(new)
78 122
79 123
80 124 #Other configurable variables
81 export_format = Unicode(
82 "", config=True,
83 help="""If specified, nbconvert will convert the document(s) specified
84 using this format.""")
125 export_format = CaselessStrEnum(get_export_names(),
126 default_value="full_html",
127 config=True,
128 help="""The export format to be used."""
129 )
85 130
86 131 notebooks = List([], config=True, help="""List of notebooks to convert.
87 Search patterns are supported.""")
88
89 nbconvert_aliases = {'format':'NbConvertApp.export_format',
90 'notebooks':'NbConvertApp.notebooks',
91 'writer':'NbConvertApp.writer_class'}
92
132 Wildcards are supported.
133 Filenames passed positionally will be added to the list.
134 """)
93 135
94 136 @catch_config_error
95 137 def initialize(self, argv=None):
96 self.aliases.update(self.nbconvert_aliases)
97
98 138 super(NbConvertApp, self).initialize(argv)
99
100 #Register class here to have help with help all
101 self.classes.insert(0, Exporter)
102 self.classes.insert(0, WriterBase)
103 self.classes.insert(0, NbConvertBase)
104
105 #Init
106 self.init_config(self.extra_args)
139 self.init_notebooks()
107 140 self.init_writer()
108 141
109
110 def init_config(self, extra_args):
111 """
112 Add notebooks to the config if needed. Glob each notebook to replace
113 notebook patterns with filenames.
142 def init_notebooks(self):
143 """Construct the list of notebooks.
144 If notebooks are passed on the command-line,
145 they override notebooks specified in config files.
146 Glob each notebook to replace notebook patterns with filenames.
114 147 """
115 148
116 #Get any additional notebook patterns from the commandline
117 if len(extra_args) > 0:
118 for pattern in extra_args:
119 self.notebooks.append(pattern)
149 # Specifying notebooks on the command-line overrides (rather than adds)
150 # the notebook list
151 if self.extra_args:
152 patterns = self.extra_args
153 else:
154 patterns = self.notebooks
120 155
121 156 #Use glob to replace all the notebook patterns with filenames.
122 157 filenames = []
123 for pattern in self.notebooks:
158 for pattern in patterns:
124 159 for filename in glob.glob(pattern):
125 160 if not filename in filenames:
126 161 filenames.append(filename)
127 162 self.notebooks = filenames
128 163
129
130 164 def init_writer(self):
131 165 """
132 166 Initialize the writer (which is stateless)
133 167 """
134 168 self._writer_class_changed(None, self.writer_class, self.writer_class)
135 169 self.writer = self.writer_factory(parent=self)
136 170
137
138 def start(self, argv=None):
171 def start(self):
139 172 """
140 Ran after initiialization completed
173 Ran after initialization completed
141 174 """
142 175 super(NbConvertApp, self).start()
143 176 self.convert_notebooks()
144 177
145
146 178 def convert_notebooks(self):
147 179 """
148 180 Convert the notebooks in the self.notebook traitlet
149 181 """
150 182 #Export each notebook
151 183 conversion_success = 0
152 184 for notebook_filename in self.notebooks:
153 185
154 186 #Get a unique key for the notebook and set it in the resources object.
155 187 basename = os.path.basename(notebook_filename)
156 188 notebook_name = basename[:basename.rfind('.')]
157 189 resources = {}
158 190 resources['unique_key'] = notebook_name
159 191
160 192 #Try to export
161 193 try:
162 194 output, resources = export_by_name(self.export_format,
163 195 notebook_filename,
164 196 resources=resources,
165 197 config=self.config)
166 198 except ExporterNameError as e:
167 199 print("Error: '%s' exporter not found." % self.export_format,
168 200 file=sys.stderr)
169 201 print("Known exporters are:",
170 202 "\n\t" + "\n\t".join(get_export_names()),
171 203 file=sys.stderr)
172 204 sys.exit(-1)
173 205 #except Exception as e:
174 206 #print("Error: could not export '%s'" % notebook_filename, file=sys.stderr)
175 207 #print(e, file=sys.stderr)
176 208 else:
177 209 self.writer.write(output, resources, notebook_name=notebook_name)
178 210 conversion_success += 1
179 211
180 212 #If nothing was converted successfully, help the user.
181 213 if conversion_success == 0:
182 214
183 215 #No notebooks were specified, show help.
184 216 if len(self.notebooks) == 0:
185 217 self.print_help()
186 218
187 219 #Notebooks were specified, but not converted successfully. Show how
188 220 #to access help.
189 221 else:
190 222 print('For help, use "ipython nbconvert --help"')
191 223
192 224
193 225 #-----------------------------------------------------------------------------
194 226 # Main entry point
195 227 #-----------------------------------------------------------------------------
196 228
197 229 launch_new_instance = NbConvertApp.launch_instance
@@ -1,12 +1,12 b''
1 1 # Class base Transformers
2 from .base import ConfigurableTransformer
2 from .base import Transformer
3 3 from .convertfigures import ConvertFiguresTransformer
4 4 from .svg2pdf import SVG2PDFTransformer
5 5 from .extractfigure import ExtractFigureTransformer
6 6 from .revealhelp import RevealHelpTransformer
7 7 from .latex import LatexTransformer
8 8 from .sphinx import SphinxTransformer
9 9 from .csshtmlheader import CSSHTMLHeaderTransformer
10 10
11 11 # decorated function Transformers
12 12 from .coalescestreams import coalesce_streams
@@ -1,109 +1,109 b''
1 1 """
2 2 Module that re-groups transformer that would be applied to ipynb files
3 3 before going through the templating machinery.
4 4
5 5 It exposes a convenient class to inherit from to access configurability.
6 6 """
7 7 #-----------------------------------------------------------------------------
8 8 # Copyright (c) 2013, the IPython Development Team.
9 9 #
10 10 # Distributed under the terms of the Modified BSD License.
11 11 #
12 12 # The full license is in the file COPYING.txt, distributed with this software.
13 13 #-----------------------------------------------------------------------------
14 14
15 15 #-----------------------------------------------------------------------------
16 16 # Imports
17 17 #-----------------------------------------------------------------------------
18 18
19 19 from ..utils.base import NbConvertBase
20 20 from IPython.utils.traitlets import Bool
21 21
22 22 #-----------------------------------------------------------------------------
23 23 # Classes and Functions
24 24 #-----------------------------------------------------------------------------
25 25
26 class ConfigurableTransformer(NbConvertBase):
26 class Transformer(NbConvertBase):
27 27 """ A configurable transformer
28 28
29 29 Inherit from this class if you wish to have configurability for your
30 30 transformer.
31 31
32 32 Any configurable traitlets this class exposed will be configurable in profiles
33 33 using c.SubClassName.atribute=value
34 34
35 35 you can overwrite transform_cell to apply a transformation independently on each cell
36 36 or __call__ if you prefer your own logic. See corresponding docstring for informations.
37 37
38 38 Disabled by default and can be enabled via the config by
39 39 'c.YourTransformerName.enabled = True'
40 40 """
41 41
42 42 enabled = Bool(False, config=True)
43 43
44 44 def __init__(self, **kw):
45 45 """
46 46 Public constructor
47 47
48 48 Parameters
49 49 ----------
50 50 config : Config
51 51 Configuration file structure
52 52 **kw : misc
53 53 Additional arguments
54 54 """
55 55
56 super(ConfigurableTransformer, self).__init__(**kw)
56 super(Transformer, self).__init__(**kw)
57 57
58 58
59 59 def __call__(self, nb, resources):
60 60 if self.enabled:
61 61 return self.call(nb,resources)
62 62 else:
63 63 return nb, resources
64 64
65 65
66 66 def call(self, nb, resources):
67 67 """
68 68 Transformation to apply on each notebook.
69 69
70 70 You should return modified nb, resources.
71 71 If you wish to apply your transform on each cell, you might want to
72 72 overwrite transform_cell method instead.
73 73
74 74 Parameters
75 75 ----------
76 76 nb : NotebookNode
77 77 Notebook being converted
78 78 resources : dictionary
79 79 Additional resources used in the conversion process. Allows
80 80 transformers to pass variables into the Jinja engine.
81 81 """
82 82 try :
83 83 for worksheet in nb.worksheets :
84 84 for index, cell in enumerate(worksheet.cells):
85 85 worksheet.cells[index], resources = self.transform_cell(cell, resources, index)
86 86 return nb, resources
87 87 except NotImplementedError:
88 88 raise NotImplementedError('should be implemented by subclass')
89 89
90 90
91 91 def transform_cell(self, cell, resources, index):
92 92 """
93 93 Overwrite if you want to apply a transformation on each cell. You
94 94 should return modified cell and resource dictionary.
95 95
96 96 Parameters
97 97 ----------
98 98 cell : NotebookNode cell
99 99 Notebook cell being processed
100 100 resources : dictionary
101 101 Additional resources used in the conversion process. Allows
102 102 transformers to pass variables into the Jinja engine.
103 103 index : int
104 104 Index of the cell being processed
105 105 """
106 106
107 107 raise NotImplementedError('should be implemented by subclass')
108 108 return cell, resources
109 109
@@ -1,79 +1,79 b''
1 1 """Module containing a transformer that converts outputs in the notebook from
2 2 one format to another.
3 3 """
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (c) 2013, the IPython Development Team.
6 6 #
7 7 # Distributed under the terms of the Modified BSD License.
8 8 #
9 9 # The full license is in the file COPYING.txt, distributed with this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 from .base import ConfigurableTransformer
16 from .base import Transformer
17 17 from IPython.utils.traitlets import Unicode
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Classes
21 21 #-----------------------------------------------------------------------------
22 22
23 class ConvertFiguresTransformer(ConfigurableTransformer):
23 class ConvertFiguresTransformer(Transformer):
24 24 """
25 25 Converts all of the outputs in a notebook from one format to another.
26 26 """
27 27
28 28 from_format = Unicode(config=True, help='Format the converter accepts')
29 29 to_format = Unicode(config=True, help='Format the converter writes')
30 30
31 31 def __init__(self, **kw):
32 32 """
33 33 Public constructor
34 34 """
35 35 super(ConvertFiguresTransformer, self).__init__(**kw)
36 36
37 37
38 38 def convert_figure(self, data_format, data):
39 39 raise NotImplementedError()
40 40
41 41
42 42 def transform_cell(self, cell, resources, cell_index):
43 43 """
44 44 Apply a transformation on each cell,
45 45
46 46 See base.py
47 47 """
48 48
49 49 #Loop through all of the datatypes of the outputs in the cell.
50 50 for index, cell_out in enumerate(cell.get('outputs', [])):
51 51 for data_type, data in cell_out.items():
52 52
53 53 #Get the name of the file exported by the extract figure
54 54 #transformer. Do not try to convert the figure if the extract
55 55 #fig transformer hasn't touched it
56 56 filename = cell_out.get(data_type + '_filename', None)
57 57 if filename:
58 58 figure_name = filename[:filename.rfind('.')]
59 59 self._convert_figure(cell_out, figure_name, resources, data_type, data)
60 60 return cell, resources
61 61
62 62
63 63 def _convert_figure(self, cell_out, resources, figure_name, data_type, data):
64 64 """
65 65 Convert a figure and output the results to the cell output
66 66 """
67 67
68 68 if not self.to_format in cell_out:
69 69 if data_type == self.from_format:
70 70 filename = figure_name + '.' + self.to_format
71 71 if filename not in resources['figures']:
72 72
73 73 #On the cell, make the figure available via
74 74 # cell.outputs[i].pdf_filename ... etc (PDF in example)
75 75 cell_out[self.to_format + '_filename'] = filename
76 76
77 77 #In the resources, make the figure available via
78 78 # resources['figures']['filename'] = data
79 79 resources['figures'][filename] = self.convert_figure(data_type, data)
@@ -1,106 +1,106 b''
1 1 """Module that pre-processes the notebook for export to HTML.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2013, the IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 import os
16 16 import io
17 17
18 18 from pygments.formatters import HtmlFormatter
19 19
20 20 from IPython.utils import path
21 21
22 from .base import ConfigurableTransformer
22 from .base import Transformer
23 23
24 24 from IPython.utils.traitlets import Unicode
25 25
26 26 #-----------------------------------------------------------------------------
27 27 # Classes and functions
28 28 #-----------------------------------------------------------------------------
29 29
30 class CSSHTMLHeaderTransformer(ConfigurableTransformer):
30 class CSSHTMLHeaderTransformer(Transformer):
31 31 """
32 32 Transformer used to pre-process notebook for HTML output. Adds IPython notebook
33 33 front-end CSS and Pygments CSS to HTML output.
34 34 """
35 35
36 36 header = []
37 37
38 38 highlight_class = Unicode('.highlight', config=True,
39 39 help="CSS highlight class identifier")
40 40
41 41 def __init__(self, config=None, **kw):
42 42 """
43 43 Public constructor
44 44
45 45 Parameters
46 46 ----------
47 47 config : Config
48 48 Configuration file structure
49 49 **kw : misc
50 50 Additional arguments
51 51 """
52 52
53 53 super(CSSHTMLHeaderTransformer, self).__init__(config=config, **kw)
54 54
55 55 if self.enabled :
56 56 self._regen_header()
57 57
58 58
59 59 def __call__(self, nb, resources):
60 60 """Fetch and add CSS to the resource dictionary
61 61
62 62 Fetch CSS from IPython and Pygments to add at the beginning
63 63 of the html files. Add this css in resources in the
64 64 "inlining.css" key
65 65
66 66 Parameters
67 67 ----------
68 68 nb : NotebookNode
69 69 Notebook being converted
70 70 resources : dictionary
71 71 Additional resources used in the conversion process. Allows
72 72 transformers to pass variables into the Jinja engine.
73 73 """
74 74
75 75 resources['inlining'] = {}
76 76 resources['inlining']['css'] = self.header
77 77
78 78 return nb, resources
79 79
80 80
81 81 def _regen_header(self):
82 82 """
83 83 Fills self.header with lines of CSS extracted from IPython
84 84 and Pygments.
85 85 """
86 86
87 87 #Clear existing header.
88 88 header = []
89 89
90 90 #Construct path to IPy CSS
91 91 sheet_filename = os.path.join(path.get_ipython_package_dir(),
92 92 'html', 'static', 'style', 'style.min.css')
93 93
94 94 #Load style CSS file.
95 95 with io.open(sheet_filename, encoding='utf-8') as file:
96 96 file_text = file.read()
97 97 header.append(file_text)
98 98
99 99 #Add pygments CSS
100 100 formatter = HtmlFormatter()
101 101 pygments_css = formatter.get_style_defs(self.highlight_class)
102 102 header.append(pygments_css)
103 103
104 104 #Set header
105 105 self.header = header
106 106
@@ -1,90 +1,90 b''
1 1 """Module containing a transformer that extracts all of the figures from the
2 2 notebook file. The extracted figures are returned in the 'resources' dictionary.
3 3 """
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (c) 2013, the IPython Development Team.
6 6 #
7 7 # Distributed under the terms of the Modified BSD License.
8 8 #
9 9 # The full license is in the file COPYING.txt, distributed with this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 16 import sys
17 17 from IPython.utils.traitlets import Unicode
18 from .base import ConfigurableTransformer
18 from .base import Transformer
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Classes
22 22 #-----------------------------------------------------------------------------
23 23
24 class ExtractFigureTransformer(ConfigurableTransformer):
24 class ExtractFigureTransformer(Transformer):
25 25 """
26 26 Extracts all of the figures from the notebook file. The extracted
27 27 figures are returned in the 'resources' dictionary.
28 28 """
29 29
30 30 figure_filename_template = Unicode(
31 31 "{unique_key}_{cell_index}_{index}.{extension}", config=True)
32 32
33 33
34 34 def transform_cell(self, cell, resources, cell_index):
35 35 """
36 36 Apply a transformation on each cell,
37 37
38 38 Parameters
39 39 ----------
40 40 cell : NotebookNode cell
41 41 Notebook cell being processed
42 42 resources : dictionary
43 43 Additional resources used in the conversion process. Allows
44 44 transformers to pass variables into the Jinja engine.
45 45 cell_index : int
46 46 Index of the cell being processed (see base.py)
47 47 """
48 48
49 49 #Get the unique key from the resource dict if it exists. If it does not
50 50 #exist, use 'figure' as the default.
51 51 unique_key = resources.get('unique_key', 'figure')
52 52
53 53 #Make sure figures key exists
54 54 if not 'figures' in resources:
55 55 resources['figures'] = {}
56 56
57 57 #Loop through all of the outputs in the cell
58 58 for index, out in enumerate(cell.get('outputs', [])):
59 59
60 60 #Get the output in data formats that the template is interested in.
61 61 for out_type in self.display_data_priority:
62 62 if out.hasattr(out_type):
63 63 data = out[out_type]
64 64
65 65 #Binary files are base64-encoded, SVG is already XML
66 66 if out_type in ('png', 'jpg', 'jpeg', 'pdf'):
67 67 data = data.decode('base64')
68 68 elif sys.platform == 'win32':
69 69 data = data.replace('\n', '\r\n').encode("UTF-8")
70 70 else:
71 71 data = data.encode("UTF-8")
72 72
73 73 #Build a figure name
74 74 figure_name = self.figure_filename_template.format(
75 75 unique_key=unique_key,
76 76 cell_index=cell_index,
77 77 index=index,
78 78 extension=out_type)
79 79
80 80 #On the cell, make the figure available via
81 81 # cell.outputs[i].svg_filename ... etc (svg in example)
82 82 # Where
83 83 # cell.outputs[i].svg contains the data
84 84 out[out_type + '_filename'] = figure_name
85 85
86 86 #In the resources, make the figure available via
87 87 # resources['figures']['filename'] = data
88 88 resources['figures'][figure_name] = data
89 89
90 90 return cell, resources
@@ -1,53 +1,53 b''
1 1 """Module that allows latex output notebooks to be conditioned before
2 2 they are converted.
3 3 """
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (c) 2013, the IPython Development Team.
6 6 #
7 7 # Distributed under the terms of the Modified BSD License.
8 8 #
9 9 # The full license is in the file COPYING.txt, distributed with this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 16 from __future__ import print_function, absolute_import
17 17
18 18 # Our own imports
19 19 # Needed to override transformer
20 from .base import (ConfigurableTransformer)
20 from .base import (Transformer)
21 21 from IPython.nbconvert import filters
22 22
23 23 #-----------------------------------------------------------------------------
24 24 # Classes
25 25 #-----------------------------------------------------------------------------
26 26
27 class LatexTransformer(ConfigurableTransformer):
27 class LatexTransformer(Transformer):
28 28 """
29 29 Converter for latex destined documents.
30 30 """
31 31
32 32 def transform_cell(self, cell, resources, index):
33 33 """
34 34 Apply a transformation on each cell,
35 35
36 36 Parameters
37 37 ----------
38 38 cell : NotebookNode cell
39 39 Notebook cell being processed
40 40 resources : dictionary
41 41 Additional resources used in the conversion process. Allows
42 42 transformers to pass variables into the Jinja engine.
43 43 index : int
44 44 Modified index of the cell being processed (see base.py)
45 45 """
46 46
47 47 #If the cell is a markdown cell, preprocess the ampersands used to
48 48 #remove the space between them and their contents. Latex will complain
49 49 #if spaces exist between the ampersands and the math content.
50 50 #See filters.latex.rm_math_space for more information.
51 51 if hasattr(cell, "source") and cell.cell_type == "markdown":
52 52 cell.source = filters.rm_math_space(cell.source)
53 53 return cell, resources
@@ -1,61 +1,61 b''
1 1 """Module that pre-processes the notebook for export via Reveal.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2013, the IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 from .base import ConfigurableTransformer
15 from .base import Transformer
16 16 from IPython.utils.traitlets import Unicode
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Classes and functions
20 20 #-----------------------------------------------------------------------------
21 21
22 class RevealHelpTransformer(ConfigurableTransformer):
22 class RevealHelpTransformer(Transformer):
23 23
24 24 url_prefix = Unicode('//cdn.jsdelivr.net/reveal.js/2.4.0',
25 25 config=True,
26 26 help="""If you want to use a local reveal.js library,
27 27 use 'url_prefix':'reveal.js' in your config object.""")
28 28
29 29 def call(self, nb, resources):
30 30 """
31 31 Called once to 'transform' contents of the notebook.
32 32
33 33 Parameters
34 34 ----------
35 35 nb : NotebookNode
36 36 Notebook being converted
37 37 resources : dictionary
38 38 Additional resources used in the conversion process. Allows
39 39 transformers to pass variables into the Jinja engine.
40 40 """
41 41
42 42 for worksheet in nb.worksheets :
43 43 for index, cell in enumerate(worksheet.cells):
44 44
45 45 #Make sure the cell has slideshow metadata.
46 46 cell.metadata.align_type = cell.get('metadata', {}).get('slideshow', {}).get('align_type', 'Left')
47 47 cell.metadata.slide_type = cell.get('metadata', {}).get('slideshow', {}).get('slide_type', '-')
48 48
49 49 #Get the slide type. If type is start of subslide or slide,
50 50 #end the last subslide/slide.
51 51 if cell.metadata.slide_type in ['slide']:
52 52 worksheet.cells[index - 1].metadata.slide_helper = 'slide_end'
53 53 if cell.metadata.slide_type in ['subslide']:
54 54 worksheet.cells[index - 1].metadata.slide_helper = 'subslide_end'
55 55
56 56
57 57 if 'reveal' not in resources:
58 58 resources['reveal'] = {}
59 59 resources['reveal']['url_prefix'] = self.url_prefix
60 60
61 61 return nb, resources
@@ -1,264 +1,264 b''
1 1 """Module that allows custom Sphinx parameters to be set on the notebook and
2 2 on the 'other' object passed into Jinja. Called prior to Jinja conversion
3 3 process.
4 4 """
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 from __future__ import print_function, absolute_import
18 18
19 19 # Stdlib imports
20 20 # Used to find Sphinx package location
21 21 import sphinx
22 22 import os.path
23 23
24 24 # Used to set the default date to today's date
25 25 from datetime import date
26 26
27 27 # Third-party imports
28 28 # Needed for Pygments latex definitions.
29 29 from pygments.formatters import LatexFormatter
30 30
31 31 # Our own imports
32 32 # Configurable traitlets
33 33 from IPython.utils.traitlets import Unicode, Bool
34 34
35 35 # Needed to override transformer
36 from .base import (ConfigurableTransformer)
36 from .base import (Transformer)
37 37
38 38 from IPython.nbconvert.utils import console
39 39
40 40 #-----------------------------------------------------------------------------
41 41 # Classes and functions
42 42 #-----------------------------------------------------------------------------
43 43
44 class SphinxTransformer(ConfigurableTransformer):
44 class SphinxTransformer(Transformer):
45 45 """
46 46 Sphinx utility transformer.
47 47
48 48 This transformer is used to set variables needed by the latex to build
49 49 Sphinx stylized templates.
50 50 """
51 51
52 52 interactive = Bool(False, config=True, help="""
53 53 Allows you to define whether or not the Sphinx exporter will prompt
54 54 you for input during the conversion process. If this is set to false,
55 55 the author, version, release, date, and chapter_style traits should
56 56 be set.
57 57 """)
58 58
59 59 author = Unicode("Unknown Author", config=True, help="Author name")
60 60
61 61 version = Unicode("", config=True, help="""
62 62 Version number
63 63 You can leave this blank if you do not want to render a version number.
64 64 Example: "1.0.0"
65 65 """)
66 66
67 67 release = Unicode("", config=True, help="""
68 68 Release name
69 69 You can leave this blank if you do not want to render a release name.
70 70 Example: "Rough Draft"
71 71 """)
72 72
73 73 publish_date = Unicode("", config=True, help="""
74 74 Publish date
75 75 This is the date to render on the document as the publish date.
76 76 Leave this blank to default to todays date.
77 77 Example: "June 12, 1990"
78 78 """)
79 79
80 80 chapter_style = Unicode("Bjarne", config=True, help="""
81 81 Sphinx chapter style
82 82 This is the style to use for the chapter headers in the document.
83 83 You may choose one of the following:
84 84 "Bjarne" (default)
85 85 "Lenny"
86 86 "Glenn"
87 87 "Conny"
88 88 "Rejne"
89 89 "Sonny" (used for international documents)
90 90 """)
91 91
92 92 output_style = Unicode("notebook", config=True, help="""
93 93 Nbconvert Ipython
94 94 notebook input/output formatting style.
95 95 You may choose one of the following:
96 96 "simple (recommended for long code segments)"
97 97 "notebook" (default)
98 98 """)
99 99
100 100 center_output = Bool(False, config=True, help="""
101 101 Optional attempt to center all output. If this is false, no additional
102 102 formatting is applied.
103 103 """)
104 104
105 105 use_headers = Bool(True, config=True, help="""
106 106 Whether not a header should be added to the document.
107 107 """)
108 108
109 109 #Allow the user to override the title of the notebook (useful for
110 110 #fancy document titles that the file system doesn't support.)
111 111 overridetitle = Unicode("", config=True, help="")
112 112
113 113
114 114 def call(self, nb, resources):
115 115 """
116 116 Sphinx transformation to apply on each notebook.
117 117
118 118 Parameters
119 119 ----------
120 120 nb : NotebookNode
121 121 Notebook being converted
122 122 resources : dictionary
123 123 Additional resources used in the conversion process. Allows
124 124 transformers to pass variables into the Jinja engine.
125 125 """
126 126
127 127 # TODO: Add versatile method of additional notebook metadata. Include
128 128 # handling of multiple files. For now use a temporay namespace,
129 129 # '_draft' to signify that this needs to change.
130 130 if not "sphinx" in resources:
131 131 resources["sphinx"] = {}
132 132
133 133 if self.interactive:
134 134
135 135 # Prompt the user for additional meta data that doesn't exist currently
136 136 # but would be usefull for Sphinx.
137 137 resources["sphinx"]["author"] = self._prompt_author()
138 138 resources["sphinx"]["version"] = self._prompt_version()
139 139 resources["sphinx"]["release"] = self._prompt_release()
140 140 resources["sphinx"]["date"] = self._prompt_date()
141 141
142 142 # Prompt the user for the document style.
143 143 resources["sphinx"]["chapterstyle"] = self._prompt_chapter_title_style()
144 144 resources["sphinx"]["outputstyle"] = self._prompt_output_style()
145 145
146 146 # Small options
147 147 resources["sphinx"]["centeroutput"] = console.prompt_boolean("Do you want to center the output? (false)", False)
148 148 resources["sphinx"]["header"] = console.prompt_boolean("Should a Sphinx document header be used? (true)", True)
149 149 else:
150 150
151 151 # Try to use the traitlets.
152 152 resources["sphinx"]["author"] = self.author
153 153 resources["sphinx"]["version"] = self.version
154 154 resources["sphinx"]["release"] = self.release
155 155
156 156 # Use todays date if none is provided.
157 157 if self.publish_date:
158 158 resources["sphinx"]["date"] = self.publish_date
159 159 elif len(resources['metadata']['modified_date'].strip()) == 0:
160 160 resources["sphinx"]["date"] = date.today().strftime("%B %-d, %Y")
161 161 else:
162 162 resources["sphinx"]["date"] = resources['metadata']['modified_date']
163 163
164 164 # Sphinx traitlets.
165 165 resources["sphinx"]["chapterstyle"] = self.chapter_style
166 166 resources["sphinx"]["outputstyle"] = self.output_style
167 167 resources["sphinx"]["centeroutput"] = self.center_output
168 168 resources["sphinx"]["header"] = self.use_headers
169 169
170 170 # Find and pass in the path to the Sphinx dependencies.
171 171 resources["sphinx"]["texinputs"] = os.path.realpath(os.path.join(sphinx.__file__, "..", "texinputs"))
172 172
173 173 # Generate Pygments definitions for Latex
174 174 resources["sphinx"]["pygment_definitions"] = self._generate_pygments_latex_def()
175 175
176 176 if not (self.overridetitle == None or len(self.overridetitle.strip()) == 0):
177 177 resources['metadata']['name'] = self.overridetitle
178 178
179 179 # End
180 180 return nb, resources
181 181
182 182
183 183 def _generate_pygments_latex_def(self):
184 184 """
185 185 Generate the pygments latex definitions that allows pygments
186 186 to work in latex.
187 187 """
188 188
189 189 return LatexFormatter().get_style_defs()
190 190
191 191
192 192 def _prompt_author(self):
193 193 """
194 194 Prompt the user to input an Author name
195 195 """
196 196 return console.input("Author name: ")
197 197
198 198
199 199 def _prompt_version(self):
200 200 """
201 201 prompt the user to enter a version number
202 202 """
203 203 return console.input("Version (ie ""1.0.0""): ")
204 204
205 205
206 206 def _prompt_release(self):
207 207 """
208 208 Prompt the user to input a release name
209 209 """
210 210
211 211 return console.input("Release Name (ie ""Rough draft""): ")
212 212
213 213
214 214 def _prompt_date(self, resources):
215 215 """
216 216 Prompt the user to enter a date
217 217 """
218 218
219 219 if resources['metadata']['modified_date']:
220 220 default_date = resources['metadata']['modified_date']
221 221 else:
222 222 default_date = date.today().strftime("%B %-d, %Y")
223 223
224 224 user_date = console.input("Date (deafults to \"" + default_date + "\"): ")
225 225 if len(user_date.strip()) == 0:
226 226 user_date = default_date
227 227 return user_date
228 228
229 229
230 230 def _prompt_output_style(self):
231 231 """
232 232 Prompts the user to pick an IPython output style.
233 233 """
234 234
235 235 # Dictionary of available output styles
236 236 styles = {1: "simple",
237 237 2: "notebook"}
238 238
239 239 #Append comments to the menu when displaying it to the user.
240 240 comments = {1: "(recommended for long code segments)",
241 241 2: "(default)"}
242 242
243 243 return console.prompt_dictionary(styles, default_style=2, menu_comments=comments)
244 244
245 245
246 246 def _prompt_chapter_title_style(self):
247 247 """
248 248 Prompts the user to pick a Sphinx chapter style
249 249 """
250 250
251 251 # Dictionary of available Sphinx styles
252 252 styles = {1: "Bjarne",
253 253 2: "Lenny",
254 254 3: "Glenn",
255 255 4: "Conny",
256 256 5: "Rejne",
257 257 6: "Sonny"}
258 258
259 259 #Append comments to the menu when displaying it to the user.
260 260 comments = {1: "(default)",
261 261 6: "(for international documents)"}
262 262
263 263 return console.prompt_dictionary(styles, menu_comments=comments)
264 264
General Comments 0
You need to be logged in to leave comments. Login now