Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1920 from getnikola/ipynb-listings
Fix #1900 -- render ipynb in listings
  • Loading branch information
Kwpolska committed Jul 31, 2015
2 parents bbfd1db + 2224f68 commit 15217bc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
10 changes: 8 additions & 2 deletions CHANGES.txt
@@ -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
Expand Down
18 changes: 11 additions & 7 deletions nikola/plugins/compile/ipynb.py
Expand Up @@ -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.
Expand Down
17 changes: 16 additions & 1 deletion nikola/plugins/task/listings.py
Expand Up @@ -30,6 +30,7 @@

import sys
import os
import lxml.html

from pygments import highlight
from pygments.lexers import get_lexer_for_filename, TextLexer
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 15217bc

Please sign in to comment.