Show More
@@ -0,0 +1,14 b'' | |||||
|
1 | """Generic script exporter class for any kernel language""" | |||
|
2 | ||||
|
3 | from .templateexporter import TemplateExporter | |||
|
4 | ||||
|
5 | class ScriptExporter(TemplateExporter): | |||
|
6 | def _template_file_default(self): | |||
|
7 | return 'script' | |||
|
8 | ||||
|
9 | def from_notebook_node(self, nb, resources=None, **kw): | |||
|
10 | langinfo = nb.metadata.get('language_info', {}) | |||
|
11 | self.file_extension = langinfo.get('file_extension', 'txt') | |||
|
12 | self.output_mimetype = langinfo.get('mimetype', 'text/plain') | |||
|
13 | ||||
|
14 | return super(ScriptExporter, self).from_notebook_node(nb, resources, **kw) |
@@ -0,0 +1,5 b'' | |||||
|
1 | {%- extends 'null.tpl' -%} | |||
|
2 | ||||
|
3 | {% block input %} | |||
|
4 | {{ cell.source }} | |||
|
5 | {% endblock input %} |
@@ -1,174 +1,177 b'' | |||||
1 | """Module containing single call export functions.""" |
|
1 | """Module containing single call export functions.""" | |
2 |
|
2 | |||
3 | # Copyright (c) IPython Development Team. |
|
3 | # Copyright (c) IPython Development Team. | |
4 | # Distributed under the terms of the Modified BSD License. |
|
4 | # Distributed under the terms of the Modified BSD License. | |
5 |
|
5 | |||
6 | from functools import wraps |
|
6 | from functools import wraps | |
7 |
|
7 | |||
8 | from IPython.nbformat import NotebookNode |
|
8 | from IPython.nbformat import NotebookNode | |
9 | from IPython.utils.decorators import undoc |
|
9 | from IPython.utils.decorators import undoc | |
10 | from IPython.utils.py3compat import string_types |
|
10 | from IPython.utils.py3compat import string_types | |
11 |
|
11 | |||
12 | from .exporter import Exporter |
|
12 | from .exporter import Exporter | |
13 | from .templateexporter import TemplateExporter |
|
13 | from .templateexporter import TemplateExporter | |
14 | from .html import HTMLExporter |
|
14 | from .html import HTMLExporter | |
15 | from .slides import SlidesExporter |
|
15 | from .slides import SlidesExporter | |
16 | from .latex import LatexExporter |
|
16 | from .latex import LatexExporter | |
17 | from .pdf import PDFExporter |
|
17 | from .pdf import PDFExporter | |
18 | from .markdown import MarkdownExporter |
|
18 | from .markdown import MarkdownExporter | |
19 | from .python import PythonExporter |
|
19 | from .python import PythonExporter | |
20 | from .rst import RSTExporter |
|
20 | from .rst import RSTExporter | |
21 | from .notebook import NotebookExporter |
|
21 | from .notebook import NotebookExporter | |
|
22 | from .script import ScriptExporter | |||
22 |
|
23 | |||
23 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
24 | # Classes |
|
25 | # Classes | |
25 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
26 |
|
27 | |||
27 | @undoc |
|
28 | @undoc | |
28 | def DocDecorator(f): |
|
29 | def DocDecorator(f): | |
29 |
|
30 | |||
30 | #Set docstring of function |
|
31 | #Set docstring of function | |
31 | f.__doc__ = f.__doc__ + """ |
|
32 | f.__doc__ = f.__doc__ + """ | |
32 | nb : :class:`~IPython.nbformat.NotebookNode` |
|
33 | nb : :class:`~IPython.nbformat.NotebookNode` | |
33 | The notebook to export. |
|
34 | The notebook to export. | |
34 | config : config (optional, keyword arg) |
|
35 | config : config (optional, keyword arg) | |
35 | User configuration instance. |
|
36 | User configuration instance. | |
36 | resources : dict (optional, keyword arg) |
|
37 | resources : dict (optional, keyword arg) | |
37 | Resources used in the conversion process. |
|
38 | Resources used in the conversion process. | |
38 |
|
39 | |||
39 | Returns |
|
40 | Returns | |
40 | ------- |
|
41 | ------- | |
41 | tuple- output, resources, exporter_instance |
|
42 | tuple- output, resources, exporter_instance | |
42 | output : str |
|
43 | output : str | |
43 | Jinja 2 output. This is the resulting converted notebook. |
|
44 | Jinja 2 output. This is the resulting converted notebook. | |
44 | resources : dictionary |
|
45 | resources : dictionary | |
45 | Dictionary of resources used prior to and during the conversion |
|
46 | Dictionary of resources used prior to and during the conversion | |
46 | process. |
|
47 | process. | |
47 | exporter_instance : Exporter |
|
48 | exporter_instance : Exporter | |
48 | Instance of the Exporter class used to export the document. Useful |
|
49 | Instance of the Exporter class used to export the document. Useful | |
49 | to caller because it provides a 'file_extension' property which |
|
50 | to caller because it provides a 'file_extension' property which | |
50 | specifies what extension the output should be saved as. |
|
51 | specifies what extension the output should be saved as. | |
51 |
|
52 | |||
52 | Notes |
|
53 | Notes | |
53 | ----- |
|
54 | ----- | |
54 | WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT |
|
55 | WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT | |
55 | """ |
|
56 | """ | |
56 |
|
57 | |||
57 | @wraps(f) |
|
58 | @wraps(f) | |
58 | def decorator(*args, **kwargs): |
|
59 | def decorator(*args, **kwargs): | |
59 | return f(*args, **kwargs) |
|
60 | return f(*args, **kwargs) | |
60 |
|
61 | |||
61 | return decorator |
|
62 | return decorator | |
62 |
|
63 | |||
63 |
|
64 | |||
64 | #----------------------------------------------------------------------------- |
|
65 | #----------------------------------------------------------------------------- | |
65 | # Functions |
|
66 | # Functions | |
66 | #----------------------------------------------------------------------------- |
|
67 | #----------------------------------------------------------------------------- | |
67 |
|
68 | |||
68 | __all__ = [ |
|
69 | __all__ = [ | |
69 | 'export', |
|
70 | 'export', | |
70 | 'export_html', |
|
71 | 'export_html', | |
71 | 'export_custom', |
|
72 | 'export_custom', | |
72 | 'export_slides', |
|
73 | 'export_slides', | |
73 | 'export_latex', |
|
74 | 'export_latex', | |
74 | 'export_pdf', |
|
75 | 'export_pdf', | |
75 | 'export_markdown', |
|
76 | 'export_markdown', | |
76 | 'export_python', |
|
77 | 'export_python', | |
|
78 | 'export_script', | |||
77 | 'export_rst', |
|
79 | 'export_rst', | |
78 | 'export_by_name', |
|
80 | 'export_by_name', | |
79 | 'get_export_names', |
|
81 | 'get_export_names', | |
80 | 'ExporterNameError' |
|
82 | 'ExporterNameError' | |
81 | ] |
|
83 | ] | |
82 |
|
84 | |||
83 |
|
85 | |||
84 | class ExporterNameError(NameError): |
|
86 | class ExporterNameError(NameError): | |
85 | pass |
|
87 | pass | |
86 |
|
88 | |||
87 | @DocDecorator |
|
89 | @DocDecorator | |
88 | def export(exporter, nb, **kw): |
|
90 | def export(exporter, nb, **kw): | |
89 | """ |
|
91 | """ | |
90 | Export a notebook object using specific exporter class. |
|
92 | Export a notebook object using specific exporter class. | |
91 |
|
93 | |||
92 | Parameters |
|
94 | Parameters | |
93 | ---------- |
|
95 | ---------- | |
94 | exporter : class:`~IPython.nbconvert.exporters.exporter.Exporter` class or instance |
|
96 | exporter : class:`~IPython.nbconvert.exporters.exporter.Exporter` class or instance | |
95 | Class type or instance of the exporter that should be used. If the |
|
97 | Class type or instance of the exporter that should be used. If the | |
96 | method initializes it's own instance of the class, it is ASSUMED that |
|
98 | method initializes it's own instance of the class, it is ASSUMED that | |
97 | the class type provided exposes a constructor (``__init__``) with the same |
|
99 | the class type provided exposes a constructor (``__init__``) with the same | |
98 | signature as the base Exporter class. |
|
100 | signature as the base Exporter class. | |
99 | """ |
|
101 | """ | |
100 |
|
102 | |||
101 | #Check arguments |
|
103 | #Check arguments | |
102 | if exporter is None: |
|
104 | if exporter is None: | |
103 | raise TypeError("Exporter is None") |
|
105 | raise TypeError("Exporter is None") | |
104 | elif not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter): |
|
106 | elif not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter): | |
105 | raise TypeError("exporter does not inherit from Exporter (base)") |
|
107 | raise TypeError("exporter does not inherit from Exporter (base)") | |
106 | if nb is None: |
|
108 | if nb is None: | |
107 | raise TypeError("nb is None") |
|
109 | raise TypeError("nb is None") | |
108 |
|
110 | |||
109 | #Create the exporter |
|
111 | #Create the exporter | |
110 | resources = kw.pop('resources', None) |
|
112 | resources = kw.pop('resources', None) | |
111 | if isinstance(exporter, Exporter): |
|
113 | if isinstance(exporter, Exporter): | |
112 | exporter_instance = exporter |
|
114 | exporter_instance = exporter | |
113 | else: |
|
115 | else: | |
114 | exporter_instance = exporter(**kw) |
|
116 | exporter_instance = exporter(**kw) | |
115 |
|
117 | |||
116 | #Try to convert the notebook using the appropriate conversion function. |
|
118 | #Try to convert the notebook using the appropriate conversion function. | |
117 | if isinstance(nb, NotebookNode): |
|
119 | if isinstance(nb, NotebookNode): | |
118 | output, resources = exporter_instance.from_notebook_node(nb, resources) |
|
120 | output, resources = exporter_instance.from_notebook_node(nb, resources) | |
119 | elif isinstance(nb, string_types): |
|
121 | elif isinstance(nb, string_types): | |
120 | output, resources = exporter_instance.from_filename(nb, resources) |
|
122 | output, resources = exporter_instance.from_filename(nb, resources) | |
121 | else: |
|
123 | else: | |
122 | output, resources = exporter_instance.from_file(nb, resources) |
|
124 | output, resources = exporter_instance.from_file(nb, resources) | |
123 | return output, resources |
|
125 | return output, resources | |
124 |
|
126 | |||
125 | exporter_map = dict( |
|
127 | exporter_map = dict( | |
126 | custom=TemplateExporter, |
|
128 | custom=TemplateExporter, | |
127 | html=HTMLExporter, |
|
129 | html=HTMLExporter, | |
128 | slides=SlidesExporter, |
|
130 | slides=SlidesExporter, | |
129 | latex=LatexExporter, |
|
131 | latex=LatexExporter, | |
130 | pdf=PDFExporter, |
|
132 | pdf=PDFExporter, | |
131 | markdown=MarkdownExporter, |
|
133 | markdown=MarkdownExporter, | |
132 | python=PythonExporter, |
|
134 | python=PythonExporter, | |
133 | rst=RSTExporter, |
|
135 | rst=RSTExporter, | |
134 | notebook=NotebookExporter, |
|
136 | notebook=NotebookExporter, | |
|
137 | script=ScriptExporter, | |||
135 | ) |
|
138 | ) | |
136 |
|
139 | |||
137 | def _make_exporter(name, E): |
|
140 | def _make_exporter(name, E): | |
138 | """make an export_foo function from a short key and Exporter class E""" |
|
141 | """make an export_foo function from a short key and Exporter class E""" | |
139 | def _export(nb, **kw): |
|
142 | def _export(nb, **kw): | |
140 | return export(E, nb, **kw) |
|
143 | return export(E, nb, **kw) | |
141 | _export.__doc__ = """Export a notebook object to {0} format""".format(name) |
|
144 | _export.__doc__ = """Export a notebook object to {0} format""".format(name) | |
142 | return _export |
|
145 | return _export | |
143 |
|
146 | |||
144 | g = globals() |
|
147 | g = globals() | |
145 |
|
148 | |||
146 | for name, E in exporter_map.items(): |
|
149 | for name, E in exporter_map.items(): | |
147 | g['export_%s' % name] = DocDecorator(_make_exporter(name, E)) |
|
150 | g['export_%s' % name] = DocDecorator(_make_exporter(name, E)) | |
148 |
|
151 | |||
149 | @DocDecorator |
|
152 | @DocDecorator | |
150 | def export_by_name(format_name, nb, **kw): |
|
153 | def export_by_name(format_name, nb, **kw): | |
151 | """ |
|
154 | """ | |
152 | Export a notebook object to a template type by its name. Reflection |
|
155 | Export a notebook object to a template type by its name. Reflection | |
153 | (Inspect) is used to find the template's corresponding explicit export |
|
156 | (Inspect) is used to find the template's corresponding explicit export | |
154 | method defined in this module. That method is then called directly. |
|
157 | method defined in this module. That method is then called directly. | |
155 |
|
158 | |||
156 | Parameters |
|
159 | Parameters | |
157 | ---------- |
|
160 | ---------- | |
158 | format_name : str |
|
161 | format_name : str | |
159 | Name of the template style to export to. |
|
162 | Name of the template style to export to. | |
160 | """ |
|
163 | """ | |
161 |
|
164 | |||
162 | function_name = "export_" + format_name.lower() |
|
165 | function_name = "export_" + format_name.lower() | |
163 |
|
166 | |||
164 | if function_name in globals(): |
|
167 | if function_name in globals(): | |
165 | return globals()[function_name](nb, **kw) |
|
168 | return globals()[function_name](nb, **kw) | |
166 | else: |
|
169 | else: | |
167 | raise ExporterNameError("template for `%s` not found" % function_name) |
|
170 | raise ExporterNameError("template for `%s` not found" % function_name) | |
168 |
|
171 | |||
169 |
|
172 | |||
170 | def get_export_names(): |
|
173 | def get_export_names(): | |
171 | """Return a list of the currently supported export targets |
|
174 | """Return a list of the currently supported export targets | |
172 |
|
175 | |||
173 | WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT""" |
|
176 | WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT""" | |
174 | return sorted(exporter_map.keys()) |
|
177 | return sorted(exporter_map.keys()) |
General Comments 0
You need to be logged in to leave comments.
Login now