##// END OF EJS Templates
Fixed bad variable names in nbconvert.py and html.py.
Ivan Djokic -
Show More
@@ -1,185 +1,186 b''
1 1 from __future__ import absolute_import
2 2
3 3 from converters.base import Converter
4 4 from converters.utils import text_cell, output_container
5 5 from converters.utils import highlight, coalesce_streams, ansi2html
6 6
7 7 from IPython.utils import path
8 8 from markdown import markdown
9 9 import os
10 10 import io
11 11
12 12
13 13 class ConverterHTML(Converter):
14 14 extension = 'html'
15 15 blank_symbol = ' '
16 16
17 17 def in_tag(self, tag, src, attrs=None):
18 18 """Return a list of elements bracketed by the given tag"""
19 19 attr_s = '' if attrs is None else \
20 20 ' '.join("%s=%s" % (attr, value)
21 21 for attr, value in attrs.iteritems())
22 22 return ['<%s %s>' % (tag, attr_s), src, '</%s>' % tag]
23 23
24 24 def _ansi_colored(self, text):
25 25 return ['<pre>%s</pre>' % ansi2html(text)]
26 26
27 27 def _stylesheet(self, fname):
28 28 with io.open(fname, encoding='utf-8') as f:
29 29 s = f.read()
30 30 return self.in_tag('style', s, dict(type='"text/css"'))
31 31
32 32 def _out_prompt(self, output):
33 33 if output.output_type == 'pyout':
34 34 content = 'Out[%s]:' % self._get_prompt_number(output)
35 35 else:
36 36 content = ''
37 37 return ['<div class="prompt output_prompt">%s</div>' % content]
38 38
39 39 def header_body(self):
40 40 """Return the body of the header as a list of strings."""
41 41
42 42 from pygments.formatters import HtmlFormatter
43 43
44 44 header = []
45 45 static = os.path.join(path.get_ipython_package_dir(),
46 46 'frontend', 'html', 'notebook', 'static',
47 47 )
48 48 here = os.path.split(os.path.realpath(__file__))[0]
49 49 css = os.path.join(static, 'css')
50 50 for sheet in [
51 51 # do we need jquery and prettify?
52 52 # os.path.join(static, 'jquery', 'css', 'themes', 'base',
53 53 # 'jquery-ui.min.css'),
54 54 # os.path.join(static, 'prettify', 'prettify.css'),
55 55 os.path.join(css, 'boilerplate.css'),
56 56 os.path.join(css, 'fbm.css'),
57 57 os.path.join(css, 'notebook.css'),
58 58 os.path.join(css, 'renderedhtml.css'),
59 59 # our overrides:
60 60 os.path.join(here, '..', 'css', 'static_html.css'),
61 61 ]:
62 62 header.extend(self._stylesheet(sheet))
63 63
64 64 # pygments css
65 65 pygments_css = HtmlFormatter().get_style_defs('.highlight')
66 66 header.extend(['<meta charset="UTF-8">'])
67 67 header.extend(self.in_tag('style', pygments_css,
68 68 dict(type='"text/css"')))
69 69
70 70 # TODO: this should be allowed to use local mathjax:
71 71 header.extend(self.in_tag('script', '', {'type': '"text/javascript"',
72 72 'src': '"https://c328740.ssl.cf1.rackcdn.com/mathjax/'
73 73 'latest/MathJax.js?config=TeX-AMS_HTML"',
74 74 }))
75 75 with io.open(os.path.join(here, '..', 'js', 'initmathjax.js'),
76 76 encoding='utf-8') as f:
77 77 header.extend(self.in_tag('script', f.read(),
78 78 {'type': '"text/javascript"'}))
79 79 return header
80 80
81 81 def optional_header(self):
82 82 return ['<html>', '<head>'] + self.header_body() + \
83 83 ['</head>', '<body>']
84 84
85 85 def optional_footer(self):
86 86 return ['</body>', '</html>']
87 87
88 88 @text_cell
89 89 def render_heading(self, cell):
90 90 marker = cell.level
91 91 return [u'<h{1}>\n {0}\n</h{1}>'.format(cell.source, marker)]
92 92
93 93 def render_code(self, cell):
94 94 if not cell.input:
95 95 return []
96 96
97 97 lines = ['<div class="cell border-box-sizing code_cell vbox">']
98 98
99 99 lines.append('<div class="input hbox">')
100 100 n = self._get_prompt_number(cell)
101 101 lines.append(
102 102 '<div class="prompt input_prompt">In&nbsp;[%s]:</div>' % n
103 103 )
104 104 lines.append('<div class="input_area box-flex1">')
105 lines.append(highlight(cell.input) if self.highlight else cell.input)
105 lines.append(highlight(cell.input) if self.highlight_code
106 else cell.input)
106 107 lines.append('</div>') # input_area
107 108 lines.append('</div>') # input
108 109
109 110 if cell.outputs:
110 111 lines.append('<div class="vbox output_wrapper">')
111 112 lines.append('<div class="output vbox">')
112 113
113 114 for output in coalesce_streams(cell.outputs):
114 115 conv_fn = self.dispatch(output.output_type)
115 116 lines.extend(conv_fn(output))
116 117
117 118 lines.append('</div>') # output
118 119 lines.append('</div>') # output_wrapper
119 120
120 121 lines.append('</div>') # cell
121 122
122 123 return lines
123 124
124 125 @text_cell
125 126 def render_markdown(self, cell):
126 127 return [markdown(cell.source)]
127 128
128 129 def render_raw(self, cell):
129 130 if self.raw_as_verbatim:
130 131 return self.in_tag('pre', cell.source)
131 132 else:
132 133 return [cell.source]
133 134
134 135 @output_container
135 136 def render_pyout(self, output):
136 137 for fmt in ['html', 'latex', 'png', 'jpeg', 'svg', 'text']:
137 138 if fmt in output:
138 139 conv_fn = self.dispatch_display_format(fmt)
139 140 return conv_fn(output)
140 141 return []
141 142
142 143 render_display_data = render_pyout
143 144
144 145 @output_container
145 146 def render_stream(self, output):
146 147 return self._ansi_colored(output.text)
147 148
148 149 @output_container
149 150 def render_pyerr(self, output):
150 151 # Note: a traceback is a *list* of frames.
151 152 # lines = []
152 153
153 154 # stb =
154 155 return self._ansi_colored('\n'.join(output.traceback))
155 156
156 157 def _img_lines(self, img_file):
157 158 return ['<img src="%s">' % img_file, '</img>']
158 159
159 160 def _unknown_lines(self, data):
160 161 return ['<h2>Warning:: Unknown cell</h2>'] + self.in_tag('pre', data)
161 162
162 163 def render_display_format_png(self, output):
163 164 return ['<img src="data:image/png;base64,%s"></img>' % output.png]
164 165
165 166 def render_display_format_svg(self, output):
166 167 return [output.svg]
167 168
168 169 def render_display_format_jpeg(self, output):
169 170 return ['<img src="data:image/jpeg;base64,%s"></img>' % output.jpeg]
170 171
171 172 def render_display_format_text(self, output):
172 173 return self._ansi_colored(output.text)
173 174
174 175 def render_display_format_html(self, output):
175 176 return [output.html]
176 177
177 178 def render_display_format_latex(self, output):
178 179 return [output.latex]
179 180
180 181 def render_display_format_json(self, output):
181 182 # html ignores json
182 183 return []
183 184
184 185 def render_display_format_javascript(self, output):
185 186 return [output.javascript]
@@ -1,87 +1,87 b''
1 1 #!/usr/bin/env python
2 2 """Convert IPython notebooks to other formats, such as ReST, and HTML.
3 3
4 4 Example:
5 5 ./nbconvert.py --format rst file.ipynb
6 6
7 7 Produces 'file.rst', along with auto-generated figure files
8 8 called nb_figure_NN.png.
9 9 """
10 10 #-----------------------------------------------------------------------------
11 11 # Imports
12 12 #-----------------------------------------------------------------------------
13 13 from __future__ import print_function
14 14
15 15 # From IPython
16 16 from IPython.external import argparse
17 17
18 18 # local
19 19 from converters.html import ConverterHTML
20 20 from converters.markdown import ConverterMarkdown
21 21 from converters.bloggerhtml import ConverterBloggerHTML
22 22 from converters.rst import ConverterRST
23 23 from converters.latex import ConverterLaTeX
24 24 from converters.python import ConverterPy
25 25
26 26
27 27 # When adding a new format, make sure to add it to the `converters`
28 28 # dictionary below. This is used to create the list of known formats,
29 29 # which gets printed in case an unknown format is encounteres, as well
30 30 # as in the help
31 31
32 32 converters = {
33 33 'rst': ConverterRST,
34 34 'markdown': ConverterMarkdown,
35 35 'html': ConverterHTML,
36 36 'blogger-html': ConverterBloggerHTML,
37 37 'latex': ConverterLaTeX,
38 38 'py': ConverterPy,
39 39 }
40 40
41 41 default_format = 'rst'
42 42
43 43 # Extract the list of known formats and mark the first format as the default.
44 44 known_formats = ', '.join([key + " (default)" if key == default_format else key
45 45 for key in converters])
46 46
47 47
48 48 def main(infile, format='rst', preamble=None, exclude=None,
49 49 highlight_code=True):
50 50 """Convert a notebook to html in one step"""
51 51 try:
52 52 ConverterClass = converters[format]
53 53 except KeyError:
54 54 raise SystemExit("Unknown format '%s', " % format +
55 55 "known formats are: " + known_formats)
56 56
57 57 converter = ConverterClass(infile, highlight_code)
58 58 converter.render()
59 59
60 60 #-----------------------------------------------------------------------------
61 61 # Script main
62 62 #-----------------------------------------------------------------------------
63 63
64 64 if __name__ == '__main__':
65 65 parser = argparse.ArgumentParser(description=__doc__,
66 66 formatter_class=argparse.RawTextHelpFormatter)
67 67 # TODO: consider passing file like object around, rather than filenames
68 68 # would allow us to process stdin, or even http streams
69 69 #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
70 70 # default=sys.stdin)
71 71
72 72 #Require a filename as a positional argument
73 73 parser.add_argument('infile', nargs=1)
74 74 parser.add_argument('-f', '--format', default='rst',
75 75 help='Output format. Supported formats: \n' +
76 76 known_formats)
77 77 parser.add_argument('-p', '--preamble',
78 78 help='Path to a user-specified preamble file')
79 79 parser.add_argument('-e', '--exclude', default='',
80 80 help='Comma-separated list of cells to exclude')
81 81 parser.add_argument('-H', '--no-highlighting', action='store_false',
82 82 help='Disable syntax highlighting for code blocks.')
83 83 args = parser.parse_args()
84 84 exclude_cells = [s.strip() for s in args.exclude.split(',')]
85 85
86 86 main(infile=args.infile[0], format=args.format, preamble=args.preamble,
87 exclude=exclude_cells, highlight_code=args.plain_output)
87 exclude=exclude_cells, highlight_code=args.no_highlighting)
General Comments 0
You need to be logged in to leave comments. Login now