Show More
@@ -9,16 +9,17 b' from markdown import markdown' | |||||
9 | import os |
|
9 | import os | |
10 | import io |
|
10 | import io | |
11 |
|
11 | |||
|
12 | ||||
12 | class ConverterHTML(Converter): |
|
13 | class ConverterHTML(Converter): | |
13 | extension = 'html' |
|
14 | extension = 'html' | |
14 |
|
15 | |||
15 | def in_tag(self, tag, src, attrs=None): |
|
16 | def in_tag(self, tag, src, attrs=None): | |
16 | """Return a list of elements bracketed by the given tag""" |
|
17 | """Return a list of elements bracketed by the given tag""" | |
17 | attr_s = '' if attrs is None else \ |
|
18 | attr_s = '' if attrs is None else \ | |
18 |
' '.join( |
|
19 | ' '.join("%s=%s" % (attr, value) | |
19 |
|
|
20 | for attr, value in attrs.iteritems()) | |
20 | return ['<%s %s>' % (tag, attr_s), src, '</%s>' % tag] |
|
21 | return ['<%s %s>' % (tag, attr_s), src, '</%s>' % tag] | |
21 |
|
22 | |||
22 | def _ansi_colored(self, text): |
|
23 | def _ansi_colored(self, text): | |
23 | return ['<pre>%s</pre>' % ansi2html(text)] |
|
24 | return ['<pre>%s</pre>' % ansi2html(text)] | |
24 |
|
25 | |||
@@ -29,7 +30,8 b' class ConverterHTML(Converter):' | |||||
29 |
|
30 | |||
30 | def _out_prompt(self, output): |
|
31 | def _out_prompt(self, output): | |
31 | if output.output_type == 'pyout': |
|
32 | if output.output_type == 'pyout': | |
32 |
n = output.prompt_number if output.prompt_number is not None |
|
33 | n = (output.prompt_number if output.prompt_number is not None | |
|
34 | else ' ') | |||
33 | content = 'Out[%s]:' % n |
|
35 | content = 'Out[%s]:' % n | |
34 | else: |
|
36 | else: | |
35 | content = '' |
|
37 | content = '' | |
@@ -37,7 +39,7 b' class ConverterHTML(Converter):' | |||||
37 |
|
39 | |||
38 | def header_body(self): |
|
40 | def header_body(self): | |
39 | """Return the body of the header as a list of strings.""" |
|
41 | """Return the body of the header as a list of strings.""" | |
40 |
|
42 | |||
41 | from pygments.formatters import HtmlFormatter |
|
43 | from pygments.formatters import HtmlFormatter | |
42 |
|
44 | |||
43 | header = [] |
|
45 | header = [] | |
@@ -48,29 +50,32 b' class ConverterHTML(Converter):' | |||||
48 | css = os.path.join(static, 'css') |
|
50 | css = os.path.join(static, 'css') | |
49 | for sheet in [ |
|
51 | for sheet in [ | |
50 | # do we need jquery and prettify? |
|
52 | # do we need jquery and prettify? | |
51 |
# os.path.join(static, 'jquery', 'css', 'themes', 'base', |
|
53 | # os.path.join(static, 'jquery', 'css', 'themes', 'base', | |
|
54 | # 'jquery-ui.min.css'), | |||
52 | # os.path.join(static, 'prettify', 'prettify.css'), |
|
55 | # os.path.join(static, 'prettify', 'prettify.css'), | |
53 | os.path.join(css, 'boilerplate.css'), |
|
56 | os.path.join(css, 'boilerplate.css'), | |
54 | os.path.join(css, 'fbm.css'), |
|
57 | os.path.join(css, 'fbm.css'), | |
55 | os.path.join(css, 'notebook.css'), |
|
58 | os.path.join(css, 'notebook.css'), | |
56 | os.path.join(css, 'renderedhtml.css'), |
|
59 | os.path.join(css, 'renderedhtml.css'), | |
57 | # our overrides: |
|
60 | # our overrides: | |
58 | os.path.join(here, '..','css', 'static_html.css'), |
|
61 | os.path.join(here, '..', 'css', 'static_html.css'), | |
59 | ]: |
|
62 | ]: | |
60 | header.extend(self._stylesheet(sheet)) |
|
63 | header.extend(self._stylesheet(sheet)) | |
61 |
|
64 | |||
62 | # pygments css |
|
65 | # pygments css | |
63 | pygments_css = HtmlFormatter().get_style_defs('.highlight') |
|
66 | pygments_css = HtmlFormatter().get_style_defs('.highlight') | |
64 | header.extend(['<meta charset="UTF-8">']) |
|
67 | header.extend(['<meta charset="UTF-8">']) | |
65 |
header.extend(self.in_tag('style', pygments_css, |
|
68 | header.extend(self.in_tag('style', pygments_css, | |
66 |
|
69 | dict(type='"text/css"'))) | ||
|
70 | ||||
67 | # TODO: this should be allowed to use local mathjax: |
|
71 | # TODO: this should be allowed to use local mathjax: | |
68 | header.extend(self.in_tag('script', '', {'type':'"text/javascript"', |
|
72 | header.extend(self.in_tag('script', '', {'type': '"text/javascript"', | |
69 |
'src': '"https://c328740.ssl.cf1.rackcdn.com/mathjax/ |
|
73 | 'src': '"https://c328740.ssl.cf1.rackcdn.com/mathjax/' | |
|
74 | 'latest/MathJax.js?config=TeX-AMS_HTML"', | |||
70 | })) |
|
75 | })) | |
71 |
with io.open(os.path.join(here, '..', 'js', 'initmathjax.js'), |
|
76 | with io.open(os.path.join(here, '..', 'js', 'initmathjax.js'), | |
72 | encoding='utf-8') as f: |
|
77 | encoding='utf-8') as f: | |
73 |
header.extend(self.in_tag('script', f.read(), |
|
78 | header.extend(self.in_tag('script', f.read(), | |
74 | {'type': '"text/javascript"'})) |
|
79 | {'type': '"text/javascript"'})) | |
75 | return header |
|
80 | return header | |
76 |
|
81 | |||
@@ -85,34 +90,38 b' class ConverterHTML(Converter):' | |||||
85 | def render_heading(self, cell): |
|
90 | def render_heading(self, cell): | |
86 | marker = cell.level |
|
91 | marker = cell.level | |
87 | return [u'<h{1}>\n {0}\n</h{1}>'.format(cell.source, marker)] |
|
92 | return [u'<h{1}>\n {0}\n</h{1}>'.format(cell.source, marker)] | |
88 |
|
93 | |||
89 | def render_code(self, cell): |
|
94 | def render_code(self, cell): | |
90 | if not cell.input: |
|
95 | if not cell.input: | |
91 | return [] |
|
96 | return [] | |
92 |
|
97 | |||
93 | lines = ['<div class="cell border-box-sizing code_cell vbox">'] |
|
98 | lines = ['<div class="cell border-box-sizing code_cell vbox">'] | |
94 |
|
99 | |||
95 | lines.append('<div class="input hbox">') |
|
100 | lines.append('<div class="input hbox">') | |
96 | n = cell.prompt_number if getattr(cell, 'prompt_number', None) is not None else ' ' |
|
101 | n = (cell.prompt_number | |
97 | lines.append('<div class="prompt input_prompt">In [%s]:</div>' % n) |
|
102 | if getattr(cell, 'prompt_number', None) is not None | |
|
103 | else ' ') | |||
|
104 | lines.append( | |||
|
105 | '<div class="prompt input_prompt">In [%s]:</div>' % n | |||
|
106 | ) | |||
98 | lines.append('<div class="input_area box-flex1">') |
|
107 | lines.append('<div class="input_area box-flex1">') | |
99 | lines.append(highlight(cell.input)) |
|
108 | lines.append(highlight(cell.input)) | |
100 | lines.append('</div>') # input_area |
|
109 | lines.append('</div>') # input_area | |
101 | lines.append('</div>') # input |
|
110 | lines.append('</div>') # input | |
102 |
|
111 | |||
103 | if cell.outputs: |
|
112 | if cell.outputs: | |
104 | lines.append('<div class="vbox output_wrapper">') |
|
113 | lines.append('<div class="vbox output_wrapper">') | |
105 | lines.append('<div class="output vbox">') |
|
114 | lines.append('<div class="output vbox">') | |
106 |
|
115 | |||
107 | for output in coalesce_streams(cell.outputs): |
|
116 | for output in coalesce_streams(cell.outputs): | |
108 | conv_fn = self.dispatch(output.output_type) |
|
117 | conv_fn = self.dispatch(output.output_type) | |
109 | lines.extend(conv_fn(output)) |
|
118 | lines.extend(conv_fn(output)) | |
110 |
|
119 | |||
111 | lines.append('</div>') # output |
|
120 | lines.append('</div>') # output | |
112 | lines.append('</div>') # output_wrapper |
|
121 | lines.append('</div>') # output_wrapper | |
113 |
|
122 | |||
114 | lines.append('</div>') # cell |
|
123 | lines.append('</div>') # cell | |
115 |
|
124 | |||
116 | return lines |
|
125 | return lines | |
117 |
|
126 | |||
118 | @text_cell |
|
127 | @text_cell | |
@@ -138,14 +147,13 b' class ConverterHTML(Converter):' | |||||
138 | @output_container |
|
147 | @output_container | |
139 | def render_stream(self, output): |
|
148 | def render_stream(self, output): | |
140 | return self._ansi_colored(output.text) |
|
149 | return self._ansi_colored(output.text) | |
141 |
|
||||
142 |
|
150 | |||
143 | @output_container |
|
151 | @output_container | |
144 | def render_pyerr(self, output): |
|
152 | def render_pyerr(self, output): | |
145 | # Note: a traceback is a *list* of frames. |
|
153 | # Note: a traceback is a *list* of frames. | |
146 | # lines = [] |
|
154 | # lines = [] | |
147 |
|
155 | |||
148 |
# stb = |
|
156 | # stb = | |
149 | return self._ansi_colored('\n'.join(output.traceback)) |
|
157 | return self._ansi_colored('\n'.join(output.traceback)) | |
150 |
|
158 | |||
151 | def _img_lines(self, img_file): |
|
159 | def _img_lines(self, img_file): | |
@@ -154,7 +162,6 b' class ConverterHTML(Converter):' | |||||
154 | def _unknown_lines(self, data): |
|
162 | def _unknown_lines(self, data): | |
155 | return ['<h2>Warning:: Unknown cell</h2>'] + self.in_tag('pre', data) |
|
163 | return ['<h2>Warning:: Unknown cell</h2>'] + self.in_tag('pre', data) | |
156 |
|
164 | |||
157 |
|
||||
158 | def render_display_format_png(self, output): |
|
165 | def render_display_format_png(self, output): | |
159 | return ['<img src="data:image/png;base64,%s"></img>' % output.png] |
|
166 | return ['<img src="data:image/png;base64,%s"></img>' % output.png] | |
160 |
|
167 | |||
@@ -177,7 +184,5 b' class ConverterHTML(Converter):' | |||||
177 | # html ignores json |
|
184 | # html ignores json | |
178 | return [] |
|
185 | return [] | |
179 |
|
186 | |||
180 |
|
||||
181 | def render_display_format_javascript(self, output): |
|
187 | def render_display_format_javascript(self, output): | |
182 | return [output.javascript] |
|
188 | return [output.javascript] | |
183 |
|
@@ -1,8 +1,8 b'' | |||||
1 | from converters.base import Converter |
|
1 | from converters.base import Converter | |
2 | from converters.utils import markdown2latex, remove_ansi |
|
2 | from converters.utils import markdown2latex, remove_ansi | |
3 |
import os |
|
3 | import os | |
4 | import subprocess |
|
4 | import subprocess | |
5 |
|
5 | import sys | ||
6 |
|
6 | |||
7 | inkscape = 'inkscape' |
|
7 | inkscape = 'inkscape' | |
8 | if sys.platform == 'darwin': |
|
8 | if sys.platform == 'darwin': | |
@@ -10,6 +10,7 b" if sys.platform == 'darwin':" | |||||
10 | if not os.path.exists(inkscape): |
|
10 | if not os.path.exists(inkscape): | |
11 | inkscape = None |
|
11 | inkscape = None | |
12 |
|
12 | |||
|
13 | ||||
13 | class ConverterLaTeX(Converter): |
|
14 | class ConverterLaTeX(Converter): | |
14 | """Converts a notebook to a .tex file suitable for pdflatex. |
|
15 | """Converts a notebook to a .tex file suitable for pdflatex. | |
15 |
|
16 | |||
@@ -17,13 +18,13 b' class ConverterLaTeX(Converter):' | |||||
17 |
|
18 | |||
18 | - `pandoc`: for all conversion of markdown cells. If your notebook only |
|
19 | - `pandoc`: for all conversion of markdown cells. If your notebook only | |
19 | has Raw cells, pandoc will not be needed. |
|
20 | has Raw cells, pandoc will not be needed. | |
20 |
|
21 | |||
21 | - `inkscape`: if your notebook has SVG figures. These need to be |
|
22 | - `inkscape`: if your notebook has SVG figures. These need to be | |
22 | converted to PDF before inclusion in the TeX file, as LaTeX doesn't |
|
23 | converted to PDF before inclusion in the TeX file, as LaTeX doesn't | |
23 | understand SVG natively. |
|
24 | understand SVG natively. | |
24 |
|
25 | |||
25 | You will in general obtain much better final PDF results if you configure |
|
26 | You will in general obtain much better final PDF results if you configure | |
26 |
the matplotlib backend to create SVG output with |
|
27 | the matplotlib backend to create SVG output with | |
27 |
|
28 | |||
28 | %config InlineBackend.figure_format = 'svg' |
|
29 | %config InlineBackend.figure_format = 'svg' | |
29 |
|
30 | |||
@@ -57,47 +58,48 b' class ConverterLaTeX(Converter):' | |||||
57 | out.append(ur'\end{%s}' % environment) |
|
58 | out.append(ur'\end{%s}' % environment) | |
58 | return out |
|
59 | return out | |
59 |
|
60 | |||
60 | def convert(self, *kwargs): |
|
61 | def convert(self, *args, **kwargs): | |
61 | # The main body is done by the logic in the parent class, and that's |
|
62 | # The main body is done by the logic in the parent class, and that's | |
62 | # all we need if preamble support has been turned off. |
|
63 | # all we need if preamble support has been turned off. | |
63 | body = super(ConverterLaTeX, self).convert(*kwargs) |
|
64 | body = super(ConverterLaTeX, self).convert(*args, **kwargs) | |
64 | if not self.with_preamble: |
|
65 | if not self.with_preamble: | |
65 | return body |
|
66 | return body | |
66 | # But if preamble is on, then we need to construct a proper, standalone |
|
67 | # But if preamble is on, then we need to construct a proper, standalone | |
67 | # tex file. |
|
68 | # tex file. | |
68 |
|
69 | |||
69 | # Tag the document at the top and set latex class |
|
70 | # Tag the document at the top and set latex class | |
70 |
final = [ |
|
71 | final = [r'%% This file was auto-generated by IPython, do NOT edit', | |
71 |
|
|
72 | r'%% Conversion from the original notebook file:', | |
72 |
|
|
73 | r'%% {0}'.format(self.infile), | |
73 |
|
|
74 | r'%%', | |
74 |
|
|
75 | r'\documentclass[%s]{%s}' % (self.documentclass_options, | |
75 |
|
|
76 | self.documentclass), | |
76 |
|
|
77 | '', | |
77 | ] |
|
78 | ] | |
78 | # Load our own preamble, which is stored next to the main file. We |
|
79 | # Load our own preamble, which is stored next to the main file. We | |
79 | # need to be careful in case the script entry point is a symlink |
|
80 | # need to be careful in case the script entry point is a symlink | |
80 | myfile = os.path.realpath(__file__) |
|
81 | myfile = os.path.realpath(__file__) | |
81 | with open(os.path.join(os.path.dirname(myfile), '../preamble.tex')) as f: |
|
82 | preamble = '../preamble.tex' | |
|
83 | with open(os.path.join(os.path.dirname(myfile), preamble)) as f: | |||
82 | final.append(f.read()) |
|
84 | final.append(f.read()) | |
83 |
|
85 | |||
84 | # Load any additional user-supplied preamble |
|
86 | # Load any additional user-supplied preamble | |
85 | if self.user_preamble: |
|
87 | if self.user_preamble: | |
86 | final.extend(['', '%% Adding user preamble from file:', |
|
88 | final.extend(['', '%% Adding user preamble from file:', | |
87 | '%% {0}'.format(self.user_preamble), '']) |
|
89 | '%% {0}'.format(self.user_preamble), '']) | |
88 | with open(self.user_preamble) as f: |
|
90 | with open(self.user_preamble) as f: | |
89 | final.append(f.read()) |
|
91 | final.append(f.read()) | |
90 |
|
92 | |||
91 | # Include document body |
|
93 | # Include document body | |
92 |
final.extend([ |
|
94 | final.extend([r'\begin{document}', '', | |
93 |
|
|
95 | body, | |
94 |
|
|
96 | r'\end{document}', '']) | |
95 | # Retun value must be a string |
|
97 | # Retun value must be a string | |
96 | return '\n'.join(final) |
|
98 | return '\n'.join(final) | |
97 |
|
99 | |||
98 | def render_heading(self, cell): |
|
100 | def render_heading(self, cell): | |
99 | marker = self.heading_map[cell.level] |
|
101 | marker = self.heading_map[cell.level] | |
100 |
return ['%s{%s}' % (marker, cell.source) |
|
102 | return ['%s{%s}' % (marker, cell.source)] | |
101 |
|
103 | |||
102 | def render_code(self, cell): |
|
104 | def render_code(self, cell): | |
103 | if not cell.input: |
|
105 | if not cell.input: | |
@@ -105,7 +107,7 b' class ConverterLaTeX(Converter):' | |||||
105 |
|
107 | |||
106 | # Cell codes first carry input code, we use lstlisting for that |
|
108 | # Cell codes first carry input code, we use lstlisting for that | |
107 | lines = [ur'\begin{codecell}'] |
|
109 | lines = [ur'\begin{codecell}'] | |
108 |
|
110 | |||
109 | lines.extend(self.in_env('codeinput', |
|
111 | lines.extend(self.in_env('codeinput', | |
110 | self.in_env('lstlisting', cell.input))) |
|
112 | self.in_env('lstlisting', cell.input))) | |
111 |
|
113 | |||
@@ -122,7 +124,6 b' class ConverterLaTeX(Converter):' | |||||
122 |
|
124 | |||
123 | return lines |
|
125 | return lines | |
124 |
|
126 | |||
125 |
|
||||
126 | def _img_lines(self, img_file): |
|
127 | def _img_lines(self, img_file): | |
127 | return self.in_env('center', |
|
128 | return self.in_env('center', | |
128 | [r'\includegraphics[width=6in]{%s}' % img_file, r'\par']) |
|
129 | [r'\includegraphics[width=6in]{%s}' % img_file, r'\par']) | |
@@ -130,13 +131,13 b' class ConverterLaTeX(Converter):' | |||||
130 | def _svg_lines(self, img_file): |
|
131 | def _svg_lines(self, img_file): | |
131 | base_file = os.path.splitext(img_file)[0] |
|
132 | base_file = os.path.splitext(img_file)[0] | |
132 | pdf_file = base_file + '.pdf' |
|
133 | pdf_file = base_file + '.pdf' | |
133 |
subprocess.check_call([ |
|
134 | subprocess.check_call([self.inkscape, '--export-pdf=%s' % pdf_file, | |
134 | img_file]) |
|
135 | img_file]) | |
135 | return self._img_lines(pdf_file) |
|
136 | return self._img_lines(pdf_file) | |
136 |
|
137 | |||
137 | def render_markdown(self, cell): |
|
138 | def render_markdown(self, cell): | |
138 | return [markdown2latex(cell.source)] |
|
139 | return [markdown2latex(cell.source)] | |
139 |
|
140 | |||
140 | def render_pyout(self, output): |
|
141 | def render_pyout(self, output): | |
141 | lines = [] |
|
142 | lines = [] | |
142 |
|
143 | |||
@@ -152,7 +153,7 b' class ConverterLaTeX(Converter):' | |||||
152 | def render_pyerr(self, output): |
|
153 | def render_pyerr(self, output): | |
153 | # Note: a traceback is a *list* of frames. |
|
154 | # Note: a traceback is a *list* of frames. | |
154 | return self.in_env('traceback', |
|
155 | return self.in_env('traceback', | |
155 |
self.in_env('verbatim', |
|
156 | self.in_env('verbatim', | |
156 | remove_ansi('\n'.join(output.traceback)))) |
|
157 | remove_ansi('\n'.join(output.traceback)))) | |
157 |
|
158 | |||
158 | def render_raw(self, cell): |
|
159 | def render_raw(self, cell): | |
@@ -165,7 +166,6 b' class ConverterLaTeX(Converter):' | |||||
165 | return [r'{\vspace{5mm}\bf WARNING:: unknown cell:}'] + \ |
|
166 | return [r'{\vspace{5mm}\bf WARNING:: unknown cell:}'] + \ | |
166 | self.in_env('verbatim', data) |
|
167 | self.in_env('verbatim', data) | |
167 |
|
168 | |||
168 |
|
||||
169 | def render_display_format_text(self, output): |
|
169 | def render_display_format_text(self, output): | |
170 | lines = [] |
|
170 | lines = [] | |
171 |
|
171 | |||
@@ -186,7 +186,6 b' class ConverterLaTeX(Converter):' | |||||
186 | # latex ignores json |
|
186 | # latex ignores json | |
187 | return [] |
|
187 | return [] | |
188 |
|
188 | |||
189 |
|
||||
190 | def render_display_format_javascript(self, output): |
|
189 | def render_display_format_javascript(self, output): | |
191 | # latex ignores javascript |
|
190 | # latex ignores javascript | |
192 | return [] |
|
191 | return [] |
@@ -17,7 +17,8 b' class IPythonLexer(PythonLexer):' | |||||
17 | filenames = ['*.ipy'] |
|
17 | filenames = ['*.ipy'] | |
18 | tokens = PythonLexer.tokens.copy() |
|
18 | tokens = PythonLexer.tokens.copy() | |
19 | tokens['root'] = [ |
|
19 | tokens['root'] = [ | |
20 |
(r'(\%+)(\w+)\s+(\.*)(\n)', bygroups(Operator, Keyword, |
|
20 | (r'(\%+)(\w+)\s+(\.*)(\n)', bygroups(Operator, Keyword, | |
|
21 | using(BashLexer), Text)), | |||
21 | (r'(\%+)(\w+)\b', bygroups(Operator, Keyword)), |
|
22 | (r'(\%+)(\w+)\b', bygroups(Operator, Keyword)), | |
22 | (r'^(!)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)), |
|
23 | (r'^(!)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)), | |
23 | ] + tokens['root'] |
|
24 | ] + tokens['root'] |
@@ -2,6 +2,7 b' from converters.base import Converter' | |||||
2 | from converters.utils import highlight, remove_ansi |
|
2 | from converters.utils import highlight, remove_ansi | |
3 | from IPython.utils.text import indent |
|
3 | from IPython.utils.text import indent | |
4 |
|
4 | |||
|
5 | ||||
5 | class ConverterMarkdown(Converter): |
|
6 | class ConverterMarkdown(Converter): | |
6 | extension = 'md' |
|
7 | extension = 'md' | |
7 |
|
8 | |||
@@ -13,7 +14,7 b' class ConverterMarkdown(Converter):' | |||||
13 | self.inline_prompt = inline_prompt |
|
14 | self.inline_prompt = inline_prompt | |
14 |
|
15 | |||
15 | def render_heading(self, cell): |
|
16 | def render_heading(self, cell): | |
16 | return ['{0} {1}'.format('#'*cell.level, cell.source), ''] |
|
17 | return ['{0} {1}'.format('#' * cell.level, cell.source), ''] | |
17 |
|
18 | |||
18 | def render_code(self, cell): |
|
19 | def render_code(self, cell): | |
19 | if not cell.input: |
|
20 | if not cell.input: | |
@@ -24,7 +25,8 b' class ConverterMarkdown(Converter):' | |||||
24 | if self.show_prompts and self.inline_prompt: |
|
25 | if self.show_prompts and self.inline_prompt: | |
25 | prompt = 'In[%s]: ' % cell.prompt_number |
|
26 | prompt = 'In[%s]: ' % cell.prompt_number | |
26 | input_lines = cell.input.split('\n') |
|
27 | input_lines = cell.input.split('\n') | |
27 |
src = prompt + input_lines[0] + '\n' + |
|
28 | src = (prompt + input_lines[0] + '\n' + | |
|
29 | indent('\n'.join(input_lines[1:]), nspaces=len(prompt))) | |||
28 | else: |
|
30 | else: | |
29 | src = cell.input |
|
31 | src = cell.input | |
30 | src = highlight(src) if self.highlight_source else indent(src) |
|
32 | src = highlight(src) if self.highlight_source else indent(src) | |
@@ -50,7 +52,7 b' class ConverterMarkdown(Converter):' | |||||
50 |
|
52 | |||
51 | def render_pyout(self, output): |
|
53 | def render_pyout(self, output): | |
52 | lines = [] |
|
54 | lines = [] | |
53 |
|
55 | |||
54 | ## if 'text' in output: |
|
56 | ## if 'text' in output: | |
55 | ## lines.extend(['*Out[%s]:*' % output.prompt_number, '']) |
|
57 | ## lines.extend(['*Out[%s]:*' % output.prompt_number, '']) | |
56 |
|
58 | |||
@@ -70,7 +72,7 b' class ConverterMarkdown(Converter):' | |||||
70 |
|
72 | |||
71 | def _img_lines(self, img_file): |
|
73 | def _img_lines(self, img_file): | |
72 | return ['', '![](%s)' % img_file, ''] |
|
74 | return ['', '![](%s)' % img_file, ''] | |
73 |
|
75 | |||
74 | def render_display_format_text(self, output): |
|
76 | def render_display_format_text(self, output): | |
75 | return [indent(output.text)] |
|
77 | return [indent(output.text)] | |
76 |
|
78 |
@@ -4,6 +4,7 b' from converters.utils import cell_to_lines' | |||||
4 | from shutil import rmtree |
|
4 | from shutil import rmtree | |
5 | import json |
|
5 | import json | |
6 |
|
6 | |||
|
7 | ||||
7 | class ConverterNotebook(Converter): |
|
8 | class ConverterNotebook(Converter): | |
8 | """ |
|
9 | """ | |
9 | A converter that is essentially a null-op. |
|
10 | A converter that is essentially a null-op. | |
@@ -25,7 +26,8 b' class ConverterNotebook(Converter):' | |||||
25 | rmtree(self.files_dir) |
|
26 | rmtree(self.files_dir) | |
26 |
|
27 | |||
27 | def convert(self): |
|
28 | def convert(self): | |
28 |
return unicode(json.dumps(json.loads(Converter.convert(self, ',')), |
|
29 | return unicode(json.dumps(json.loads(Converter.convert(self, ',')), | |
|
30 | indent=1, sort_keys=True)) | |||
29 |
|
31 | |||
30 | def optional_header(self): |
|
32 | def optional_header(self): | |
31 | s = \ |
|
33 | s = \ | |
@@ -37,7 +39,6 b' class ConverterNotebook(Converter):' | |||||
37 | "worksheets": [ |
|
39 | "worksheets": [ | |
38 | { |
|
40 | { | |
39 | "cells": [""" % {'name': os.path.basename(self.outbase)} |
|
41 | "cells": [""" % {'name': os.path.basename(self.outbase)} | |
40 |
|
||||
41 | return s.split('\n') |
|
42 | return s.split('\n') | |
42 |
|
43 | |||
43 | def optional_footer(self): |
|
44 | def optional_footer(self): | |
@@ -78,6 +79,5 b' class ConverterNotebook(Converter):' | |||||
78 | def render_display_format_json(self, output): |
|
79 | def render_display_format_json(self, output): | |
79 | return [output.json] |
|
80 | return [output.json] | |
80 |
|
81 | |||
81 |
|
||||
82 | def render_display_format_javascript(self, output): |
|
82 | def render_display_format_javascript(self, output): | |
83 | return [output.javascript] |
|
83 | return [output.javascript] |
@@ -2,6 +2,7 b' from converters.base import Converter' | |||||
2 | from IPython.utils.text import indent |
|
2 | from IPython.utils.text import indent | |
3 | from converters.utils import remove_ansi |
|
3 | from converters.utils import remove_ansi | |
4 |
|
4 | |||
|
5 | ||||
5 | class ConverterPy(Converter): |
|
6 | class ConverterPy(Converter): | |
6 | """ |
|
7 | """ | |
7 | A converter that takes a notebook and converts it to a .py file. |
|
8 | A converter that takes a notebook and converts it to a .py file. | |
@@ -27,10 +28,10 b' class ConverterPy(Converter):' | |||||
27 | @staticmethod |
|
28 | @staticmethod | |
28 | def comment(input): |
|
29 | def comment(input): | |
29 | "returns every line in input as commented out" |
|
30 | "returns every line in input as commented out" | |
30 | return "# "+input.replace("\n", "\n# ") |
|
31 | return "# " + input.replace("\n", "\n# ") | |
31 |
|
32 | |||
32 | def render_heading(self, cell): |
|
33 | def render_heading(self, cell): | |
33 | return ['#{0} {1}'.format('#'*cell.level, cell.source), ''] |
|
34 | return ['#{0} {1}'.format('#' * cell.level, cell.source), ''] | |
34 |
|
35 | |||
35 | def render_code(self, cell): |
|
36 | def render_code(self, cell): | |
36 | if not cell.input: |
|
37 | if not cell.input: | |
@@ -41,7 +42,7 b' class ConverterPy(Converter):' | |||||
41 | src = cell.input |
|
42 | src = cell.input | |
42 | lines.extend([src, '']) |
|
43 | lines.extend([src, '']) | |
43 | if self.show_output: |
|
44 | if self.show_output: | |
44 |
if cell.outputs |
|
45 | if cell.outputs: | |
45 | lines.extend(['# Out[%s]:' % cell.prompt_number]) |
|
46 | lines.extend(['# Out[%s]:' % cell.prompt_number]) | |
46 | for output in cell.outputs: |
|
47 | for output in cell.outputs: | |
47 | conv_fn = self.dispatch(output.output_type) |
|
48 | conv_fn = self.dispatch(output.output_type) | |
@@ -78,13 +79,13 b' class ConverterPy(Converter):' | |||||
78 | return [indent(remove_ansi('\n'.join(output.traceback))), ''] |
|
79 | return [indent(remove_ansi('\n'.join(output.traceback))), ''] | |
79 |
|
80 | |||
80 | def _img_lines(self, img_file): |
|
81 | def _img_lines(self, img_file): | |
81 |
return [ |
|
82 | return [self.comment('image file: %s' % img_file), ''] | |
82 |
|
83 | |||
83 | def render_display_format_text(self, output): |
|
84 | def render_display_format_text(self, output): | |
84 | return [self.comment(indent(output.text))] |
|
85 | return [self.comment(indent(output.text))] | |
85 |
|
86 | |||
86 | def _unknown_lines(self, data): |
|
87 | def _unknown_lines(self, data): | |
87 | return [self.comment('Warning: Unknown cell'+ str(data))] |
|
88 | return [self.comment('Warning: Unknown cell' + str(data))] | |
88 |
|
89 | |||
89 | def render_display_format_html(self, output): |
|
90 | def render_display_format_html(self, output): | |
90 | return [self.comment(output.html)] |
|
91 | return [self.comment(output.html)] |
@@ -6,6 +6,7 b' captures display data and deletes the cell inputs.' | |||||
6 | import copy |
|
6 | import copy | |
7 | import nbconvert as nb |
|
7 | import nbconvert as nb | |
8 |
|
8 | |||
|
9 | ||||
9 | class CustomNotebookConverter(nb.ConverterNotebook): |
|
10 | class CustomNotebookConverter(nb.ConverterNotebook): | |
10 |
|
11 | |||
11 | def render_code(self, cell): |
|
12 | def render_code(self, cell): | |
@@ -24,4 +25,3 b" if __name__ == '__main__':" | |||||
24 | infile = 'tests/test_display.ipynb' |
|
25 | infile = 'tests/test_display.ipynb' | |
25 | converter = CustomNotebookConverter(infile, 'test_only_display') |
|
26 | converter = CustomNotebookConverter(infile, 'test_only_display') | |
26 | converter.render() |
|
27 | converter.render() | |
27 |
|
@@ -16,22 +16,27 b' dollar_pat = r"(?:^|(?<=\\s))[$]([^\\n]*?)(?<![\\\\])[$](?:$|(?=\\s|[.,;\\\\]))"' | |||||
16 | _dollar = re.compile(dollar_pat) |
|
16 | _dollar = re.compile(dollar_pat) | |
17 | _notdollar = re.compile(r"\\[$]") |
|
17 | _notdollar = re.compile(r"\\[$]") | |
18 |
|
18 | |||
|
19 | ||||
19 | def replace_dollar(content): |
|
20 | def replace_dollar(content): | |
20 | content = _dollar.sub(r":math:`\1`", content) |
|
21 | content = _dollar.sub(r":math:`\1`", content) | |
21 | content = _notdollar.sub("$", content) |
|
22 | content = _notdollar.sub("$", content) | |
22 | return content |
|
23 | return content | |
23 |
|
24 | |||
|
25 | ||||
24 | def rewrite_rst(app, docname, source): |
|
26 | def rewrite_rst(app, docname, source): | |
25 | source[0] = replace_dollar(source[0]) |
|
27 | source[0] = replace_dollar(source[0]) | |
26 |
|
28 | |||
|
29 | ||||
27 | def rewrite_autodoc(app, what, name, obj, options, lines): |
|
30 | def rewrite_autodoc(app, what, name, obj, options, lines): | |
28 | lines[:] = [replace_dollar(L) for L in lines] |
|
31 | lines[:] = [replace_dollar(L) for L in lines] | |
29 |
|
32 | |||
|
33 | ||||
30 | def setup(app): |
|
34 | def setup(app): | |
31 | app.connect('source-read', rewrite_rst) |
|
35 | app.connect('source-read', rewrite_rst) | |
32 | if 'autodoc-process-docstring' in app._events: |
|
36 | if 'autodoc-process-docstring' in app._events: | |
33 | app.connect('autodoc-process-docstring', rewrite_autodoc) |
|
37 | app.connect('autodoc-process-docstring', rewrite_autodoc) | |
34 |
|
38 | |||
|
39 | ||||
35 | def test_expr(expr, expect): |
|
40 | def test_expr(expr, expect): | |
36 | result = replace_dollar(expr) |
|
41 | result = replace_dollar(expr) | |
37 | print 'A math expression: %s' % expr |
|
42 | print 'A math expression: %s' % expr | |
@@ -41,6 +46,7 b' def test_expr(expr, expect):' | |||||
41 | else: |
|
46 | else: | |
42 | print 'NG: A result %s does not match expected one!' % result |
|
47 | print 'NG: A result %s does not match expected one!' % result | |
43 |
|
48 | |||
|
49 | ||||
44 | def test_dollar(): |
|
50 | def test_dollar(): | |
45 | samples = { |
|
51 | samples = { | |
46 | u"no dollar": u"no dollar", |
|
52 | u"no dollar": u"no dollar", | |
@@ -59,8 +65,11 b' def test_dollar():' | |||||
59 | for expr, expect in samples.items(): |
|
65 | for expr, expect in samples.items(): | |
60 | test_expr(expr, expect) |
|
66 | test_expr(expr, expect) | |
61 |
|
67 | |||
|
68 | ||||
62 | if __name__ == "__main__": |
|
69 | if __name__ == "__main__": | |
63 |
import sys |
|
70 | import sys | |
|
71 | import locale | |||
|
72 | import codecs | |||
64 | encoding = locale.getpreferredencoding() |
|
73 | encoding = locale.getpreferredencoding() | |
65 | sys.stdout = codecs.getwriter(encoding)(sys.stdout) |
|
74 | sys.stdout = codecs.getwriter(encoding)(sys.stdout) | |
66 | sys.stdin = codecs.getreader(encoding)(sys.stdin) |
|
75 | sys.stdin = codecs.getreader(encoding)(sys.stdin) |
@@ -67,7 +67,8 b" if __name__ == '__main__':" | |||||
67 | formatter_class=argparse.RawTextHelpFormatter) |
|
67 | formatter_class=argparse.RawTextHelpFormatter) | |
68 | # TODO: consider passing file like object around, rather than filenames |
|
68 | # TODO: consider passing file like object around, rather than filenames | |
69 | # would allow us to process stdin, or even http streams |
|
69 | # would allow us to process stdin, or even http streams | |
70 |
#parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), |
|
70 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), | |
|
71 | # default=sys.stdin) | |||
71 |
|
72 | |||
72 | #Require a filename as a positional argument |
|
73 | #Require a filename as a positional argument | |
73 | parser.add_argument('infile', nargs=1) |
|
74 | parser.add_argument('infile', nargs=1) |
@@ -95,7 +95,6 b' class IPYNBTranslator(nodes.GenericNodeVisitor):' | |||||
95 | p = nbformat.new_text_cell('markdown', source=text) |
|
95 | p = nbformat.new_text_cell('markdown', source=text) | |
96 | self.add_cell(p) |
|
96 | self.add_cell(p) | |
97 |
|
97 | |||
98 |
|
||||
99 | def visit_section(self, node): |
|
98 | def visit_section(self, node): | |
100 | self.section_level += 1 |
|
99 | self.section_level += 1 | |
101 | self.default_visit(node) |
|
100 | self.default_visit(node) |
@@ -19,7 +19,6 b' def test_roundtrip():' | |||||
19 | 'test.ipynb') |
|
19 | 'test.ipynb') | |
20 | converter = ConverterNotebook(fname, out1) |
|
20 | converter = ConverterNotebook(fname, out1) | |
21 | converter.render() |
|
21 | converter.render() | |
22 |
|
||||
23 | converter2 = ConverterNotebook(out1 + '.ipynb', out2) |
|
22 | converter2 = ConverterNotebook(out1 + '.ipynb', out2) | |
24 | converter2.render() |
|
23 | converter2.render() | |
25 |
|
24 | |||
@@ -30,6 +29,5 b' def test_roundtrip():' | |||||
30 |
|
29 | |||
31 | nt.assert_true(s1.replace(outbase1, outbase2) == s2) |
|
30 | nt.assert_true(s1.replace(outbase1, outbase2) == s2) | |
32 | shutil.rmtree(directory) |
|
31 | shutil.rmtree(directory) | |
33 |
|
||||
34 | s0 = json.dumps(json.load(file(fname)), indent=1, sort_keys=True) |
|
32 | s0 = json.dumps(json.load(file(fname)), indent=1, sort_keys=True) | |
35 | nt.assert_true(s0 == s2) |
|
33 | nt.assert_true(s0 == s2) |
@@ -1,9 +1,9 b'' | |||||
1 | import io |
|
1 | import io | |
2 | import nose.tools as nt |
|
2 | import nose.tools as nt | |
3 |
from n |
|
3 | from nose.tools import nottest | |
|
4 | from converts import ( | |||
4 | ConverterLaTeX, ConverterMarkdown, ConverterPy, ConverterHTML |
|
5 | ConverterLaTeX, ConverterMarkdown, ConverterPy, ConverterHTML | |
5 | ) |
|
6 | ) | |
6 | from nose.tools import nottest |
|
|||
7 |
|
7 | |||
8 |
|
8 | |||
9 | def test_evens(): |
|
9 | def test_evens(): | |
@@ -17,7 +17,7 b' def test_evens():' | |||||
17 | # commenting rst for now as travis build |
|
17 | # commenting rst for now as travis build | |
18 | # fail because of pandoc version. |
|
18 | # fail because of pandoc version. | |
19 | converters = [ |
|
19 | converters = [ | |
20 | #(ConverterRST,'rst'), |
|
20 | #(ConverterRST, 'rst'), | |
21 | (ConverterMarkdown, 'md'), |
|
21 | (ConverterMarkdown, 'md'), | |
22 | (ConverterLaTeX, 'tex'), |
|
22 | (ConverterLaTeX, 'tex'), | |
23 | (ConverterPy, 'py'), |
|
23 | (ConverterPy, 'py'), |
@@ -40,7 +40,7 b' def test_render_heading():' | |||||
40 | cell = { |
|
40 | cell = { | |
41 | 'cell_type': 'heading', |
|
41 | 'cell_type': 'heading', | |
42 | 'level': level, |
|
42 | 'level': level, | |
43 |
'source': |
|
43 | 'source': ['Test for heading type H{0}'.format(level)] | |
44 | } |
|
44 | } | |
45 | # Convert cell dictionaries to NotebookNode |
|
45 | # Convert cell dictionaries to NotebookNode | |
46 | cell_nb = nbformat.NotebookNode(cell) |
|
46 | cell_nb = nbformat.NotebookNode(cell) |
General Comments 0
You need to be logged in to leave comments.
Login now