public
Created

autolink all ipynb link to nbviewer

  • Download Gist
autoipynb.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
url_root='http://nbviewer.ipython.org/';
url=null;
 
var transform_url = function(href){
if( href===undefined){return}
if (href.search(/^https?:\/\/nbviewer/) == 0 ){
console.log('case 1')
return null
} else if (href.search(/^https?:\/\/gist\.github\.com\/(?:\w+\/)?[a-f0-9]+$/) !== -1) {
console.log('case 2')
gist = href.match(/^https?:\/\/gist\.github\.com\/(?:\w+\/)?([a-f0-9]+)$/);
return url_root + gist[1];
} else if (href.search(/^https:\/\/.*\.ipynb$/) !== -1) {
console.log('case 3')
path = href.match(/^https:\/\/(.*\.ipynb)$/);
return url_root + 'urls/' + path[1];
} else if (href.search(/^http:\/\/.*\.ipynb$/) !== -1) {
console.log('case 4')
path = href.match(/^http:\/\/(.*\.ipynb)$/);
return url_root + 'url/' + path[1];
}
 
console.log('case none')
return null
}
 
autoviewlink = function(index,link){
var href = link.href
var nhref = transform_url(href)
console.log('nhref', nhref);
if(nhref != null){
$(link).after($('<a/>').attr('href',nhref).text(' (nbviewer)'))
}
}
 
$.each($('a'),autoviewlink)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.