Show More
@@ -44,7 +44,7 default_filters = { | |||||
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. |
|
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, |
@@ -18,11 +18,11 from pygments import highlight as pygements_highlight | |||||
18 | from pygments.lexers import get_lexer_by_name |
|
18 | 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 | from IPython.config import catch_config_error, Configurable |
|
21 | ||
22 | from IPython.utils.traitlets import Unicode |
|
|||
23 |
|
22 | |||
24 | # Our own imports |
|
23 | # Our own imports | |
25 | from IPython.nbconvert.utils.lexers import IPythonLexer |
|
24 | from IPython.nbconvert.utils.lexers import IPythonLexer | |
|
25 | from IPython.nbconvert.utils.base import NbConvertBase | |||
26 |
|
26 | |||
27 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
28 | # Globals and constants |
|
28 | # Globals and constants | |
@@ -36,13 +36,11 MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] | |||||
36 |
|
36 | |||
37 | __all__ = [ |
|
37 | __all__ = [ | |
38 | 'Highlight2Html', |
|
38 | 'Highlight2Html', | |
39 |
' |
|
39 | 'Highlight2Latex' | |
40 | ] |
|
40 | ] | |
41 |
|
41 | |||
42 |
|
42 | |||
43 |
class Highlight2Html( |
|
43 | class Highlight2Html(NbConvertBase): | |
44 |
|
||||
45 | language = Unicode('ipython', config=True, help='default highlight language') |
|
|||
46 |
|
44 | |||
47 | def __call__(self, source, language=None, metadata=None): |
|
45 | def __call__(self, source, language=None, metadata=None): | |
48 | """ |
|
46 | """ | |
@@ -58,32 +56,37 class Highlight2Html(Configurable): | |||||
58 | metadata of the cell to highlight |
|
56 | metadata of the cell to highlight | |
59 | """ |
|
57 | """ | |
60 | if not language: |
|
58 | if not language: | |
61 | language=self.language |
|
59 | language=self.default_language | |
62 |
|
60 | |||
63 | return _pygment_highlight(source, HtmlFormatter(), language, metadata) |
|
61 | return _pygment_highlight(source, HtmlFormatter(), language, metadata) | |
64 |
|
62 | |||
65 |
|
63 | |||
66 | def highlight2latex(source, language='ipython', metadata=None, strip_verbatim=False): |
|
64 | class Highlight2Latex(NbConvertBase): | |
67 | """ |
|
|||
68 | Return a syntax-highlighted version of the input source as latex output. |
|
|||
69 |
|
65 | |||
70 | Parameters |
|
66 | def __call__(self, source, language=None, metadata=None, strip_verbatim=False): | |
71 | ---------- |
|
67 | """ | |
72 | source : str |
|
68 | Return a syntax-highlighted version of the input source as latex output. | |
73 | source of the cell to highlight |
|
69 | ||
74 | language : str |
|
70 | Parameters | |
75 | language to highlight the syntax of |
|
71 | ---------- | |
76 | metadata : NotebookNode cell metadata |
|
72 | source : str | |
77 |
|
|
73 | source of the cell to highlight | |
78 | strip_verbatim : bool |
|
74 | language : str | |
79 | remove the Verbatim environment that pygments provides by default |
|
75 | language to highlight the syntax of | |
80 | """ |
|
76 | metadata : NotebookNode cell metadata | |
81 | latex = _pygment_highlight(source, LatexFormatter(), language, metadata) |
|
77 | metadata of the cell to highlight | |
82 |
|
|
78 | strip_verbatim : bool | |
83 | latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '') |
|
79 | remove the Verbatim environment that pygments provides by default | |
84 | return latex.replace('\n\\end{Verbatim}\n', '') |
|
80 | """ | |
85 | else: |
|
81 | if not language: | |
86 | return latex |
|
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 | |||
87 |
|
90 | |||
88 |
|
91 | |||
89 |
|
92 |
@@ -15,14 +15,19 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, |
|
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 | |||
25 | highlight2html = Highlight2Html() |
|
26 | highlight2html = Highlight2Html() | |
|
27 | highlight2latex = Highlight2Latex() | |||
|
28 | c = Config() | |||
|
29 | c.Highlight2Html.default_language='ruby' | |||
|
30 | highlight2html_ruby = Highlight2Html(config=c) | |||
26 |
|
31 | |||
27 | class TestHighlight(TestsBase): |
|
32 | class TestHighlight(TestsBase): | |
28 | """Contains test functions for highlight.py""" |
|
33 | """Contains test functions for highlight.py""" | |
@@ -35,6 +40,8 class TestHighlight(TestsBase): | |||||
35 | def say(text): |
|
40 | def say(text): | |
36 | print(text) |
|
41 | print(text) | |
37 |
|
42 | |||
|
43 | end | |||
|
44 | ||||
38 | say('Hello World!') |
|
45 | say('Hello World!') | |
39 | """, |
|
46 | """, | |
40 | """ |
|
47 | """ | |
@@ -59,6 +66,20 class TestHighlight(TestsBase): | |||||
59 | for index, test in enumerate(self.tests): |
|
66 | for index, test in enumerate(self.tests): | |
60 | self._try_highlight(highlight2latex, test, self.tokens[index]) |
|
67 | self._try_highlight(highlight2latex, test, self.tokens[index]) | |
61 |
|
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+"']"))) | |||
62 |
|
83 | |||
63 | def _try_highlight(self, method, test, tokens): |
|
84 | def _try_highlight(self, method, test, tokens): | |
64 | """Try highlighting source, look for key tokens""" |
|
85 | """Try highlighting source, look for key tokens""" |
@@ -13,6 +13,7 | |||||
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 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