-
Notifications
You must be signed in to change notification settings - Fork 2
/
custom.js
executable file
·109 lines (80 loc) · 3.13 KB
/
custom.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Hyrule Theme Loader for iPython Notebooks
Until iPython develops an official way of adding themes to iPython Notebooks,
this hack will allow you to load custom CSS and JS in any ipython notebook viewer.
Just include and execute the following in the first cell of your notebook
%%javascript
window.load_remote_theme = true
var theme_js = "https://odhk.github.io/hyrule_theme/custom.js";
window.load_local_theme = function(){
var hostname = document.location.hostname
return ((hostname == "localhost" || hostname == '127.0.0.1') && !load_remote_theme)
}
var url = load_local_theme() ? document.location.origin + "/files/theme/custom.js" : theme_js
$.getScript(url)
The snippet has two options:
* 'local_theme' : when true, will load a local theme if the ipython notebook
is served on localhost. By default, the local theme files should be placed in
a sub-directory alongside the .ipynb file called 'theme'.
* 'theme_url' : the url for the javascript file to load. The javascript file
should contain logic for injecting styles and functionality. Reference the
provided `custom.js` for an example.
*/
// Theme and Asset base URLs, change these to your fork.
var theme_url = 'http://odhk.github.io/hyrule_theme/'
var asset_url = 'https://drostehk.github.io/notebook-assets/'
// Hide the theme Cell
$('.cell:first').hide()
// Load the styles
if (load_local_theme()){
theme_url = document.location.origin + '/theme/'
}
$('<link>')
.appendTo($('head'))
.attr({type : 'text/css', rel : 'stylesheet'})
.attr('href', theme_url + 'custom.css');
// Insert stylised elements
$('<img>')
.prependTo($('h1'))
.attr({src : 'assets/nodes.png', alt : 'break', class : 'lead'});
// Load the assets
if (load_local_theme()){
asset_url = document.location.origin + '/assets/'
}
$('img[src^="assets/"]').each(
function(){
var $this = $(this);
var img = $this.attr('src').split('/')[1];
$this.attr('src', asset_url + img)
}
)
// Create events for jQuery show and hide methods
$.each(['show', 'hide'], function (i, ev) {
var el = $.fn[ev];
$.fn[ev] = function () {
this.trigger(ev);
return el.apply(this, arguments);
};
});
// Render Resource Blocks - see for example http://prologue.datascience.hk
$('.rendered_html').on('show', function() {
var resource_img = $('[alt="resource"]').map(function(i,e){return $(e).attr('src')})
var resource_text = $('[alt="resource"]').siblings('a').map(function(i,e){return $(e).text()})
var resource_links = $('[alt="resource"]').siblings('a').map(function(i,e){return $(e).attr('href')})
$('[alt="resource"]').each(function(i,e){
$p = $(e).parent('p');
$p.empty()
.addClass('resource-container')
.append('<a>')
.find('a')
.attr('href', resource_links[i])
.append('<div>')
.find('div')
.css('background-image','url(' + resource_img[i]+')')
.parent()
.append('<p>')
.find('p')
.text(resource_text[i])
})
})
$('.rendered_html').trigger('show')