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