From d93b4231677e6dfe3d835a47d818ff80ea5f6dbd 2013-02-13 19:30:13 From: Matthias BUSSONNIER Date: 2013-02-13 19:30:13 Subject: [PATCH] move html header out --- diff --git a/converters/template.py b/converters/template.py index 88fa853..2f54361 100755 --- a/converters/template.py +++ b/converters/template.py @@ -27,8 +27,7 @@ from converters.utils import markdown2rst # Stdlib imports import io -import os -from IPython.utils import path + from IPython.utils.traitlets import MetaHasTraits from jinja2 import Environment, FileSystemLoader @@ -59,50 +58,6 @@ from IPython.utils.traitlets import ( Unicode, Any, List, Bool) class ConversionException(Exception): pass -#todo move this out -def header_body(): - """Return the body of the header as a list of strings.""" - - from pygments.formatters import HtmlFormatter - - header = [] - static = os.path.join(path.get_ipython_package_dir(), - 'frontend', 'html', 'notebook', 'static', - ) - here = os.path.split(os.path.realpath(__file__))[0] - css = os.path.join(static, 'css') - for sheet in [ - # do we need jquery and prettify? - # os.path.join(static, 'jquery', 'css', 'themes', 'base', - # 'jquery-ui.min.css'), - # os.path.join(static, 'prettify', 'prettify.css'), - 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'), - # our overrides: - os.path.join(here, '..', 'css', 'static_html.css'), - ]: - try: - with io.open(sheet, encoding='utf-8') as f: - s = f.read() - header.append(s) - except IOError: - # new version of ipython with style.min.css, pass - pass - - pygments_css = HtmlFormatter().get_style_defs('.highlight') - header.append(pygments_css) - return header - - - - - -inlining = {} -inlining['css'] = header_body() - texenv.block_start_string = '((*' @@ -176,6 +131,7 @@ class ConverterTemplate(Configurable): ## for compat, remove later self.preprocessors.append(trans.ExtractFigureTransformer(config=config)) self.preprocessors.append(trans.RevealHelpTransformer(config=config)) + self.preprocessors.append(trans.CSSHtmlHeaderTransformer(config=config)) ## self.env.filters['filter_data_type'] = FilterDataType(config=config) @@ -219,7 +175,7 @@ class ConverterTemplate(Configurable): other resources """ nb, resources = self.process(nb) - return self.template.render(nb=nb, inlining=inlining), resources + return self.template.render(nb=nb, resources=resources), resources def from_filename(self, filename): @@ -228,3 +184,4 @@ class ConverterTemplate(Configurable): return self.convert(nbformat.read(f, 'json')) + diff --git a/converters/transformers.py b/converters/transformers.py index 4a68bb2..1c45655 100755 --- a/converters/transformers.py +++ b/converters/transformers.py @@ -2,13 +2,13 @@ """ -from __future__ import print_function, absolute_import +from __future__ import print_function + from IPython.config.configurable import Configurable from IPython.utils.traitlets import Unicode, Bool, Dict, List -from .jinja_filters import GlobalConfigurable -class ConfigurableTransformers(GlobalConfigurable): +class ConfigurableTransformers(Configurable): """ A configurable transformer """ def __init__(self, config=None, **kw): @@ -85,6 +85,15 @@ class ExtractFigureTransformer(ActivatableTransformer): Usefull for latex where svg will be converted to pdf before inclusion """ ) + display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'], + config=True, + help= """ + An ordered list of prefered output type, the first + encounterd will usually be used when converting discarding + the others. + """ + ) + #to do change this to .format {} syntax key_tpl = Unicode('_fig_%02i.%s', config=True) @@ -223,3 +232,62 @@ class RevealHelpTransformer(ConfigurableTransformers): cell.metadata.slideshow.open_fragment = False return cell,other + +class CSSHtmlHeaderTransformer(ActivatableTransformer): + + def __call__(self, nb, resources): + """Fetch and add css to the resource dict + + Fetch css from IPython adn Pygment to add at the beginning + of the html files. + + Add this css in resources in the "inlining.css" key + """ + resources['inlining']= {} + resources['inlining']['css'] = self.header + return nb, resources + + header= [] + + def __init__(self, config=None, **kw): + super(CSSHtmlHeaderTransformer,self).__init__(config=config, **kw) + if self.enabled : + self.regen_header() + + def regen_header(self): + ## lazy load asa this might not be use in many transformers + import os + from IPython.utils import path + import io + from pygments.formatters import HtmlFormatter + header = [] + static = os.path.join(path.get_ipython_package_dir(), + 'frontend', 'html', 'notebook', 'static', + ) + here = os.path.split(os.path.realpath(__file__))[0] + css = os.path.join(static, 'css') + for sheet in [ + # do we need jquery and prettify? + # os.path.join(static, 'jquery', 'css', 'themes', 'base', + # 'jquery-ui.min.css'), + # os.path.join(static, 'prettify', 'prettify.css'), + 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'), + # our overrides: + os.path.join(here, '..', 'css', 'static_html.css'), + ]: + try: + with io.open(sheet, encoding='utf-8') as f: + s = f.read() + header.append(s) + except IOError: + # new version of ipython with style.min.css, pass + pass + + pygments_css = HtmlFormatter().get_style_defs('.highlight') + header.append(pygments_css) + self.header = header + diff --git a/profile/base_html.nbcv b/profile/base_html.nbcv index db1c9d1..8320378 100644 --- a/profile/base_html.nbcv +++ b/profile/base_html.nbcv @@ -8,3 +8,4 @@ c.ConverterTemplate.tex_environement=False c.NbconvertApp.fileext='html' c.ExtractFigureTransformer.enabled = False +c.CSSHtmlHeaderTransformer.enabled=True diff --git a/profile/full_html.nbcv b/profile/full_html.nbcv index 88182d4..2e18e58 100644 --- a/profile/full_html.nbcv +++ b/profile/full_html.nbcv @@ -1,9 +1,5 @@ c = get_config() - +load_subconfig('base_html.nbcv') c.ConverterTemplate.template_file='fullhtml' -c.ConverterTemplate.tex_environement=False - -c.NbconvertApp.fileext='html' -c.ExtractFigureTransformer.enabled = False diff --git a/templates/fullhtml.tpl b/templates/fullhtml.tpl index e884b0e..aa3f16d 100644 --- a/templates/fullhtml.tpl +++ b/templates/fullhtml.tpl @@ -5,7 +5,7 @@ [{{nb.metadata.name}}] -{% for css in inlining.css -%} +{% for css in resources.inlining.css -%} diff --git a/templates/reveal.tpl b/templates/reveal.tpl index ffd1b3a..c036815 100644 --- a/templates/reveal.tpl +++ b/templates/reveal.tpl @@ -104,7 +104,7 @@ document.write( ' -{% for css in inlining.css -%} +{% for css in resources.inlining.css -%}