##// END OF EJS Templates
Improve conversion to latex of output cells...
Improve conversion to latex of output cells Improve the latex convert possibilites of pyout and display_data cells. Detailed changes: - set display_data_priority for latex output to start with 'latex' - use numbered equations - define relative figure size instead of absolute - specify image file position relative to input file position - pyout.text is only rendered if latex representation is not available - place display_data_format_latex output in equation environment - some minor code clean-up

File last commit:

r8478:011ccbfa
r8804:70796c14
Show More
notebook_sphinxext.py
87 lines | 2.6 KiB | text/x-python | PythonLexer
/ notebook_sphinxext.py
Jake Vanderplas
add raw-based sphinx notebook extension
r8465 import sys
import os.path
import re
import time
from docutils import io, nodes, statemachine, utils
from docutils.error_reporting import ErrorString
from docutils.parsers.rst import Directive, convert_directive_function
from docutils.parsers.rst import directives, roles, states
from docutils.parsers.rst.roles import set_classes
from docutils.transforms import misc
from nbconvert import ConverterHTML
Jake Vanderplas
cleanup implementation
r8476 class Notebook(Directive):
Jake Vanderplas
add raw-based sphinx notebook extension
r8465 """
Jake Vanderplas
cleanup implementation
r8476 Use nbconvert to insert a notebook into the environment.
This is based on the Raw directive in docutils
Jake Vanderplas
add raw-based sphinx notebook extension
r8465 """
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True
option_spec = {}
has_content = False
def run(self):
Jake Vanderplas
sphinx: remove duplicate html, head, & body tags; use scoped attribute
r8478 # check if raw html is supported
if not self.state.document.settings.raw_enabled:
Jake Vanderplas
add raw-based sphinx notebook extension
r8465 raise self.warning('"%s" directive disabled.' % self.name)
Jake Vanderplas
sphinx: remove duplicate html, head, & body tags; use scoped attribute
r8478
# set up encoding
Jake Vanderplas
add raw-based sphinx notebook extension
r8465 attributes = {'format': 'html'}
encoding = self.options.get(
'encoding', self.state.document.settings.input_encoding)
Jake Vanderplas
cleanup implementation
r8476 e_handler = self.state.document.settings.input_encoding_error_handler
Jake Vanderplas
add raw-based sphinx notebook extension
r8465
# get path to notebook
source_dir = os.path.dirname(
os.path.abspath(self.state.document.current_source))
Jake Vanderplas
sphinx: remove duplicate html, head, & body tags; use scoped attribute
r8478 nb_path = os.path.normpath(os.path.join(source_dir,
self.arguments[0]))
nb_path = utils.relative_path(None, nb_path)
Jake Vanderplas
add raw-based sphinx notebook extension
r8465
# convert notebook to html
Jake Vanderplas
sphinx: remove duplicate html, head, & body tags; use scoped attribute
r8478 converter = ConverterHTML(nb_path)
converter.read()
# add HTML5 scoped attribute to header style tags
header = map(lambda s: s.replace('<style', '<style scoped="scoped"'),
converter.header_body())
# concatenate raw html lines
lines = ['<div class="ipynotebook">']
lines.extend(header)
lines.extend(converter.main_body())
lines.append('</div>')
text = '\n'.join(lines)
Jake Vanderplas
add raw-based sphinx notebook extension
r8465
Jake Vanderplas
sphinx: remove duplicate html, head, & body tags; use scoped attribute
r8478 # add dependency
self.state.document.settings.record_dependencies.add(nb_path)
attributes['source'] = nb_path
# create notebook node
Jake Vanderplas
cleanup implementation
r8476 nb_node = notebook('', text, **attributes)
Jake Vanderplas
sphinx: remove duplicate html, head, & body tags; use scoped attribute
r8478 (nb_node.source, nb_node.line) = \
self.state_machine.get_source_and_line(self.lineno)
Jake Vanderplas
cleanup implementation
r8476 return [nb_node]
Jake Vanderplas
add raw-based sphinx notebook extension
r8465
Jake Vanderplas
cleanup implementation
r8476 class notebook(nodes.raw):
Jake Vanderplas
add raw-based sphinx notebook extension
r8465 pass
Jake Vanderplas
cleanup implementation
r8476
Jake Vanderplas
add raw-based sphinx notebook extension
r8465 def visit_notebook_node(self, node):
self.visit_raw(node)
Jake Vanderplas
cleanup implementation
r8476
Jake Vanderplas
add raw-based sphinx notebook extension
r8465 def depart_notebook_node(self, node):
self.depart_raw(node)
Jake Vanderplas
cleanup implementation
r8476
Jake Vanderplas
add raw-based sphinx notebook extension
r8465 def setup(app):
app.add_node(notebook,
html=(visit_notebook_node, depart_notebook_node))
app.add_directive('notebook', Notebook)