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