Permalink
Browse files

Fix #1900 -- render ipynb in listings

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information...
1 parent bbfd1db commit 2224f6810ee588177978da8ea267ae7829a50691 @Kwpolska Kwpolska committed Jul 31, 2015
Showing with 35 additions and 10 deletions.
  1. +8 −2 CHANGES.txt
  2. +11 −7 nikola/plugins/compile/ipynb.py
  3. +16 −1 nikola/plugins/task/listings.py
View
@@ -1,10 +1,16 @@
-New in Master
+New in master
=============
+Features
+--------
+
+* Render Jupyter notebooks (ipynb) in listings (Issue #1900)
+
Bugfixes
--------
-* Handle non-integer shutter speeds in WP importer (Issue #1917)
+* Handle non-integer shutter speeds and other variables in WordPress
+ importer (Issue #1917)
New in v7.6.2
@@ -66,19 +66,23 @@ def set_site(self, site):
self.logger = get_logger('compile_ipynb', STDERR_HANDLER)
super(CompileIPynb, self).set_site(site)
- def compile_html(self, source, dest, is_two_file=True):
- """Compile source file into HTML and save as dest."""
+ def compile_html_string(self, source, is_two_file=True):
+ """Export notebooks as HTML strings."""
if flag is None:
req_missing(['ipython[notebook]>=2.0.0'], 'build this site (compile ipynb)')
- makedirs(os.path.dirname(dest))
HTMLExporter.default_template = 'basic'
c = Config(self.site.config['IPYNB_CONFIG'])
exportHtml = HTMLExporter(config=c)
+ with io.open(source, "r", encoding="utf8") as in_file:
+ nb_json = nbformat.read(in_file, current_nbformat)
+ (body, resources) = exportHtml.from_notebook_node(nb_json)
+ return body
+
+ def compile_html(self, source, dest, is_two_file=True):
+ """Compile source file into HTML and save as dest."""
+ makedirs(os.path.dirname(dest))
with io.open(dest, "w+", encoding="utf8") as out_file:
- with io.open(source, "r", encoding="utf8") as in_file:
- nb_json = nbformat.read(in_file, current_nbformat)
- (body, resources) = exportHtml.from_notebook_node(nb_json)
- out_file.write(body)
+ out_file.write(self.compile_html_string(source, is_two_file))
def read_metadata(self, post, file_metadata_regexp=None, unslugify_titles=False, lang=None):
"""Read metadata directly from ipynb file.
@@ -30,6 +30,7 @@
import sys
import os
+import lxml.html
from pygments import highlight
from pygments.lexers import get_lexer_for_filename, TextLexer
@@ -113,7 +114,17 @@ def gen_tasks(self):
ignored_extensions = (".pyc", ".pyo")
def render_listing(in_name, out_name, input_folder, output_folder, folders=[], files=[]):
- if in_name:
+ needs_ipython_css = False
+ if in_name and in_name.endswith('.ipynb'):
+ # Special handling: render ipynbs in listings (Issue #1900)
+ ipynb_compiler = self.site.plugin_manager.getPluginByName("ipynb", "PageCompiler").plugin_object
+ ipynb_raw = ipynb_compiler.compile_html_string(in_name, True)
+ ipynb_html = lxml.html.fromstring(ipynb_raw)
+ # The raw HTML contains garbage (scripts and styles), we can’t leave it in
+ code = lxml.html.tostring(ipynb_html.xpath('//*[@id="notebook"]')[0], encoding='unicode')
+ title = os.path.basename(in_name)
+ needs_ipython_css = True
+ elif in_name:
with open(in_name, 'r') as fd:
try:
lexer = get_lexer_for_filename(in_name)
@@ -154,6 +165,10 @@ def render_listing(in_name, out_name, input_folder, output_folder, folders=[], f
'source_link': source_link,
'pagekind': ['listing'],
}
+ if needs_ipython_css:
+ # If someone does not have ipynb posts and only listings, we
+ # need to enable ipynb CSS for ipynb listings.
+ context['needs_ipython_css'] = True
self.site.render_template('listing.tmpl', out_name, context)
yield self.group_task()

0 comments on commit 2224f68

Please sign in to comment.