public
Last active

GitHub IPynb rendering

  • Download Gist
gh-ipynb.user.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
// ==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);
 
})();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.