##// END OF EJS Templates
highlight filter: pass metadata as an optional argument
Pablo de Oliveira -
Show More
@@ -39,45 +39,51 b' __all__ = ['
39 'highlight2latex'
39 'highlight2latex'
40 ]
40 ]
41
41
42 def highlight2html(cell, language='ipython'):
42 def highlight2html(source, metadata=None, language='ipython'):
43 """
43 """
44 Return a syntax-highlighted version of the input source as html output.
44 Return a syntax-highlighted version of the input source as html output.
45
45
46 Parameters
46 Parameters
47 ----------
47 ----------
48 cell : NotebookNode cell
48 source : str
49 cell to highlight
49 source of the cell to highlight.
50 metadata : NotebookNode cell metadata
51 metadata of the cell to highlight.
50 language : str
52 language : str
51 Language to highlight the syntax of.
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 Return a syntax-highlighted version of the input source as latex output.
61 Return a syntax-highlighted version of the input source as latex output.
60
62
61 Parameters
63 Parameters
62 ----------
64 ----------
63 cell : NotebookNode cell
65 source : str
64 cell to highlight
66 source of the cell to highlight.
67 metadata : NotebookNode cell metadata
68 metadata of the cell to highlight.
65 language : str
69 language : str
66 Language to highlight the syntax of.
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 Return a syntax-highlighted version of the input source
78 Return a syntax-highlighted version of the input source
75
79
76 Parameters
80 Parameters
77 ----------
81 ----------
78 cell : NotebookNode cell
82 source : str
79 cell to highlight
83 source of the cell to highlight.
80 output_formatter : Pygments formatter
84 output_formatter : Pygments formatter
85 metadata : NotebookNode cell metadata
86 metadata of the cell to highlight.
81 language : str
87 language : str
82 Language to highlight the syntax of.
88 Language to highlight the syntax of.
83 """
89 """
@@ -85,14 +91,14 b" def _pygment_highlight(cell, output_formatter, language='ipython'):"
85 # If the cell uses a magic extension language,
91 # If the cell uses a magic extension language,
86 # use the magic language instead.
92 # use the magic language instead.
87 if language == 'ipython' \
93 if language == 'ipython' \
88 and 'metadata' in cell \
94 and metadata \
89 and 'magics_language' in cell['metadata']:
95 and 'magics_language' in metadata:
90
96
91 language = cell['metadata']['magics_language']
97 language = metadata['magics_language']
92
98
93 if language == 'ipython':
99 if language == 'ipython':
94 lexer = IPythonLexer()
100 lexer = IPythonLexer()
95 else:
101 else:
96 lexer = get_lexer_by_name(language, stripall=True)
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 def _try_highlight(self, method, test, tokens):
61 def _try_highlight(self, method, test, tokens):
62 """Try highlighting source, look for key tokens"""
62 """Try highlighting source, look for key tokens"""
63 results = method({'input': test})
63 results = method(test)
64 for token in tokens:
64 for token in tokens:
65 assert token in results
65 assert token in results
@@ -36,7 +36,7 b' In [{{ cell.prompt_number }}]:'
36
36
37 {% block input %}
37 {% block input %}
38 <div class="input_area box-flex1">
38 <div class="input_area box-flex1">
39 {{ cell | highlight2html }}
39 {{ cell.input | highlight2html(metadata=cell.metadata) }}
40 </div>
40 </div>
41 {%- endblock input %}
41 {%- endblock input %}
42
42
@@ -284,7 +284,7 b' Note: For best display, use latex syntax highlighting. =))'
284 \vspace{-25pt}
284 \vspace{-25pt}
285
285
286 % Add contents below.
286 % Add contents below.
287 ((( cell | highlight2latex )))
287 ((( cell.input | highlight2latex(metadata=cell.metadata) )))
288
288
289 ((* elif resources.sphinx.outputstyle == 'notebook' *))
289 ((* elif resources.sphinx.outputstyle == 'notebook' *))
290 \vspace{6pt}
290 \vspace{6pt}
@@ -292,7 +292,7 b' Note: For best display, use latex syntax highlighting. =))'
292 \vspace{-2.65\baselineskip}
292 \vspace{-2.65\baselineskip}
293 \begin{ColorVerbatim}
293 \begin{ColorVerbatim}
294 \vspace{-0.7\baselineskip}
294 \vspace{-0.7\baselineskip}
295 ((( cell | highlight2latex )))
295 ((( cell.input | highlight2latex(metadata=cell.metadata) )))
296 ((* if cell.input == None or cell.input == '' *))
296 ((* if cell.input == None or cell.input == '' *))
297 \vspace{0.3\baselineskip}
297 \vspace{0.3\baselineskip}
298 ((* else *))
298 ((* else *))
General Comments 0
You need to be logged in to leave comments. Login now