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