Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
122 lines (100 sloc) 4.22 KB
# -*- coding: utf-8 -*-
@brief Customer notebook exporters.
.. versionadded:: 1.7
import os
from textwrap import indent
from traitlets import default
from traitlets.config import Config
from nbconvert.exporters import RSTExporter
from jinja2 import DictLoader
from nbconvert.filters.pandoc import convert_pandoc
def convert_pandoc_rst(source, from_format, to_format, extra_args=None):
Overwrites `convert_pandoc <>`_.
@param source string to convert
@param from_format from format
@param to_format to format
@param extra_args extra arguments
@return results
return convert_pandoc(source, from_format, to_format, extra_args=None)
def process_raw_html(source, extra_args=None):
Replaces the output of
`add_menu_notebook <
.. contents::
if source is None:
return source
if 'var update_menu = function() {' in source:
return "\n\n.. contents::\n :local:\n\n"
return "\n\n.. raw:: html\n\n" + indent(source, prefix=' ')
class UpgradedRSTExporter(RSTExporter):
Exports :epkg:`rst` documents.
Overwrites `RSTExporter <>`_.
* It replaces `convert_pandoc <>`_
by @see fn convert_pandoc_rst.
* It converts :epkg:`svg` into :epkg:`png` if possible,
see @see fn process_raw_html.
* It replaces some known :epkg:`javascript`. The output of function
`add_menu_notebook <
is replaced by ``.. contents::``.
.. index:: notebook export, nbconvert
It extends the template
`rst.tpl <>`_.
New template is `rst_modified.tpl <
It follows the hints given at
`Programatically creating templates
:epkg:`jyquickhelper` should add a string highly recognizable when adding a menu.
def __init__(self, *args, **kwargs):
Overwrites the extra loaders to get the right template.
filename = os.path.join(os.path.dirname(__file__), 'rst_modified.tpl')
with open(filename, 'r', encoding='utf-8') as f:
content =
dl = DictLoader({'rst_modified.tpl': content})
kwargs['extra_loaders'] = [dl]
RSTExporter.__init__(self, *args, **kwargs)
def default_filters(self):
Overrides in subclasses to provide extra filters.
This should return an iterable of 2-tuples: (name, class-or-function).
You should call the method on the parent class and include the filters
it provides.
If a name is repeated, the last filter provided wins. Filters from
user-supplied config win over filters provided by classes.
for k, v in RSTExporter.default_filters(self):
yield (k, v)
yield ('convert_pandoc_rst', convert_pandoc_rst)
yield ('process_raw_html', process_raw_html)
def _template_file_default(self):
return "rst_modified.tpl"
def default_config(self):
c = Config({
'ExtractOutputPreprocessor': {
'enabled': True,
'output_filename_template': '{unique_key}_{cell_index}_{index}{extension}'
'HighlightMagicsPreprocessor': {
'enabled': True
c.merge(super(UpgradedRSTExporter, self).default_config)
return c
You can’t perform that action at this time.