##// END OF EJS Templates
Merge pull request #4403 from Carreau/global-high...
Min RK -
r13561:94a3c168 merge
parent child Browse files
Show More
@@ -43,8 +43,8 b' default_filters = {'
43 43 'ansi2html': filters.ansi2html,
44 44 'filter_data_type': filters.DataTypeFilter,
45 45 'get_lines': filters.get_lines,
46 'highlight2html': filters.highlight2html,
47 'highlight2latex': filters.highlight2latex,
46 'highlight2html': filters.Highlight2Html,
47 'highlight2latex': filters.Highlight2Latex,
48 48 'ipython2python': filters.ipython2python,
49 49 'posix_path': filters.posix_path,
50 50 'markdown2latex': filters.markdown2latex,
@@ -19,8 +19,10 b' from pygments.lexers import get_lexer_by_name'
19 19 from pygments.formatters import HtmlFormatter
20 20 from pygments.formatters import LatexFormatter
21 21
22
22 23 # Our own imports
23 24 from IPython.nbconvert.utils.lexers import IPythonLexer
25 from IPython.nbconvert.utils.base import NbConvertBase
24 26
25 27 #-----------------------------------------------------------------------------
26 28 # Globals and constants
@@ -33,49 +35,58 b" MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']"
33 35 #-----------------------------------------------------------------------------
34 36
35 37 __all__ = [
36 'highlight2html',
37 'highlight2latex'
38 'Highlight2Html',
39 'Highlight2Latex'
38 40 ]
39 41
40 42
41 def highlight2html(source, language='ipython', metadata=None):
42 """
43 Return a syntax-highlighted version of the input source as html output.
44
45 Parameters
46 ----------
47 source : str
48 source of the cell to highlight
49 language : str
50 language to highlight the syntax of
51 metadata : NotebookNode cell metadata
52 metadata of the cell to highlight
53 """
54
55 return _pygment_highlight(source, HtmlFormatter(), language, metadata)
56
57
58 def highlight2latex(source, language='ipython', metadata=None, strip_verbatim=False):
59 """
60 Return a syntax-highlighted version of the input source as latex output.
61
62 Parameters
63 ----------
64 source : str
65 source of the cell to highlight
66 language : str
67 language to highlight the syntax of
68 metadata : NotebookNode cell metadata
69 metadata of the cell to highlight
70 strip_verbatim : bool
71 remove the Verbatim environment that pygments provides by default
72 """
73 latex = _pygment_highlight(source, LatexFormatter(), language, metadata)
74 if strip_verbatim:
75 latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '')
76 return latex.replace('\n\\end{Verbatim}\n', '')
77 else:
78 return latex
43 class Highlight2Html(NbConvertBase):
44
45 def __call__(self, source, language=None, metadata=None):
46 """
47 Return a syntax-highlighted version of the input source as html output.
48
49 Parameters
50 ----------
51 source : str
52 source of the cell to highlight
53 language : str
54 language to highlight the syntax of
55 metadata : NotebookNode cell metadata
56 metadata of the cell to highlight
57 """
58 if not language:
59 language=self.default_language
60
61 return _pygment_highlight(source, HtmlFormatter(), language, metadata)
62
63
64 class Highlight2Latex(NbConvertBase):
65
66 def __call__(self, source, language=None, metadata=None, strip_verbatim=False):
67 """
68 Return a syntax-highlighted version of the input source as latex output.
69
70 Parameters
71 ----------
72 source : str
73 source of the cell to highlight
74 language : str
75 language to highlight the syntax of
76 metadata : NotebookNode cell metadata
77 metadata of the cell to highlight
78 strip_verbatim : bool
79 remove the Verbatim environment that pygments provides by default
80 """
81 if not language:
82 language=self.default_language
83
84 latex = _pygment_highlight(source, LatexFormatter(), language, metadata)
85 if strip_verbatim:
86 latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '')
87 return latex.replace('\n\\end{Verbatim}\n', '')
88 else:
89 return latex
79 90
80 91
81 92
@@ -15,13 +15,20 b' Module with tests for Highlight'
15 15 #-----------------------------------------------------------------------------
16 16
17 17 from ...tests.base import TestsBase
18 from ..highlight import highlight2html, highlight2latex
19
18 from ..highlight import Highlight2Html, Highlight2Latex
19 from IPython.config import Config
20 import xml
20 21
21 22 #-----------------------------------------------------------------------------
22 23 # Class
23 24 #-----------------------------------------------------------------------------
24 25
26 highlight2html = Highlight2Html()
27 highlight2latex = Highlight2Latex()
28 c = Config()
29 c.Highlight2Html.default_language='ruby'
30 highlight2html_ruby = Highlight2Html(config=c)
31
25 32 class TestHighlight(TestsBase):
26 33 """Contains test functions for highlight.py"""
27 34
@@ -33,6 +40,8 b' class TestHighlight(TestsBase):'
33 40 def say(text):
34 41 print(text)
35 42
43 end
44
36 45 say('Hello World!')
37 46 """,
38 47 """
@@ -57,6 +66,20 b' class TestHighlight(TestsBase):'
57 66 for index, test in enumerate(self.tests):
58 67 self._try_highlight(highlight2latex, test, self.tokens[index])
59 68
69 def test_parse_html_many_lang(self):
70
71 ht = highlight2html(self.tests[0])
72 rb = highlight2html_ruby(self.tests[0])
73
74 for lang,tkns in [
75 ( ht, ('def','print') ),
76 ( rb, ('def','end' ) )
77 ]:
78 root = xml.etree.ElementTree.fromstring(lang)
79 assert self._extract_tokens(root,'k') == set(tkns)
80
81 def _extract_tokens(self, root, cls):
82 return set(map(lambda x:x.text,root.findall(".//*[@class='"+cls+"']")))
60 83
61 84 def _try_highlight(self, method, test, tokens):
62 85 """Try highlighting source, look for key tokens"""
@@ -13,6 +13,7 b''
13 13
14 14 from IPython.utils.traitlets import List
15 15 from IPython.config.configurable import LoggingConfigurable
16 from IPython.utils.traitlets import Unicode
16 17
17 18 #-----------------------------------------------------------------------------
18 19 # Classes and functions
@@ -33,5 +34,7 b' class NbConvertBase(LoggingConfigurable):'
33 34 """
34 35 )
35 36
37 default_language = Unicode('ipython', config=True, help='default highlight language')
38
36 39 def __init__(self, **kw):
37 40 super(NbConvertBase, self).__init__(**kw)
General Comments 0
You need to be logged in to leave comments. Login now