Show More
@@ -30,20 +30,6 b' from IPython.nbconvert.utils.lexers import IPythonLexer' | |||
|
30 | 30 | |
|
31 | 31 | MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] |
|
32 | 32 | |
|
33 | # list of magic language extensions and their associated pygment lexers | |
|
34 | magic_languages = {'%%R': 'r', | |
|
35 | '%%bash': 'bash', | |
|
36 | '%%octave': 'octave', | |
|
37 | '%%perl': 'perl', | |
|
38 | '%%ruby': 'ruby' | |
|
39 | } | |
|
40 | ||
|
41 | # build a RE to catch language extensions and choose an adequate | |
|
42 | # pygments lexer | |
|
43 | re_languages = "|".join(magic_languages.keys()) | |
|
44 | re_magic_language = re.compile(r'^\s*({})\s+'.format(re_languages), | |
|
45 | re.MULTILINE) | |
|
46 | ||
|
47 | 33 | #----------------------------------------------------------------------------- |
|
48 | 34 | # Utility functions |
|
49 | 35 | #----------------------------------------------------------------------------- |
@@ -53,79 +39,60 b' __all__ = [' | |||
|
53 | 39 | 'highlight2latex' |
|
54 | 40 | ] |
|
55 | 41 | |
|
56 | ||
|
57 | def highlight2html(source, language='ipython'): | |
|
42 | def highlight2html(cell, language='ipython'): | |
|
58 | 43 | """ |
|
59 | 44 | Return a syntax-highlighted version of the input source as html output. |
|
60 | 45 | |
|
61 | 46 | Parameters |
|
62 | 47 | ---------- |
|
63 | source : str | |
|
64 |
|
|
|
48 | cell : NotebookNode cell | |
|
49 | cell to highlight | |
|
65 | 50 | language : str |
|
66 | 51 | Language to highlight the syntax of. |
|
67 | 52 | """ |
|
68 | 53 | |
|
69 |
return _pygment_highlight( |
|
|
54 | return _pygment_highlight(cell, HtmlFormatter(), language) | |
|
70 | 55 | |
|
71 | 56 | |
|
72 |
def highlight2latex( |
|
|
57 | def highlight2latex(cell, language='ipython'): | |
|
73 | 58 | """ |
|
74 | 59 | Return a syntax-highlighted version of the input source as latex output. |
|
75 | 60 | |
|
76 | 61 | Parameters |
|
77 | 62 | ---------- |
|
78 | source : str | |
|
79 |
|
|
|
63 | cell : NotebookNode cell | |
|
64 | cell to highlight | |
|
80 | 65 | language : str |
|
81 | 66 | Language to highlight the syntax of. |
|
82 | 67 | """ |
|
83 |
return _pygment_highlight( |
|
|
84 | ||
|
85 | ||
|
86 | def which_magic_language(source): | |
|
87 | """ | |
|
88 | When a cell uses another language through a magic extension, | |
|
89 | the other language is returned. | |
|
90 | If no language magic is detected, this function returns None. | |
|
91 | ||
|
92 | Parameters | |
|
93 | ---------- | |
|
94 | source: str | |
|
95 | Source code of the cell to highlight | |
|
96 | """ | |
|
68 | return _pygment_highlight(cell, LatexFormatter(), language) | |
|
97 | 69 | |
|
98 | m = re_magic_language.search(source) | |
|
99 | ||
|
100 | if m: | |
|
101 | # By construction of the re, the matched language must be in the | |
|
102 | # language dictionnary | |
|
103 | assert(m.group(1) in magic_languages) | |
|
104 | return magic_languages[m.group(1)] | |
|
105 | else: | |
|
106 | return None | |
|
107 | 70 | |
|
108 | 71 | |
|
109 |
def _pygment_highlight( |
|
|
72 | def _pygment_highlight(cell, output_formatter, language='ipython'): | |
|
110 | 73 | """ |
|
111 | 74 | Return a syntax-highlighted version of the input source |
|
112 | 75 | |
|
113 | 76 | Parameters |
|
114 | 77 | ---------- |
|
115 | source : str | |
|
116 |
|
|
|
78 | cell : NotebookNode cell | |
|
79 | cell to highlight | |
|
117 | 80 | output_formatter : Pygments formatter |
|
118 | 81 | language : str |
|
119 | 82 | Language to highlight the syntax of. |
|
120 | 83 | """ |
|
121 | 84 | |
|
122 | magic_language = which_magic_language(source) | |
|
123 |
|
|
|
124 | language = magic_language | |
|
85 | # If the cell uses a magic extension language, | |
|
86 | # use the magic language instead. | |
|
87 | if language == 'ipython' \ | |
|
88 | and 'metadata' in cell \ | |
|
89 | and 'magics_language' in cell['metadata']: | |
|
90 | ||
|
91 | language = cell['metadata']['magics_language'] | |
|
125 | 92 | |
|
126 | 93 | if language == 'ipython': |
|
127 | 94 | lexer = IPythonLexer() |
|
128 | 95 | else: |
|
129 | 96 | lexer = get_lexer_by_name(language, stripall=True) |
|
130 | 97 | |
|
131 |
return pygements_highlight( |
|
|
98 | return pygements_highlight(cell, lexer, output_formatter) |
@@ -15,7 +15,7 b' Module with tests for Highlight' | |||
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | from ...tests.base import TestsBase |
|
18 |
from ..highlight import highlight2html, highlight2latex |
|
|
18 | from ..highlight import highlight2html, highlight2latex | |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
@@ -39,59 +39,27 b' class TestHighlight(TestsBase):' | |||
|
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 | def test_highlight2html(self): |
|
49 | 50 | """highlight2html test""" |
|
50 | 51 | for index, test in enumerate(self.tests): |
|
51 | 52 | self._try_highlight(highlight2html, test, self.tokens[index]) |
|
52 | 53 | |
|
54 | ||
|
53 | 55 | def test_highlight2latex(self): |
|
54 | 56 | """highlight2latex test""" |
|
55 | 57 | for index, test in enumerate(self.tests): |
|
56 | 58 | self._try_highlight(highlight2latex, test, self.tokens[index]) |
|
57 | 59 | |
|
60 | ||
|
58 | 61 | def _try_highlight(self, method, test, tokens): |
|
59 | 62 | """Try highlighting source, look for key tokens""" |
|
60 | 63 | results = method(test) |
|
61 | 64 | for token in tokens: |
|
62 | 65 | assert token in results |
|
63 | ||
|
64 | magic_tests = { | |
|
65 | 'r': | |
|
66 | """%%R -i x,y -o XYcoef | |
|
67 | lm.fit <- lm(y~x) | |
|
68 | par(mfrow=c(2,2)) | |
|
69 | print(summary(lm.fit)) | |
|
70 | plot(lm.fit) | |
|
71 | XYcoef <- coef(lm.fit) | |
|
72 | """, | |
|
73 | 'bash': | |
|
74 | """# the following code is in bash | |
|
75 | %%bash | |
|
76 | echo "test" > out | |
|
77 | """, | |
|
78 | 'ipython': | |
|
79 | """# this should not be detected | |
|
80 | print("%%R") | |
|
81 | """ | |
|
82 | } | |
|
83 | ||
|
84 | def test_highlight_rmagic(self): | |
|
85 | """Test %%R magic highlighting""" | |
|
86 | language = which_magic_language(self.magic_tests['r']) | |
|
87 | assert language == 'r' | |
|
88 | ||
|
89 | def test_highlight_bashmagic(self): | |
|
90 | """Test %%bash magic highlighting""" | |
|
91 | language = which_magic_language(self.magic_tests['bash']) | |
|
92 | assert language == 'bash' | |
|
93 | ||
|
94 | def test_highlight_interferemagic(self): | |
|
95 | """Test that magic highlighting does not interfere with ipython code""" | |
|
96 | language = which_magic_language(self.magic_tests['ipython']) | |
|
97 | assert language == None No newline at end of file |
@@ -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 |
|
|
39 | {{ cell | highlight2html }} | |
|
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 |
|
|
287 | ((( cell | highlight2latex ))) | |
|
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 |
|
|
295 | ((( cell | highlight2latex ))) | |
|
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