Show More
@@ -1,162 +1,151 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 |
|
|||
22 | import codecs |
|
|||
23 | import io |
|
21 | import io | |
24 | import logging |
|
|||
25 | import os |
|
22 | import os | |
26 | from IPython.utils import path |
|
23 | from IPython.utils import path | |
27 | import pprint |
|
|||
28 | import re |
|
|||
29 | from types import FunctionType |
|
|||
30 |
|
24 | |||
31 |
from jinja2 import Environment, |
|
25 | from jinja2 import Environment, FileSystemLoader | |
32 | env = Environment( |
|
26 | env = Environment( | |
33 | loader=FileSystemLoader('./templates/'), |
|
27 | loader=FileSystemLoader('./templates/'), | |
34 | extensions=['jinja2.ext.loopcontrols'] |
|
28 | extensions=['jinja2.ext.loopcontrols'] | |
35 | ) |
|
29 | ) | |
36 |
|
30 | |||
37 | # IPython imports |
|
31 | # IPython imports | |
38 | from IPython.nbformat import current as nbformat |
|
32 | from IPython.nbformat import current as nbformat | |
39 |
from IPython.config.configurable import Configurable |
|
33 | from IPython.config.configurable import Configurable | |
40 |
from IPython.utils.traitlets import ( |
|
34 | from IPython.utils.traitlets import ( Unicode, Any) | |
41 | Any) |
|
|||
42 |
|
35 | |||
43 | # Our own imports |
|
36 | # Our own imports | |
44 | from IPython.utils.text import indent |
|
37 | from IPython.utils.text import indent | |
45 | from .utils import remove_ansi |
|
38 | from .utils import remove_ansi | |
46 | from markdown import markdown |
|
39 | from markdown import markdown | |
47 | from .utils import highlight,ansi2html |
|
40 | from .utils import highlight, ansi2html | |
48 | #----------------------------------------------------------------------------- |
|
41 | #----------------------------------------------------------------------------- | |
49 | # Class declarations |
|
42 | # Class declarations | |
50 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
51 | def rm_fake(strng): |
|
44 | def rm_fake(strng): | |
52 | return strng.replace('/files/', '') |
|
45 | return strng.replace('/files/', '') | |
53 |
|
46 | |||
54 | class ConversionException(Exception): |
|
47 | class ConversionException(Exception): | |
55 | pass |
|
48 | pass | |
56 |
|
49 | |||
57 |
|
50 | |||
58 | def python_comment(string): |
|
51 | def python_comment(string): | |
59 | return '# '+'\n# '.join(string.split('\n')) |
|
52 | return '# '+'\n# '.join(string.split('\n')) | |
60 |
|
53 | |||
61 |
|
54 | |||
62 |
|
55 | |||
63 | def header_body(): |
|
56 | def header_body(): | |
64 |
|
|
57 | """Return the body of the header as a list of strings.""" | |
65 |
|
58 | |||
66 |
|
|
59 | from pygments.formatters import HtmlFormatter | |
67 |
|
60 | |||
68 |
|
|
61 | header = [] | |
69 |
|
|
62 | static = os.path.join(path.get_ipython_package_dir(), | |
70 |
|
|
63 | 'frontend', 'html', 'notebook', 'static', | |
71 |
|
|
64 | ) | |
72 |
|
|
65 | here = os.path.split(os.path.realpath(__file__))[0] | |
73 |
|
|
66 | css = os.path.join(static, 'css') | |
74 |
|
|
67 | for sheet in [ | |
75 |
|
|
68 | # do we need jquery and prettify? | |
76 |
|
|
69 | # os.path.join(static, 'jquery', 'css', 'themes', 'base', | |
77 |
|
|
70 | # 'jquery-ui.min.css'), | |
78 |
|
|
71 | # os.path.join(static, 'prettify', 'prettify.css'), | |
79 |
|
|
72 | os.path.join(css, 'boilerplate.css'), | |
80 |
|
|
73 | os.path.join(css, 'fbm.css'), | |
81 |
|
|
74 | os.path.join(css, 'notebook.css'), | |
82 |
|
|
75 | os.path.join(css, 'renderedhtml.css'), | |
83 |
|
|
76 | # our overrides: | |
84 |
|
|
77 | os.path.join(here, '..', 'css', 'static_html.css'), | |
85 |
|
|
78 | ]: | |
86 |
|
79 | |||
87 |
|
|
80 | with io.open(sheet, encoding='utf-8') as f: | |
88 |
|
|
81 | s = f.read() | |
89 |
|
|
82 | header.append(s) | |
90 |
|
83 | |||
91 |
|
|
84 | pygments_css = HtmlFormatter().get_style_defs('.highlight') | |
92 |
|
|
85 | header.append(pygments_css) | |
93 |
|
|
86 | return header | |
94 |
|
87 | |||
95 | inlining= {} |
|
88 | inlining = {} | |
96 | inlining['css'] = header_body() |
|
89 | inlining['css'] = header_body() | |
97 |
|
90 | |||
98 |
|
91 | |||
99 | def filter_data_type(output): |
|
92 | def filter_data_type(output): | |
100 | for fmt in ['html', 'pdf', 'svg', 'latex','png', 'jpg','jpeg' , 'text']: |
|
93 | for fmt in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text']: | |
101 | if fmt in output: |
|
94 | if fmt in output: | |
102 | return [fmt] |
|
95 | return [fmt] | |
103 |
|
96 | |||
104 |
|
97 | |||
105 | env.filters['filter_data_type'] = filter_data_type |
|
98 | env.filters['filter_data_type'] = filter_data_type | |
106 | env.filters['pycomment'] = python_comment |
|
99 | env.filters['pycomment'] = python_comment | |
107 | env.filters['indent'] = indent |
|
100 | env.filters['indent'] = indent | |
108 | env.filters['rm_fake'] = rm_fake |
|
101 | env.filters['rm_fake'] = rm_fake | |
109 | env.filters['rm_ansi'] = remove_ansi |
|
102 | env.filters['rm_ansi'] = remove_ansi | |
110 | env.filters['markdown'] = markdown |
|
103 | env.filters['markdown'] = markdown | |
111 | env.filters['highlight'] = highlight |
|
104 | env.filters['highlight'] = highlight | |
112 | env.filters['ansi2html'] = ansi2html |
|
105 | env.filters['ansi2html'] = ansi2html | |
113 |
|
106 | |||
114 | class ConverterTemplate(Configurable): |
|
107 | class ConverterTemplate(Configurable): | |
115 |
|
108 | |||
116 | display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text'] |
|
109 | display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text'] | |
117 | #------------------------------------------------------------------------- |
|
110 | #------------------------------------------------------------------------- | |
118 | # Instance-level attributes that are set in the constructor for this |
|
111 | # Instance-level attributes that are set in the constructor for this | |
119 | # class. |
|
112 | # class. | |
120 | #------------------------------------------------------------------------- |
|
113 | #------------------------------------------------------------------------- | |
121 | infile = Any() |
|
114 | infile = Any() | |
122 |
|
115 | |||
123 |
|
116 | |||
124 | infile_dir = Unicode() |
|
117 | infile_dir = Unicode() | |
125 |
|
118 | |||
126 | def __init__(self, tplfile='fullhtml', config=None, **kw): |
|
119 | def __init__(self, tplfile='fullhtml', config=None, **kw): | |
127 | self.template = env.get_template(tplfile+'.tpl') |
|
120 | self.template = env.get_template(tplfile+'.tpl') | |
128 | super(ConverterTemplate,self).__init__(config=config) |
|
121 | self.nb = None | |
129 |
|
122 | super(ConverterTemplate, self).__init__(config=config, **kw) | ||
130 | def _get_prompt_number(self, cell): |
|
|||
131 | return cell.prompt_number if hasattr(cell, 'prompt_number') \ |
|
|||
132 | else self.blank_symbol |
|
|||
133 |
|
||||
134 |
|
123 | |||
135 | def process(self): |
|
124 | def process(self): | |
136 | converted_cells = [] |
|
125 | converted_cells = [] | |
137 | for worksheet in self.nb.worksheets: |
|
126 | for worksheet in self.nb.worksheets: | |
138 | for cell in worksheet.cells: |
|
127 | for cell in worksheet.cells: | |
139 | cell.type = cell.cell_type |
|
128 | cell.type = cell.cell_type | |
140 | cell.haspyout = False |
|
129 | cell.haspyout = False | |
141 | for out in cell.get('outputs',[]): |
|
130 | for out in cell.get('outputs', []): | |
142 | if out.output_type == 'pyout': |
|
131 | if out.output_type == 'pyout': | |
143 | cell.haspyout = True |
|
132 | cell.haspyout = True | |
144 | break |
|
133 | break | |
145 | converted_cells.append(worksheet) |
|
134 | converted_cells.append(worksheet) | |
146 |
|
135 | |||
147 | return converted_cells |
|
136 | return converted_cells | |
148 |
|
137 | |||
149 |
def convert(self |
|
138 | def convert(self): | |
150 | """ convert the ipynb file |
|
139 | """ convert the ipynb file | |
151 |
|
140 | |||
152 | return both the converted ipynb file and a dict containing potential |
|
141 | return both the converted ipynb file and a dict containing potential | |
153 | other resources |
|
142 | other resources | |
154 | """ |
|
143 | """ | |
155 | return self.template.render(worksheets=self.process(), inlining=inlining),{} |
|
144 | return self.template.render(worksheets=self.process(), inlining=inlining), {} | |
156 |
|
145 | |||
157 |
|
146 | |||
158 | def read(self, filename): |
|
147 | def read(self, filename): | |
159 | "read and parse notebook into NotebookNode called self.nb" |
|
148 | "read and parse notebook into NotebookNode called self.nb" | |
160 | with io.open(filename) as f: |
|
149 | with io.open(filename) as f: | |
161 | self.nb = nbformat.read(f, 'json') |
|
150 | self.nb = nbformat.read(f, 'json') | |
162 |
|
151 |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now