##// END OF EJS Templates
Fix style
Pablo de Oliveira -
Show More
@@ -1,114 +1,113 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 from .base import Preprocessor
25 from .base import (Preprocessor)
26 from IPython.utils.traitlets import Dict
25 from IPython.utils.traitlets import Dict
27
26
28 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
29 # Classes
28 # Classes
30 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
31
30
32
31
33 class HighlightMagicsPreprocessor(Preprocessor):
32 class HighlightMagicsPreprocessor(Preprocessor):
34 """
33 """
35 Detects and tags code cells that use a different languages than Python.
34 Detects and tags code cells that use a different languages than Python.
36 """
35 """
37
36
38 # list of magic language extensions and their associated pygment lexers
37 # list of magic language extensions and their associated pygment lexers
39 default_languages = Dict(
38 default_languages = Dict(
40 default_value={
39 default_value={
41 '%%R': 'r',
40 '%%R': 'r',
42 '%%bash': 'bash',
41 '%%bash': 'bash',
43 '%%cython': 'cython',
42 '%%cython': 'cython',
44 '%%javascript': 'javascript',
43 '%%javascript': 'javascript',
45 '%%julia': 'julia',
44 '%%julia': 'julia',
46 '%%latex': 'latex',
45 '%%latex': 'latex',
47 '%%octave': 'octave',
46 '%%octave': 'octave',
48 '%%perl': 'perl',
47 '%%perl': 'perl',
49 '%%ruby': 'ruby',
48 '%%ruby': 'ruby',
50 '%%sh': 'sh'})
49 '%%sh': 'sh'})
51
50
52 # user defined language extensions
51 # user defined language extensions
53 languages = Dict(
52 languages = Dict(
54 config=True,
53 config=True,
55 help=("Syntax highlighting for magic's extension languages. "
54 help=("Syntax highlighting for magic's extension languages. "
56 "Each item associates a language magic extension such as %%R, "
55 "Each item associates a language magic extension such as %%R, "
57 "with a pygments lexer such as r."))
56 "with a pygments lexer such as r."))
58
57
59 def __init__(self, config=None, **kw):
58 def __init__(self, config=None, **kw):
60 """Public constructor"""
59 """Public constructor"""
61
60
62 super(HighlightMagicsPreprocessor, self).__init__(config=config, **kw)
61 super(HighlightMagicsPreprocessor, self).__init__(config=config, **kw)
63
62
64 # Update the default languages dict with the user configured ones
63 # Update the default languages dict with the user configured ones
65 self.default_languages.update(self.languages)
64 self.default_languages.update(self.languages)
66
65
67 # build a regular expression to catch language extensions and choose
66 # build a regular expression to catch language extensions and choose
68 # an adequate pygments lexer
67 # an adequate pygments lexer
69 any_language = "|".join(self.default_languages.keys())
68 any_language = "|".join(self.default_languages.keys())
70 self.re_magic_language = re.compile(
69 self.re_magic_language = re.compile(
71 r'^\s*({0})\s+'.format(any_language))
70 r'^\s*({0})\s+'.format(any_language))
72
71
73 def which_magic_language(self, source):
72 def which_magic_language(self, source):
74 """
73 """
75 When a cell uses another language through a magic extension,
74 When a cell uses another language through a magic extension,
76 the other language is returned.
75 the other language is returned.
77 If no language magic is detected, this function returns None.
76 If no language magic is detected, this function returns None.
78
77
79 Parameters
78 Parameters
80 ----------
79 ----------
81 source: str
80 source: str
82 Source code of the cell to highlight
81 Source code of the cell to highlight
83 """
82 """
84
83
85 m = self.re_magic_language.match(source)
84 m = self.re_magic_language.match(source)
86
85
87 if m:
86 if m:
88 # By construction of the re, the matched language must be in the
87 # By construction of the re, the matched language must be in the
89 # languages dictionary
88 # languages dictionary
90 return self.default_languages[m.group(1)]
89 return self.default_languages[m.group(1)]
91 else:
90 else:
92 return None
91 return None
93
92
94 def preprocess_cell(self, cell, resources, cell_index):
93 def preprocess_cell(self, cell, resources, cell_index):
95 """
94 """
96 Tags cells using a magic extension language
95 Tags cells using a magic extension language
97
96
98 Parameters
97 Parameters
99 ----------
98 ----------
100 cell : NotebookNode cell
99 cell : NotebookNode cell
101 Notebook cell being processed
100 Notebook cell being processed
102 resources : dictionary
101 resources : dictionary
103 Additional resources used in the conversion process. Allows
102 Additional resources used in the conversion process. Allows
104 preprocessors to pass variables into the Jinja engine.
103 preprocessors to pass variables into the Jinja engine.
105 cell_index : int
104 cell_index : int
106 Index of the cell being processed (see base.py)
105 Index of the cell being processed (see base.py)
107 """
106 """
108
107
109 # Only tag code cells
108 # Only tag code cells
110 if hasattr(cell, "input") and cell.cell_type == "code":
109 if hasattr(cell, "input") and cell.cell_type == "code":
111 magic_language = self.which_magic_language(cell.input)
110 magic_language = self.which_magic_language(cell.input)
112 if magic_language:
111 if magic_language:
113 cell['metadata']['magics_language'] = magic_language
112 cell['metadata']['magics_language'] = magic_language
114 return cell, resources
113 return cell, resources
General Comments 0
You need to be logged in to leave comments. Login now