##// END OF EJS Templates
highlight: Swap metadata and language keyword arguments
Pablo de Oliveira -
Show More
@@ -1,104 +1,104 b''
1 """
1 """
2 Module containing filter functions that allow code to be highlighted
2 Module containing filter functions that allow code to be highlighted
3 from within Jinja templates.
3 from within Jinja templates.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import re
17 import re
18
18
19 from pygments import highlight as pygements_highlight
19 from pygments import highlight as pygements_highlight
20 from pygments.lexers import get_lexer_by_name
20 from pygments.lexers import get_lexer_by_name
21 from pygments.formatters import HtmlFormatter
21 from pygments.formatters import HtmlFormatter
22 from pygments.formatters import LatexFormatter
22 from pygments.formatters import LatexFormatter
23
23
24 # Our own imports
24 # Our own imports
25 from IPython.nbconvert.utils.lexers import IPythonLexer
25 from IPython.nbconvert.utils.lexers import IPythonLexer
26
26
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 # Globals and constants
28 # Globals and constants
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30
30
31 MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
31 MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
32
32
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34 # Utility functions
34 # Utility functions
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36
36
37 __all__ = [
37 __all__ = [
38 'highlight2html',
38 'highlight2html',
39 'highlight2latex'
39 'highlight2latex'
40 ]
40 ]
41
41
42 def highlight2html(source, metadata=None, language='ipython'):
42 def highlight2html(source, language='ipython', metadata=None):
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 source : str
48 source : str
49 source of the cell to highlight.
49 source of the cell to highlight.
50 metadata : NotebookNode cell metadata
51 metadata of the cell to highlight.
52 language : str
50 language : str
53 Language to highlight the syntax of.
51 Language to highlight the syntax of.
52 metadata : NotebookNode cell metadata
53 metadata of the cell to highlight.
54 """
54 """
55
55
56 return _pygment_highlight(source, HtmlFormatter(), metadata, language)
56 return _pygment_highlight(source, HtmlFormatter(), language, metadata)
57
57
58
58
59 def highlight2latex(source, metadata=None, language='ipython'):
59 def highlight2latex(source, language='ipython', metadata=None):
60 """
60 """
61 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.
62
62
63 Parameters
63 Parameters
64 ----------
64 ----------
65 source : str
65 source : str
66 source of the cell to highlight.
66 source of the cell to highlight.
67 metadata : NotebookNode cell metadata
68 metadata of the cell to highlight.
69 language : str
67 language : str
70 Language to highlight the syntax of.
68 Language to highlight the syntax of.
69 metadata : NotebookNode cell metadata
70 metadata of the cell to highlight.
71 """
71 """
72 return _pygment_highlight(source, LatexFormatter(), metadata, language)
72 return _pygment_highlight(source, LatexFormatter(), language, metadata)
73
73
74
74
75
75
76 def _pygment_highlight(source, output_formatter, metadata=None, language='ipython'):
76 def _pygment_highlight(source, output_formatter, language='ipython', metadata=None):
77 """
77 """
78 Return a syntax-highlighted version of the input source
78 Return a syntax-highlighted version of the input source
79
79
80 Parameters
80 Parameters
81 ----------
81 ----------
82 source : str
82 source : str
83 source of the cell to highlight.
83 source of the cell to highlight.
84 output_formatter : Pygments formatter
84 output_formatter : Pygments formatter
85 metadata : NotebookNode cell metadata
86 metadata of the cell to highlight.
87 language : str
85 language : str
88 Language to highlight the syntax of.
86 Language to highlight the syntax of.
87 metadata : NotebookNode cell metadata
88 metadata of the cell to highlight.
89 """
89 """
90
90
91 # If the cell uses a magic extension language,
91 # If the cell uses a magic extension language,
92 # use the magic language instead.
92 # use the magic language instead.
93 if language == 'ipython' \
93 if language == 'ipython' \
94 and metadata \
94 and metadata \
95 and 'magics_language' in metadata:
95 and 'magics_language' in metadata:
96
96
97 language = metadata['magics_language']
97 language = metadata['magics_language']
98
98
99 if language == 'ipython':
99 if language == 'ipython':
100 lexer = IPythonLexer()
100 lexer = IPythonLexer()
101 else:
101 else:
102 lexer = get_lexer_by_name(language, stripall=True)
102 lexer = get_lexer_by_name(language, stripall=True)
103
103
104 return pygements_highlight(source, lexer, output_formatter)
104 return pygements_highlight(source, lexer, output_formatter)
General Comments 0
You need to be logged in to leave comments. Login now