Show More
@@ -1,104 +1,111 | |||||
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 | 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 | '%%octave': 'octave', |
|
43 | '%%octave': 'octave', | |
44 | '%%perl': 'perl', |
|
44 | '%%perl': 'perl', | |
45 | '%%ruby': 'ruby'}, |
|
45 | '%%ruby': 'ruby'}, | |
|
46 | config=False) | |||
|
47 | ||||
|
48 | # user defined language extensions | |||
|
49 | languages = Dict( | |||
46 | config=True, |
|
50 | config=True, | |
47 | help=("Syntax highlighting for magic's extension languages. " |
|
51 | help=("Syntax highlighting for magic's extension languages. " | |
48 | "Each item associates a language magic extension such as %%R, " |
|
52 | "Each item associates a language magic extension such as %%R, " | |
49 | "with a pygments lexer such as r.")) |
|
53 | "with a pygments lexer such as r.")) | |
50 |
|
54 | |||
51 | def __init__(self, config=None, **kw): |
|
55 | def __init__(self, config=None, **kw): | |
52 | """Public constructor""" |
|
56 | """Public constructor""" | |
53 |
|
57 | |||
54 | super(HighlightMagicsPreprocessor, self).__init__(config=config, **kw) |
|
58 | super(HighlightMagicsPreprocessor, self).__init__(config=config, **kw) | |
55 |
|
59 | |||
|
60 | # Update the default languages dict with the user configured ones | |||
|
61 | self.default_languages.update(self.languages) | |||
|
62 | ||||
56 | # build a regular expression to catch language extensions and choose |
|
63 | # build a regular expression to catch language extensions and choose | |
57 | # an adequate pygments lexer |
|
64 | # an adequate pygments lexer | |
58 | any_language = "|".join(self.languages.keys()) |
|
65 | any_language = "|".join(self.default_languages.keys()) | |
59 | self.re_magic_language = re.compile( |
|
66 | self.re_magic_language = re.compile( | |
60 | r'^\s*({0})\s+'.format(any_language)) |
|
67 | r'^\s*({0})\s+'.format(any_language)) | |
61 |
|
68 | |||
62 | def which_magic_language(self, source): |
|
69 | def which_magic_language(self, source): | |
63 | """ |
|
70 | """ | |
64 | When a cell uses another language through a magic extension, |
|
71 | When a cell uses another language through a magic extension, | |
65 | the other language is returned. |
|
72 | the other language is returned. | |
66 | If no language magic is detected, this function returns None. |
|
73 | If no language magic is detected, this function returns None. | |
67 |
|
74 | |||
68 | Parameters |
|
75 | Parameters | |
69 | ---------- |
|
76 | ---------- | |
70 | source: str |
|
77 | source: str | |
71 | Source code of the cell to highlight |
|
78 | Source code of the cell to highlight | |
72 | """ |
|
79 | """ | |
73 |
|
80 | |||
74 | m = self.re_magic_language.match(source) |
|
81 | m = self.re_magic_language.match(source) | |
75 |
|
82 | |||
76 | if m: |
|
83 | if m: | |
77 | # By construction of the re, the matched language must be in the |
|
84 | # By construction of the re, the matched language must be in the | |
78 | # languages dictionnary |
|
85 | # languages dictionnary | |
79 | assert(m.group(1) in self.languages) |
|
86 | assert(m.group(1) in self.default_languages) | |
80 | return self.languages[m.group(1)] |
|
87 | return self.default_languages[m.group(1)] | |
81 | else: |
|
88 | else: | |
82 | return None |
|
89 | return None | |
83 |
|
90 | |||
84 | def preprocess_cell(self, cell, resources, cell_index): |
|
91 | def preprocess_cell(self, cell, resources, cell_index): | |
85 | """ |
|
92 | """ | |
86 | Tags cells using a magic extension language |
|
93 | Tags cells using a magic extension language | |
87 |
|
94 | |||
88 | Parameters |
|
95 | Parameters | |
89 | ---------- |
|
96 | ---------- | |
90 | cell : NotebookNode cell |
|
97 | cell : NotebookNode cell | |
91 | Notebook cell being processed |
|
98 | Notebook cell being processed | |
92 | resources : dictionary |
|
99 | resources : dictionary | |
93 | Additional resources used in the conversion process. Allows |
|
100 | Additional resources used in the conversion process. Allows | |
94 | preprocessors to pass variables into the Jinja engine. |
|
101 | preprocessors to pass variables into the Jinja engine. | |
95 | cell_index : int |
|
102 | cell_index : int | |
96 | Index of the cell being processed (see base.py) |
|
103 | Index of the cell being processed (see base.py) | |
97 | """ |
|
104 | """ | |
98 |
|
105 | |||
99 | # Only tag code cells |
|
106 | # Only tag code cells | |
100 | if hasattr(cell, "input") and cell.cell_type == "code": |
|
107 | if hasattr(cell, "input") and cell.cell_type == "code": | |
101 | magic_language = self.which_magic_language(cell.input) |
|
108 | magic_language = self.which_magic_language(cell.input) | |
102 | if magic_language: |
|
109 | if magic_language: | |
103 | cell['metadata']['magics_language'] = magic_language |
|
110 | cell['metadata']['magics_language'] = magic_language | |
104 | return cell, resources |
|
111 | return cell, resources |
General Comments 0
You need to be logged in to leave comments.
Login now