##// END OF EJS Templates
Merge pull request #4154 from ellisonbg/mathstyle...
Merge pull request #4154 from ellisonbg/mathstyle Center LaTeX and figures in markdown

File last commit:

r12221:017ada76
r12947:7b800aa6 merge
Show More
csshtmlheader.py
106 lines | 3.1 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
Jonathan Frederic
Removed ActivatableTransformer and moved enabled key into base.
r11388
Jonathan Frederic
Cleanup and refactor, transformers
r10674 from IPython.utils import path
Paul Ivanov
replace 'transformer' with 'preprocessor'
r12219 from .base import Preprocessor
Jake Vanderplas
use traitlet for highlight_class
r11123
from IPython.utils.traitlets import Unicode
Jonathan Frederic
Removed ActivatableTransformer and moved enabled key into base.
r11388
Jonathan Frederic
Cleanup and refactor, transformers
r10674 #-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
Jonathan Frederic
Split transformer code
r10437
Paul Ivanov
replace 'transformer' with 'preprocessor'
r12219 class CSSHTMLHeaderPreprocessor(Preprocessor):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
Paul Ivanov
replace 'transformer' with 'preprocessor'
r12219 Preprocessor used to pre-process notebook for HTML output. Adds IPython notebook
Jonathan Frederic
Cleanup and refactor, transformers
r10674 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
Jake Vanderplas
Allow specification of full highlight class in CSSHTMLHeader
r11140 highlight_class = Unicode('.highlight', config=True,
Jake Vanderplas
use traitlet for highlight_class
r11123 help="CSS highlight class identifier")
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
"""
Paul Ivanov
replace 'transformer' with 'preprocessor'
r12219 super(CSSHTMLHeaderPreprocessor, self).__init__(config=config, **kw)
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 if self.enabled :
self._regen_header()
Paul Ivanov
replace 'transformer' with 'preprocessor'
r12219 def preprocess(self, nb, resources):
Jonathan Frederic
Cleanup and refactor, transformers
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
Paul Ivanov
replace 'transformer' with 'preprocessor'
r12219 preprocessors 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):
"""
MinRK
various spelling and capitalization fixes
r11046 Fills self.header with lines of CSS extracted from IPython
Jonathan Frederic
Cleanup and refactor, transformers
r10674 and Pygments.
"""
#Clear existing header.
Jonathan Frederic
Split transformer code
r10437 header = []
Jonathan Frederic
Cleanup and refactor, transformers
r10674
MinRK
various spelling and capitalization fixes
r11046 #Construct path to IPy CSS
damianavila
Change css path because the flatten of notebook dir.
r11043 sheet_filename = os.path.join(path.get_ipython_package_dir(),
damianavila
Fixing the path again.
r11044 'html', 'static', 'style', 'style.min.css')
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Only 'style.min.css' should be needed - (suggested by @minrk)
r10766 #Load style CSS file.
Matthias BUSSONNIER
style.min.css shoudl always exist......
r11219 with io.open(sheet_filename, encoding='utf-8') as file:
file_text = file.read()
header.append(file_text)
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 #Add pygments CSS
Jake Vanderplas
use traitlet for highlight_class
r11123 formatter = HtmlFormatter()
Jake Vanderplas
Allow specification of full highlight class in CSSHTMLHeader
r11140 pygments_css = formatter.get_style_defs(self.highlight_class)
Jonathan Frederic
Split transformer code
r10437 header.append(pygments_css)
Jonathan Frederic
Cleanup and refactor, transformers
r10674
#Set header
Jonathan Frederic
Split transformer code
r10437 self.header = header