Show More
@@ -1,105 +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 |
|
18 | from pygments.formatters import HtmlFormatter | |
19 |
|
19 | |||
20 | from IPython.utils import path |
|
20 | from IPython.utils import path | |
21 |
|
21 | |||
22 | from .activatable import ActivatableTransformer |
|
22 | from .activatable import ActivatableTransformer | |
23 |
|
23 | |||
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 | # Classes and functions |
|
25 | # Classes and functions | |
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 |
|
27 | |||
28 | class CSSHtmlHeaderTransformer(ActivatableTransformer): |
|
28 | class CSSHtmlHeaderTransformer(ActivatableTransformer): | |
29 | """ |
|
29 | """ | |
30 | Transformer used to pre-process notebook for HTML output. Adds IPython notebook |
|
30 | Transformer used to pre-process notebook for HTML output. Adds IPython notebook | |
31 | front-end CSS and Pygments CSS to HTML output. |
|
31 | front-end CSS and Pygments CSS to HTML output. | |
32 | """ |
|
32 | """ | |
33 |
|
33 | |||
34 | header = [] |
|
34 | header = [] | |
35 |
|
35 | |||
36 | def __init__(self, config=None, **kw): |
|
36 | def __init__(self, config=None, **kw): | |
37 | """ |
|
37 | """ | |
38 | Public constructor |
|
38 | Public constructor | |
39 |
|
39 | |||
40 | Parameters |
|
40 | Parameters | |
41 | ---------- |
|
41 | ---------- | |
42 | config : Config |
|
42 | config : Config | |
43 | Configuration file structure |
|
43 | Configuration file structure | |
44 | **kw : misc |
|
44 | **kw : misc | |
45 | Additional arguments |
|
45 | Additional arguments | |
46 | """ |
|
46 | """ | |
47 |
|
47 | |||
48 | super(CSSHtmlHeaderTransformer, self).__init__(config=config, **kw) |
|
48 | super(CSSHtmlHeaderTransformer, self).__init__(config=config, **kw) | |
49 |
|
49 | |||
50 | if self.enabled : |
|
50 | if self.enabled : | |
51 | self._regen_header() |
|
51 | self._regen_header() | |
52 |
|
52 | |||
53 |
|
53 | |||
54 | def __call__(self, nb, resources): |
|
54 | def __call__(self, nb, resources): | |
55 | """Fetch and add CSS to the resource dictionary |
|
55 | """Fetch and add CSS to the resource dictionary | |
56 |
|
56 | |||
57 | Fetch CSS from IPython and Pygments to add at the beginning |
|
57 | Fetch CSS from IPython and Pygments to add at the beginning | |
58 | of the html files. Add this css in resources in the |
|
58 | of the html files. Add this css in resources in the | |
59 | "inlining.css" key |
|
59 | "inlining.css" key | |
60 |
|
60 | |||
61 | Parameters |
|
61 | Parameters | |
62 | ---------- |
|
62 | ---------- | |
63 | nb : NotebookNode |
|
63 | nb : NotebookNode | |
64 | Notebook being converted |
|
64 | Notebook being converted | |
65 | resources : dictionary |
|
65 | resources : dictionary | |
66 | Additional resources used in the conversion process. Allows |
|
66 | Additional resources used in the conversion process. Allows | |
67 | transformers to pass variables into the Jinja engine. |
|
67 | transformers to pass variables into the Jinja engine. | |
68 | """ |
|
68 | """ | |
69 |
|
69 | |||
70 | resources['inlining'] = {} |
|
70 | resources['inlining'] = {} | |
71 | resources['inlining']['css'] = self.header |
|
71 | resources['inlining']['css'] = self.header | |
72 |
|
72 | |||
73 | return nb, resources |
|
73 | return nb, resources | |
74 |
|
74 | |||
75 |
|
75 | |||
76 | def _regen_header(self): |
|
76 | def _regen_header(self): | |
77 | """ |
|
77 | """ | |
78 | Fills self.header with lines of CSS extracted from iPython |
|
78 | Fills self.header with lines of CSS extracted from iPython | |
79 | and Pygments. |
|
79 | and Pygments. | |
80 | """ |
|
80 | """ | |
81 |
|
81 | |||
82 | #Clear existing header. |
|
82 | #Clear existing header. | |
83 | header = [] |
|
83 | header = [] | |
84 |
|
84 | |||
85 | #Construct path to iPy CSS |
|
85 | #Construct path to iPy CSS | |
86 | sheet_filename = os.path.join(path.get_ipython_package_dir(), |
|
86 | sheet_filename = os.path.join(path.get_ipython_package_dir(), | |
87 |
'html |
|
87 | 'html', 'static', 'style', 'style.min.css') | |
88 |
|
88 | |||
89 | #Load style CSS file. |
|
89 | #Load style CSS file. | |
90 | try: |
|
90 | try: | |
91 | with io.open(sheet_filename, encoding='utf-8') as file: |
|
91 | with io.open(sheet_filename, encoding='utf-8') as file: | |
92 | file_text = file.read() |
|
92 | file_text = file.read() | |
93 | header.append(file_text) |
|
93 | header.append(file_text) | |
94 | except IOError: |
|
94 | except IOError: | |
95 |
|
95 | |||
96 | # New version of iPython with style.min.css, pass |
|
96 | # New version of iPython with style.min.css, pass | |
97 | pass |
|
97 | pass | |
98 |
|
98 | |||
99 | #Add pygments CSS |
|
99 | #Add pygments CSS | |
100 | pygments_css = HtmlFormatter().get_style_defs('.highlight') |
|
100 | pygments_css = HtmlFormatter().get_style_defs('.highlight') | |
101 | header.append(pygments_css) |
|
101 | header.append(pygments_css) | |
102 |
|
102 | |||
103 | #Set header |
|
103 | #Set header | |
104 | self.header = header |
|
104 | self.header = header | |
105 |
|
105 |
General Comments 0
You need to be logged in to leave comments.
Login now