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