Show More
@@ -1,218 +1,216 b'' | |||
|
1 | 1 | """Implements conversion to ordinary HTML output. |
|
2 | 2 | |
|
3 | 3 | This file implements a class that handles rendering IPython notebooks as |
|
4 | 4 | HTML, suitable for posting to the web. |
|
5 | 5 | |
|
6 | 6 | Converters for more specific HTML generation needs (suitable for posting to |
|
7 | 7 | a particular web service) can usefully subclass `ConverterHTML` and override |
|
8 | 8 | certain methods. For output tuned to the Blogger blogging platform, see the |
|
9 | 9 | `ConverterBloggerHTML` class. |
|
10 | 10 | """ |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Copyright (c) 2012, the IPython Development Team. |
|
13 | 13 | # |
|
14 | 14 | # Distributed under the terms of the Modified BSD License. |
|
15 | 15 | # |
|
16 | 16 | # The full license is in the file COPYING.txt, distributed with this software. |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | from __future__ import absolute_import |
|
20 | 20 | |
|
21 | 21 | # Stdlib imports |
|
22 | 22 | import io |
|
23 | 23 | import os |
|
24 | 24 | |
|
25 | 25 | # Third-party imports |
|
26 | 26 | from markdown import markdown |
|
27 | 27 | |
|
28 | 28 | # IPython imports |
|
29 | 29 | from IPython.utils import path |
|
30 | 30 | |
|
31 | 31 | # Our own imports |
|
32 | 32 | from .base import Converter |
|
33 | 33 | from .utils import text_cell, output_container |
|
34 | 34 | from .utils import highlight, coalesce_streams, ansi2html |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | #----------------------------------------------------------------------------- |
|
38 | 38 | # Class declarations |
|
39 | 39 | #----------------------------------------------------------------------------- |
|
40 | 40 | |
|
41 | 41 | class ConverterHTML(Converter): |
|
42 | 42 | #------------------------------------------------------------------------- |
|
43 | 43 | # Class-level attributes determining the behaviour of the class but |
|
44 | 44 | # probably not varying from instance to instance. |
|
45 | 45 | #------------------------------------------------------------------------- |
|
46 | 46 | extension = 'html' |
|
47 | 47 | blank_symbol = ' ' |
|
48 | 48 | |
|
49 | 49 | def in_tag(self, tag, src, attrs=None): |
|
50 | 50 | """Return a list of elements bracketed by the given tag""" |
|
51 | 51 | attr_s = '' if attrs is None else \ |
|
52 | 52 | ' '.join("%s=%s" % (attr, value) |
|
53 | 53 | for attr, value in attrs.iteritems()) |
|
54 | 54 | return ['<%s %s>' % (tag, attr_s), src, '</%s>' % tag] |
|
55 | 55 | |
|
56 | 56 | def _ansi_colored(self, text): |
|
57 | 57 | return ['<pre>%s</pre>' % ansi2html(text)] |
|
58 | 58 | |
|
59 | 59 | def _stylesheet(self, fname): |
|
60 | 60 | with io.open(fname, encoding='utf-8') as f: |
|
61 | 61 | s = f.read() |
|
62 | 62 | return self.in_tag('style', s, dict(type='"text/css"')) |
|
63 | 63 | |
|
64 | 64 | def _out_prompt(self, output): |
|
65 | 65 | if output.output_type == 'pyout': |
|
66 | 66 | content = 'Out[%s]:' % self._get_prompt_number(output) |
|
67 | 67 | else: |
|
68 | 68 | content = '' |
|
69 | 69 | return ['<div class="prompt output_prompt">%s</div>' % content] |
|
70 | 70 | |
|
71 | 71 | def header_body(self): |
|
72 | 72 | """Return the body of the header as a list of strings.""" |
|
73 | 73 | |
|
74 | 74 | from pygments.formatters import HtmlFormatter |
|
75 | 75 | |
|
76 | 76 | header = [] |
|
77 | 77 | static = os.path.join(path.get_ipython_package_dir(), |
|
78 | 78 | 'frontend', 'html', 'notebook', 'static', |
|
79 | 79 | ) |
|
80 | 80 | here = os.path.split(os.path.realpath(__file__))[0] |
|
81 | 81 | css = os.path.join(static, 'css') |
|
82 | 82 | for sheet in [ |
|
83 | 83 | # do we need jquery and prettify? |
|
84 | 84 | # os.path.join(static, 'jquery', 'css', 'themes', 'base', |
|
85 | 85 | # 'jquery-ui.min.css'), |
|
86 | 86 | # os.path.join(static, 'prettify', 'prettify.css'), |
|
87 | 87 | os.path.join(css, 'boilerplate.css'), |
|
88 |
os.path.join(css, ' |
|
|
89 | os.path.join(css, 'notebook.css'), | |
|
90 | os.path.join(css, 'renderedhtml.css'), | |
|
88 | os.path.join(css, 'style.min.css'), | |
|
91 | 89 | # our overrides: |
|
92 | 90 | os.path.join(here, '..', 'css', 'static_html.css'), |
|
93 | 91 | ]: |
|
94 | 92 | header.extend(self._stylesheet(sheet)) |
|
95 | 93 | |
|
96 | 94 | # pygments css |
|
97 | 95 | pygments_css = HtmlFormatter().get_style_defs('.highlight') |
|
98 | 96 | header.extend(['<meta charset="UTF-8">']) |
|
99 | 97 | header.extend(self.in_tag('style', pygments_css, |
|
100 | 98 | dict(type='"text/css"'))) |
|
101 | 99 | |
|
102 | 100 | # TODO: this should be allowed to use local mathjax: |
|
103 | 101 | header.extend(self.in_tag('script', '', {'type': '"text/javascript"', |
|
104 | 102 | 'src': '"https://c328740.ssl.cf1.rackcdn.com/mathjax/' |
|
105 | 103 | 'latest/MathJax.js?config=TeX-AMS_HTML"', |
|
106 | 104 | })) |
|
107 | 105 | with io.open(os.path.join(here, '..', 'js', 'initmathjax.js'), |
|
108 | 106 | encoding='utf-8') as f: |
|
109 | 107 | header.extend(self.in_tag('script', f.read(), |
|
110 | 108 | {'type': '"text/javascript"'})) |
|
111 | 109 | return header |
|
112 | 110 | |
|
113 | 111 | def optional_header(self): |
|
114 | 112 | return ['<html>', '<head>'] + self.header_body() + \ |
|
115 | 113 | ['</head>', '<body>'] |
|
116 | 114 | |
|
117 | 115 | def optional_footer(self): |
|
118 | 116 | return ['</body>', '</html>'] |
|
119 | 117 | |
|
120 | 118 | @text_cell |
|
121 | 119 | def render_heading(self, cell): |
|
122 | 120 | marker = cell.level |
|
123 | 121 | return [u'<h{1}>\n {0}\n</h{1}>'.format(cell.source, marker)] |
|
124 | 122 | |
|
125 | 123 | def render_code(self, cell): |
|
126 | 124 | if not cell.input: |
|
127 | 125 | return [] |
|
128 | 126 | |
|
129 | 127 | lines = ['<div class="cell border-box-sizing code_cell vbox">'] |
|
130 | 128 | |
|
131 | 129 | lines.append('<div class="input hbox">') |
|
132 | 130 | n = self._get_prompt_number(cell) |
|
133 | 131 | lines.append( |
|
134 | 132 | '<div class="prompt input_prompt">In [%s]:</div>' % n |
|
135 | 133 | ) |
|
136 | 134 | lines.append('<div class="input_area box-flex1">') |
|
137 | 135 | lines.append(highlight(cell.input) if self.highlight_source |
|
138 | 136 | else cell.input) |
|
139 | 137 | lines.append('</div>') # input_area |
|
140 | 138 | lines.append('</div>') # input |
|
141 | 139 | |
|
142 | 140 | if cell.outputs: |
|
143 | 141 | lines.append('<div class="vbox output_wrapper">') |
|
144 | 142 | lines.append('<div class="output vbox">') |
|
145 | 143 | |
|
146 | 144 | for output in coalesce_streams(cell.outputs): |
|
147 | 145 | conv_fn = self.dispatch(output.output_type) |
|
148 | 146 | lines.extend(conv_fn(output)) |
|
149 | 147 | |
|
150 | 148 | lines.append('</div>') # output |
|
151 | 149 | lines.append('</div>') # output_wrapper |
|
152 | 150 | |
|
153 | 151 | lines.append('</div>') # cell |
|
154 | 152 | |
|
155 | 153 | return lines |
|
156 | 154 | |
|
157 | 155 | @text_cell |
|
158 | 156 | def render_markdown(self, cell): |
|
159 | 157 | return [markdown(cell.source)] |
|
160 | 158 | |
|
161 | 159 | def render_raw(self, cell): |
|
162 | 160 | if self.raw_as_verbatim: |
|
163 | 161 | return self.in_tag('pre', cell.source) |
|
164 | 162 | else: |
|
165 | 163 | return [cell.source] |
|
166 | 164 | |
|
167 | 165 | @output_container |
|
168 | 166 | def render_pyout(self, output): |
|
169 | 167 | for fmt in ['html', 'latex', 'png', 'jpeg', 'svg', 'text']: |
|
170 | 168 | if fmt in output: |
|
171 | 169 | conv_fn = self.dispatch_display_format(fmt) |
|
172 | 170 | return conv_fn(output) |
|
173 | 171 | return [] |
|
174 | 172 | |
|
175 | 173 | render_display_data = render_pyout |
|
176 | 174 | |
|
177 | 175 | @output_container |
|
178 | 176 | def render_stream(self, output): |
|
179 | 177 | return self._ansi_colored(output.text) |
|
180 | 178 | |
|
181 | 179 | @output_container |
|
182 | 180 | def render_pyerr(self, output): |
|
183 | 181 | # Note: a traceback is a *list* of frames. |
|
184 | 182 | # lines = [] |
|
185 | 183 | |
|
186 | 184 | # stb = |
|
187 | 185 | return self._ansi_colored('\n'.join(output.traceback)) |
|
188 | 186 | |
|
189 | 187 | def _img_lines(self, img_file): |
|
190 | 188 | return ['<img src="%s">' % img_file, '</img>'] |
|
191 | 189 | |
|
192 | 190 | def _unknown_lines(self, data): |
|
193 | 191 | return ['<h2>Warning:: Unknown cell</h2>'] + self.in_tag('pre', data) |
|
194 | 192 | |
|
195 | 193 | def render_display_format_png(self, output): |
|
196 | 194 | return ['<img src="data:image/png;base64,%s"></img>' % output.png] |
|
197 | 195 | |
|
198 | 196 | def render_display_format_svg(self, output): |
|
199 | 197 | return [output.svg] |
|
200 | 198 | |
|
201 | 199 | def render_display_format_jpeg(self, output): |
|
202 | 200 | return ['<img src="data:image/jpeg;base64,%s"></img>' % output.jpeg] |
|
203 | 201 | |
|
204 | 202 | def render_display_format_text(self, output): |
|
205 | 203 | return self._ansi_colored(output.text) |
|
206 | 204 | |
|
207 | 205 | def render_display_format_html(self, output): |
|
208 | 206 | return [output.html] |
|
209 | 207 | |
|
210 | 208 | def render_display_format_latex(self, output): |
|
211 | 209 | return [output.latex] |
|
212 | 210 | |
|
213 | 211 | def render_display_format_json(self, output): |
|
214 | 212 | # html ignores json |
|
215 | 213 | return [] |
|
216 | 214 | |
|
217 | 215 | def render_display_format_javascript(self, output): |
|
218 | 216 | return [output.javascript] |
General Comments 0
You need to be logged in to leave comments.
Login now