Show More
@@ -1,116 +1,115 b'' | |||||
1 | """This preprocessor detect cells using a different language through |
|
1 | """This preprocessor detect cells using a different language through | |
2 | magic extensions such as `%%R` or `%%octave`. Cell's metadata is marked |
|
2 | magic extensions such as `%%R` or `%%octave`. Cell's metadata is marked | |
3 | so that the appropriate highlighter can be used in the `highlight` |
|
3 | so that the appropriate highlighter can be used in the `highlight` | |
4 | filter. |
|
4 | filter. | |
5 | """ |
|
5 | """ | |
6 |
|
6 | |||
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (c) 2013, the IPython Development Team. |
|
8 | # Copyright (c) 2013, the IPython Development Team. | |
9 | # |
|
9 | # | |
10 | # Distributed under the terms of the Modified BSD License. |
|
10 | # Distributed under the terms of the Modified BSD License. | |
11 | # |
|
11 | # | |
12 | # The full license is in the file COPYING.txt, distributed with this software. |
|
12 | # The full license is in the file COPYING.txt, distributed with this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | from __future__ import print_function, absolute_import |
|
19 | from __future__ import print_function, absolute_import | |
20 |
|
20 | |||
21 | import re |
|
21 | import re | |
22 |
|
22 | |||
23 | # Our own imports |
|
23 | # Our own imports | |
24 | # Needed to override preprocessor |
|
24 | # Needed to override preprocessor | |
25 | from .base import (Preprocessor) |
|
25 | from .base import (Preprocessor) | |
26 | from IPython.utils.traitlets import Dict |
|
26 | from IPython.utils.traitlets import Dict | |
27 |
|
27 | |||
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 | # Classes |
|
29 | # Classes | |
30 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | class HighlightMagicsPreprocessor(Preprocessor): |
|
33 | class HighlightMagicsPreprocessor(Preprocessor): | |
34 | """ |
|
34 | """ | |
35 | Detects and tags code cells that use a different languages than Python. |
|
35 | Detects and tags code cells that use a different languages than Python. | |
36 | """ |
|
36 | """ | |
37 |
|
37 | |||
38 | # list of magic language extensions and their associated pygment lexers |
|
38 | # list of magic language extensions and their associated pygment lexers | |
39 | default_languages = Dict( |
|
39 | default_languages = Dict( | |
40 | default_value={ |
|
40 | default_value={ | |
41 | '%%R': 'r', |
|
41 | '%%R': 'r', | |
42 | '%%bash': 'bash', |
|
42 | '%%bash': 'bash', | |
43 | '%%cython': 'cython', |
|
43 | '%%cython': 'cython', | |
44 | '%%javascript': 'javascript', |
|
44 | '%%javascript': 'javascript', | |
45 | '%%julia': 'julia', |
|
45 | '%%julia': 'julia', | |
46 | '%%latex': 'latex', |
|
46 | '%%latex': 'latex', | |
47 | '%%octave': 'octave', |
|
47 | '%%octave': 'octave', | |
48 | '%%perl': 'perl', |
|
48 | '%%perl': 'perl', | |
49 | '%%ruby': 'ruby', |
|
49 | '%%ruby': 'ruby', | |
50 |
'%%sh': 'sh'} |
|
50 | '%%sh': 'sh'}) | |
51 | config=False) |
|
|||
52 |
|
51 | |||
53 | # user defined language extensions |
|
52 | # user defined language extensions | |
54 | languages = Dict( |
|
53 | languages = Dict( | |
55 | config=True, |
|
54 | config=True, | |
56 | help=("Syntax highlighting for magic's extension languages. " |
|
55 | help=("Syntax highlighting for magic's extension languages. " | |
57 | "Each item associates a language magic extension such as %%R, " |
|
56 | "Each item associates a language magic extension such as %%R, " | |
58 | "with a pygments lexer such as r.")) |
|
57 | "with a pygments lexer such as r.")) | |
59 |
|
58 | |||
60 | def __init__(self, config=None, **kw): |
|
59 | def __init__(self, config=None, **kw): | |
61 | """Public constructor""" |
|
60 | """Public constructor""" | |
62 |
|
61 | |||
63 | super(HighlightMagicsPreprocessor, self).__init__(config=config, **kw) |
|
62 | super(HighlightMagicsPreprocessor, self).__init__(config=config, **kw) | |
64 |
|
63 | |||
65 | # Update the default languages dict with the user configured ones |
|
64 | # Update the default languages dict with the user configured ones | |
66 | self.default_languages.update(self.languages) |
|
65 | self.default_languages.update(self.languages) | |
67 |
|
66 | |||
68 | # build a regular expression to catch language extensions and choose |
|
67 | # build a regular expression to catch language extensions and choose | |
69 | # an adequate pygments lexer |
|
68 | # an adequate pygments lexer | |
70 | any_language = "|".join(self.default_languages.keys()) |
|
69 | any_language = "|".join(self.default_languages.keys()) | |
71 | self.re_magic_language = re.compile( |
|
70 | self.re_magic_language = re.compile( | |
72 | r'^\s*({0})\s+'.format(any_language)) |
|
71 | r'^\s*({0})\s+'.format(any_language)) | |
73 |
|
72 | |||
74 | def which_magic_language(self, source): |
|
73 | def which_magic_language(self, source): | |
75 | """ |
|
74 | """ | |
76 | When a cell uses another language through a magic extension, |
|
75 | When a cell uses another language through a magic extension, | |
77 | the other language is returned. |
|
76 | the other language is returned. | |
78 | If no language magic is detected, this function returns None. |
|
77 | If no language magic is detected, this function returns None. | |
79 |
|
78 | |||
80 | Parameters |
|
79 | Parameters | |
81 | ---------- |
|
80 | ---------- | |
82 | source: str |
|
81 | source: str | |
83 | Source code of the cell to highlight |
|
82 | Source code of the cell to highlight | |
84 | """ |
|
83 | """ | |
85 |
|
84 | |||
86 | m = self.re_magic_language.match(source) |
|
85 | m = self.re_magic_language.match(source) | |
87 |
|
86 | |||
88 | if m: |
|
87 | if m: | |
89 | # By construction of the re, the matched language must be in the |
|
88 | # By construction of the re, the matched language must be in the | |
90 | # languages dictionnary |
|
89 | # languages dictionnary | |
91 | assert(m.group(1) in self.default_languages) |
|
90 | assert(m.group(1) in self.default_languages) | |
92 | return self.default_languages[m.group(1)] |
|
91 | return self.default_languages[m.group(1)] | |
93 | else: |
|
92 | else: | |
94 | return None |
|
93 | return None | |
95 |
|
94 | |||
96 | def preprocess_cell(self, cell, resources, cell_index): |
|
95 | def preprocess_cell(self, cell, resources, cell_index): | |
97 | """ |
|
96 | """ | |
98 | Tags cells using a magic extension language |
|
97 | Tags cells using a magic extension language | |
99 |
|
98 | |||
100 | Parameters |
|
99 | Parameters | |
101 | ---------- |
|
100 | ---------- | |
102 | cell : NotebookNode cell |
|
101 | cell : NotebookNode cell | |
103 | Notebook cell being processed |
|
102 | Notebook cell being processed | |
104 | resources : dictionary |
|
103 | resources : dictionary | |
105 | Additional resources used in the conversion process. Allows |
|
104 | Additional resources used in the conversion process. Allows | |
106 | preprocessors to pass variables into the Jinja engine. |
|
105 | preprocessors to pass variables into the Jinja engine. | |
107 | cell_index : int |
|
106 | cell_index : int | |
108 | Index of the cell being processed (see base.py) |
|
107 | Index of the cell being processed (see base.py) | |
109 | """ |
|
108 | """ | |
110 |
|
109 | |||
111 | # Only tag code cells |
|
110 | # Only tag code cells | |
112 | if hasattr(cell, "input") and cell.cell_type == "code": |
|
111 | if hasattr(cell, "input") and cell.cell_type == "code": | |
113 | magic_language = self.which_magic_language(cell.input) |
|
112 | magic_language = self.which_magic_language(cell.input) | |
114 | if magic_language: |
|
113 | if magic_language: | |
115 | cell['metadata']['magics_language'] = magic_language |
|
114 | cell['metadata']['magics_language'] = magic_language | |
116 | return cell, resources |
|
115 | return cell, resources |
General Comments 0
You need to be logged in to leave comments.
Login now