revealhelp.py
94 lines
| 3.6 KiB
| text/x-python
|
PythonLexer
Jonathan Frederic
|
r10674 | """Module that pre-processes the notebook for export via Reveal. | ||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# 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 | ||||
#----------------------------------------------------------------------------- | ||||
damianavila
|
r11894 | import os | ||
import urllib2 | ||||
MinRK
|
r11452 | from .base import Transformer | ||
damianavila
|
r11894 | from IPython.utils.traitlets import Unicode, Bool | ||
damianavila
|
r11181 | |||
Jonathan Frederic
|
r10674 | #----------------------------------------------------------------------------- | ||
# Classes and functions | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r11452 | class RevealHelpTransformer(Transformer): | ||
Jonathan Frederic
|
r10437 | |||
damianavila
|
r11183 | url_prefix = Unicode('//cdn.jsdelivr.net/reveal.js/2.4.0', | ||
config=True, | ||||
help="""If you want to use a local reveal.js library, | ||||
use 'url_prefix':'reveal.js' in your config object.""") | ||||
damianavila
|
r11179 | |||
damianavila
|
r12051 | speaker_notes = Bool(False, | ||
config=True, | ||||
help="""If you want to use the speaker notes | ||||
set this to True.""") | ||||
damianavila
|
r11179 | |||
damianavila
|
r10851 | def call(self, nb, resources): | ||
Jonathan Frederic
|
r10674 | """ | ||
Called once to 'transform' contents of the notebook. | ||||
damianavila
|
r11183 | |||
Jonathan Frederic
|
r10674 | Parameters | ||
---------- | ||||
nb : NotebookNode | ||||
Notebook being converted | ||||
resources : dictionary | ||||
Additional resources used in the conversion process. Allows | ||||
transformers to pass variables into the Jinja engine. | ||||
""" | ||||
damianavila
|
r11183 | |||
Jonathan Frederic
|
r10437 | for worksheet in nb.worksheets : | ||
Jonathan Frederic
|
r11367 | for index, cell in enumerate(worksheet.cells): | ||
damianavila
|
r10823 | |||
#Make sure the cell has slideshow metadata. | ||||
damianavila
|
r10947 | cell.metadata.align_type = cell.get('metadata', {}).get('slideshow', {}).get('align_type', 'Left') | ||
damianavila
|
r10823 | cell.metadata.slide_type = cell.get('metadata', {}).get('slideshow', {}).get('slide_type', '-') | ||
Jonathan Frederic
|
r10674 | #Get the slide type. If type is start of subslide or slide, | ||
#end the last subslide/slide. | ||||
Jonathan Frederic
|
r10437 | if cell.metadata.slide_type in ['slide']: | ||
Jonathan Frederic
|
r11367 | worksheet.cells[index - 1].metadata.slide_helper = 'slide_end' | ||
Jonathan Frederic
|
r10437 | if cell.metadata.slide_type in ['subslide']: | ||
Jonathan Frederic
|
r11367 | worksheet.cells[index - 1].metadata.slide_helper = 'subslide_end' | ||
damianavila
|
r11179 | |||
damianavila
|
r11183 | |||
Jonathan Frederic
|
r12143 | if not isinstance(resources['reveal'], dict): | ||
damianavila
|
r11184 | resources['reveal'] = {} | ||
damianavila
|
r11183 | resources['reveal']['url_prefix'] = self.url_prefix | ||
damianavila
|
r11894 | resources['reveal']['notes_prefix'] = self.url_prefix | ||
cdn = 'http://cdn.jsdelivr.net/reveal.js/2.4.0' | ||||
local = 'local' | ||||
html_path = 'plugin/notes/notes.html' | ||||
js_path = 'plugin/notes/notes.js' | ||||
html_infile = os.path.join(cdn, html_path) | ||||
js_infile = os.path.join(cdn, js_path) | ||||
html_outfile = os.path.join(local, html_path) | ||||
js_outfile = os.path.join(local, js_path) | ||||
if self.speaker_notes: | ||||
if 'outputs' not in resources: | ||||
resources['outputs'] = {} | ||||
resources['outputs'][html_outfile] = self.notes_helper(html_infile) | ||||
resources['outputs'][js_outfile] = self.notes_helper(js_infile) | ||||
resources['reveal']['notes_prefix'] = local | ||||
damianavila
|
r11183 | |||
Jonathan Frederic
|
r10674 | return nb, resources | ||
damianavila
|
r11894 | |||
def notes_helper(self, infile): | ||||
"""Helper function to get the content from an url.""" | ||||
content = urllib2.urlopen(infile).read() | ||||
return content | ||||