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. |
|
46 | 'highlight2html': filters.Highlight2Html, | |
47 |
'highlight2latex': filters. |
|
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,12 +35,14 b" MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json']" | |||||
33 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
34 |
|
36 | |||
35 | __all__ = [ |
|
37 | __all__ = [ | |
36 |
' |
|
38 | 'Highlight2Html', | |
37 |
' |
|
39 | 'Highlight2Latex' | |
38 | ] |
|
40 | ] | |
39 |
|
41 | |||
40 |
|
42 | |||
41 | def highlight2html(source, language='ipython', metadata=None): |
|
43 | class Highlight2Html(NbConvertBase): | |
|
44 | ||||
|
45 | def __call__(self, source, language=None, metadata=None): | |||
42 | """ |
|
46 | """ | |
43 | Return a syntax-highlighted version of the input source as html output. |
|
47 | Return a syntax-highlighted version of the input source as html output. | |
44 |
|
48 | |||
@@ -51,11 +55,15 b" def highlight2html(source, language='ipython', metadata=None):" | |||||
51 | metadata : NotebookNode cell metadata |
|
55 | metadata : NotebookNode cell metadata | |
52 | metadata of the cell to highlight |
|
56 | metadata of the cell to highlight | |
53 | """ |
|
57 | """ | |
|
58 | if not language: | |||
|
59 | language=self.default_language | |||
54 |
|
60 | |||
55 | return _pygment_highlight(source, HtmlFormatter(), language, metadata) |
|
61 | return _pygment_highlight(source, HtmlFormatter(), language, metadata) | |
56 |
|
62 | |||
57 |
|
63 | |||
58 | def highlight2latex(source, language='ipython', metadata=None, strip_verbatim=False): |
|
64 | class Highlight2Latex(NbConvertBase): | |
|
65 | ||||
|
66 | def __call__(self, source, language=None, metadata=None, strip_verbatim=False): | |||
59 | """ |
|
67 | """ | |
60 | Return a syntax-highlighted version of the input source as latex output. |
|
68 | Return a syntax-highlighted version of the input source as latex output. | |
61 |
|
69 | |||
@@ -70,6 +78,9 b" def highlight2latex(source, language='ipython', metadata=None, strip_verbatim=Fa" | |||||
70 | strip_verbatim : bool |
|
78 | strip_verbatim : bool | |
71 | remove the Verbatim environment that pygments provides by default |
|
79 | remove the Verbatim environment that pygments provides by default | |
72 | """ |
|
80 | """ | |
|
81 | if not language: | |||
|
82 | language=self.default_language | |||
|
83 | ||||
73 | latex = _pygment_highlight(source, LatexFormatter(), language, metadata) |
|
84 | latex = _pygment_highlight(source, LatexFormatter(), language, metadata) | |
74 | if strip_verbatim: |
|
85 | if strip_verbatim: | |
75 | latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '') |
|
86 | latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '') |
@@ -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 |
|
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