Show More
@@ -123,7 +123,7 b' class KernelSpecManager(HasTraits):' | |||||
123 | 'language': 'python', |
|
123 | 'language': 'python', | |
124 | 'codemirror_mode': {'name': 'ipython', |
|
124 | 'codemirror_mode': {'name': 'ipython', | |
125 | 'version': sys.version_info[0]}, |
|
125 | 'version': sys.version_info[0]}, | |
126 | 'pygments_lexer': 'ipython' |
|
126 | 'pygments_lexer': 'ipython%d' % (3 if PY3 else 2), | |
127 | } |
|
127 | } | |
128 |
|
128 | |||
129 | @property |
|
129 | @property |
@@ -14,7 +14,7 b'' | |||||
14 |
|
14 | |||
15 | import os |
|
15 | import os | |
16 |
|
16 | |||
17 | from IPython.nbconvert import preprocessors |
|
17 | from IPython.nbconvert.filters.highlight import Highlight2HTML | |
18 | from IPython.config import Config |
|
18 | from IPython.config import Config | |
19 |
|
19 | |||
20 | from .templateexporter import TemplateExporter |
|
20 | from .templateexporter import TemplateExporter | |
@@ -57,3 +57,10 b' class HTMLExporter(TemplateExporter):' | |||||
57 | }) |
|
57 | }) | |
58 | c.merge(super(HTMLExporter,self).default_config) |
|
58 | c.merge(super(HTMLExporter,self).default_config) | |
59 | return c |
|
59 | return c | |
|
60 | ||||
|
61 | def from_notebook_node(self, nb, resources=None, **kw): | |||
|
62 | kernelspec = nb.metadata.get('kernelspec', {}) | |||
|
63 | lexer = kernelspec.get('pygments_lexer', kernelspec.get('language', None)) | |||
|
64 | self.register_filter('highlight_code', | |||
|
65 | Highlight2HTML(pygments_lexer=lexer, parent=self)) | |||
|
66 | return super(HTMLExporter, self).from_notebook_node(nb, resources, **kw) |
@@ -19,7 +19,7 b' import os' | |||||
19 | from IPython.utils.traitlets import Unicode |
|
19 | from IPython.utils.traitlets import Unicode | |
20 | from IPython.config import Config |
|
20 | from IPython.config import Config | |
21 |
|
21 | |||
22 | from IPython.nbconvert import filters, preprocessors |
|
22 | from IPython.nbconvert.filters.highlight import Highlight2Latex | |
23 | from .templateexporter import TemplateExporter |
|
23 | from .templateexporter import TemplateExporter | |
24 |
|
24 | |||
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
@@ -87,3 +87,10 b' class LatexExporter(TemplateExporter):' | |||||
87 | }) |
|
87 | }) | |
88 | c.merge(super(LatexExporter,self).default_config) |
|
88 | c.merge(super(LatexExporter,self).default_config) | |
89 | return c |
|
89 | return c | |
|
90 | ||||
|
91 | def from_notebook_node(self, nb, resources=None, **kw): | |||
|
92 | kernelspec = nb.metadata.get('kernelspec', {}) | |||
|
93 | lexer = kernelspec.get('pygments_lexer', kernelspec.get('language', None)) | |||
|
94 | self.register_filter('highlight_code', | |||
|
95 | Highlight2Latex(pygments_lexer=lexer, parent=self)) | |||
|
96 | return super(LatexExporter, self).from_notebook_node(nb, resources, **kw) |
@@ -11,6 +11,7 b' from within Jinja templates.' | |||||
11 | # not import time, when it may not be needed. |
|
11 | # not import time, when it may not be needed. | |
12 |
|
12 | |||
13 | from IPython.nbconvert.utils.base import NbConvertBase |
|
13 | from IPython.nbconvert.utils.base import NbConvertBase | |
|
14 | from warnings import warn | |||
14 |
|
15 | |||
15 | MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] |
|
16 | MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] | |
16 |
|
17 | |||
@@ -20,6 +21,14 b' __all__ = [' | |||||
20 | ] |
|
21 | ] | |
21 |
|
22 | |||
22 | class Highlight2HTML(NbConvertBase): |
|
23 | class Highlight2HTML(NbConvertBase): | |
|
24 | def __init__(self, pygments_lexer=None, **kwargs): | |||
|
25 | self.pygments_lexer = pygments_lexer or 'ipython3' | |||
|
26 | super(Highlight2HTML, self).__init__(**kwargs) | |||
|
27 | ||||
|
28 | def _default_language_changed(self, name, old, new): | |||
|
29 | warn('Setting default_language in config is deprecated, ' | |||
|
30 | 'please use kernelspecs instead.') | |||
|
31 | self.pygments_lexer = new | |||
23 |
|
32 | |||
24 | def __call__(self, source, language=None, metadata=None): |
|
33 | def __call__(self, source, language=None, metadata=None): | |
25 | """ |
|
34 | """ | |
@@ -35,8 +44,9 b' class Highlight2HTML(NbConvertBase):' | |||||
35 | metadata of the cell to highlight |
|
44 | metadata of the cell to highlight | |
36 | """ |
|
45 | """ | |
37 | from pygments.formatters import HtmlFormatter |
|
46 | from pygments.formatters import HtmlFormatter | |
|
47 | ||||
38 | if not language: |
|
48 | if not language: | |
39 |
language=self. |
|
49 | language=self.pygments_lexer | |
40 |
|
50 | |||
41 | return _pygments_highlight(source if len(source) > 0 else ' ', |
|
51 | return _pygments_highlight(source if len(source) > 0 else ' ', | |
42 | # needed to help post processors: |
|
52 | # needed to help post processors: | |
@@ -45,6 +55,14 b' class Highlight2HTML(NbConvertBase):' | |||||
45 |
|
55 | |||
46 |
|
56 | |||
47 | class Highlight2Latex(NbConvertBase): |
|
57 | class Highlight2Latex(NbConvertBase): | |
|
58 | def __init__(self, pygments_lexer=None, **kwargs): | |||
|
59 | self.pygments_lexer = pygments_lexer or 'ipython3' | |||
|
60 | super(Highlight2Latex, self).__init__(**kwargs) | |||
|
61 | ||||
|
62 | def _default_language_changed(self, name, old, new): | |||
|
63 | warn('Setting default_language in config is deprecated, ' | |||
|
64 | 'please use kernelspecs instead.') | |||
|
65 | self.pygments_lexer = new | |||
48 |
|
66 | |||
49 | def __call__(self, source, language=None, metadata=None, strip_verbatim=False): |
|
67 | def __call__(self, source, language=None, metadata=None, strip_verbatim=False): | |
50 | """ |
|
68 | """ | |
@@ -63,7 +81,7 b' class Highlight2Latex(NbConvertBase):' | |||||
63 | """ |
|
81 | """ | |
64 | from pygments.formatters import LatexFormatter |
|
82 | from pygments.formatters import LatexFormatter | |
65 | if not language: |
|
83 | if not language: | |
66 |
language=self. |
|
84 | language=self.pygments_lexer | |
67 |
|
85 | |||
68 | latex = _pygments_highlight(source, LatexFormatter(), language, metadata) |
|
86 | latex = _pygments_highlight(source, LatexFormatter(), language, metadata) | |
69 | if strip_verbatim: |
|
87 | if strip_verbatim: | |
@@ -90,7 +108,7 b" def _pygments_highlight(source, output_formatter, language='ipython', metadata=N" | |||||
90 | """ |
|
108 | """ | |
91 | from pygments import highlight |
|
109 | from pygments import highlight | |
92 | from pygments.lexers import get_lexer_by_name |
|
110 | from pygments.lexers import get_lexer_by_name | |
93 | from IPython.nbconvert.utils.lexers import IPythonLexer |
|
111 | from IPython.nbconvert.utils.lexers import IPythonLexer, IPython3Lexer | |
94 |
|
112 | |||
95 | # If the cell uses a magic extension language, |
|
113 | # If the cell uses a magic extension language, | |
96 | # use the magic language instead. |
|
114 | # use the magic language instead. | |
@@ -100,8 +118,10 b" def _pygments_highlight(source, output_formatter, language='ipython', metadata=N" | |||||
100 |
|
118 | |||
101 | language = metadata['magics_language'] |
|
119 | language = metadata['magics_language'] | |
102 |
|
120 | |||
103 | if language == 'ipython': |
|
121 | if language == 'ipython2': | |
104 | lexer = IPythonLexer() |
|
122 | lexer = IPythonLexer() | |
|
123 | elif language == 'ipython3': | |||
|
124 | lexer = IPython3Lexer() | |||
105 | else: |
|
125 | else: | |
106 | lexer = get_lexer_by_name(language, stripall=True) |
|
126 | lexer = get_lexer_by_name(language, stripall=True) | |
107 |
|
127 |
@@ -25,9 +25,7 b' import xml' | |||||
25 |
|
25 | |||
26 | highlight2html = Highlight2HTML() |
|
26 | highlight2html = Highlight2HTML() | |
27 | highlight2latex = Highlight2Latex() |
|
27 | highlight2latex = Highlight2Latex() | |
28 | c = Config() |
|
28 | highlight2html_ruby = Highlight2HTML(pygments_lexer='ruby') | |
29 | c.Highlight2HTML.default_language='ruby' |
|
|||
30 | highlight2html_ruby = Highlight2HTML(config=c) |
|
|||
31 |
|
29 | |||
32 | class TestHighlight(TestsBase): |
|
30 | class TestHighlight(TestsBase): | |
33 | """Contains test functions for highlight.py""" |
|
31 | """Contains test functions for highlight.py""" | |
@@ -37,8 +35,10 b' class TestHighlight(TestsBase):' | |||||
37 | """ |
|
35 | """ | |
38 | #Hello World Example |
|
36 | #Hello World Example | |
39 |
|
37 | |||
|
38 | import foo | |||
|
39 | ||||
40 | def say(text): |
|
40 | def say(text): | |
41 |
|
|
41 | foo.bar(text) | |
42 |
|
42 | |||
43 | end |
|
43 | end | |
44 |
|
44 | |||
@@ -51,7 +51,7 b' class TestHighlight(TestsBase):' | |||||
51 | ] |
|
51 | ] | |
52 |
|
52 | |||
53 | tokens = [ |
|
53 | tokens = [ | |
54 |
['Hello World Example', 'say', 'text', 'pr |
|
54 | ['Hello World Example', 'say', 'text', 'import', 'def'], | |
55 | ['pylab', 'plot']] |
|
55 | ['pylab', 'plot']] | |
56 |
|
56 | |||
57 |
|
57 | |||
@@ -72,11 +72,13 b' class TestHighlight(TestsBase):' | |||||
72 | rb = highlight2html_ruby(self.tests[0]) |
|
72 | rb = highlight2html_ruby(self.tests[0]) | |
73 |
|
73 | |||
74 | for lang,tkns in [ |
|
74 | for lang,tkns in [ | |
75 |
( ht, ('def', |
|
75 | ( ht, ('def', )), | |
76 | ( rb, ('def','end' ) ) |
|
76 | ( rb, ('def','end' ) ) | |
77 | ]: |
|
77 | ]: | |
|
78 | print(tkns) | |||
|
79 | print(lang) | |||
78 | root = xml.etree.ElementTree.fromstring(lang) |
|
80 | root = xml.etree.ElementTree.fromstring(lang) | |
79 |
assert |
|
81 | self.assertEqual(self._extract_tokens(root,'k'), set(tkns)) | |
80 |
|
82 | |||
81 | def _extract_tokens(self, root, cls): |
|
83 | def _extract_tokens(self, root, cls): | |
82 | return set(map(lambda x:x.text,root.findall(".//*[@class='"+cls+"']"))) |
|
84 | return set(map(lambda x:x.text,root.findall(".//*[@class='"+cls+"']"))) |
@@ -46,7 +46,7 b' In [ ]:' | |||||
46 | {% block input %} |
|
46 | {% block input %} | |
47 | <div class="inner_cell"> |
|
47 | <div class="inner_cell"> | |
48 | <div class="input_area"> |
|
48 | <div class="input_area"> | |
49 |
{{ cell.input | highlight |
|
49 | {{ cell.input | highlight_code(metadata=cell.metadata) }} | |
50 | </div> |
|
50 | </div> | |
51 | </div> |
|
51 | </div> | |
52 | {%- endblock input %} |
|
52 | {%- endblock input %} |
@@ -20,7 +20,7 b'' | |||||
20 | %=============================================================================== |
|
20 | %=============================================================================== | |
21 |
|
21 | |||
22 | ((* block input scoped *)) |
|
22 | ((* block input scoped *)) | |
23 |
((( add_prompt(cell.input | highlight |
|
23 | ((( add_prompt(cell.input | highlight_code(strip_verbatim=True), cell, 'In ', 'incolor') ))) | |
24 | ((* endblock input *)) |
|
24 | ((* endblock input *)) | |
25 |
|
25 | |||
26 |
|
26 |
@@ -16,6 +16,6 b'' | |||||
16 |
|
16 | |||
17 | ((* block input scoped *)) |
|
17 | ((* block input scoped *)) | |
18 | \begin{Verbatim}[commandchars=\\\{\}] |
|
18 | \begin{Verbatim}[commandchars=\\\{\}] | |
19 |
((( cell.input | highlight |
|
19 | ((( cell.input | highlight_code(strip_verbatim=True) | add_prompts ))) | |
20 | \end{Verbatim} |
|
20 | \end{Verbatim} | |
21 | ((* endblock input *)) |
|
21 | ((* endblock input *)) |
General Comments 0
You need to be logged in to leave comments.
Login now