##// END OF EJS Templates
Address comments
Jonathan Frederic -
Show More
@@ -1,74 +1,84 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 # Copyright (c) IPython Development Team.
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5 import os
5 import os
6 import io
6 import io
7 import hashlib
7
8
8 from IPython.utils import path
9 from IPython.utils import path
9 from IPython.utils.traitlets import Unicode
10 from IPython.utils.traitlets import Unicode
10 from .base import Preprocessor
11 from .base import Preprocessor
11
12
12 class CSSHTMLHeaderPreprocessor(Preprocessor):
13 class CSSHTMLHeaderPreprocessor(Preprocessor):
13 """
14 """
14 Preprocessor used to pre-process notebook for HTML output. Adds IPython notebook
15 Preprocessor used to pre-process notebook for HTML output. Adds IPython notebook
15 front-end CSS and Pygments CSS to HTML output.
16 front-end CSS and Pygments CSS to HTML output.
16 """
17 """
17 highlight_class = Unicode('.highlight', config=True,
18 highlight_class = Unicode('.highlight', config=True,
18 help="CSS highlight class identifier")
19 help="CSS highlight class identifier")
19
20
21 def __init__(self, *pargs, **kwargs):
22 Preprocessor.__init__(self, *pargs, **kwargs)
23 self._default_css_hash = None
24
20 def preprocess(self, nb, resources):
25 def preprocess(self, nb, resources):
21 """Fetch and add CSS to the resource dictionary
26 """Fetch and add CSS to the resource dictionary
22
27
23 Fetch CSS from IPython and Pygments to add at the beginning
28 Fetch CSS from IPython and Pygments to add at the beginning
24 of the html files. Add this css in resources in the
29 of the html files. Add this css in resources in the
25 "inlining.css" key
30 "inlining.css" key
26
31
27 Parameters
32 Parameters
28 ----------
33 ----------
29 nb : NotebookNode
34 nb : NotebookNode
30 Notebook being converted
35 Notebook being converted
31 resources : dictionary
36 resources : dictionary
32 Additional resources used in the conversion process. Allows
37 Additional resources used in the conversion process. Allows
33 preprocessors to pass variables into the Jinja engine.
38 preprocessors to pass variables into the Jinja engine.
34 """
39 """
35 resources['inlining'] = {}
40 resources['inlining'] = {}
36 resources['inlining']['css'] = self._generate_header(resources)
41 resources['inlining']['css'] = self._generate_header(resources)
37 return nb, resources
42 return nb, resources
38
43
39 def _generate_header(self, resources):
44 def _generate_header(self, resources):
40 """
45 """
41 Fills self.header with lines of CSS extracted from IPython
46 Fills self.header with lines of CSS extracted from IPython
42 and Pygments.
47 and Pygments.
43 """
48 """
44 from pygments.formatters import HtmlFormatter
49 from pygments.formatters import HtmlFormatter
45 header = []
50 header = []
46
51
47 # Construct path to IPy CSS
52 # Construct path to IPy CSS
48 from IPython.html import DEFAULT_STATIC_FILES_PATH
53 from IPython.html import DEFAULT_STATIC_FILES_PATH
49 sheet_filename = os.path.join(DEFAULT_STATIC_FILES_PATH,
54 sheet_filename = os.path.join(DEFAULT_STATIC_FILES_PATH,
50 'style', 'style.min.css')
55 'style', 'style.min.css')
51
56
52 # Load style CSS file.
57 # Load style CSS file.
53 with io.open(sheet_filename, encoding='utf-8') as f:
58 with io.open(sheet_filename, encoding='utf-8') as f:
54 header.append(f.read())
59 header.append(f.read())
55
60
61 # Add pygments CSS
62 formatter = HtmlFormatter()
63 pygments_css = formatter.get_style_defs(self.highlight_class)
64 header.append(pygments_css)
65
56 # Load the user's custom CSS and IPython's default custom CSS. If they
66 # Load the user's custom CSS and IPython's default custom CSS. If they
57 # differ, assume the user has made modifications to his/her custom CSS
67 # differ, assume the user has made modifications to his/her custom CSS
58 # and that we should inline it in the nbconvert output.
68 # and that we should inline it in the nbconvert output.
59 profile_dir = resources['profile_dir']
69 profile_dir = resources['profile_dir']
60 custom_css_filename = os.path.join(profile_dir, 'static', 'custom', 'custom.css')
70 custom_css_filename = os.path.join(profile_dir, 'static', 'custom', 'custom.css')
61 if os.path.isfile(custom_css_filename):
71 if os.path.isfile(custom_css_filename):
72 if self._default_css_hash is None:
73 self._default_css_hash = self._hash(os.path.join(DEFAULT_STATIC_FILES_PATH, 'custom', 'custom.css'))
74 if self._hash(custom_css_filename) != self._default_css_hash:
62 with io.open(custom_css_filename, encoding='utf-8') as f:
75 with io.open(custom_css_filename, encoding='utf-8') as f:
63 custom_css = f.read()
76 header.append(f.read())
64 with io.open(os.path.join(DEFAULT_STATIC_FILES_PATH, 'custom', 'custom.css'), encoding='utf-8') as f:
65 default_css = f.read()
66 if custom_css != default_css:
67 header.append(custom_css)
68
69 # Add pygments CSS
70 formatter = HtmlFormatter()
71 pygments_css = formatter.get_style_defs(self.highlight_class)
72 header.append(pygments_css)
73 return header
77 return header
74
78
79 def _hash(self, filename):
80 """Compute the hash of a file."""
81 md5 = hashlib.md5()
82 with open(filename) as f:
83 md5.update(f.read())
84 return md5.digest()
General Comments 0
You need to be logged in to leave comments. Login now