Show More
@@ -1,121 +1,123 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | Module containing filter functions that allow code to be highlighted |
|
3 | 3 | from within Jinja templates. |
|
4 | 4 | """ |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | # |
|
10 | 10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | from pygments import highlight as pygements_highlight | |
|
18 | from pygments.lexers import get_lexer_by_name | |
|
19 | from pygments.formatters import HtmlFormatter | |
|
20 | from pygments.formatters import LatexFormatter | |
|
21 | ||
|
17 | # pygments must not be imported at the module level | |
|
18 | # because errors should be raised at runtime if it's actually needed, | |
|
19 | # not import time, when it may not be needed. | |
|
22 | 20 | |
|
23 | 21 | # Our own imports |
|
24 | from IPython.nbconvert.utils.lexers import IPythonLexer | |
|
25 | 22 | from IPython.nbconvert.utils.base import NbConvertBase |
|
26 | 23 | |
|
27 | 24 | #----------------------------------------------------------------------------- |
|
28 | 25 | # Globals and constants |
|
29 | 26 | #----------------------------------------------------------------------------- |
|
30 | 27 | |
|
31 | 28 | MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] |
|
32 | 29 | |
|
33 | 30 | #----------------------------------------------------------------------------- |
|
34 | 31 | # Utility functions |
|
35 | 32 | #----------------------------------------------------------------------------- |
|
36 | 33 | |
|
37 | 34 | __all__ = [ |
|
38 | 35 | 'Highlight2Html', |
|
39 | 36 | 'Highlight2Latex' |
|
40 | 37 | ] |
|
41 | 38 | |
|
42 | 39 | |
|
43 | 40 | class Highlight2Html(NbConvertBase): |
|
44 | 41 | |
|
45 | 42 | def __call__(self, source, language=None, metadata=None): |
|
46 | 43 | """ |
|
47 | 44 | Return a syntax-highlighted version of the input source as html output. |
|
48 | 45 | |
|
49 | 46 | Parameters |
|
50 | 47 | ---------- |
|
51 | 48 | source : str |
|
52 | 49 | source of the cell to highlight |
|
53 | 50 | language : str |
|
54 | 51 | language to highlight the syntax of |
|
55 | 52 | metadata : NotebookNode cell metadata |
|
56 | 53 | metadata of the cell to highlight |
|
57 | 54 | """ |
|
55 | from pygments.formatters import HtmlFormatter | |
|
58 | 56 | if not language: |
|
59 | 57 | language=self.default_language |
|
60 | 58 | |
|
61 | return _pygment_highlight(source, HtmlFormatter(), language, metadata) | |
|
59 | return _pygments_highlight(source, HtmlFormatter(), language, metadata) | |
|
62 | 60 | |
|
63 | 61 | |
|
64 | 62 | class Highlight2Latex(NbConvertBase): |
|
65 | 63 | |
|
66 | 64 | def __call__(self, source, language=None, metadata=None, strip_verbatim=False): |
|
67 | 65 | """ |
|
68 | 66 | Return a syntax-highlighted version of the input source as latex output. |
|
69 | 67 | |
|
70 | 68 | Parameters |
|
71 | 69 | ---------- |
|
72 | 70 | source : str |
|
73 | 71 | source of the cell to highlight |
|
74 | 72 | language : str |
|
75 | 73 | language to highlight the syntax of |
|
76 | 74 | metadata : NotebookNode cell metadata |
|
77 | 75 | metadata of the cell to highlight |
|
78 | 76 | strip_verbatim : bool |
|
79 | 77 | remove the Verbatim environment that pygments provides by default |
|
80 | 78 | """ |
|
79 | from pygments.formatters import LatexFormatter | |
|
81 | 80 | if not language: |
|
82 | 81 | language=self.default_language |
|
83 | 82 | |
|
84 | latex = _pygment_highlight(source, LatexFormatter(), language, metadata) | |
|
83 | latex = _pygments_highlight(source, LatexFormatter(), language, metadata) | |
|
85 | 84 | if strip_verbatim: |
|
86 | 85 | latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '') |
|
87 | 86 | return latex.replace('\n\\end{Verbatim}\n', '') |
|
88 | 87 | else: |
|
89 | 88 | return latex |
|
90 | 89 | |
|
91 | 90 | |
|
92 | 91 | |
|
93 | def _pygment_highlight(source, output_formatter, language='ipython', metadata=None): | |
|
92 | def _pygments_highlight(source, output_formatter, language='ipython', metadata=None): | |
|
94 | 93 | """ |
|
95 | 94 | Return a syntax-highlighted version of the input source |
|
96 | 95 | |
|
97 | 96 | Parameters |
|
98 | 97 | ---------- |
|
99 | 98 | source : str |
|
100 | 99 | source of the cell to highlight |
|
101 | 100 | output_formatter : Pygments formatter |
|
102 | 101 | language : str |
|
103 | 102 | language to highlight the syntax of |
|
104 | 103 | metadata : NotebookNode cell metadata |
|
105 | 104 | metadata of the cell to highlight |
|
106 | 105 | """ |
|
106 | from pygments import highlight | |
|
107 | from pygments.lexers import get_lexer_by_name | |
|
108 | from IPython.nbconvert.utils.lexers import IPythonLexer | |
|
107 | 109 | |
|
108 | 110 | # If the cell uses a magic extension language, |
|
109 | 111 | # use the magic language instead. |
|
110 | 112 | if language == 'ipython' \ |
|
111 | 113 | and metadata \ |
|
112 | 114 | and 'magics_language' in metadata: |
|
113 | 115 | |
|
114 | 116 | language = metadata['magics_language'] |
|
115 | 117 | |
|
116 | 118 | if language == 'ipython': |
|
117 | 119 | lexer = IPythonLexer() |
|
118 | 120 | else: |
|
119 | 121 | lexer = get_lexer_by_name(language, stripall=True) |
|
120 | 122 | |
|
121 |
return |
|
|
123 | return highlight(source, lexer, output_formatter) |
@@ -1,106 +1,105 b'' | |||
|
1 | 1 | """Module that pre-processes the notebook for export to HTML. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2013, the IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | import os |
|
16 | 16 | import io |
|
17 | 17 | |
|
18 | from pygments.formatters import HtmlFormatter | |
|
19 | ||
|
20 | 18 | from IPython.utils import path |
|
21 | 19 | |
|
22 | 20 | from .base import Preprocessor |
|
23 | 21 | |
|
24 | 22 | from IPython.utils.traitlets import Unicode |
|
25 | 23 | |
|
26 | 24 | #----------------------------------------------------------------------------- |
|
27 | 25 | # Classes and functions |
|
28 | 26 | #----------------------------------------------------------------------------- |
|
29 | 27 | |
|
30 | 28 | class CSSHTMLHeaderPreprocessor(Preprocessor): |
|
31 | 29 | """ |
|
32 | 30 | Preprocessor used to pre-process notebook for HTML output. Adds IPython notebook |
|
33 | 31 | front-end CSS and Pygments CSS to HTML output. |
|
34 | 32 | """ |
|
35 | 33 | |
|
36 | 34 | header = [] |
|
37 | 35 | |
|
38 | 36 | highlight_class = Unicode('.highlight', config=True, |
|
39 | 37 | help="CSS highlight class identifier") |
|
40 | 38 | |
|
41 | 39 | def __init__(self, config=None, **kw): |
|
42 | 40 | """ |
|
43 | 41 | Public constructor |
|
44 | 42 | |
|
45 | 43 | Parameters |
|
46 | 44 | ---------- |
|
47 | 45 | config : Config |
|
48 | 46 | Configuration file structure |
|
49 | 47 | **kw : misc |
|
50 | 48 | Additional arguments |
|
51 | 49 | """ |
|
52 | 50 | |
|
53 | 51 | super(CSSHTMLHeaderPreprocessor, self).__init__(config=config, **kw) |
|
54 | 52 | |
|
55 | 53 | if self.enabled : |
|
56 | 54 | self._regen_header() |
|
57 | 55 | |
|
58 | 56 | |
|
59 | 57 | def preprocess(self, nb, resources): |
|
60 | 58 | """Fetch and add CSS to the resource dictionary |
|
61 | 59 | |
|
62 | 60 | Fetch CSS from IPython and Pygments to add at the beginning |
|
63 | 61 | of the html files. Add this css in resources in the |
|
64 | 62 | "inlining.css" key |
|
65 | 63 | |
|
66 | 64 | Parameters |
|
67 | 65 | ---------- |
|
68 | 66 | nb : NotebookNode |
|
69 | 67 | Notebook being converted |
|
70 | 68 | resources : dictionary |
|
71 | 69 | Additional resources used in the conversion process. Allows |
|
72 | 70 | preprocessors to pass variables into the Jinja engine. |
|
73 | 71 | """ |
|
74 | 72 | |
|
75 | 73 | resources['inlining'] = {} |
|
76 | 74 | resources['inlining']['css'] = self.header |
|
77 | 75 | |
|
78 | 76 | return nb, resources |
|
79 | 77 | |
|
80 | 78 | |
|
81 | 79 | def _regen_header(self): |
|
82 | 80 | """ |
|
83 | 81 | Fills self.header with lines of CSS extracted from IPython |
|
84 | 82 | and Pygments. |
|
85 | 83 | """ |
|
84 | from pygments.formatters import HtmlFormatter | |
|
86 | 85 | |
|
87 | 86 | #Clear existing header. |
|
88 | 87 | header = [] |
|
89 | 88 | |
|
90 | 89 | #Construct path to IPy CSS |
|
91 | 90 | sheet_filename = os.path.join(path.get_ipython_package_dir(), |
|
92 | 91 | 'html', 'static', 'style', 'style.min.css') |
|
93 | 92 | |
|
94 | 93 | #Load style CSS file. |
|
95 | 94 | with io.open(sheet_filename, encoding='utf-8') as file: |
|
96 | 95 | file_text = file.read() |
|
97 | 96 | header.append(file_text) |
|
98 | 97 | |
|
99 | 98 | #Add pygments CSS |
|
100 | 99 | formatter = HtmlFormatter() |
|
101 | 100 | pygments_css = formatter.get_style_defs(self.highlight_class) |
|
102 | 101 | header.append(pygments_css) |
|
103 | 102 | |
|
104 | 103 | #Set header |
|
105 | 104 | self.header = header |
|
106 | 105 |
General Comments 0
You need to be logged in to leave comments.
Login now