# -*- coding: utf-8 -*- r""" These pre and postprocessor preserve highlights when converting the notebook to html or LaTeX. Preprocessor: - for html conversion: the preprocessor replaces html tags by a "neutral" text versions, which enables markdown conversion of text included in the data field e.g. :: *text* is translated into:: !oph!span class="mark"!clh! *text* !oph!/span!clh! - for LaTeX conversion: the preprocessor replaces html tags by a "neutral" text version of some associated LaTeX commands or environments. This, again enables markdown conversion of text included in the command or environment. e.g. :: *text* is translated into:: !sl!highlighta!op! *text* !cl! Postprocessor: - replaces the "neutral" text versions by the destination language tags e.g. the [html example] becomes :: text (the data text have been correctly emphasized) e.g. [LaTeX example] becomes :: \highlighta{\emph{text}} (the data text have been correctly emphasized) The LaTeX commands and environments are defined in the LaTeX template highlighter.tplx """ from __future__ import print_function import re from nbconvert.postprocessors.base import PostProcessorBase from nbconvert.preprocessors import Preprocessor class HighlighterPreprocessor(Preprocessor): """ :mod:`nbconvert` Preprocessor for the ``highlighter`` nbextension. The preprocessor replaces highlighter html tags in markdown with a "neutral" text version, which enables markdown conversion of text included in the data field, command or environment. Then the neutral text is translated into LaTeX/html output by the corresponding :class:`HighlighterPostProcessor`. For example the highlighter-created markdown :: *text* is translated for html conversion into:: !oph!span class="mark"!clh! *text* !oph!/span!clh! or for LaTeX conversion is translated into:: !sl!highlighta!op! *text* !cl! """ def latex_scheme_cell(self, match): schemes = { "mark": "highlightA", "burk": "highlightB", "girk": "highlightC" } return ("!sl!begin!op!" + schemes[match.group(1)] + '!cl!\n' + match.group(2) + "\n!sl!end!op!" + schemes[match.group(1)] + '!cl!\n') def latex_scheme(self, match): schemes = { "mark": r"!sl!highlighta", "burk": r"!sl!highlightb", "girk": r"!sl!highlightc" } return schemes[match.group(1)] + '!op!' + match.group(2) + '!cl!' def html_replacements(self, match): return match.group(0).replace("<", "!oph!").replace(">", "!clh!") def replace_highlights_with_latex(self, cell_text): cell_text = re.sub( "^