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