From 94a3c168ad99ef37654cc06ee996de41a252d11b 2013-11-12 19:20:03 From: Min RK Date: 2013-11-12 19:20:03 Subject: [PATCH] Merge pull request #4403 from Carreau/global-high set global highlight default --- diff --git a/IPython/nbconvert/exporters/templateexporter.py b/IPython/nbconvert/exporters/templateexporter.py index 10d6b3e..e50bd36 100644 --- a/IPython/nbconvert/exporters/templateexporter.py +++ b/IPython/nbconvert/exporters/templateexporter.py @@ -43,8 +43,8 @@ default_filters = { 'ansi2html': filters.ansi2html, 'filter_data_type': filters.DataTypeFilter, 'get_lines': filters.get_lines, - 'highlight2html': filters.highlight2html, - 'highlight2latex': filters.highlight2latex, + 'highlight2html': filters.Highlight2Html, + 'highlight2latex': filters.Highlight2Latex, 'ipython2python': filters.ipython2python, 'posix_path': filters.posix_path, 'markdown2latex': filters.markdown2latex, diff --git a/IPython/nbconvert/filters/highlight.py b/IPython/nbconvert/filters/highlight.py index b390e6c..e246d7b 100644 --- a/IPython/nbconvert/filters/highlight.py +++ b/IPython/nbconvert/filters/highlight.py @@ -19,8 +19,10 @@ from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter from pygments.formatters import LatexFormatter + # Our own imports from IPython.nbconvert.utils.lexers import IPythonLexer +from IPython.nbconvert.utils.base import NbConvertBase #----------------------------------------------------------------------------- # Globals and constants @@ -33,49 +35,58 @@ MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] #----------------------------------------------------------------------------- __all__ = [ - 'highlight2html', - 'highlight2latex' + 'Highlight2Html', + 'Highlight2Latex' ] -def highlight2html(source, language='ipython', metadata=None): - """ - Return a syntax-highlighted version of the input source as html output. - - Parameters - ---------- - source : str - source of the cell to highlight - language : str - language to highlight the syntax of - metadata : NotebookNode cell metadata - metadata of the cell to highlight - """ - - return _pygment_highlight(source, HtmlFormatter(), language, metadata) - - -def highlight2latex(source, language='ipython', metadata=None, strip_verbatim=False): - """ - Return a syntax-highlighted version of the input source as latex output. - - Parameters - ---------- - source : str - source of the cell to highlight - language : str - language to highlight the syntax of - metadata : NotebookNode cell metadata - metadata of the cell to highlight - strip_verbatim : bool - remove the Verbatim environment that pygments provides by default - """ - latex = _pygment_highlight(source, LatexFormatter(), language, metadata) - if strip_verbatim: - latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '') - return latex.replace('\n\\end{Verbatim}\n', '') - else: - return latex +class Highlight2Html(NbConvertBase): + + def __call__(self, source, language=None, metadata=None): + """ + Return a syntax-highlighted version of the input source as html output. + + Parameters + ---------- + source : str + source of the cell to highlight + language : str + language to highlight the syntax of + metadata : NotebookNode cell metadata + metadata of the cell to highlight + """ + if not language: + language=self.default_language + + return _pygment_highlight(source, HtmlFormatter(), language, metadata) + + +class Highlight2Latex(NbConvertBase): + + def __call__(self, source, language=None, metadata=None, strip_verbatim=False): + """ + Return a syntax-highlighted version of the input source as latex output. + + Parameters + ---------- + source : str + source of the cell to highlight + language : str + language to highlight the syntax of + metadata : NotebookNode cell metadata + metadata of the cell to highlight + strip_verbatim : bool + remove the Verbatim environment that pygments provides by default + """ + if not language: + language=self.default_language + + latex = _pygment_highlight(source, LatexFormatter(), language, metadata) + if strip_verbatim: + latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '') + return latex.replace('\n\\end{Verbatim}\n', '') + else: + return latex diff --git a/IPython/nbconvert/filters/tests/test_highlight.py b/IPython/nbconvert/filters/tests/test_highlight.py index df25a42..ed71ff4 100644 --- a/IPython/nbconvert/filters/tests/test_highlight.py +++ b/IPython/nbconvert/filters/tests/test_highlight.py @@ -15,13 +15,20 @@ Module with tests for Highlight #----------------------------------------------------------------------------- from ...tests.base import TestsBase -from ..highlight import highlight2html, highlight2latex - +from ..highlight import Highlight2Html, Highlight2Latex +from IPython.config import Config +import xml #----------------------------------------------------------------------------- # Class #----------------------------------------------------------------------------- +highlight2html = Highlight2Html() +highlight2latex = Highlight2Latex() +c = Config() +c.Highlight2Html.default_language='ruby' +highlight2html_ruby = Highlight2Html(config=c) + class TestHighlight(TestsBase): """Contains test functions for highlight.py""" @@ -33,6 +40,8 @@ class TestHighlight(TestsBase): def say(text): print(text) + end + say('Hello World!') """, """ @@ -57,6 +66,20 @@ class TestHighlight(TestsBase): for index, test in enumerate(self.tests): self._try_highlight(highlight2latex, test, self.tokens[index]) + def test_parse_html_many_lang(self): + + ht = highlight2html(self.tests[0]) + rb = highlight2html_ruby(self.tests[0]) + + for lang,tkns in [ + ( ht, ('def','print') ), + ( rb, ('def','end' ) ) + ]: + root = xml.etree.ElementTree.fromstring(lang) + assert self._extract_tokens(root,'k') == set(tkns) + + def _extract_tokens(self, root, cls): + return set(map(lambda x:x.text,root.findall(".//*[@class='"+cls+"']"))) def _try_highlight(self, method, test, tokens): """Try highlighting source, look for key tokens""" diff --git a/IPython/nbconvert/utils/base.py b/IPython/nbconvert/utils/base.py index 85e8426..08ca3e2 100644 --- a/IPython/nbconvert/utils/base.py +++ b/IPython/nbconvert/utils/base.py @@ -13,6 +13,7 @@ from IPython.utils.traitlets import List from IPython.config.configurable import LoggingConfigurable +from IPython.utils.traitlets import Unicode #----------------------------------------------------------------------------- # Classes and functions @@ -33,5 +34,7 @@ class NbConvertBase(LoggingConfigurable): """ ) + default_language = Unicode('ipython', config=True, help='default highlight language') + def __init__(self, **kw): super(NbConvertBase, self).__init__(**kw)