##// 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 'ansi2html': filters.ansi2html,
43 'ansi2html': filters.ansi2html,
44 'filter_data_type': filters.DataTypeFilter,
44 'filter_data_type': filters.DataTypeFilter,
45 'get_lines': filters.get_lines,
45 'get_lines': filters.get_lines,
46 'highlight2html': filters.highlight2html,
46 'highlight2html': filters.Highlight2Html,
47 'highlight2latex': filters.highlight2latex,
47 'highlight2latex': filters.Highlight2Latex,
48 'ipython2python': filters.ipython2python,
48 'ipython2python': filters.ipython2python,
49 'posix_path': filters.posix_path,
49 'posix_path': filters.posix_path,
50 'markdown2latex': filters.markdown2latex,
50 'markdown2latex': filters.markdown2latex,
@@ -19,8 +19,10 b' from pygments.lexers import get_lexer_by_name'
19 from pygments.formatters import HtmlFormatter
19 from pygments.formatters import HtmlFormatter
20 from pygments.formatters import LatexFormatter
20 from pygments.formatters import LatexFormatter
21
21
22
22 # Our own imports
23 # Our own imports
23 from IPython.nbconvert.utils.lexers import IPythonLexer
24 from IPython.nbconvert.utils.lexers import IPythonLexer
25 from IPython.nbconvert.utils.base import NbConvertBase
24
26
25 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
26 # Globals and constants
28 # Globals and constants
@@ -33,49 +35,58 b" MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']"
33 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
34
36
35 __all__ = [
37 __all__ = [
36 'highlight2html',
38 'Highlight2Html',
37 'highlight2latex'
39 'Highlight2Latex'
38 ]
40 ]
39
41
40
42
41 def highlight2html(source, language='ipython', metadata=None):
43 class Highlight2Html(NbConvertBase):
42 """
44
43 Return a syntax-highlighted version of the input source as html output.
45 def __call__(self, source, language=None, metadata=None):
44
46 """
45 Parameters
47 Return a syntax-highlighted version of the input source as html output.
46 ----------
48
47 source : str
49 Parameters
48 source of the cell to highlight
50 ----------
49 language : str
51 source : str
50 language to highlight the syntax of
52 source of the cell to highlight
51 metadata : NotebookNode cell metadata
53 language : str
52 metadata of the cell to highlight
54 language to highlight the syntax of
53 """
55 metadata : NotebookNode cell metadata
54
56 metadata of the cell to highlight
55 return _pygment_highlight(source, HtmlFormatter(), language, metadata)
57 """
56
58 if not language:
57
59 language=self.default_language
58 def highlight2latex(source, language='ipython', metadata=None, strip_verbatim=False):
60
59 """
61 return _pygment_highlight(source, HtmlFormatter(), language, metadata)
60 Return a syntax-highlighted version of the input source as latex output.
62
61
63
62 Parameters
64 class Highlight2Latex(NbConvertBase):
63 ----------
65
64 source : str
66 def __call__(self, source, language=None, metadata=None, strip_verbatim=False):
65 source of the cell to highlight
67 """
66 language : str
68 Return a syntax-highlighted version of the input source as latex output.
67 language to highlight the syntax of
69
68 metadata : NotebookNode cell metadata
70 Parameters
69 metadata of the cell to highlight
71 ----------
70 strip_verbatim : bool
72 source : str
71 remove the Verbatim environment that pygments provides by default
73 source of the cell to highlight
72 """
74 language : str
73 latex = _pygment_highlight(source, LatexFormatter(), language, metadata)
75 language to highlight the syntax of
74 if strip_verbatim:
76 metadata : NotebookNode cell metadata
75 latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '')
77 metadata of the cell to highlight
76 return latex.replace('\n\\end{Verbatim}\n', '')
78 strip_verbatim : bool
77 else:
79 remove the Verbatim environment that pygments provides by default
78 return latex
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 from ...tests.base import TestsBase
17 from ...tests.base import TestsBase
18 from ..highlight import highlight2html, highlight2latex
18 from ..highlight import Highlight2Html, Highlight2Latex
19
19 from IPython.config import Config
20 import xml
20
21
21 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
22 # Class
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 class TestHighlight(TestsBase):
32 class TestHighlight(TestsBase):
26 """Contains test functions for highlight.py"""
33 """Contains test functions for highlight.py"""
27
34
@@ -33,6 +40,8 b' class TestHighlight(TestsBase):'
33 def say(text):
40 def say(text):
34 print(text)
41 print(text)
35
42
43 end
44
36 say('Hello World!')
45 say('Hello World!')
37 """,
46 """,
38 """
47 """
@@ -57,6 +66,20 b' class TestHighlight(TestsBase):'
57 for index, test in enumerate(self.tests):
66 for index, test in enumerate(self.tests):
58 self._try_highlight(highlight2latex, test, self.tokens[index])
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 def _try_highlight(self, method, test, tokens):
84 def _try_highlight(self, method, test, tokens):
62 """Try highlighting source, look for key tokens"""
85 """Try highlighting source, look for key tokens"""
@@ -13,6 +13,7 b''
13
13
14 from IPython.utils.traitlets import List
14 from IPython.utils.traitlets import List
15 from IPython.config.configurable import LoggingConfigurable
15 from IPython.config.configurable import LoggingConfigurable
16 from IPython.utils.traitlets import Unicode
16
17
17 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
18 # Classes and functions
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 def __init__(self, **kw):
39 def __init__(self, **kw):
37 super(NbConvertBase, self).__init__(**kw)
40 super(NbConvertBase, self).__init__(**kw)
General Comments 0
You need to be logged in to leave comments. Login now