##// END OF EJS Templates
Start to think on api...
Matthias BUSSONNIER -
Show More
@@ -1,157 +1,162 b''
1 """Base classes for the notebook conversion pipeline.
1 """Base classes for the notebook conversion pipeline.
2
2
3 This module defines Converter, from which all objects designed to implement
3 This module defines Converter, from which all objects designed to implement
4 a conversion of IPython notebooks to some other format should inherit.
4 a conversion of IPython notebooks to some other format should inherit.
5 """
5 """
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2012, the IPython Development Team.
7 # Copyright (c) 2012, the IPython Development Team.
8 #
8 #
9 # Distributed under the terms of the Modified BSD License.
9 # Distributed under the terms of the Modified BSD License.
10 #
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 from __future__ import print_function, absolute_import
18 from __future__ import print_function, absolute_import
19
19
20 # Stdlib imports
20 # Stdlib imports
21 import jinja2
21 import jinja2
22 import codecs
22 import codecs
23 import io
23 import io
24 import logging
24 import logging
25 import os
25 import os
26 from IPython.utils import path
26 from IPython.utils import path
27 import pprint
27 import pprint
28 import re
28 import re
29 from types import FunctionType
29 from types import FunctionType
30
30
31 from jinja2 import Environment, PackageLoader, FileSystemLoader
31 from jinja2 import Environment, PackageLoader, FileSystemLoader
32 env = Environment(
32 env = Environment(
33 loader=FileSystemLoader('./templates/'),
33 loader=FileSystemLoader('./templates/'),
34 extensions=['jinja2.ext.loopcontrols']
34 extensions=['jinja2.ext.loopcontrols']
35 )
35 )
36
36
37 # IPython imports
37 # IPython imports
38 from IPython.nbformat import current as nbformat
38 from IPython.nbformat import current as nbformat
39 from IPython.config.configurable import Configurable, SingletonConfigurable
39 from IPython.config.configurable import Configurable, SingletonConfigurable
40 from IPython.utils.traitlets import (List, Unicode, Type, Bool, Dict, CaselessStrEnum,
40 from IPython.utils.traitlets import (List, Unicode, Type, Bool, Dict, CaselessStrEnum,
41 Any)
41 Any)
42
42
43 # Our own imports
43 # Our own imports
44 from IPython.utils.text import indent
44 from IPython.utils.text import indent
45 from .utils import remove_ansi
45 from .utils import remove_ansi
46 from markdown import markdown
46 from markdown import markdown
47 from .utils import highlight,ansi2html
47 from .utils import highlight,ansi2html
48 #-----------------------------------------------------------------------------
48 #-----------------------------------------------------------------------------
49 # Class declarations
49 # Class declarations
50 #-----------------------------------------------------------------------------
50 #-----------------------------------------------------------------------------
51 def rm_fake(strng):
51 def rm_fake(strng):
52 return strng.replace('/files/', '')
52 return strng.replace('/files/', '')
53
53
54 class ConversionException(Exception):
54 class ConversionException(Exception):
55 pass
55 pass
56
56
57
57
58 def python_comment(string):
58 def python_comment(string):
59 return '# '+'\n# '.join(string.split('\n'))
59 return '# '+'\n# '.join(string.split('\n'))
60
60
61
61
62
62
63 def header_body():
63 def header_body():
64 """Return the body of the header as a list of strings."""
64 """Return the body of the header as a list of strings."""
65
65
66 from pygments.formatters import HtmlFormatter
66 from pygments.formatters import HtmlFormatter
67
67
68 header = []
68 header = []
69 static = os.path.join(path.get_ipython_package_dir(),
69 static = os.path.join(path.get_ipython_package_dir(),
70 'frontend', 'html', 'notebook', 'static',
70 'frontend', 'html', 'notebook', 'static',
71 )
71 )
72 here = os.path.split(os.path.realpath(__file__))[0]
72 here = os.path.split(os.path.realpath(__file__))[0]
73 css = os.path.join(static, 'css')
73 css = os.path.join(static, 'css')
74 for sheet in [
74 for sheet in [
75 # do we need jquery and prettify?
75 # do we need jquery and prettify?
76 # os.path.join(static, 'jquery', 'css', 'themes', 'base',
76 # os.path.join(static, 'jquery', 'css', 'themes', 'base',
77 # 'jquery-ui.min.css'),
77 # 'jquery-ui.min.css'),
78 # os.path.join(static, 'prettify', 'prettify.css'),
78 # os.path.join(static, 'prettify', 'prettify.css'),
79 os.path.join(css, 'boilerplate.css'),
79 os.path.join(css, 'boilerplate.css'),
80 os.path.join(css, 'fbm.css'),
80 os.path.join(css, 'fbm.css'),
81 os.path.join(css, 'notebook.css'),
81 os.path.join(css, 'notebook.css'),
82 os.path.join(css, 'renderedhtml.css'),
82 os.path.join(css, 'renderedhtml.css'),
83 # our overrides:
83 # our overrides:
84 os.path.join(here, '..', 'css', 'static_html.css'),
84 os.path.join(here, '..', 'css', 'static_html.css'),
85 ]:
85 ]:
86
86
87 with io.open(sheet, encoding='utf-8') as f:
87 with io.open(sheet, encoding='utf-8') as f:
88 s = f.read()
88 s = f.read()
89 header.append(s)
89 header.append(s)
90
90
91 pygments_css = HtmlFormatter().get_style_defs('.highlight')
91 pygments_css = HtmlFormatter().get_style_defs('.highlight')
92 header.append(pygments_css)
92 header.append(pygments_css)
93 return header
93 return header
94
94
95 inlining= {}
95 inlining= {}
96 inlining['css'] = header_body()
96 inlining['css'] = header_body()
97
97
98
98
99 def filter_data_type(output):
99 def filter_data_type(output):
100 for fmt in ['html', 'pdf', 'svg', 'latex','png', 'jpg','jpeg' , 'text']:
100 for fmt in ['html', 'pdf', 'svg', 'latex','png', 'jpg','jpeg' , 'text']:
101 if fmt in output:
101 if fmt in output:
102 return [fmt]
102 return [fmt]
103
103
104
104
105 env.filters['filter_data_type'] = filter_data_type
105 env.filters['filter_data_type'] = filter_data_type
106 env.filters['pycomment'] = python_comment
106 env.filters['pycomment'] = python_comment
107 env.filters['indent'] = indent
107 env.filters['indent'] = indent
108 env.filters['rm_fake'] = rm_fake
108 env.filters['rm_fake'] = rm_fake
109 env.filters['rm_ansi'] = remove_ansi
109 env.filters['rm_ansi'] = remove_ansi
110 env.filters['markdown'] = markdown
110 env.filters['markdown'] = markdown
111 env.filters['highlight'] = highlight
111 env.filters['highlight'] = highlight
112 env.filters['ansi2html'] = ansi2html
112 env.filters['ansi2html'] = ansi2html
113
113
114 class ConverterTemplate(Configurable):
114 class ConverterTemplate(Configurable):
115
115
116 display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text']
116 display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text']
117 #-------------------------------------------------------------------------
117 #-------------------------------------------------------------------------
118 # Instance-level attributes that are set in the constructor for this
118 # Instance-level attributes that are set in the constructor for this
119 # class.
119 # class.
120 #-------------------------------------------------------------------------
120 #-------------------------------------------------------------------------
121 infile = Any()
121 infile = Any()
122
122
123
123
124 infile_dir = Unicode()
124 infile_dir = Unicode()
125
125
126 def __init__(self, tplfile='fullhtml', config=None, **kw):
126 def __init__(self, tplfile='fullhtml', config=None, **kw):
127 self.template = env.get_template(tplfile+'.tpl')
127 self.template = env.get_template(tplfile+'.tpl')
128 super(ConverterTemplate,self).__init__(config=config)
128 super(ConverterTemplate,self).__init__(config=config)
129
129
130 def _get_prompt_number(self, cell):
130 def _get_prompt_number(self, cell):
131 return cell.prompt_number if hasattr(cell, 'prompt_number') \
131 return cell.prompt_number if hasattr(cell, 'prompt_number') \
132 else self.blank_symbol
132 else self.blank_symbol
133
133
134
134
135 def process(self):
135 def process(self):
136 converted_cells = []
136 converted_cells = []
137 for worksheet in self.nb.worksheets:
137 for worksheet in self.nb.worksheets:
138 for cell in worksheet.cells:
138 for cell in worksheet.cells:
139 cell.type = cell.cell_type
139 cell.type = cell.cell_type
140 cell.haspyout = False
140 cell.haspyout = False
141 for out in cell.get('outputs',[]):
141 for out in cell.get('outputs',[]):
142 if out.output_type == 'pyout':
142 if out.output_type == 'pyout':
143 cell.haspyout = True
143 cell.haspyout = True
144 break
144 break
145 converted_cells.append(worksheet)
145 converted_cells.append(worksheet)
146
146
147 return converted_cells
147 return converted_cells
148
148
149 def convert(self, cell_separator='\n'):
149 def convert(self, cell_separator='\n'):
150 return self.template.render(worksheets=self.process(), inlining=inlining)
150 """ convert the ipynb file
151
152 return both the converted ipynb file and a dict containing potential
153 other resources
154 """
155 return self.template.render(worksheets=self.process(), inlining=inlining),{}
151
156
152
157
153 def read(self, filename):
158 def read(self, filename):
154 "read and parse notebook into NotebookNode called self.nb"
159 "read and parse notebook into NotebookNode called self.nb"
155 with io.open(filename) as f:
160 with io.open(filename) as f:
156 self.nb = nbformat.read(f, 'json')
161 self.nb = nbformat.read(f, 'json')
157
162
@@ -1,11 +1,13 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # coding: utf-8
2 # coding: utf-8
3
3
4 from __future__ import print_function
4 from __future__ import print_function
5 import sys
5 import sys
6 import io
6 import io
7 from converters.template import *
7 from converters.template import *
8 C = ConverterTemplate(tplfile=sys.argv[1])
8 C = ConverterTemplate(tplfile=sys.argv[1])
9 C.read(sys.argv[2])
9 C.read(sys.argv[2])
10
10
11 print(C.convert().encode('utf-8'))
11 output,rest = C.convert()
12
13 print(output.encode('utf-8'))
General Comments 0
You need to be logged in to leave comments. Login now