Show More
@@ -4,6 +4,7 b'' | |||
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | import os |
|
6 | 6 | import io |
|
7 | import hashlib | |
|
7 | 8 | |
|
8 | 9 | from IPython.utils import path |
|
9 | 10 | from IPython.utils.traitlets import Unicode |
@@ -17,6 +18,10 b' class CSSHTMLHeaderPreprocessor(Preprocessor):' | |||
|
17 | 18 | highlight_class = Unicode('.highlight', config=True, |
|
18 | 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 | 25 | def preprocess(self, nb, resources): |
|
21 | 26 | """Fetch and add CSS to the resource dictionary |
|
22 | 27 | |
@@ -53,22 +58,27 b' class CSSHTMLHeaderPreprocessor(Preprocessor):' | |||
|
53 | 58 | with io.open(sheet_filename, encoding='utf-8') as f: |
|
54 | 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 | 66 | # Load the user's custom CSS and IPython's default custom CSS. If they |
|
57 | 67 | # differ, assume the user has made modifications to his/her custom CSS |
|
58 | 68 | # and that we should inline it in the nbconvert output. |
|
59 | 69 | profile_dir = resources['profile_dir'] |
|
60 | 70 | custom_css_filename = os.path.join(profile_dir, 'static', 'custom', 'custom.css') |
|
61 | 71 | if os.path.isfile(custom_css_filename): |
|
62 | with io.open(custom_css_filename, encoding='utf-8') as f: | |
|
63 | custom_css = 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) | |
|
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: | |
|
75 | with io.open(custom_css_filename, encoding='utf-8') as f: | |
|
76 | header.append(f.read()) | |
|
73 | 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