Show More
@@ -1,183 +1,183 b'' | |||||
1 | from __future__ import absolute_import |
|
1 | from __future__ import absolute_import | |
2 |
|
2 | |||
3 | from converters.base import Converter |
|
3 | from converters.base import Converter | |
4 | from converters.utils import text_cell, output_container |
|
4 | from converters.utils import text_cell, output_container | |
5 | from converters.utils import highlight, coalesce_streams, ansi2html |
|
5 | from converters.utils import highlight, coalesce_streams, ansi2html | |
6 |
|
6 | |||
7 | from IPython.utils import path |
|
7 | from IPython.utils import path | |
8 | from markdown import markdown |
|
8 | from markdown import markdown | |
9 | import os |
|
9 | import os | |
10 | import io |
|
10 | import io | |
11 |
|
11 | |||
12 | class ConverterHTML(Converter): |
|
12 | class ConverterHTML(Converter): | |
13 | extension = 'html' |
|
13 | extension = 'html' | |
14 |
|
14 | |||
15 | def in_tag(self, tag, src, attrs=None): |
|
15 | def in_tag(self, tag, src, attrs=None): | |
16 | """Return a list of elements bracketed by the given tag""" |
|
16 | """Return a list of elements bracketed by the given tag""" | |
17 | attr_s = '' if attrs is None else \ |
|
17 | attr_s = '' if attrs is None else \ | |
18 | ' '.join( "%s=%s" % (attr, value) |
|
18 | ' '.join( "%s=%s" % (attr, value) | |
19 | for attr, value in attrs.iteritems() ) |
|
19 | for attr, value in attrs.iteritems() ) | |
20 | return ['<%s %s>' % (tag, attr_s), src, '</%s>' % tag] |
|
20 | return ['<%s %s>' % (tag, attr_s), src, '</%s>' % tag] | |
21 |
|
21 | |||
22 | def _ansi_colored(self, text): |
|
22 | def _ansi_colored(self, text): | |
23 | return ['<pre>%s</pre>' % ansi2html(text)] |
|
23 | return ['<pre>%s</pre>' % ansi2html(text)] | |
24 |
|
24 | |||
25 | def _stylesheet(self, fname): |
|
25 | def _stylesheet(self, fname): | |
26 | with io.open(fname, encoding='utf-8') as f: |
|
26 | with io.open(fname, encoding='utf-8') as f: | |
27 | s = f.read() |
|
27 | s = f.read() | |
28 | return self.in_tag('style', s, dict(type='"text/css"')) |
|
28 | return self.in_tag('style', s, dict(type='"text/css"')) | |
29 |
|
29 | |||
30 | def _out_prompt(self, output): |
|
30 | def _out_prompt(self, output): | |
31 | if output.output_type == 'pyout': |
|
31 | if output.output_type == 'pyout': | |
32 | n = output.prompt_number if output.prompt_number is not None else ' ' |
|
32 | n = output.prompt_number if output.prompt_number is not None else ' ' | |
33 |
content = 'Out |
|
33 | content = 'Out [%s]:' % n | |
34 | else: |
|
34 | else: | |
35 | content = '' |
|
35 | content = '' | |
36 | return ['<div class="prompt output_prompt">%s</div>' % content] |
|
36 | return ['<div class="prompt output_prompt">%s</div>' % content] | |
37 |
|
37 | |||
38 | def header_body(self): |
|
38 | def header_body(self): | |
39 | """Return the body of the header as a list of strings.""" |
|
39 | """Return the body of the header as a list of strings.""" | |
40 |
|
40 | |||
41 | from pygments.formatters import HtmlFormatter |
|
41 | from pygments.formatters import HtmlFormatter | |
42 |
|
42 | |||
43 | header = [] |
|
43 | header = [] | |
44 | static = os.path.join(path.get_ipython_package_dir(), |
|
44 | static = os.path.join(path.get_ipython_package_dir(), | |
45 | 'frontend', 'html', 'notebook', 'static', |
|
45 | 'frontend', 'html', 'notebook', 'static', | |
46 | ) |
|
46 | ) | |
47 | here = os.path.split(os.path.realpath(__file__))[0] |
|
47 | here = os.path.split(os.path.realpath(__file__))[0] | |
48 | css = os.path.join(static, 'css') |
|
48 | css = os.path.join(static, 'css') | |
49 | for sheet in [ |
|
49 | for sheet in [ | |
50 | # do we need jquery and prettify? |
|
50 | # do we need jquery and prettify? | |
51 | # os.path.join(static, 'jquery', 'css', 'themes', 'base', 'jquery-ui.min.css'), |
|
51 | # os.path.join(static, 'jquery', 'css', 'themes', 'base', 'jquery-ui.min.css'), | |
52 | # os.path.join(static, 'prettify', 'prettify.css'), |
|
52 | # os.path.join(static, 'prettify', 'prettify.css'), | |
53 | os.path.join(css, 'boilerplate.css'), |
|
53 | os.path.join(css, 'boilerplate.css'), | |
54 | os.path.join(css, 'fbm.css'), |
|
54 | os.path.join(css, 'fbm.css'), | |
55 | os.path.join(css, 'notebook.css'), |
|
55 | os.path.join(css, 'notebook.css'), | |
56 | os.path.join(css, 'renderedhtml.css'), |
|
56 | os.path.join(css, 'renderedhtml.css'), | |
57 | # our overrides: |
|
57 | # our overrides: | |
58 | os.path.join(here, '..','css', 'static_html.css'), |
|
58 | os.path.join(here, '..','css', 'static_html.css'), | |
59 | ]: |
|
59 | ]: | |
60 | header.extend(self._stylesheet(sheet)) |
|
60 | header.extend(self._stylesheet(sheet)) | |
61 |
|
61 | |||
62 | # pygments css |
|
62 | # pygments css | |
63 | pygments_css = HtmlFormatter().get_style_defs('.highlight') |
|
63 | pygments_css = HtmlFormatter().get_style_defs('.highlight') | |
64 | header.extend(['<meta charset="UTF-8">']) |
|
64 | header.extend(['<meta charset="UTF-8">']) | |
65 | header.extend(self.in_tag('style', pygments_css, dict(type='"text/css"'))) |
|
65 | header.extend(self.in_tag('style', pygments_css, dict(type='"text/css"'))) | |
66 |
|
66 | |||
67 | # TODO: this should be allowed to use local mathjax: |
|
67 | # TODO: this should be allowed to use local mathjax: | |
68 | header.extend(self.in_tag('script', '', {'type':'"text/javascript"', |
|
68 | header.extend(self.in_tag('script', '', {'type':'"text/javascript"', | |
69 | 'src': '"https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"', |
|
69 | 'src': '"https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"', | |
70 | })) |
|
70 | })) | |
71 | with io.open(os.path.join(here, '..', 'js', 'initmathjax.js'), |
|
71 | with io.open(os.path.join(here, '..', 'js', 'initmathjax.js'), | |
72 | encoding='utf-8') as f: |
|
72 | encoding='utf-8') as f: | |
73 | header.extend(self.in_tag('script', f.read(), |
|
73 | header.extend(self.in_tag('script', f.read(), | |
74 | {'type': '"text/javascript"'})) |
|
74 | {'type': '"text/javascript"'})) | |
75 | return header |
|
75 | return header | |
76 |
|
76 | |||
77 | def optional_header(self): |
|
77 | def optional_header(self): | |
78 | return ['<html>', '<head>'] + self.header_body() + \ |
|
78 | return ['<html>', '<head>'] + self.header_body() + \ | |
79 | ['</head>', '<body>'] |
|
79 | ['</head>', '<body>'] | |
80 |
|
80 | |||
81 | def optional_footer(self): |
|
81 | def optional_footer(self): | |
82 | return ['</body>', '</html>'] |
|
82 | return ['</body>', '</html>'] | |
83 |
|
83 | |||
84 | @text_cell |
|
84 | @text_cell | |
85 | def render_heading(self, cell): |
|
85 | def render_heading(self, cell): | |
86 | marker = cell.level |
|
86 | marker = cell.level | |
87 | return [u'<h{1}>\n {0}\n</h{1}>'.format(cell.source, marker)] |
|
87 | return [u'<h{1}>\n {0}\n</h{1}>'.format(cell.source, marker)] | |
88 |
|
88 | |||
89 | def render_code(self, cell): |
|
89 | def render_code(self, cell): | |
90 | if not cell.input: |
|
90 | if not cell.input: | |
91 | return [] |
|
91 | return [] | |
92 |
|
92 | |||
93 | lines = ['<div class="cell border-box-sizing code_cell vbox">'] |
|
93 | lines = ['<div class="cell border-box-sizing code_cell vbox">'] | |
94 |
|
94 | |||
95 | lines.append('<div class="input hbox">') |
|
95 | lines.append('<div class="input hbox">') | |
96 | n = cell.prompt_number if getattr(cell, 'prompt_number', None) is not None else ' ' |
|
96 | n = cell.prompt_number if getattr(cell, 'prompt_number', None) is not None else ' ' | |
97 |
lines.append('<div class="prompt input_prompt">In |
|
97 | lines.append('<div class="prompt input_prompt">In [%s]:</div>' % n) | |
98 | lines.append('<div class="input_area box-flex1">') |
|
98 | lines.append('<div class="input_area box-flex1">') | |
99 | lines.append(highlight(cell.input)) |
|
99 | lines.append(highlight(cell.input)) | |
100 | lines.append('</div>') # input_area |
|
100 | lines.append('</div>') # input_area | |
101 | lines.append('</div>') # input |
|
101 | lines.append('</div>') # input | |
102 |
|
102 | |||
103 | if cell.outputs: |
|
103 | if cell.outputs: | |
104 | lines.append('<div class="vbox output_wrapper">') |
|
104 | lines.append('<div class="vbox output_wrapper">') | |
105 | lines.append('<div class="output vbox">') |
|
105 | lines.append('<div class="output vbox">') | |
106 |
|
106 | |||
107 | for output in coalesce_streams(cell.outputs): |
|
107 | for output in coalesce_streams(cell.outputs): | |
108 | conv_fn = self.dispatch(output.output_type) |
|
108 | conv_fn = self.dispatch(output.output_type) | |
109 | lines.extend(conv_fn(output)) |
|
109 | lines.extend(conv_fn(output)) | |
110 |
|
110 | |||
111 | lines.append('</div>') # output |
|
111 | lines.append('</div>') # output | |
112 | lines.append('</div>') # output_wrapper |
|
112 | lines.append('</div>') # output_wrapper | |
113 |
|
113 | |||
114 | lines.append('</div>') # cell |
|
114 | lines.append('</div>') # cell | |
115 |
|
115 | |||
116 | return lines |
|
116 | return lines | |
117 |
|
117 | |||
118 | @text_cell |
|
118 | @text_cell | |
119 | def render_markdown(self, cell): |
|
119 | def render_markdown(self, cell): | |
120 | return [markdown(cell.source)] |
|
120 | return [markdown(cell.source)] | |
121 |
|
121 | |||
122 | def render_raw(self, cell): |
|
122 | def render_raw(self, cell): | |
123 | if self.raw_as_verbatim: |
|
123 | if self.raw_as_verbatim: | |
124 | return self.in_tag('pre', cell.source) |
|
124 | return self.in_tag('pre', cell.source) | |
125 | else: |
|
125 | else: | |
126 | return [cell.source] |
|
126 | return [cell.source] | |
127 |
|
127 | |||
128 | @output_container |
|
128 | @output_container | |
129 | def render_pyout(self, output): |
|
129 | def render_pyout(self, output): | |
130 | for fmt in ['html', 'latex', 'png', 'jpeg', 'svg', 'text']: |
|
130 | for fmt in ['html', 'latex', 'png', 'jpeg', 'svg', 'text']: | |
131 | if fmt in output: |
|
131 | if fmt in output: | |
132 | conv_fn = self.dispatch_display_format(fmt) |
|
132 | conv_fn = self.dispatch_display_format(fmt) | |
133 | return conv_fn(output) |
|
133 | return conv_fn(output) | |
134 | return [] |
|
134 | return [] | |
135 |
|
135 | |||
136 | render_display_data = render_pyout |
|
136 | render_display_data = render_pyout | |
137 |
|
137 | |||
138 | @output_container |
|
138 | @output_container | |
139 | def render_stream(self, output): |
|
139 | def render_stream(self, output): | |
140 | return self._ansi_colored(output.text) |
|
140 | return self._ansi_colored(output.text) | |
141 |
|
141 | |||
142 |
|
142 | |||
143 | @output_container |
|
143 | @output_container | |
144 | def render_pyerr(self, output): |
|
144 | def render_pyerr(self, output): | |
145 | # Note: a traceback is a *list* of frames. |
|
145 | # Note: a traceback is a *list* of frames. | |
146 | # lines = [] |
|
146 | # lines = [] | |
147 |
|
147 | |||
148 | # stb = |
|
148 | # stb = | |
149 | return self._ansi_colored('\n'.join(output.traceback)) |
|
149 | return self._ansi_colored('\n'.join(output.traceback)) | |
150 |
|
150 | |||
151 | def _img_lines(self, img_file): |
|
151 | def _img_lines(self, img_file): | |
152 | return ['<img src="%s">' % img_file, '</img>'] |
|
152 | return ['<img src="%s">' % img_file, '</img>'] | |
153 |
|
153 | |||
154 | def _unknown_lines(self, data): |
|
154 | def _unknown_lines(self, data): | |
155 | return ['<h2>Warning:: Unknown cell</h2>'] + self.in_tag('pre', data) |
|
155 | return ['<h2>Warning:: Unknown cell</h2>'] + self.in_tag('pre', data) | |
156 |
|
156 | |||
157 |
|
157 | |||
158 | def render_display_format_png(self, output): |
|
158 | def render_display_format_png(self, output): | |
159 | return ['<img src="data:image/png;base64,%s"></img>' % output.png] |
|
159 | return ['<img src="data:image/png;base64,%s"></img>' % output.png] | |
160 |
|
160 | |||
161 | def render_display_format_svg(self, output): |
|
161 | def render_display_format_svg(self, output): | |
162 | return [output.svg] |
|
162 | return [output.svg] | |
163 |
|
163 | |||
164 | def render_display_format_jpeg(self, output): |
|
164 | def render_display_format_jpeg(self, output): | |
165 | return ['<img src="data:image/jpeg;base64,%s"></img>' % output.jpeg] |
|
165 | return ['<img src="data:image/jpeg;base64,%s"></img>' % output.jpeg] | |
166 |
|
166 | |||
167 | def render_display_format_text(self, output): |
|
167 | def render_display_format_text(self, output): | |
168 | return self._ansi_colored(output.text) |
|
168 | return self._ansi_colored(output.text) | |
169 |
|
169 | |||
170 | def render_display_format_html(self, output): |
|
170 | def render_display_format_html(self, output): | |
171 | return [output.html] |
|
171 | return [output.html] | |
172 |
|
172 | |||
173 | def render_display_format_latex(self, output): |
|
173 | def render_display_format_latex(self, output): | |
174 | return [output.latex] |
|
174 | return [output.latex] | |
175 |
|
175 | |||
176 | def render_display_format_json(self, output): |
|
176 | def render_display_format_json(self, output): | |
177 | # html ignores json |
|
177 | # html ignores json | |
178 | return [] |
|
178 | return [] | |
179 |
|
179 | |||
180 |
|
180 | |||
181 | def render_display_format_javascript(self, output): |
|
181 | def render_display_format_javascript(self, output): | |
182 | return [output.javascript] |
|
182 | return [output.javascript] | |
183 |
|
183 |
1 | NO CONTENT: file renamed from tests/IntroNumPy.orig.ipynb to tests/ipynbref/IntroNumPy.orig.ipynb |
|
NO CONTENT: file renamed from tests/IntroNumPy.orig.ipynb to tests/ipynbref/IntroNumPy.orig.ipynb |
General Comments 0
You need to be logged in to leave comments.
Login now