##// END OF EJS Templates
Cleanup and refactor of API, almost complete....
Jonathan Frederic -
Show More
@@ -1,4 +1,5 b''
1 """TODO: Docstring
1 """
2 Exporter that exports Basic HTML.
2 """
3 """
3
4
4 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
@@ -13,16 +14,25 b''
13 # Imports
14 # Imports
14 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
15
16
17 from IPython.utils.traitlets import Unicode
18
19 import nbconvert.transformers.csshtmlheader
20
16 # local import
21 # local import
17 import exporter
22 import exporter
18 import nbconvert.transformers.csshtmlheader
19 from IPython.utils.traitlets import Unicode
20
23
21 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
22 # Classes
25 # Classes
23 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
24 class BasicHtmlExporter(exporter.Exporter):
25
27
28 class BasicHtmlExporter(exporter.Exporter):
29 """
30 Exports a basic HTML document. This exporter assists with the export of
31 HTML. Inherit from it if you are writing your own HTML template and need
32 custom tranformers/filters. If you don't need custom tranformers/
33 filters, just change the 'template_file' config option.
34 """
35
26 file_extension = Unicode(
36 file_extension = Unicode(
27 'html', config=True,
37 'html', config=True,
28 help="Extension of the file that should be written to disk"
38 help="Extension of the file that should be written to disk"
@@ -1,7 +1,5 b''
1 """Latex transformer.
1 """
2
2 Module containing easy to use conversion functions.
3 Module that allows latex output notebooks to be conditioned before
4 they are converted.
5 """
3 """
6 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
7 # Copyright (c) 2013, the IPython Development Team.
5 # Copyright (c) 2013, the IPython Development Team.
@@ -42,10 +40,12 b' def export(nb, config=None, transformers=None, filters=None, exporter_type=Expor'
42 output, resources = exporter_instance.from_file(nb)
40 output, resources = exporter_instance.from_file(nb)
43 return output, resources, exporter_instance
41 return output, resources, exporter_instance
44
42
43
45 def load_class(template_name):
44 def load_class(template_name):
46 class_name = template_name[0].upper() + template_name[1:] + "Exporter"
45 class_name = template_name[0].upper() + template_name[1:] + "Exporter"
47 module = __import__('nbconvert.api.' + template_name, fromlist=[class_name])
46 module = __import__('nbconvert.api.' + template_name, fromlist=[class_name])
48 return getattr(module, class_name)
47 return getattr(module, class_name)
49
48
49
50 def export_by_name(nb, template_name, config=None, transformers=None, filters=None):
50 def export_by_name(nb, template_name, config=None, transformers=None, filters=None):
51 return export(nb, config, transformers, filters, load_class(template_name)) No newline at end of file
51 return export(nb, config, transformers, filters, load_class(template_name))
@@ -18,6 +18,7 b' before conversion and jinja filter that would then be available in the templates'
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21 from __future__ import print_function, absolute_import
22 from __future__ import print_function, absolute_import
22
23
23 # Stdlib imports
24 # Stdlib imports
@@ -59,16 +60,10 b' TEMPLATE_SKELETON_PATH = "/../templates/skeleton/"'
59 JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols']
60 JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols']
60
61
61 #-----------------------------------------------------------------------------
62 #-----------------------------------------------------------------------------
62 # Classes and functions
63 # Class
63 #-----------------------------------------------------------------------------
64 #-----------------------------------------------------------------------------
65
64 class Exporter(Configurable):
66 class Exporter(Configurable):
65 pre_transformer_order = List(['haspyout_transformer'],
66 config=True,
67 help= """
68 An ordered list of pre-transformer to apply to the IPYNB
69 file before running through templates
70 """
71 )
72
67
73 template_file = Unicode(
68 template_file = Unicode(
74 '', config=True,
69 '', config=True,
@@ -88,7 +83,7 b' class Exporter(Configurable):'
88
83
89 # Public Constructor #####################################################
84 # Public Constructor #####################################################
90
85
91 def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw):
86 def __init__(self, transformers=None, filters=None, config=None, **kw):
92
87
93 #Call the base class constructor
88 #Call the base class constructor
94 super(Exporter, self).__init__(config=config, **kw)
89 super(Exporter, self).__init__(config=config, **kw)
@@ -96,24 +91,20 b' class Exporter(Configurable):'
96 #Standard environment
91 #Standard environment
97 self._init_environment()
92 self._init_environment()
98
93
99 #TODO: Implement reflection style methods to get user transformers.
100 #if not preprocessors is None:
101 # for name in self.pre_transformer_order:
102 # # get the user-defined transformer first
103 # transformer = preprocessors.get(name, getattr(trans, name, None))
104 # if isinstance(transformer, MetaHasTraits):
105 # transformer = transformer(config=config)
106 # self.preprocessors.append(transformer)
107
108 #Add transformers
94 #Add transformers
109 self._register_transformers()
95 self._register_transformers()
110
96
111 #Add filters to the Jinja2 environment
97 #Add filters to the Jinja2 environment
112 self._register_filters()
98 self._register_filters()
113
99
100 #Load user transformers. Overwrite existing transformers if need be.
101 if not transformers is None:
102 for transformer in transformers:
103 self.register_transformer(transformer)
104
114 #Load user filters. Overwrite existing filters if need be.
105 #Load user filters. Overwrite existing filters if need be.
115 if not jinja_filters is None:
106 if not filters is None:
116 for key, user_filter in jinja_filters.iteritems():
107 for key, user_filter in filters.iteritems():
117 if issubclass(user_filter, MetaHasTraits):
108 if issubclass(user_filter, MetaHasTraits):
118 self.environment.filters[key] = user_filter(config=config)
109 self.environment.filters[key] = user_filter(config=config)
119 else:
110 else:
@@ -1,4 +1,5 b''
1 """TODO: Docstring
1 """
2 Exporter for exporting full HTML documents.
2 """
3 """
3
4
4 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
@@ -13,15 +14,20 b''
13 # Imports
14 # Imports
14 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
15
16
17 from IPython.utils.traitlets import Unicode
18
16 # local import
19 # local import
17 import basichtml.BasicHtmlExporter
20 import basichtml.BasicHtmlExporter
18 from IPython.utils.traitlets import Unicode
19
21
20 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
21 # Classes
23 # Classes
22 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
23 class FullHtmlExporter(basichtml.BasicHtmlExporter):
24
25
26 class FullHtmlExporter(basichtml.BasicHtmlExporter):
27 """
28 Exports a full HTML document.
29 """
30
25 template_file = Unicode(
31 template_file = Unicode(
26 'fullhtml', config=True,
32 'fullhtml', config=True,
27 help="Name of the template file to use") No newline at end of file
33 help="Name of the template file to use")
@@ -1,3 +1,9 b''
1 """
2 Exporter that allows Latex Jinja templates to work. Contains logic to
3 appropriately prepare IPYNB files for export to LaTeX. Including but
4 not limited to escaping LaTeX, fixing math region tags, using special
5 tags to circumvent Jinja/Latex syntax conflicts.
6 """
1 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
2 # Copyright (c) 2013, the IPython Development Team.
8 # Copyright (c) 2013, the IPython Development Team.
3 #
9 #
@@ -19,11 +25,13 b' from IPython.utils.traitlets import Unicode'
19 # other libs/dependencies
25 # other libs/dependencies
20 from jinja2 import Environment, FileSystemLoader
26 from jinja2 import Environment, FileSystemLoader
21
27
22 # local import
23 import exporter
24 import nbconvert.filters.latex
28 import nbconvert.filters.latex
25 import nbconvert.filters.highlight
29 import nbconvert.filters.highlight
26 from nbconvert.transformers.latex import LatexTransformer
30 from nbconvert.transformers.latex import LatexTransformer
31
32 # local import
33 import exporter
34
27 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
28 # Globals and constants
36 # Globals and constants
29 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
@@ -40,8 +48,17 b' LATEX_JINJA_LOGIC_BLOCK = ["((*", "*))"]'
40 #-----------------------------------------------------------------------------
48 #-----------------------------------------------------------------------------
41 # Classes and functions
49 # Classes and functions
42 #-----------------------------------------------------------------------------
50 #-----------------------------------------------------------------------------
43 class LatexExporter(exporter.Exporter):
44
51
52 class LatexExporter(exporter.Exporter):
53 """
54 Exports to a Latex template. Inherit from this class if your template is
55 LaTeX based and you need custom tranformers/filters. Inherit from it if
56 you are writing your own HTML template and need custom tranformers/filters.
57 If you don't need custom tranformers/filters, just change the
58 'template_file' config option. Place your template in the special "/latex"
59 subfolder of the "../templates" folder.
60 """
61
45 #Extension that the template files use.
62 #Extension that the template files use.
46 template_extension = ".tplx"
63 template_extension = ".tplx"
47
64
@@ -53,10 +70,10 b' class LatexExporter(exporter.Exporter):'
53 'base', config=True,
70 'base', config=True,
54 help="Name of the template file to use")
71 help="Name of the template file to use")
55
72
56 def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw):
73 def __init__(self, transformers=None, filters=None, config=None, **kw):
57
74
58 #Call base class constructor.
75 #Call base class constructor.
59 super(LatexExporter, self).__init__(preprocessors, jinja_filters, config, **kw)
76 super(LatexExporter, self).__init__(transformers, filters, config, **kw)
60
77
61 self.extract_figure_transformer.display_data_priority = ['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text']
78 self.extract_figure_transformer.display_data_priority = ['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text']
62 self.extract_figure_transformer.extra_ext_map={'svg':'pdf'}
79 self.extract_figure_transformer.extra_ext_map={'svg':'pdf'}
@@ -86,9 +103,10 b' class LatexExporter(exporter.Exporter):'
86 super(LatexExporter, self)._register_filters()
103 super(LatexExporter, self)._register_filters()
87
104
88 #Add latex filters to the Jinja2 environment
105 #Add latex filters to the Jinja2 environment
89 self.register_filter('escape_tex', nbconvert.filters.latex.escape_tex)
106 self.register_filter('escape_tex', nbconvert.filters.latex.escape_latex)
90 self.register_filter('highlight', nbconvert.filters.highlight.highlight2latex)
107 self.register_filter('highlight', nbconvert.filters.highlight.highlight2latex)
91
108
109
92 def _register_transformers(self):
110 def _register_transformers(self):
93
111
94 #Register the transformers of the base class.
112 #Register the transformers of the base class.
@@ -1,4 +1,6 b''
1
1 """
2 Exporter that will export your iPynb to Markdown.
3 """
2 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
3 # Copyright (c) 2013, the IPython Development Team.
5 # Copyright (c) 2013, the IPython Development Team.
4 #
6 #
@@ -11,15 +13,20 b''
11 # Imports
13 # Imports
12 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
13
15
16 from IPython.utils.traitlets import Unicode
17
14 # local import
18 # local import
15 import exporter
19 import exporter
16 from IPython.utils.traitlets import Unicode
17
20
18 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
19 # Classes
22 # Classes
20 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
21 class MarkdownExporter(exporter.Exporter):
22
24
25 class MarkdownExporter(exporter.Exporter):
26 """
27 Exports to a markdown document (.md)
28 """
29
23 file_extension = Unicode(
30 file_extension = Unicode(
24 'md', config=True,
31 'md', config=True,
25 help="Extension of the file that should be written to disk")
32 help="Extension of the file that should be written to disk")
@@ -1,6 +1,6 b''
1 """TODO: Docstring
2 """
1 """
3
2 Python exporter which exports Notebook code into a PY file.
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2013, the IPython Development Team.
5 # Copyright (c) 2013, the IPython Development Team.
6 #
6 #
@@ -13,15 +13,20 b''
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 from IPython.utils.traitlets import Unicode
17
16 # local import
18 # local import
17 import exporter
19 import exporter
18 from IPython.utils.traitlets import Unicode
19
20
20 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
21 # Classes
22 # Classes
22 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
23 class PythonExporter(exporter.Exporter):
24
24
25 class PythonExporter(exporter.Exporter):
26 """
27 Exports a Python code file.
28 """
29
25 file_extension = Unicode(
30 file_extension = Unicode(
26 'py', config=True,
31 'py', config=True,
27 help="Extension of the file that should be written to disk")
32 help="Extension of the file that should be written to disk")
@@ -30,10 +35,10 b' class PythonExporter(exporter.Exporter):'
30 'python', config=True,
35 'python', config=True,
31 help="Name of the template file to use")
36 help="Name of the template file to use")
32
37
33 def __init__(self, preprocessors=None, jinja_filters=None, config=None, armor=False, **kw):
38 def __init__(self, transformers=None, filters=None, config=None, armor=False, **kw):
34
39
35 #Call base class constructor.
40 #Call base class constructor.
36 super(PythonExporter, self).__init__(preprocessors, jinja_filters, config, **kw)
41 super(PythonExporter, self).__init__(transformers, filters, config, **kw)
37
42
38 #Set defaults
43 #Set defaults
39 self.extract_figure_transformer.enabled = False
44 self.extract_figure_transformer.enabled = False
@@ -1,4 +1,5 b''
1 """TODO: Docstring
1 """
2 Exporter that exports a Python-Armor code file (.py)
2 """
3 """
3
4
4 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
@@ -13,14 +14,19 b''
13 # Imports
14 # Imports
14 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
15
16
17 from IPython.utils.traitlets import Unicode
18
16 # local import
19 # local import
17 import python.PythonExporter
20 import python.PythonExporter
18 from IPython.utils.traitlets import Unicode
19
21
20 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
21 # Classes
23 # Classes
22 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
23 class PythonArmorExporter(python.PythonExporter):
26 class PythonArmorExporter(python.PythonExporter):
27 """
28 Exports a Python-Armor code file (.py)
29 """
24
30
25 template_file = Unicode(
31 template_file = Unicode(
26 'python_armor', config=True,
32 'python_armor', config=True,
@@ -1,6 +1,6 b''
1 """TODO: Docstring
2 """
1 """
3
2 Reveal slide show exporter.
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2013, the IPython Development Team.
5 # Copyright (c) 2013, the IPython Development Team.
6 #
6 #
@@ -13,16 +13,22 b''
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 from IPython.utils.traitlets import Unicode
17
16 # local import
18 # local import
17 import html
19 import basichtml
20
18 import nbconvert.transformers.revealhelp
21 import nbconvert.transformers.revealhelp
19 from IPython.utils.traitlets import Unicode
20
22
21 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
22 # Classes
24 # Classes
23 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
24 class RevealExporter(html.HtmlExporter):
25
26
27 class RevealExporter(basichtml.BasicHtmlExporter):
28 """
29 Exports a Reveal slide show (.HTML) which may be rendered in a web browser.
30 """
31
26 file_extension = Unicode(
32 file_extension = Unicode(
27 'reveal.html', config=True,
33 'reveal.html', config=True,
28 help="Extension of the file that should be written to disk")
34 help="Extension of the file that should be written to disk")
@@ -1,6 +1,6 b''
1 """TODO: Docstring
2 """
1 """
3
2 Exporter for exporting notebooks to restructured text.
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2013, the IPython Development Team.
5 # Copyright (c) 2013, the IPython Development Team.
6 #
6 #
@@ -13,15 +13,20 b''
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 from IPython.utils.traitlets import Unicode
17
16 # local import
18 # local import
17 import exporter
19 import exporter
18 from IPython.utils.traitlets import Unicode
19
20
20 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
21 # Classes
22 # Classes
22 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
23 class RstExporter(exporter.Exporter):
24
24
25 class RstExporter(exporter.Exporter):
26 """
27 Exports restructured text documents.
28 """
29
25 file_extension = Unicode(
30 file_extension = Unicode(
26 'rst', config=True,
31 'rst', config=True,
27 help="Extension of the file that should be written to disk")
32 help="Extension of the file that should be written to disk")
@@ -1,4 +1,7 b''
1
1 """
2 Exporter for exporting notebooks to Sphinx 'HowTo' style latex. Latex
3 formatted for use with PDFLatex.
4 """
2 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
3 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
4 #
7 #
@@ -11,15 +14,23 b''
11 # Imports
14 # Imports
12 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
13
16
17 from IPython.utils.traitlets import Unicode
18
14 # local import
19 # local import
15 import latex
20 import latex
16 from IPython.utils.traitlets import Unicode
21
17 from nbconvert.transformers.sphinx import SphinxTransformer
22 from nbconvert.transformers.sphinx import SphinxTransformer
23
18 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
19 # Classes
25 # Classes
20 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
21 class SphinxHowtoExporter(latex.LatexExporter):
22
27
28 class SphinxHowtoExporter(latex.LatexExporter):
29 """
30 Exports Sphinx "HowTo" LaTeX documents. The Sphinx "HowTo" exporter
31 produces short document format latex for use with PDFLatex.
32 """
33
23 template_file = Unicode(
34 template_file = Unicode(
24 'sphinx_howto', config=True,
35 'sphinx_howto', config=True,
25 help="Name of the template file to use")
36 help="Name of the template file to use")
@@ -1,4 +1,7 b''
1
1 """
2 Exporter for exporting notebooks to Sphinx 'Manual' style latex. Latex
3 formatted for use with PDFLatex.
4 """
2 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
3 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
4 #
7 #
@@ -11,14 +14,21 b''
11 # Imports
14 # Imports
12 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
13
16
17 from IPython.utils.traitlets import Unicode
18
14 # local import
19 # local import
15 import sphinx_howto.SphinxHowtoExporter
20 import sphinx_howto.SphinxHowtoExporter
16 from IPython.utils.traitlets import Unicode
21
17 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
18 # Classes
23 # Classes
19 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
20 class SphinxManualExporter(sphinx_howto.SphinxHowtoExporter):
21
25
26 class SphinxManualExporter(sphinx_howto.SphinxHowtoExporter):
27 """
28 Exports Sphinx "Manual" LaTeX documents. The Sphinx "Manual" exporter
29 produces book like latex output for use with PDFLatex.
30 """
31
22 template_file = Unicode(
32 template_file = Unicode(
23 'sphinx_manual', config=True,
33 'sphinx_manual', config=True,
24 help="Name of the template file to use")
34 help="Name of the template file to use")
General Comments 0
You need to be logged in to leave comments. Login now