##// END OF EJS Templates
Rename SVG2PDFTransformer class
Jonathan Frederic -
Show More
@@ -0,0 +1,203 b''
1 #!/usr/bin/env python
2 """NBConvert is a utility for conversion of IPYNB files.
3
4 Commandline interface for the NBConvert conversion utility. Read the
5 readme.rst for usage information
6 """
7 #-----------------------------------------------------------------------------
8 #Copyright (c) 2013, the IPython Development Team.
9 #
10 #Distributed under the terms of the Modified BSD License.
11 #
12 #The full license is in the file COPYING.txt, distributed with this software.
13 #-----------------------------------------------------------------------------
14
15 #-----------------------------------------------------------------------------
16 #Imports
17 #-----------------------------------------------------------------------------
18
19 #Stdlib imports
20 from __future__ import print_function
21 import sys
22 import os
23 import glob
24
25 #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
29 from IPython.utils.importstring import import_item
30
31 from .exporters.export import export_by_name, get_export_names, ExporterNameError
32 from .exporters.exporter import Exporter
33 from .writers.base import WriterBase
34 from .utils.base import NbConvertBase
35
36 #-----------------------------------------------------------------------------
37 #Classes and functions
38 #-----------------------------------------------------------------------------
39
40 class NbConvertApp(BaseIPythonApplication):
41 """Application used to convert to and from notebook file type (*.ipynb)"""
42
43 <<<<<<< HEAD
44 name = 'ipython-nbconvert'
45 =======
46 name = Unicode(u'ipython-nbconvert')
47
48 config_file_name = Unicode(u'ipython_nbconvert_config.py')
49 >>>>>>> Moved config_file_name up
50
51 description = Unicode(
52 u"""This application is used to convert notebook files (*.ipynb).
53 An ipython config file can be used to batch convert notebooks in the
54 current directory.""")
55
56 examples = Unicode(u"""
57 Running `ipython nbconvert` will read the directory config file and then
58 apply it to one or more notebooks.
59
60 Multiple notebooks can be given at the command line in a couple of
61 different ways:
62
63 > ipython nbconvert notebook*.ipynb
64 > ipython nbconvert notebook1.ipynb notebook2.ipynb
65 > ipython nbconvert # this will use the config file to fill in the notebooks
66 """)
67
68 #Writer specific variables
69 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
70 help="""Instance of the writer class used to write the
71 results of the conversion.""")
72 writer_class = DottedObjectName('FilesWriter', config=True,
73 help="""Writer class used to write the
74 results of the conversion""")
75 writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter',
76 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter',
77 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'}
78 writer_factory = Type()
79
80 def _writer_class_changed(self, name, old, new):
81 if new in self.writer_aliases:
82 new = self.writer_aliases[new]
83 self.writer_factory = import_item(new)
84
85
86 #Other configurable variables
87 export_format = Unicode(
88 "", config=True,
89 help="""If specified, nbconvert will convert the document(s) specified
90 using this format.""")
91
92 notebooks = List([], config=True, help="""List of notebooks to convert.
93 Search patterns are supported.""")
94
95 nbconvert_aliases = {'format':'NbConvertApp.export_format',
96 'notebooks':'NbConvertApp.notebooks',
97 'writer':'NbConvertApp.writer_class'}
98
99
100 @catch_config_error
101 def initialize(self, argv=None):
102 self.aliases.update(self.nbconvert_aliases)
103
104 super(NbConvertApp, self).initialize(argv)
105
106 #Register class here to have help with help all
107 self.classes.insert(0, Exporter)
108 self.classes.insert(0, WriterBase)
109 self.classes.insert(0, NbConvertBase)
110
111 #Init
112 self.init_config(self.extra_args)
113 self.init_writer()
114
115
116 def init_config(self, extra_args):
117 """
118 Add notebooks to the config if needed. Glob each notebook to replace
119 notebook patterns with filenames.
120 """
121
122 #Get any additional notebook patterns from the commandline
123 if len(extra_args) > 0:
124 for pattern in extra_args:
125 self.notebooks.append(pattern)
126
127 #Use glob to replace all the notebook patterns with filenames.
128 filenames = []
129 for pattern in self.notebooks:
130 for filename in glob.glob(pattern):
131 if not filename in filenames:
132 filenames.append(filename)
133 self.notebooks = filenames
134
135
136 def init_writer(self):
137 """
138 Initialize the writer (which is stateless)
139 """
140 self._writer_class_changed(None, self.writer_class, self.writer_class)
141 self.writer = self.writer_factory(parent=self)
142
143
144 def start(self, argv=None):
145 """
146 Ran after initiialization completed
147 """
148 super(NbConvertApp, self).start()
149 self.convert_notebooks()
150
151
152 def convert_notebooks(self):
153 """
154 Convert the notebooks in the self.notebook traitlet
155 """
156 #Export each notebook
157 conversion_success = 0
158 for notebook_filename in self.notebooks:
159
160 #Get a unique key for the notebook and set it in the resources object.
161 basename = os.path.basename(notebook_filename)
162 notebook_name = basename[:basename.rfind('.')]
163 resources = {}
164 resources['unique_key'] = notebook_name
165
166 #Try to export
167 try:
168 output, resources = export_by_name(self.export_format,
169 notebook_filename,
170 resources=resources,
171 config=self.config)
172 except ExporterNameError as e:
173 print("Error: '%s' exporter not found." % self.export_format,
174 file=sys.stderr)
175 print("Known exporters are:",
176 "\n\t" + "\n\t".join(get_export_names()),
177 file=sys.stderr)
178 sys.exit(-1)
179 #except Exception as e:
180 #print("Error: could not export '%s'" % notebook_filename, file=sys.stderr)
181 #print(e, file=sys.stderr)
182 else:
183 self.writer.write(output, resources, notebook_name=notebook_name)
184 conversion_success += 1
185
186 #If nothing was converted successfully, help the user.
187 if conversion_success == 0:
188
189 #No notebooks were specified, show help.
190 if len(self.notebooks) == 0:
191 self.print_help()
192
193 #Notebooks were specified, but not converted successfully. Show how
194 #to access help.
195 else:
196 print('For help, use "ipython nbconvert --help"')
197
198
199 #-----------------------------------------------------------------------------
200 # Main entry point
201 #-----------------------------------------------------------------------------
202
203 launch_new_instance = NbConvertApp.launch_instance
@@ -71,7 +71,7 b' class LatexExporter(Exporter):'
71 default_transformers = List([transformers.ExtractFigureTransformer,
71 default_transformers = List([transformers.ExtractFigureTransformer,
72 transformers.CSSHTMLHeaderTransformer,
72 transformers.CSSHTMLHeaderTransformer,
73 transformers.LatexTransformer,
73 transformers.LatexTransformer,
74 transformers.Svg2PdfTransformer],
74 transformers.SVG2PDFTransformer],
75 config=True,
75 config=True,
76 help="""List of transformers available by default, by name, namespace,
76 help="""List of transformers available by default, by name, namespace,
77 instance, or type.""")
77 instance, or type.""")
@@ -99,7 +99,7 b' class LatexExporter(Exporter):'
99 'ExtractFigureTransformer': {
99 'ExtractFigureTransformer': {
100 'enabled':True
100 'enabled':True
101 },
101 },
102 'Svg2PdfTransformer': {
102 'SVG2PDFTransformer': {
103 'enabled':True
103 'enabled':True
104 },
104 },
105 'LatexTransformer': {
105 'LatexTransformer': {
@@ -35,7 +35,7 b" INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inksca"
35 # Classes
35 # Classes
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37
37
38 class Svg2PdfTransformer(ConvertFiguresTransformer):
38 class SVG2PDFTransformer(ConvertFiguresTransformer):
39 """
39 """
40 Converts all of the outputs in a notebook from one format to another.
40 Converts all of the outputs in a notebook from one format to another.
41 """
41 """
@@ -1,7 +1,7 b''
1 # Class base Transformers
1 # Class base Transformers
2 from .base import ConfigurableTransformer
2 from .base import ConfigurableTransformer
3 from .convertfigures import ConvertFiguresTransformer
3 from .convertfigures import ConvertFiguresTransformer
4 from .SVG2PDF import Svg2PdfTransformer
4 from .SVG2PDF import SVG2PDFTransformer
5 from .extractfigure import ExtractFigureTransformer
5 from .extractfigure import ExtractFigureTransformer
6 from .revealhelp import RevealHelpTransformer
6 from .revealhelp import RevealHelpTransformer
7 from .latex import LatexTransformer
7 from .latex import LatexTransformer
General Comments 0
You need to be logged in to leave comments. Login now