From f093d0160481721e92a0403d90d2f0df9e42b9dc 2014-06-03 18:33:04 From: Jonathan Frederic Date: 2014-06-03 18:33:04 Subject: [PATCH] Inline user custom CSS if it's defined. --- diff --git a/IPython/nbconvert/nbconvertapp.py b/IPython/nbconvert/nbconvertapp.py index a1612ec..2b6f14d 100755 --- a/IPython/nbconvert/nbconvertapp.py +++ b/IPython/nbconvert/nbconvertapp.py @@ -280,6 +280,7 @@ class NbConvertApp(BaseIPythonApplication): if self.output_base: notebook_name = self.output_base resources = {} + resources['profile_dir'] = self.profile_dir.location resources['unique_key'] = notebook_name resources['output_files_dir'] = '%s_files' % notebook_name self.log.info("Support files will be in %s", os.path.join(resources['output_files_dir'], '')) diff --git a/IPython/nbconvert/preprocessors/csshtmlheader.py b/IPython/nbconvert/preprocessors/csshtmlheader.py index 49f66f8..8d102e0 100755 --- a/IPython/nbconvert/preprocessors/csshtmlheader.py +++ b/IPython/nbconvert/preprocessors/csshtmlheader.py @@ -1,59 +1,22 @@ """Module that pre-processes the notebook for export to HTML. """ -#----------------------------------------------------------------------------- -# Copyright (c) 2013, the IPython Development Team. -# +# Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -# -# The full license is in the file COPYING.txt, distributed with this software. -#----------------------------------------------------------------------------- - -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- - import os import io from IPython.utils import path - -from .base import Preprocessor - from IPython.utils.traitlets import Unicode - -#----------------------------------------------------------------------------- -# Classes and functions -#----------------------------------------------------------------------------- +from .base import Preprocessor class CSSHTMLHeaderPreprocessor(Preprocessor): """ Preprocessor used to pre-process notebook for HTML output. Adds IPython notebook front-end CSS and Pygments CSS to HTML output. """ - - header = [] - highlight_class = Unicode('.highlight', config=True, help="CSS highlight class identifier") - def __init__(self, config=None, **kw): - """ - Public constructor - - Parameters - ---------- - config : Config - Configuration file structure - **kw : misc - Additional arguments - """ - - super(CSSHTMLHeaderPreprocessor, self).__init__(config=config, **kw) - - if self.enabled : - self._regen_header() - - def preprocess(self, nb, resources): """Fetch and add CSS to the resource dictionary @@ -69,38 +32,43 @@ class CSSHTMLHeaderPreprocessor(Preprocessor): Additional resources used in the conversion process. Allows preprocessors to pass variables into the Jinja engine. """ - resources['inlining'] = {} - resources['inlining']['css'] = self.header - + resources['inlining']['css'] = self._generate_header(resources) return nb, resources - - def _regen_header(self): + def _generate_header(self, resources): """ Fills self.header with lines of CSS extracted from IPython and Pygments. """ from pygments.formatters import HtmlFormatter - - #Clear existing header. header = [] - #Construct path to IPy CSS + # Construct path to IPy CSS from IPython.html import DEFAULT_STATIC_FILES_PATH sheet_filename = os.path.join(DEFAULT_STATIC_FILES_PATH, 'style', 'style.min.css') - #Load style CSS file. - with io.open(sheet_filename, encoding='utf-8') as file: - file_text = file.read() - header.append(file_text) - - #Add pygments CSS + # Load style CSS file. + with io.open(sheet_filename, encoding='utf-8') as f: + header.append(f.read()) + + # Load the user's custom CSS and IPython's default custom CSS. If they + # differ, assume the user has made modifications to his/her custom CSS + # and that we should inline it in the nbconvert output. + profile_dir = resources['profile_dir'] + custom_css_filename = os.path.join(profile_dir, 'static', 'custom', 'custom.css') + if os.path.isfile(custom_css_filename): + with io.open(custom_css_filename, encoding='utf-8') as f: + custom_css = f.read() + with io.open(os.path.join(DEFAULT_STATIC_FILES_PATH, 'custom', 'custom.css'), encoding='utf-8') as f: + default_css = f.read() + if custom_css != default_css: + header.append(custom_css) + + # Add pygments CSS formatter = HtmlFormatter() pygments_css = formatter.get_style_defs(self.highlight_class) header.append(pygments_css) - - #Set header - self.header = header + return header