Skip to content

Commit

Permalink
Merge pull request #892 from jcb91/ch
Browse files Browse the repository at this point in the history
[collapsible_headings] new features!
  • Loading branch information
jcb91 authored Feb 18, 2017
2 parents ecfcd89 + f4430a3 commit 38fa967
Show file tree
Hide file tree
Showing 14 changed files with 687 additions and 361 deletions.
2 changes: 1 addition & 1 deletion conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ test:
- jupyter_contrib_nbextensions.install
- jupyter_contrib_nbextensions.migrate
- jupyter_contrib_nbextensions.nbconvert_support
- jupyter_contrib_nbextensions.nbconvert_support.collapsible_headings
- jupyter_contrib_nbextensions.nbconvert_support.embedhtml
- jupyter_contrib_nbextensions.nbconvert_support.pp_highlighter
- jupyter_contrib_nbextensions.nbconvert_support.pre_codefolding
- jupyter_contrib_nbextensions.nbconvert_support.pre_collapsible_headings
- jupyter_contrib_nbextensions.nbconvert_support.pre_pymarkdown
- jupyter_contrib_nbextensions.nbconvert_support.pre_svg2pdf
- jupyter_contrib_nbextensions.nbconvert_support.toc2
Expand Down
32 changes: 15 additions & 17 deletions docs/source/exporting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ Retaining Codefolding
.. autoclass:: CodeFoldingPreprocessor


Collapsible Headings
^^^^^^^^^^^^^^^^^^^^

.. autoclass:: CollapsibleHeadingsPreprocessor


Retaining Highlighting
^^^^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -90,6 +84,20 @@ Export Table of Contents
.. autoclass:: TocExporter


Inlining css & javascript
^^^^^^^^^^^^^^^^^^^^^^^^^

.. autoclass:: ExporterInliner

Forms the basis for :class:`ExporterCollapsibleHeadings`.


Collapsible Headings
^^^^^^^^^^^^^^^^^^^^

.. autoclass:: ExporterCollapsibleHeadings


Templates
---------

Expand Down Expand Up @@ -122,16 +130,6 @@ The supported cell metadata tags are:

Detailed description:

This will hide any cell marked as `hidden` (used for collapsible headings extension):
.. code-block::
{% block any_cell scoped %}
{%- if cell.metadata.hidden -%}
{%- else -%}
{{ super() }}
{%- endif -%}
{% endblock any_cell %}
This will hide the input of either an individual code cell or all code cells of the notebook:
.. code-block::
Expand Down Expand Up @@ -166,7 +164,7 @@ This will suppress the prompt string if the input of a code cell is hidden:
nbextensions.tplx
^^^^^^^^^^^^^^^^^

This template implements the features to hide cells used by extensions like collapsible_headings, hide_input, etc.
This template implements the features to hide cells used by extensions like hide_input, etc.
It won't produce a valid LaTeX file alone, use it to extend your own template (see printviewlatex.tplx).

printviewlatex.tplx
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def main():
'html_toc = jupyter_contrib_nbextensions.nbconvert_support.toc2:TocExporter', # noqa: E501
'selectLanguage = jupyter_contrib_nbextensions.nbconvert_support.nbTranslate:NotebookLangExporter', # noqa: E501
'html_embed = jupyter_contrib_nbextensions.nbconvert_support.embedhtml:EmbedHTMLExporter', # noqa: E501
'html_ch = jupyter_contrib_nbextensions.nbconvert_support.collapsible_headings:ExporterCollapsibleHeadings', # noqa: E501
],
},
scripts=[os.path.join('scripts', p) for p in [
Expand Down
12 changes: 7 additions & 5 deletions src/jupyter_contrib_nbextensions/nbconvert_support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@

import os

from .collapsible_headings import ExporterCollapsibleHeadings
from .embedhtml import EmbedHTMLExporter
from .exporter_inliner import ExporterInliner
from .nbTranslate import NotebookLangExporter
from .pp_highlighter import HighlighterPostProcessor, HighlighterPreprocessor
from .pre_codefolding import CodeFoldingPreprocessor
from .pre_collapsible_headings import CollapsibleHeadingsPreprocessor
from .pre_pymarkdown import PyMarkdownPreprocessor
from .pre_svg2pdf import SVG2PDFPreprocessor
from .toc2 import TocExporter
from .nbTranslate import NotebookLangExporter

__all__ = [
'templates_directory',
'CodeFoldingPreprocessor',
'CollapsibleHeadingsPreprocessor',
'EmbedHTMLExporter',
'ExporterCollapsibleHeadings',
'ExporterInliner',
'HighlighterPostProcessor',
'HighlighterPreprocessor',
'NotebookLangExporter',
'PyMarkdownPreprocessor',
'SVG2PDFPreprocessor',
'templates_directory',
'TocExporter',
'NotebookLangExporter'
]


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Nbconvert exporter to inline css & js for collapsible_headings."""

import json
import os

from notebook.services.config import ConfigManager

from jupyter_contrib_nbextensions import __file__ as contrib_init
from .exporter_inliner import ExporterInliner


class ExporterCollapsibleHeadings(ExporterInliner):
"""
HTMLExporter which inlines the collapsible_headings nbextension.
Export collapsible_headings nbextension functionality to html
by inlining relevant css and js content.
Example usage::
jupyter nbconvert --to html_ch FILE.ipynb
"""

def _template_file_default(self):
return 'collapsible_headings'

def __init__(self, *args, **kwargs):
super(ExporterCollapsibleHeadings, self).__init__(*args, **kwargs)

ch_dir = os.path.join(
os.path.dirname(contrib_init), 'nbextensions',
'collapsible_headings')

with open(os.path.join(ch_dir, 'main.css'), 'r') as f:
main_css = f.read()
self.inliner_resources['css'].append(main_css)

self.inliner_resources['css'].append("""
/* no local copies of fontawesome fonts from basic templates, so get them from cdn */
@font-face {
font-family: 'FontAwesome';
src: url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.eot');
src: url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.eot?#iefix') format('embedded-opentype'),
url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.woff') format('woff'),
url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.ttf') format('truetype'),
url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.svg#fontawesomeregular') format('svg');
font-weight: normal;
font-style: normal;
}
""") # noqa: E501

with open(os.path.join(ch_dir, 'main.js'), 'r') as f:
self.inliner_resources['js'].append(
f.read().replace(
"define([",
"define('nbextensions/collapsible_headings/main', [")
)

cm = ConfigManager()
collapsible_headings_options = cm.get('notebook').get(
'collapsible_headings', {})
self.inliner_resources['js'].append("""
require([
'jquery',
'nbextensions/collapsible_headings/main'
], function (
$,
nbext
) {
nbext.set_collapsible_headings_options(%s);
$(document).ready(function () {
nbext.refresh_all_headings();
});
});
""" % json.dumps(collapsible_headings_options))
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Nbconvert exporter to inline css & js for collapsible_headings."""

from __future__ import print_function

from nbconvert.exporters.html import HTMLExporter
from traitlets import Dict


class ExporterInliner(HTMLExporter):

inliner_resources = Dict(
{'css': [], 'js': []}, config=True,
help='css and js scripts to wrap in html <style> or <script> tags')

def _template_file_default(self):
return 'inliner'

def from_notebook_node(self, nb, resources=None, **kw):

# ensure resources used by template actually exist, add in any from
# config
if resources is None:
resources = {}
inliner_resources = resources.setdefault('inliner', {})
for tt in ('css', 'js'):
existing_items = inliner_resources.setdefault(tt, [])
existing_items += [
item for item in self.inliner_resources[tt]
if item not in existing_items]

return super(ExporterInliner, self).from_notebook_node(
nb, resources, **kw)

@property
def default_config(self):
c = super(ExporterInliner, self).default_config
# import here to avoid circular import
from jupyter_contrib_nbextensions.nbconvert_support import (
templates_directory)
contrib_templates_dir = templates_directory()

template_path = c.TemplateExporter.setdefault('template_path', [])
if templates_directory not in template_path:
template_path.append(contrib_templates_dir)

return c

This file was deleted.

Loading

0 comments on commit 38fa967

Please sign in to comment.