csshtmlheader.py
107 lines
| 3.1 KiB
| text/x-python
|
PythonLexer
|
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 | ||||
|
r11388 | |||
|
r10674 | from IPython.utils import path | ||
|
r11452 | from .base import Transformer | ||
|
r11123 | |||
from IPython.utils.traitlets import Unicode | ||||
|
r11388 | |||
|
r10674 | #----------------------------------------------------------------------------- | ||
# Classes and functions | ||||
#----------------------------------------------------------------------------- | ||||
|
r10437 | |||
|
r11452 | class CSSHTMLHeaderTransformer(Transformer): | ||
|
r10674 | """ | ||
Transformer used to pre-process notebook for HTML output. Adds IPython notebook | ||||
front-end CSS and Pygments CSS to HTML output. | ||||
""" | ||||
|
r10437 | |||
|
r10674 | header = [] | ||
|
r10437 | |||
|
r11140 | highlight_class = Unicode('.highlight', config=True, | ||
|
r11123 | help="CSS highlight class identifier") | ||
|
r10674 | def __init__(self, config=None, **kw): | ||
""" | ||||
Public constructor | ||||
Parameters | ||||
---------- | ||||
config : Config | ||||
Configuration file structure | ||||
**kw : misc | ||||
Additional arguments | ||||
""" | ||||
|
r11108 | super(CSSHTMLHeaderTransformer, self).__init__(config=config, **kw) | ||
|
r10437 | |||
|
r10674 | if self.enabled : | ||
self._regen_header() | ||||
|
r12009 | def call(self, nb, resources): | ||
|
r10674 | """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. | ||||
|
r10437 | """ | ||
|
r10674 | |||
|
r10437 | resources['inlining'] = {} | ||
resources['inlining']['css'] = self.header | ||||
|
r10674 | |||
|
r10437 | return nb, resources | ||
|
r10674 | def _regen_header(self): | ||
""" | ||||
|
r11046 | Fills self.header with lines of CSS extracted from IPython | ||
|
r10674 | and Pygments. | ||
""" | ||||
#Clear existing header. | ||||
|
r10437 | header = [] | ||
|
r10674 | |||
|
r11046 | #Construct path to IPy CSS | ||
|
r14756 | from IPython.html import DEFAULT_STATIC_FILES_PATH | ||
sheet_filename = os.path.join(DEFAULT_STATIC_FILES_PATH, | ||||
'style', 'style.min.css') | ||||
|
r10674 | |||
|
r10766 | #Load style CSS file. | ||
|
r11219 | with io.open(sheet_filename, encoding='utf-8') as file: | ||
file_text = file.read() | ||||
header.append(file_text) | ||||
|
r10437 | |||
|
r10674 | #Add pygments CSS | ||
|
r11123 | formatter = HtmlFormatter() | ||
|
r11140 | pygments_css = formatter.get_style_defs(self.highlight_class) | ||
|
r10437 | header.append(pygments_css) | ||
|
r10674 | |||
#Set header | ||||
|
r10437 | self.header = header | ||