##// END OF EJS Templates
highlight filter: pass metadata as an optional argument
Pablo de Oliveira -
Show More
@@ -39,45 +39,51 b' __all__ = ['
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 """
@@ -85,14 +91,14 b" def _pygment_highlight(cell, output_formatter, language='ipython'):"
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)
@@ -60,6 +60,6 b' class TestHighlight(TestsBase):'
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
@@ -36,7 +36,7 b' In [{{ cell.prompt_number }}]:'
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
@@ -284,7 +284,7 b' Note: For best display, use latex syntax highlighting. =))'
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}
@@ -292,7 +292,7 b' Note: For best display, use latex syntax highlighting. =))'
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 *))
General Comments 0
You need to be logged in to leave comments. Login now