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