##// END OF EJS Templates
Fix eafc8cb070: highlight tests should now pass a cell object
Pablo de Oliveira -
Show More
@@ -1,98 +1,98
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(cell, language='ipython'):
42 def highlight2html(cell, 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 cell : NotebookNode cell
49 cell to highlight
49 cell to highlight
50 language : str
50 language : str
51 Language to highlight the syntax of.
51 Language to highlight the syntax of.
52 """
52 """
53
53
54 return _pygment_highlight(cell, HtmlFormatter(), language)
54 return _pygment_highlight(cell, HtmlFormatter(), language)
55
55
56
56
57 def highlight2latex(cell, language='ipython'):
57 def highlight2latex(cell, language='ipython'):
58 """
58 """
59 Return a syntax-highlighted version of the input source as latex output.
59 Return a syntax-highlighted version of the input source as latex output.
60
60
61 Parameters
61 Parameters
62 ----------
62 ----------
63 cell : NotebookNode cell
63 cell : NotebookNode cell
64 cell to highlight
64 cell to highlight
65 language : str
65 language : str
66 Language to highlight the syntax of.
66 Language to highlight the syntax of.
67 """
67 """
68 return _pygment_highlight(cell, LatexFormatter(), language)
68 return _pygment_highlight(cell, LatexFormatter(), language)
69
69
70
70
71
71
72 def _pygment_highlight(cell, output_formatter, language='ipython'):
72 def _pygment_highlight(cell, output_formatter, language='ipython'):
73 """
73 """
74 Return a syntax-highlighted version of the input source
74 Return a syntax-highlighted version of the input source
75
75
76 Parameters
76 Parameters
77 ----------
77 ----------
78 cell : NotebookNode cell
78 cell : NotebookNode cell
79 cell to highlight
79 cell to highlight
80 output_formatter : Pygments formatter
80 output_formatter : Pygments formatter
81 language : str
81 language : str
82 Language to highlight the syntax of.
82 Language to highlight the syntax of.
83 """
83 """
84
84
85 # If the cell uses a magic extension language,
85 # If the cell uses a magic extension language,
86 # use the magic language instead.
86 # use the magic language instead.
87 if language == 'ipython' \
87 if language == 'ipython' \
88 and 'metadata' in cell \
88 and 'metadata' in cell \
89 and 'magics_language' in cell['metadata']:
89 and 'magics_language' in cell['metadata']:
90
90
91 language = cell['metadata']['magics_language']
91 language = cell['metadata']['magics_language']
92
92
93 if language == 'ipython':
93 if language == 'ipython':
94 lexer = IPythonLexer()
94 lexer = IPythonLexer()
95 else:
95 else:
96 lexer = get_lexer_by_name(language, stripall=True)
96 lexer = get_lexer_by_name(language, stripall=True)
97
97
98 return pygements_highlight(cell, lexer, output_formatter)
98 return pygements_highlight(cell['input'], lexer, output_formatter)
@@ -1,65 +1,65
1 """
1 """
2 Module with tests for Highlight
2 Module with tests for Highlight
3 """
3 """
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 from ...tests.base import TestsBase
17 from ...tests.base import TestsBase
18 from ..highlight import highlight2html, highlight2latex
18 from ..highlight import highlight2html, highlight2latex
19
19
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Class
22 # Class
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 class TestHighlight(TestsBase):
25 class TestHighlight(TestsBase):
26 """Contains test functions for highlight.py"""
26 """Contains test functions for highlight.py"""
27
27
28 #Hello world test, magics test, blank string test
28 #Hello world test, magics test, blank string test
29 tests = [
29 tests = [
30 """
30 """
31 #Hello World Example
31 #Hello World Example
32
32
33 def say(text):
33 def say(text):
34 print(text)
34 print(text)
35
35
36 say('Hello World!')
36 say('Hello World!')
37 """,
37 """,
38 """
38 """
39 %%pylab
39 %%pylab
40 plot(x,y, 'r')
40 plot(x,y, 'r')
41 """
41 """
42 ]
42 ]
43
43
44 tokens = [
44 tokens = [
45 ['Hello World Example', 'say', 'text', 'print', 'def'],
45 ['Hello World Example', 'say', 'text', 'print', 'def'],
46 ['pylab', 'plot']]
46 ['pylab', 'plot']]
47
47
48
48
49 def test_highlight2html(self):
49 def test_highlight2html(self):
50 """highlight2html test"""
50 """highlight2html test"""
51 for index, test in enumerate(self.tests):
51 for index, test in enumerate(self.tests):
52 self._try_highlight(highlight2html, test, self.tokens[index])
52 self._try_highlight(highlight2html, test, self.tokens[index])
53
53
54
54
55 def test_highlight2latex(self):
55 def test_highlight2latex(self):
56 """highlight2latex test"""
56 """highlight2latex test"""
57 for index, test in enumerate(self.tests):
57 for index, test in enumerate(self.tests):
58 self._try_highlight(highlight2latex, test, self.tokens[index])
58 self._try_highlight(highlight2latex, test, self.tokens[index])
59
59
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(test)
63 results = method({'input': test})
64 for token in tokens:
64 for token in tokens:
65 assert token in results
65 assert token in results
General Comments 0
You need to be logged in to leave comments. Login now