Skip to content

Instantly share code, notes, and snippets.

@punchagan
Last active October 8, 2017 00:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save punchagan/5882879 to your computer and use it in GitHub Desktop.
Save punchagan/5882879 to your computer and use it in GitHub Desktop.
GitHub IPynb rendering
// ==UserScript==
// @name GitHub IPynb rendering
// @namespace ghipynb
// @version 0.3.1
// @description Renders raw IPython notebooks on GitHub using nbviewer and Frames!
// @license WTFPL
// @match *://*.github.com/*
// @match *://github.com/*
// @match *://gist.github.com/*
// @grant none
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js
// ==/UserScript==
/*
Uses frames to replace elements containing raw IPython notebooks with rendered
IPython notebooks on GitHub. Uses rendering service provided by
nbviewer.ipython.org
- Works with Greasemonkey on Firefox
- Works with Tampermonkey on Chromium/Chrome
*/
(function () {
'use strict';
// Base URL for NB-viewer
// NOTE: other code assumes / at the end
var nbviewer_url = 'http://nbviewer.ipython.org/urls/';
// Replaces the div containing the nb with an iframe that renders it
var replace_raw_nb = function(url, div){
if (!url || url.substr(-6) != '.ipynb') { return null; }
var container = $('<div>')
var nbviewer_link = $('<a>').attr('href', nbviewer_url).text('NB viewer');
nbviewer_link.attr('href', nbviewer_link[0].protocol + '//' + nbviewer_link[0].hostname);
$('<div>').text('Powered by ').append(nbviewer_link)
.appendTo(container).css({'text-align': 'right'});
var iframe = $('<iframe class="ipynb-render">').attr('src', url);
iframe.height(div.height()).width(div.width()).prependTo(container);
div.replaceWith(container);
};
// Have code to identify the div and url
var search_and_replace_ipynbs = function(){
var url, div;
if (document.location.hostname.search('raw')>=0 || document.location.pathname.search('/raw/')>=0) {
var proto_length = document.location.protocol.length + 2;
url = nbviewer_url + document.location.href.toString().substr(proto_length);
div = $('pre');
return replace_raw_nb(url, div);
} else {
// Figure out what the raw url would be
var gist_page = document.location.hostname == 'gist.github.com';
if (gist_page) {
var divs = $('.file-box');
divs.each(function(idx, el){
div = $(el).find('div.file-data');
url = nbviewer_url + document.location.host.toString() + $(el).find('a.raw-url').attr('href');
replace_raw_nb(url, div)
});
return;
} else {
url = nbviewer_url + document.location.host.toString() + $('a#raw-url').attr('href');
div = $('.blob-wrapper');
return replace_raw_nb(url, div);
}
}
// FIXME: If not found from page, do url munging.
};
search_and_replace_ipynbs();
// Using the file finder on GH, doesn't load another page, instead magically changes current page
// Listen to DOM changes, and replace ipynbs
// This wouldn't create a recusive loop because, we are replacing the original divs
document.body.addEventListener("DOMSubtreeModified", search_and_replace_ipynbs);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment