##// END OF EJS Templates
Cleanup and refactor, transformers
Cleanup and refactor, transformers

File last commit:

r10674:6e8e0573
r10674:6e8e0573
Show More
csshtmlheader.py
119 lines | 3.6 KiB | text/x-python | PythonLexer
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """Module that pre-processes the notebook for export to HTML.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the 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 pygments.formatters import HtmlFormatter
from IPython.utils import path
Jonathan Frederic
Fixed all broken references, refactored some stuff here and there,...
r10624 from .activatable import ActivatableTransformer
Jonathan Frederic
Cleanup and refactor, transformers
r10674
#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
Jonathan Frederic
Split transformer code
r10437
class CSSHtmlHeaderTransformer(ActivatableTransformer):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
Transformer used to pre-process notebook for HTML output. Adds IPython notebook
front-end CSS and Pygments CSS to HTML output.
"""
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 header = []
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 def __init__(self, config=None, **kw):
"""
Public constructor
Parameters
----------
config : Config
Configuration file structure
**kw : misc
Additional arguments
"""
super(CSSHtmlHeaderTransformer, self).__init__(config=config, **kw)
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 if self.enabled :
self._regen_header()
def __call__(self, nb, resources):
"""Fetch and add CSS to the resource dictionary
Fetch CSS from IPython and Pygments to add at the beginning
of the html files. Add this css in resources in the
"inlining.css" key
Parameters
----------
nb : NotebookNode
Notebook being converted
resources : dictionary
Additional resources used in the conversion process. Allows
transformers to pass variables into the Jinja engine.
Jonathan Frederic
Split transformer code
r10437 """
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Split transformer code
r10437 resources['inlining'] = {}
resources['inlining']['css'] = self.header
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Split transformer code
r10437 return nb, resources
Jonathan Frederic
Cleanup and refactor, transformers
r10674 def _regen_header(self):
"""
Fills self.header with lines of CSS extracted from iPython
and Pygments.
"""
#Clear existing header.
Jonathan Frederic
Split transformer code
r10437 header = []
Jonathan Frederic
Cleanup and refactor, transformers
r10674
#Construct path to iPy CSS
static = os.path.join(path.get_ipython_package_dir(), 'frontend',
'html', 'notebook', 'static')
Jonathan Frederic
Split transformer code
r10437 css = os.path.join(static, 'css')
Jonathan Frederic
Cleanup and refactor, transformers
r10674
#Load each known CSS file.
Jonathan Frederic
Split transformer code
r10437 for sheet in [
Jonathan Frederic
Cleanup and refactor, transformers
r10674 # TODO: do we need jquery and prettify?
Jonathan Frederic
Split transformer code
r10437 # os.path.join(static, 'jquery', 'css', 'themes', 'base',
# 'jquery-ui.min.css'),
# os.path.join(static, 'prettify', 'prettify.css'),
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Split transformer code
r10437 os.path.join(css, 'boilerplate.css'),
os.path.join(css, 'fbm.css'),
os.path.join(css, 'notebook.css'),
os.path.join(css, 'renderedhtml.css'),
os.path.join(css, 'style.min.css'),
Jonathan Frederic
Cleanup and refactor, transformers
r10674 ]:
Jonathan Frederic
Split transformer code
r10437 try:
Jonathan Frederic
Cleanup and refactor, transformers
r10674 with io.open(sheet, encoding='utf-8') as file:
file_text = file.read()
header.append(file_text)
Jonathan Frederic
Split transformer code
r10437 except IOError:
Jonathan Frederic
Cleanup and refactor, transformers
r10674
# New version of iPython with style.min.css, pass
Jonathan Frederic
Split transformer code
r10437 pass
Jonathan Frederic
Cleanup and refactor, transformers
r10674 #Add pygments CSS
Jonathan Frederic
Split transformer code
r10437 pygments_css = HtmlFormatter().get_style_defs('.highlight')
header.append(pygments_css)
Jonathan Frederic
Cleanup and refactor, transformers
r10674
#Set header
Jonathan Frederic
Split transformer code
r10437 self.header = header