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