##// END OF EJS Templates
highlight filter: pass metadata as an optional argument
Pablo de Oliveira -
Show More
@@ -1,98 +1,104 b''
1 1 """
2 2 Module containing filter functions that allow code to be highlighted
3 3 from within Jinja templates.
4 4 """
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 import re
18 18
19 19 from pygments import highlight as pygements_highlight
20 20 from pygments.lexers import get_lexer_by_name
21 21 from pygments.formatters import HtmlFormatter
22 22 from pygments.formatters import LatexFormatter
23 23
24 24 # Our own imports
25 25 from IPython.nbconvert.utils.lexers import IPythonLexer
26 26
27 27 #-----------------------------------------------------------------------------
28 28 # Globals and constants
29 29 #-----------------------------------------------------------------------------
30 30
31 31 MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
32 32
33 33 #-----------------------------------------------------------------------------
34 34 # Utility functions
35 35 #-----------------------------------------------------------------------------
36 36
37 37 __all__ = [
38 38 'highlight2html',
39 39 'highlight2latex'
40 40 ]
41 41
42 def highlight2html(cell, language='ipython'):
42 def highlight2html(source, metadata=None, language='ipython'):
43 43 """
44 44 Return a syntax-highlighted version of the input source as html output.
45 45
46 46 Parameters
47 47 ----------
48 cell : NotebookNode cell
49 cell to highlight
48 source : str
49 source of the cell to highlight.
50 metadata : NotebookNode cell metadata
51 metadata of the cell to highlight.
50 52 language : str
51 53 Language to highlight the syntax of.
52 54 """
53 55
54 return _pygment_highlight(cell, HtmlFormatter(), language)
56 return _pygment_highlight(source, HtmlFormatter(), metadata, language)
55 57
56 58
57 def highlight2latex(cell, language='ipython'):
59 def highlight2latex(source, metadata=None, language='ipython'):
58 60 """
59 61 Return a syntax-highlighted version of the input source as latex output.
60 62
61 63 Parameters
62 64 ----------
63 cell : NotebookNode cell
64 cell to highlight
65 source : str
66 source of the cell to highlight.
67 metadata : NotebookNode cell metadata
68 metadata of the cell to highlight.
65 69 language : str
66 70 Language to highlight the syntax of.
67 71 """
68 return _pygment_highlight(cell, LatexFormatter(), language)
72 return _pygment_highlight(source, LatexFormatter(), metadata, language)
69 73
70 74
71 75
72 def _pygment_highlight(cell, output_formatter, language='ipython'):
76 def _pygment_highlight(source, output_formatter, metadata=None, language='ipython'):
73 77 """
74 78 Return a syntax-highlighted version of the input source
75 79
76 80 Parameters
77 81 ----------
78 cell : NotebookNode cell
79 cell to highlight
82 source : str
83 source of the cell to highlight.
80 84 output_formatter : Pygments formatter
85 metadata : NotebookNode cell metadata
86 metadata of the cell to highlight.
81 87 language : str
82 88 Language to highlight the syntax of.
83 89 """
84 90
85 91 # If the cell uses a magic extension language,
86 92 # use the magic language instead.
87 93 if language == 'ipython' \
88 and 'metadata' in cell \
89 and 'magics_language' in cell['metadata']:
94 and metadata \
95 and 'magics_language' in metadata:
90 96
91 language = cell['metadata']['magics_language']
97 language = metadata['magics_language']
92 98
93 99 if language == 'ipython':
94 100 lexer = IPythonLexer()
95 101 else:
96 102 lexer = get_lexer_by_name(language, stripall=True)
97 103
98 return pygements_highlight(cell['input'], lexer, output_formatter)
104 return pygements_highlight(source, lexer, output_formatter)
@@ -1,65 +1,65 b''
1 1 """
2 2 Module with tests for Highlight
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 from ...tests.base import TestsBase
18 18 from ..highlight import highlight2html, highlight2latex
19 19
20 20
21 21 #-----------------------------------------------------------------------------
22 22 # Class
23 23 #-----------------------------------------------------------------------------
24 24
25 25 class TestHighlight(TestsBase):
26 26 """Contains test functions for highlight.py"""
27 27
28 28 #Hello world test, magics test, blank string test
29 29 tests = [
30 30 """
31 31 #Hello World Example
32 32
33 33 def say(text):
34 34 print(text)
35 35
36 36 say('Hello World!')
37 37 """,
38 38 """
39 39 %%pylab
40 40 plot(x,y, 'r')
41 41 """
42 42 ]
43 43
44 44 tokens = [
45 45 ['Hello World Example', 'say', 'text', 'print', 'def'],
46 46 ['pylab', 'plot']]
47 47
48 48
49 49 def test_highlight2html(self):
50 50 """highlight2html test"""
51 51 for index, test in enumerate(self.tests):
52 52 self._try_highlight(highlight2html, test, self.tokens[index])
53 53
54 54
55 55 def test_highlight2latex(self):
56 56 """highlight2latex test"""
57 57 for index, test in enumerate(self.tests):
58 58 self._try_highlight(highlight2latex, test, self.tokens[index])
59 59
60 60
61 61 def _try_highlight(self, method, test, tokens):
62 62 """Try highlighting source, look for key tokens"""
63 results = method({'input': test})
63 results = method(test)
64 64 for token in tokens:
65 65 assert token in results
@@ -1,148 +1,148 b''
1 1 {%- extends 'display_priority.tpl' -%}
2 2
3 3
4 4 {% block codecell %}
5 5 <div class="cell border-box-sizing code_cell vbox">
6 6 {{ super() }}
7 7 </div>
8 8 {%- endblock codecell %}
9 9
10 10 {% block input_group -%}
11 11 <div class="input hbox">
12 12 {{ super() }}
13 13 </div>
14 14 {% endblock input_group %}
15 15
16 16 {% block output_group %}
17 17 <div class="vbox output_wrapper">
18 18 <div class="output vbox">
19 19 {{ super() }}
20 20 </div>
21 21 </div>
22 22 {% endblock output_group %}
23 23
24 24 {% block in_prompt -%}
25 25 <div class="prompt input_prompt">
26 26 In&nbsp;[{{ cell.prompt_number }}]:
27 27 </div>
28 28 {%- endblock in_prompt %}
29 29
30 30 {#
31 31 output_prompt doesn't do anything in HTML,
32 32 because there is a prompt div in each output area (see output block)
33 33 #}
34 34 {% block output_prompt %}
35 35 {% endblock output_prompt %}
36 36
37 37 {% block input %}
38 38 <div class="input_area box-flex1">
39 {{ cell | highlight2html }}
39 {{ cell.input | highlight2html(metadata=cell.metadata) }}
40 40 </div>
41 41 {%- endblock input %}
42 42
43 43 {% block output %}
44 44 <div class="hbox output_area">
45 45 {%- if output.output_type == 'pyout' -%}
46 46 <div class="prompt output_prompt">
47 47 Out[{{ cell.prompt_number }}]:
48 48 {%- else -%}
49 49 <div class="prompt">
50 50 {%- endif -%}
51 51 </div>
52 52 {{ super() }}
53 53 </div>
54 54 {% endblock output %}
55 55
56 56 {% block markdowncell scoped %}
57 57 <div class="text_cell_render border-box-sizing rendered_html">
58 58 {{ cell.source | strip_math_space | markdown2html | strip_files_prefix }}
59 59 </div>
60 60 {%- endblock markdowncell %}
61 61
62 62 {% block headingcell scoped %}
63 63 <div class="text_cell_render border-box-sizing rendered_html">
64 64 {{ ("#" * cell.level + cell.source) | replace('\n', ' ') | strip_math_space | markdown2html | strip_files_prefix | add_anchor }}
65 65 </div>
66 66 {% endblock headingcell %}
67 67
68 68 {% block rawcell scoped %}
69 69 {{ cell.source }}
70 70 {% endblock rawcell %}
71 71
72 72 {% block unknowncell scoped %}
73 73 unknown type {{ cell.type }}
74 74 {% endblock unknowncell %}
75 75
76 76 {% block pyout -%}
77 77 <div class="box-flex1 output_subarea output_pyout">
78 78 {% block data_priority scoped %}
79 79 {{ super() }}
80 80 {% endblock %}
81 81 </div>
82 82 {%- endblock pyout %}
83 83
84 84 {% block stream_stdout -%}
85 85 <div class="box-flex1 output_subarea output_stream output_stdout">
86 86 <pre>
87 87 {{ output.text | ansi2html }}
88 88 </pre>
89 89 </div>
90 90 {%- endblock stream_stdout %}
91 91
92 92 {% block stream_stderr -%}
93 93 <div class="box-flex1 output_subarea output_stream output_stderr">
94 94 <pre>
95 95 {{ output.text | ansi2html }}
96 96 </pre>
97 97 </div>
98 98 {%- endblock stream_stderr %}
99 99
100 100 {% block data_svg -%}
101 101 {{ output.svg }}
102 102 {%- endblock data_svg %}
103 103
104 104 {% block data_html -%}
105 105 <div class="output_html rendered_html">
106 106 {{ output.html }}
107 107 </div>
108 108 {%- endblock data_html %}
109 109
110 110 {% block data_png %}
111 111 <img src="data:image/png;base64,{{ output.png }}">
112 112 {%- endblock data_png %}
113 113
114 114 {% block data_jpg %}
115 115 <img src="data:image/jpeg;base64,{{ output.jpeg }}">
116 116 {%- endblock data_jpg %}
117 117
118 118 {% block data_latex %}
119 119 {{ output.latex }}
120 120 {%- endblock data_latex %}
121 121
122 122 {% block pyerr -%}
123 123 <div class="box-flex1 output_subarea output_pyerr">
124 124 <pre>{{ super() }}</pre>
125 125 </div>
126 126 {%- endblock pyerr %}
127 127
128 128 {%- block traceback_line %}
129 129 {{ line | ansi2html }}
130 130 {%- endblock traceback_line %}
131 131
132 132 {%- block data_text %}
133 133 <pre>
134 134 {{ output.text | ansi2html }}
135 135 </pre>
136 136 {%- endblock -%}
137 137
138 138 {%- block data_javascript %}
139 139 <script type="text/javascript">
140 140 {{ output.javascript }}
141 141 </script>
142 142 {%- endblock -%}
143 143
144 144 {%- block display_data scoped -%}
145 145 <div class="box-flex1 output_subarea output_display_data">
146 146 {{ super() }}
147 147 </div>
148 148 {%- endblock display_data -%} No newline at end of file
@@ -1,472 +1,472 b''
1 1 ((= NBConvert Sphinx-Latex Template
2 2
3 3 Purpose: Allow export of PDF friendly Latex inspired by Sphinx. Most of the
4 4 template is derived directly from Sphinx source.
5 5
6 6 Inheritance: null>display_priority
7 7
8 8 Note: For best display, use latex syntax highlighting. =))
9 9
10 10 ((*- extends 'display_priority.tplx' -*))
11 11
12 12
13 13 \nonstopmode
14 14
15 15 %==============================================================================
16 16 % Declarations
17 17 %==============================================================================
18 18
19 19 % In order to make sure that the input/output header follows the code it
20 20 % preceeds, the needspace package is used to request that a certain
21 21 % amount of lines (specified by this variable) are reserved. If those
22 22 % lines aren't available on the current page, the documenter will break
23 23 % to the next page and the header along with accomanying lines will be
24 24 % rendered together. This value specifies the number of lines that
25 25 % the header will be forced to group with without a page break.
26 26 ((*- set min_header_lines = 4 -*))
27 27
28 28 % This is the number of characters that are permitted per line. It's
29 29 % important that this limit is set so characters do not run off the
30 30 % edges of latex pages (since latex does not always seem smart enough
31 31 % to prevent this in some cases.) This is only applied to textual output
32 32 ((* if resources.sphinx.outputstyle == 'simple' *))
33 33 ((*- set wrap_size = 85 -*))
34 34 ((* elif resources.sphinx.outputstyle == 'notebook' *))
35 35 ((*- set wrap_size = 70 -*))
36 36 ((* endif *))
37 37
38 38 %==============================================================================
39 39 % Header
40 40 %==============================================================================
41 41 ((* block header *))
42 42
43 43 % Header, overrides base
44 44
45 45 % Make sure that the sphinx doc style knows who it inherits from.
46 46 \def\sphinxdocclass{(((parentdocumentclass)))}
47 47
48 48 % Declare the document class
49 49 \documentclass[letterpaper,10pt,english]{((( resources.sphinx.texinputs | posix_path )))/sphinx(((documentclass)))}
50 50
51 51 % Imports
52 52 \usepackage[utf8]{inputenc}
53 53 \DeclareUnicodeCharacter{00A0}{\\nobreakspace}
54 54 \usepackage[T1]{fontenc}
55 55 \usepackage{babel}
56 56 \usepackage{times}
57 57 \usepackage{import}
58 58 \usepackage[((( resources.sphinx.chapterstyle )))]{((( resources.sphinx.texinputs | posix_path )))/fncychap}
59 59 \usepackage{longtable}
60 60 \usepackage{((( resources.sphinx.texinputs | posix_path )))/sphinx}
61 61 \usepackage{multirow}
62 62
63 63 \usepackage{amsmath}
64 64 \usepackage{amssymb}
65 65 \usepackage{ucs}
66 66 \usepackage{enumerate}
67 67
68 68 % Used to make the Input/Output rules follow around the contents.
69 69 \usepackage{needspace}
70 70
71 71 % Pygments requirements
72 72 \usepackage{fancyvrb}
73 73 \usepackage{color}
74 74 % ansi colors additions
75 75 \definecolor{darkgreen}{rgb}{.12,.54,.11}
76 76 \definecolor{lightgray}{gray}{.95}
77 77 \definecolor{brown}{rgb}{0.54,0.27,0.07}
78 78 \definecolor{purple}{rgb}{0.5,0.0,0.5}
79 79 \definecolor{darkgray}{gray}{0.25}
80 80 \definecolor{lightred}{rgb}{1.0,0.39,0.28}
81 81 \definecolor{lightgreen}{rgb}{0.48,0.99,0.0}
82 82 \definecolor{lightblue}{rgb}{0.53,0.81,0.92}
83 83 \definecolor{lightpurple}{rgb}{0.87,0.63,0.87}
84 84 \definecolor{lightcyan}{rgb}{0.5,1.0,0.83}
85 85
86 86 % Needed to box output/input
87 87 \usepackage{tikz}
88 88 \usetikzlibrary{calc,arrows,shadows}
89 89 \usepackage[framemethod=tikz]{mdframed}
90 90
91 91 \usepackage{alltt}
92 92
93 93 % Used to load and display graphics
94 94 \usepackage{graphicx}
95 95 \graphicspath{ {figs/} }
96 96 \usepackage[Export]{adjustbox} % To resize
97 97
98 98 % used so that images for notebooks which have spaces in the name can still be included
99 99 \usepackage{grffile}
100 100
101 101
102 102 % For formatting output while also word wrapping.
103 103 \usepackage{listings}
104 104 \lstset{breaklines=true}
105 105 \lstset{basicstyle=\small\ttfamily}
106 106 \def\smaller{\fontsize{9.5pt}{9.5pt}\selectfont}
107 107
108 108 %Pygments definitions
109 109 ((( resources.sphinx.pygment_definitions )))
110 110
111 111 %Set pygments styles if needed...
112 112 ((* if resources.sphinx.outputstyle == 'notebook' *))
113 113 \definecolor{nbframe-border}{rgb}{0.867,0.867,0.867}
114 114 \definecolor{nbframe-bg}{rgb}{0.969,0.969,0.969}
115 115 \definecolor{nbframe-in-prompt}{rgb}{0.0,0.0,0.502}
116 116 \definecolor{nbframe-out-prompt}{rgb}{0.545,0.0,0.0}
117 117
118 118 \newenvironment{ColorVerbatim}
119 119 {\begin{mdframed}[%
120 120 roundcorner=1.0pt, %
121 121 backgroundcolor=nbframe-bg, %
122 122 userdefinedwidth=1\linewidth, %
123 123 leftmargin=0.1\linewidth, %
124 124 innerleftmargin=0pt, %
125 125 innerrightmargin=0pt, %
126 126 linecolor=nbframe-border, %
127 127 linewidth=1pt, %
128 128 usetwoside=false, %
129 129 everyline=true, %
130 130 innerlinewidth=3pt, %
131 131 innerlinecolor=nbframe-bg, %
132 132 middlelinewidth=1pt, %
133 133 middlelinecolor=nbframe-bg, %
134 134 outerlinewidth=0.5pt, %
135 135 outerlinecolor=nbframe-border, %
136 136 needspace=0pt
137 137 ]}
138 138 {\end{mdframed}}
139 139
140 140 \newenvironment{InvisibleVerbatim}
141 141 {\begin{mdframed}[leftmargin=0.1\linewidth,innerleftmargin=3pt,innerrightmargin=3pt, userdefinedwidth=1\linewidth, linewidth=0pt, linecolor=white, usetwoside=false]}
142 142 {\end{mdframed}}
143 143
144 144 \renewenvironment{Verbatim}[1][\unskip]
145 145 {\begin{alltt}\smaller}
146 146 {\end{alltt}}
147 147 ((* endif *))
148 148
149 149 % Help prevent overflowing lines due to urls and other hard-to-break
150 150 % entities. This doesn't catch everything...
151 151 \sloppy
152 152
153 153 % Document level variables
154 154 \title{((( resources.metadata.name | escape_latex )))}
155 155 \date{((( resources.sphinx.date | escape_latex )))}
156 156 \release{((( resources.sphinx.version | escape_latex )))}
157 157 \author{((( resources.sphinx.author | escape_latex )))}
158 158 \renewcommand{\releasename}{((( resources.sphinx.release | escape_latex )))}
159 159
160 160 % TODO: Add option for the user to specify a logo for his/her export.
161 161 \newcommand{\sphinxlogo}{}
162 162
163 163 % Make the index page of the document.
164 164 \makeindex
165 165
166 166 % Import sphinx document type specifics.
167 167 ((* block sphinxheader *))((* endblock sphinxheader *))
168 168 ((* endblock header *))
169 169
170 170 %==============================================================================
171 171 % Body
172 172 %==============================================================================
173 173 ((* block body *))
174 174 ((* block bodyBegin *))
175 175 % Body
176 176
177 177 % Start of the document
178 178 \begin{document}
179 179
180 180 ((* if resources.sphinx.header *))
181 181 \maketitle
182 182 ((* endif *))
183 183
184 184 ((* block toc *))
185 185 \tableofcontents
186 186 ((* endblock toc *))
187 187
188 188 ((* endblock bodyBegin *))
189 189 ((( super() )))
190 190 ((* block bodyEnd *))
191 191
192 192 \renewcommand{\indexname}{Index}
193 193 \printindex
194 194
195 195 ((* block bibliography *))
196 196 ((* endblock bibliography *))
197 197
198 198 % End of document
199 199 \end{document}
200 200 ((* endblock bodyEnd *))
201 201 ((* endblock body *))
202 202
203 203 %==============================================================================
204 204 % Footer
205 205 %==============================================================================
206 206 ((* block footer *))
207 207 ((* endblock footer *))
208 208
209 209 %==============================================================================
210 210 % Headings
211 211 %
212 212 % Purpose: Format pynb headers as sphinx headers. Depending on the Sphinx
213 213 % style that is active, this will change. Thus sphinx styles will
214 214 % override the values here.
215 215 %==============================================================================
216 216 ((* block headingcell -*))
217 217 \
218 218 ((*- if cell.level == 1 -*))
219 219 ((* block h1 -*))part((* endblock h1 -*))
220 220 ((*- elif cell.level == 2 -*))
221 221 ((* block h2 -*))chapter((* endblock h2 -*))
222 222 ((*- elif cell.level == 3 -*))
223 223 ((* block h3 -*))section((* endblock h3 -*))
224 224 ((*- elif cell.level == 4 -*))
225 225 ((* block h4 -*))subsection((* endblock h4 -*))
226 226 ((*- elif cell.level == 5 -*))
227 227 ((* block h5 -*))subsubsection((* endblock h5 -*))
228 228 ((*- elif cell.level == 6 -*))
229 229 ((* block h6 -*))paragraph((* endblock h6 -*))
230 230
231 231 ((= It's important to make sure that underscores (which tend to be common
232 232 in IPYNB file titles) do not make their way into latex. Sometimes this
233 233 causes latex to barf. =))
234 234 ((*- endif -*))
235 235 {((( cell.source | replace('\n', ' ') | citation2latex | markdown2latex )))}
236 236 ((*- endblock headingcell *))
237 237
238 238 %==============================================================================
239 239 % Markdown
240 240 %
241 241 % Purpose: Convert markdown to latex. Here markdown2latex is explicitly
242 242 % called since we know we want latex output.
243 243 %==============================================================================
244 244 ((*- block markdowncell scoped-*))
245 245 ((( cell.source | citation2latex | markdown2latex )))
246 246 ((*- endblock markdowncell -*))
247 247
248 248 %==============================================================================
249 249 % Rawcell
250 250 %
251 251 % Purpose: Raw text cells allow the user to manually inject document code that
252 252 % will not get touched by the templating system.
253 253 %==============================================================================
254 254 ((*- block rawcell *))
255 255 ((( cell.source | wrap_text(wrap_size) )))
256 256 ((* endblock rawcell -*))
257 257
258 258 %==============================================================================
259 259 % Unknowncell
260 260 %
261 261 % Purpose: This is the catch anything unhandled. To display this data, we
262 262 % remove all possible latex conflicts and wrap the characters so they
263 263 % can't flow off of the page.
264 264 %==============================================================================
265 265 ((* block unknowncell scoped*))
266 266 % Unsupported cell type, no formatting
267 267 ((( cell.source | wrap_text | escape_latex )))
268 268 ((* endblock unknowncell *))
269 269
270 270 %==============================================================================
271 271 % Input
272 272 %==============================================================================
273 273 ((* block input *))
274 274
275 275 % Make sure that atleast 4 lines are below the HR
276 276 \needspace{((( min_header_lines )))\baselineskip}
277 277
278 278 ((* if resources.sphinx.outputstyle == 'simple' *))
279 279
280 280 % Add a horizantal break, along with break title.
281 281 \vspace{10pt}
282 282 {\scriptsize Input}\\*
283 283 \rule[10pt]{\linewidth}{0.5pt}
284 284 \vspace{-25pt}
285 285
286 286 % Add contents below.
287 ((( cell | highlight2latex )))
287 ((( cell.input | highlight2latex(metadata=cell.metadata) )))
288 288
289 289 ((* elif resources.sphinx.outputstyle == 'notebook' *))
290 290 \vspace{6pt}
291 291 ((( write_prompt("In", cell.prompt_number, "nbframe-in-prompt") )))
292 292 \vspace{-2.65\baselineskip}
293 293 \begin{ColorVerbatim}
294 294 \vspace{-0.7\baselineskip}
295 ((( cell | highlight2latex )))
295 ((( cell.input | highlight2latex(metadata=cell.metadata) )))
296 296 ((* if cell.input == None or cell.input == '' *))
297 297 \vspace{0.3\baselineskip}
298 298 ((* else *))
299 299 \vspace{-0.2\baselineskip}
300 300 ((* endif *))
301 301 \end{ColorVerbatim}
302 302 ((* endif *))
303 303 ((* endblock input *))
304 304
305 305 %==============================================================================
306 306 % Output_Group
307 307 %
308 308 % Purpose: Make sure that only one header bar only attaches to the output
309 309 % once. By keeping track of when an input group is started
310 310 %==============================================================================
311 311 ((* block output_group *))
312 312 ((* if cell.outputs.__len__() > 0 *))
313 313
314 314 % If the first block is an image, minipage the image. Else
315 315 % request a certain amount of space for the input text.
316 316 ((( iff_figure(cell.outputs[0], "\\begin{minipage}{1.0\\textwidth}", "\\needspace{" ~ min_header_lines ~ "\\baselineskip}") )))
317 317
318 318 ((* if resources.sphinx.outputstyle == 'simple' *))
319 319
320 320 % Add a horizantal break, along with break title.
321 321 \vspace{10pt}
322 322 {\scriptsize Output}\\*
323 323 \rule[10pt]{\linewidth}{0.5pt}
324 324 \vspace{-20pt}
325 325
326 326 % Add the contents of the first block.
327 327 ((( render_output(cell.outputs[0]) )))
328 328
329 329 % Close the minipage.
330 330 ((( iff_figure(cell.outputs[0], "\\end{minipage}", "") )))
331 331
332 332 % Add remainer of the document contents below.
333 333 ((* for output in cell.outputs[1:] *))
334 334 ((( render_output(output, cell.prompt_number) )))
335 335 ((* endfor *))
336 336 ((* elif resources.sphinx.outputstyle == 'notebook' *))
337 337
338 338 % Add document contents.
339 339 ((* for output in cell.outputs *))
340 340 ((( render_output(output, cell.prompt_number) )))
341 341 ((* endfor *))
342 342 ((* endif *))
343 343 ((* endif *))
344 344 ((* endblock *))
345 345
346 346 %==============================================================================
347 347 % Additional formating
348 348 %==============================================================================
349 349 ((* block data_text *))
350 350 ((( custom_verbatim(output.text) | ansi2latex )))
351 351 ((* endblock *))
352 352
353 353 ((* block traceback_line *))
354 354 ((( conditionally_center_output( line | indent| strip_ansi ) )))
355 355 ((* endblock traceback_line *))
356 356
357 357 %==============================================================================
358 358 % Supported image formats
359 359 %==============================================================================
360 360 ((*- block data_png -*))
361 361 ((( conditionally_center_output(insert_graphics(output.png_filename | posix_path)) )))
362 362 ((*- endblock -*))
363 363
364 364 ((*- block data_jpg -*))
365 365 ((( conditionally_center_output(insert_graphics(output.jpeg_filename | posix_path)) )))
366 366 ((*- endblock -*))
367 367
368 368 ((*- block data_svg -*))
369 369 ((( conditionally_center_output(insert_graphics(output.svg_filename | posix_path)) )))
370 370 ((*- endblock -*))
371 371
372 372 ((*- block data_pdf -*))
373 373 ((( conditionally_center_output(insert_graphics(output.pdf_filename | posix_path)) )))
374 374 ((*- endblock -*))
375 375
376 376 ((*- block data_latex *))
377 377 ((* if resources.sphinx.centeroutput *))
378 378 \begin{center}
379 379 ((* endif -*))
380 380 ((( output.latex | strip_math_space )))
381 381 ((*- if resources.sphinx.centeroutput *))
382 382 \end{center}
383 383 ((* endif -*))
384 384 ((*- endblock -*))
385 385
386 386 %==============================================================================
387 387 % Support Macros
388 388 %==============================================================================
389 389
390 390 % Name: write_prompt
391 391 % Purpose: Renders an output/input prompt for notebook style pdfs
392 392 ((* macro write_prompt(prompt, number, color) -*))
393 393 \makebox[0.1\linewidth]{\smaller\hfill\tt\color{((( color )))}((( prompt )))\hspace{4pt}{[}((( number ))){]}:\hspace{4pt}}\\*
394 394 ((*- endmacro *))
395 395
396 396 % Name: render_output
397 397 % Purpose: Renders an output block appropriately.
398 398 ((* macro render_output(output, prompt_number) -*))
399 399 ((*- if output.output_type == 'pyerr' -*))
400 400 ((*- block pyerr scoped *))
401 401 ((( custom_verbatim(super()) )))
402 402 ((* endblock pyerr -*))
403 403 ((*- else -*))
404 404
405 405 ((* if resources.sphinx.outputstyle == 'notebook' *))
406 406 ((*- if output.output_type == 'pyout' -*))
407 407 ((( write_prompt("Out", prompt_number, "nbframe-out-prompt") )))
408 408 \vspace{-2.55\baselineskip}
409 409 ((*- endif -*))
410 410
411 411 \begin{InvisibleVerbatim}
412 412 \vspace{-0.5\baselineskip}
413 413 ((*- endif -*))
414 414
415 415 ((*- block display_data scoped -*))
416 416 ((( super() )))
417 417 ((*- endblock display_data -*))
418 418
419 419 ((* if resources.sphinx.outputstyle == 'notebook' *))
420 420 \end{InvisibleVerbatim}
421 421 ((*- endif -*))
422 422 ((*- endif -*))
423 423 ((*- endmacro *))
424 424
425 425 % Name: iff_figure
426 426 % Purpose: If the output block provided is a figure type, the 'true_content'
427 427 % parameter will be returned. Else, the 'false_content'.
428 428 ((* macro iff_figure(output, true_content, false_content) -*))
429 429 ((*- set is_figure = false -*))
430 430 ((*- for type in output | filter_data_type -*))
431 431 ((*- if type in ['pdf', 'svg', 'png', 'jpeg','html']*))
432 432 ((*- set is_figure = true -*))
433 433 ((*- endif -*))
434 434 ((*- endfor -*))
435 435
436 436 ((* if is_figure -*))
437 437 ((( true_content )))
438 438 ((*- else -*))
439 439 ((( false_content )))
440 440 ((*- endif *))
441 441 ((*- endmacro *))
442 442
443 443 % Name: custom_verbatim
444 444 % Purpose: This macro creates a verbatim style block that fits the existing
445 445 % sphinx style more readily than standard verbatim blocks.
446 446 ((* macro custom_verbatim(text) -*))
447 447 \begin{alltt}
448 448 ((*- if resources.sphinx.centeroutput *))\begin{center} ((* endif -*))
449 449 ((( text | wrap_text(wrap_size) | escape_latex )))
450 450 ((*- if resources.sphinx.centeroutput *))\end{center}((* endif -*))
451 451 \end{alltt}
452 452 ((*- endmacro *))
453 453
454 454 % Name: conditionally_center_output
455 455 % Purpose: This macro centers the output if the output centering is enabled.
456 456 ((* macro conditionally_center_output(text) -*))
457 457 ((* if resources.sphinx.centeroutput *))
458 458 {\centering
459 459 ((* endif *))
460 460 ((( text )))
461 461 ((* if resources.sphinx.centeroutput *))}
462 462 ((* endif *))
463 463 ((*- endmacro *))
464 464
465 465 % Name: insert_graphics
466 466 % Purpose: This macro will insert an image in the latex document given a path.
467 467 ((* macro insert_graphics(path) -*))
468 468 \begin{center}
469 469 \includegraphics[max size={\textwidth}{\textheight}]{((( path )))}
470 470 \par
471 471 \end{center}
472 472 ((*- endmacro *))
General Comments 0
You need to be logged in to leave comments. Login now