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