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