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