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