|
43 | 43 |
|
44 | 44 | _ipynbversion = 4
|
45 | 45 |
|
| 46 | +# See nbconvert/exporters/html.py: |
| 47 | +DISPLAY_DATA_PRIORITY_HTML = ( |
| 48 | + 'application/javascript', |
| 49 | + 'text/html', |
| 50 | + 'text/markdown', |
| 51 | + 'image/svg+xml', |
| 52 | + 'text/latex', |
| 53 | + 'image/png', |
| 54 | + 'image/jpeg', |
| 55 | + 'text/plain', |
| 56 | +) |
| 57 | +# See nbconvert/exporters/latex.py: |
| 58 | +DISPLAY_DATA_PRIORITY_LATEX = ( |
| 59 | + 'text/latex', |
| 60 | + 'application/pdf', |
| 61 | + 'image/png', |
| 62 | + 'image/jpeg', |
| 63 | + 'image/svg+xml', |
| 64 | + 'text/markdown', |
| 65 | + 'text/plain', |
| 66 | +) |
| 67 | + |
46 | 68 | RST_TEMPLATE = """
|
47 | 69 | {% extends 'rst.tpl' %}
|
48 | 70 |
|
|
85 | 107 | {% endblock input %}
|
86 | 108 |
|
87 | 109 |
|
88 |
| -{% block nboutput %} |
89 |
| -{%- if output.output_type == 'stream' %} |
90 |
| - {%- set datatype = 'ansi' %} |
91 |
| - {%- set outputdata = output.text[:-1] %}{# trailing \n is stripped #} |
92 |
| -{%- elif output.output_type == 'error' %} |
93 |
| - {%- set datatype = 'ansi' %} |
94 |
| - {%- set outputdata = '\n'.join(output.traceback) %} |
95 |
| -{%- else %} |
96 |
| - {%- set datatype = (output.data | filter_data_type)[0] %} |
97 |
| - {%- set outputdata = output.data[datatype] %} |
98 |
| -{%- endif -%} |
| 110 | +{% macro insert_nboutput(datatype, output, cell) -%} |
99 | 111 | .. nboutput::
|
100 | 112 | {%- if datatype == 'text/plain' %}{# nothing #}
|
101 | 113 | {%- else %} rst
|
|
110 | 122 | :class: stderr
|
111 | 123 | {%- endif %}
|
112 | 124 | {%- if datatype == 'text/plain' -%}
|
113 |
| -{{ insert_empty_lines(outputdata) }} |
| 125 | +{{ insert_empty_lines(output.data[datatype]) }} |
114 | 126 |
|
115 |
| -{{ outputdata.strip(\n) | indent }} |
| 127 | +{{ output.data[datatype].strip(\n) | indent }} |
116 | 128 | {%- elif datatype in ['image/svg+xml', 'image/png', 'image/jpeg', 'application/pdf'] %}
|
117 | 129 |
|
118 |
| - .. image:: {{ output.metadata.filenames[datatype].rsplit('.', 1)[0] + '.*' | posix_path }} |
| 130 | + .. image:: {{ output.metadata.filenames[datatype] | posix_path }} |
119 | 131 | {%- elif datatype in ['text/markdown'] %}
|
120 | 132 |
|
121 | 133 | {{ output.data['text/markdown'] | markdown2rst | indent }}
|
|
136 | 148 | .. raw:: html
|
137 | 149 |
|
138 | 150 | <pre>
|
139 |
| -{{ outputdata | ansi2html | indent | indent }} |
| 151 | +{{ output.data[datatype] | ansi2html | indent | indent }} |
140 | 152 | </pre>
|
141 | 153 |
|
142 | 154 | .. raw:: latex
|
143 | 155 |
|
144 | 156 | % This comment is needed to force a line break for adjacent ANSI cells
|
145 | 157 | \\begin{OriginalVerbatim}[commandchars=\\\\\\{\\}]
|
146 |
| -{{ outputdata | ansi2latex | indent | indent }} |
| 158 | +{{ output.data[datatype] | ansi2latex | indent | indent }} |
147 | 159 | \\end{OriginalVerbatim}
|
148 | 160 | {% else %}
|
149 | 161 |
|
150 | 162 | WARNING! Data type not implemented: {{ datatype }}
|
151 | 163 | {%- endif %}
|
| 164 | +{% endmacro %} |
| 165 | +
|
| 166 | +
|
| 167 | +{% block nboutput -%} |
| 168 | +{%- set html_datatype, latex_datatype = output | get_output_type %} |
| 169 | +{%- if html_datatype == latex_datatype %} |
| 170 | +{{ insert_nboutput(html_datatype, output, cell) }} |
| 171 | +{%- else %} |
| 172 | +.. only:: html |
| 173 | +
|
| 174 | +{{ insert_nboutput(html_datatype, output, cell) | indent }} |
| 175 | +.. only:: latex |
| 176 | +
|
| 177 | +{{ insert_nboutput(latex_datatype, output, cell) | indent }} |
| 178 | +{%- endif %} |
152 | 179 | {% endblock nboutput %}
|
153 | 180 |
|
154 | 181 |
|
@@ -411,6 +438,7 @@ def __init__(self, execute='auto', allow_errors=False, timeout=30,
|
411 | 438 | 'markdown2rst': markdown2rst,
|
412 | 439 | 'get_empty_lines': _get_empty_lines,
|
413 | 440 | 'extract_toctree': _extract_toctree,
|
| 441 | + 'get_output_type': _get_output_type, |
414 | 442 | })
|
415 | 443 |
|
416 | 444 | def from_notebook_node(self, nb, resources=None, **kw):
|
@@ -747,6 +775,27 @@ def _get_empty_lines(text):
|
747 | 775 | return before, after
|
748 | 776 |
|
749 | 777 |
|
| 778 | +def _get_output_type(output): |
| 779 | + """Choose appropriate output data types for HTML and LaTeX.""" |
| 780 | + if output.output_type == 'stream': |
| 781 | + html_datatype = latex_datatype = 'ansi' |
| 782 | + text = output.text |
| 783 | + output.data = {'ansi': text[:-1] if text.endswith('\n') else text} |
| 784 | + elif output.output_type == 'error': |
| 785 | + html_datatype = latex_datatype = 'ansi' |
| 786 | + output.data = {'ansi': '\n'.join(output.traceback)} |
| 787 | + else: |
| 788 | + for datatype in DISPLAY_DATA_PRIORITY_HTML: |
| 789 | + if datatype in output.data: |
| 790 | + html_datatype = datatype |
| 791 | + break |
| 792 | + for datatype in DISPLAY_DATA_PRIORITY_LATEX: |
| 793 | + if datatype in output.data: |
| 794 | + latex_datatype = datatype |
| 795 | + break |
| 796 | + return html_datatype, latex_datatype |
| 797 | + |
| 798 | + |
750 | 799 | def _set_empty_lines(node, options):
|
751 | 800 | """Set "empty lines" attributes on a CodeNode.
|
752 | 801 |
|
|
0 commit comments