ROOT  6.06/09
Reference Guide
cpphighlighter.py
Go to the documentation of this file.
1 """This preprocessor marks cell's metadata so that the appropriate
2 highlighter can be used in the `highlight` filter afterwards.
3 More precisely, the language of a cell is set to C++ in two scenarios:
4 - Python notebooks: cells with `%%cpp` or `%%dcl` magic extensions.
5 - ROOT prompt C++ notebooks: all cells.
6 This preprocessor relies on the metadata section of the notebook to
7 find out about the notebook's language.
8 """
9 
10 from IPython.nbconvert.preprocessors.base import Preprocessor
11 import re
12 
13 
14 class CppHighlighter(Preprocessor):
15  """
16  Detects and tags code cells that use the C++ language.
17  """
18 
19  magics = [ '%%cpp', '%%dcl' ]
20  cpp = 'cpp'
21  python = 'python'
22 
23  def __init__(self, config=None, **kw):
24  super(CppHighlighter, self).__init__(config=config, **kw)
25 
26  # Build regular expressions to catch language extensions or switches and choose
27  # an adequate pygments lexer
28  any_magic = "|".join(self.magics)
29  self.re_magic_language = re.compile(r"^\s*({0}).*".format(any_magic), re.DOTALL)
30 
31  def matches(self, source, reg_expr):
32  m = reg_expr.match(source)
33  if m:
34  return True
35  else:
36  return False
37 
38  def _preprocess_cell_python(self, cell, resources, cell_index):
39  # Mark %%cpp and %%dcl code cells as cpp
40  if cell.cell_type == "code" and self.matches(cell.source, self.re_magic_language):
41  cell['metadata']['magics_language'] = self.cpp
42 
43  return cell, resources
44 
45  def _preprocess_cell_cpp(self, cell, resources, cell_index):
46  # Mark all code cells as cpp
47  if cell.cell_type == "code":
48  cell['metadata']['magics_language'] = self.cpp
49 
50  return cell, resources
51 
52  def preprocess(self, nb, resources):
54  try:
55  if nb.metadata.kernelspec.language == "c++":
57  except:
58  # if no language metadata, keep python as default
59  pass
60  return super(CppHighlighter, self).preprocess(nb, resources)
static std::string format(double x, double y, int digits, int width)
def _preprocess_cell_cpp(self, cell, resources, cell_index)
def _preprocess_cell_python(self, cell, resources, cell_index)