ipython/ipython — pull request #4187 by pablooliveira (2013-09-09 16:20:54+00:00)
Title: Select adequate highlighter for cell magic languages

Detect cells that use language extension magics such as %%R or %%bash
and call the appropriate pygmentize lexer.

Closes #4184

--
@Carreau
>Sure, wherever the PR come from, it will be reviewed, we don't treat regular of new user differently. Just ask if >we are not clear on how to do things, it is often hard to judge how people are used to code.

>You will probably need an heuristic in nbconvert preprocessor that inspect the cell source and decide for a >highlighting scheme, stuff this in metadata along with the cell, and use those metadata to feed them to the >hightlight filter in templates.

>@jdfreder should also be able to help you too.

Thanks ! I first tried to follow your proposed architecture, using a preprocessor.
Writing the preprocessor and tagging the cell metadata is feasible.

The problem comes when the highlight filter is called. Indeed the higlight filter interface
only sees the cell content,
```python
def _pygment_highlight(source, output_formatter, language='ipython'):
```
therefore the metadata is lost at that point.

Since the preprocessor was anyways deciding the extension language based on the
cell content, I propose the following simpler (but less modular) implementation.

Thanks for your review,

Pablo