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