##// END OF EJS Templates
Inline user custom CSS if it's defined.
Jonathan Frederic -
Show More
@@ -280,6 +280,7 b' class NbConvertApp(BaseIPythonApplication):'
280 280 if self.output_base:
281 281 notebook_name = self.output_base
282 282 resources = {}
283 resources['profile_dir'] = self.profile_dir.location
283 284 resources['unique_key'] = notebook_name
284 285 resources['output_files_dir'] = '%s_files' % notebook_name
285 286 self.log.info("Support files will be in %s", os.path.join(resources['output_files_dir'], ''))
@@ -1,59 +1,22 b''
1 1 """Module that pre-processes the notebook for export to HTML.
2 2 """
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2013, the IPython Development Team.
5 #
3 # Copyright (c) IPython Development Team.
6 4 # Distributed under the terms of the Modified BSD License.
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
10
11 #-----------------------------------------------------------------------------
12 # Imports
13 #-----------------------------------------------------------------------------
14
15 5 import os
16 6 import io
17 7
18 8 from IPython.utils import path
19
20 from .base import Preprocessor
21
22 9 from IPython.utils.traitlets import Unicode
23
24 #-----------------------------------------------------------------------------
25 # Classes and functions
26 #-----------------------------------------------------------------------------
10 from .base import Preprocessor
27 11
28 12 class CSSHTMLHeaderPreprocessor(Preprocessor):
29 13 """
30 14 Preprocessor used to pre-process notebook for HTML output. Adds IPython notebook
31 15 front-end CSS and Pygments CSS to HTML output.
32 16 """
33
34 header = []
35
36 17 highlight_class = Unicode('.highlight', config=True,
37 18 help="CSS highlight class identifier")
38 19
39 def __init__(self, config=None, **kw):
40 """
41 Public constructor
42
43 Parameters
44 ----------
45 config : Config
46 Configuration file structure
47 **kw : misc
48 Additional arguments
49 """
50
51 super(CSSHTMLHeaderPreprocessor, self).__init__(config=config, **kw)
52
53 if self.enabled :
54 self._regen_header()
55
56
57 20 def preprocess(self, nb, resources):
58 21 """Fetch and add CSS to the resource dictionary
59 22
@@ -69,38 +32,43 b' class CSSHTMLHeaderPreprocessor(Preprocessor):'
69 32 Additional resources used in the conversion process. Allows
70 33 preprocessors to pass variables into the Jinja engine.
71 34 """
72
73 35 resources['inlining'] = {}
74 resources['inlining']['css'] = self.header
75
36 resources['inlining']['css'] = self._generate_header(resources)
76 37 return nb, resources
77 38
78
79 def _regen_header(self):
39 def _generate_header(self, resources):
80 40 """
81 41 Fills self.header with lines of CSS extracted from IPython
82 42 and Pygments.
83 43 """
84 44 from pygments.formatters import HtmlFormatter
85
86 #Clear existing header.
87 45 header = []
88 46
89 #Construct path to IPy CSS
47 # Construct path to IPy CSS
90 48 from IPython.html import DEFAULT_STATIC_FILES_PATH
91 49 sheet_filename = os.path.join(DEFAULT_STATIC_FILES_PATH,
92 50 'style', 'style.min.css')
93 51
94 #Load style CSS file.
95 with io.open(sheet_filename, encoding='utf-8') as file:
96 file_text = file.read()
97 header.append(file_text)
98
99 #Add pygments CSS
52 # Load style CSS file.
53 with io.open(sheet_filename, encoding='utf-8') as f:
54 header.append(f.read())
55
56 # 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
58 # and that we should inline it in the nbconvert output.
59 profile_dir = resources['profile_dir']
60 custom_css_filename = os.path.join(profile_dir, 'static', 'custom', 'custom.css')
61 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
100 70 formatter = HtmlFormatter()
101 71 pygments_css = formatter.get_style_defs(self.highlight_class)
102 72 header.append(pygments_css)
103
104 #Set header
105 self.header = header
73 return header
106 74
General Comments 0
You need to be logged in to leave comments. Login now