Show More
@@ -4,6 +4,7 b'' | |||||
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 | |
@@ -17,6 +18,10 b' class CSSHTMLHeaderPreprocessor(Preprocessor):' | |||||
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 | |||
@@ -53,22 +58,27 b' class CSSHTMLHeaderPreprocessor(Preprocessor):' | |||||
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): | |
62 | with io.open(custom_css_filename, encoding='utf-8') as f: |
|
72 | if self._default_css_hash is None: | |
63 | custom_css = f.read() |
|
73 | self._default_css_hash = self._hash(os.path.join(DEFAULT_STATIC_FILES_PATH, 'custom', 'custom.css')) | |
64 | with io.open(os.path.join(DEFAULT_STATIC_FILES_PATH, 'custom', 'custom.css'), encoding='utf-8') as f: |
|
74 | if self._hash(custom_css_filename) != self._default_css_hash: | |
65 | default_css = f.read() |
|
75 | with io.open(custom_css_filename, encoding='utf-8') as f: | |
66 | if custom_css != default_css: |
|
76 | header.append(f.read()) | |
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