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