##// END OF EJS Templates
Require jsonschema for nbformat, html and nbconvert tests
Require jsonschema for nbformat, html and nbconvert tests

File last commit:

r12587:8563f83d
r16981:96681f4a
Show More
highlightmagics.py
113 lines | 3.8 KiB | text/x-python | PythonLexer
Pablo de Oliveira
Add HighlightMagicsPreprocessor...
r12573 """This preprocessor detect cells using a different language through
magic extensions such as `%%R` or `%%octave`. Cell's metadata is marked
so that the appropriate highlighter can be used in the `highlight`
filter.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function, absolute_import
import re
# Our own imports
Pablo de Oliveira
Fix style
r12587 from .base import Preprocessor
Pablo de Oliveira
Add HighlightMagicsPreprocessor...
r12573 from IPython.utils.traitlets import Dict
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
class HighlightMagicsPreprocessor(Preprocessor):
"""
Detects and tags code cells that use a different languages than Python.
"""
# list of magic language extensions and their associated pygment lexers
Pablo de Oliveira
HighlightMagics: merge default languages with the user config languages...
r12575 default_languages = Dict(
Pablo de Oliveira
Add HighlightMagicsPreprocessor...
r12573 default_value={
'%%R': 'r',
'%%bash': 'bash',
Pablo de Oliveira
HighlightMagics: Add new default languages
r12576 '%%cython': 'cython',
'%%javascript': 'javascript',
'%%julia': 'julia',
'%%latex': 'latex',
Pablo de Oliveira
Add HighlightMagicsPreprocessor...
r12573 '%%octave': 'octave',
'%%perl': 'perl',
Pablo de Oliveira
HighlightMagics: Add new default languages
r12576 '%%ruby': 'ruby',
Pablo de Oliveira
HighlightMagics: Remove unnecessary config=false
r12579 '%%sh': 'sh'})
Pablo de Oliveira
HighlightMagics: merge default languages with the user config languages...
r12575
# user defined language extensions
languages = Dict(
Pablo de Oliveira
Add HighlightMagicsPreprocessor...
r12573 config=True,
help=("Syntax highlighting for magic's extension languages. "
"Each item associates a language magic extension such as %%R, "
"with a pygments lexer such as r."))
def __init__(self, config=None, **kw):
"""Public constructor"""
super(HighlightMagicsPreprocessor, self).__init__(config=config, **kw)
Pablo de Oliveira
HighlightMagics: merge default languages with the user config languages...
r12575 # Update the default languages dict with the user configured ones
self.default_languages.update(self.languages)
Pablo de Oliveira
Add HighlightMagicsPreprocessor...
r12573 # build a regular expression to catch language extensions and choose
# an adequate pygments lexer
Pablo de Oliveira
HighlightMagics: merge default languages with the user config languages...
r12575 any_language = "|".join(self.default_languages.keys())
Pablo de Oliveira
Add HighlightMagicsPreprocessor...
r12573 self.re_magic_language = re.compile(
r'^\s*({0})\s+'.format(any_language))
def which_magic_language(self, source):
"""
When a cell uses another language through a magic extension,
the other language is returned.
If no language magic is detected, this function returns None.
Parameters
----------
source: str
Source code of the cell to highlight
"""
m = self.re_magic_language.match(source)
if m:
# By construction of the re, the matched language must be in the
Pablo de Oliveira
HighlightMagics: Remove assert and fix typo
r12580 # languages dictionary
Pablo de Oliveira
HighlightMagics: merge default languages with the user config languages...
r12575 return self.default_languages[m.group(1)]
Pablo de Oliveira
Add HighlightMagicsPreprocessor...
r12573 else:
return None
def preprocess_cell(self, cell, resources, cell_index):
"""
Tags cells using a magic extension language
Parameters
----------
cell : NotebookNode cell
Notebook cell being processed
resources : dictionary
Additional resources used in the conversion process. Allows
preprocessors to pass variables into the Jinja engine.
cell_index : int
Index of the cell being processed (see base.py)
"""
# Only tag code cells
if hasattr(cell, "input") and cell.cell_type == "code":
magic_language = self.which_magic_language(cell.input)
if magic_language:
cell['metadata']['magics_language'] = magic_language
return cell, resources