-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedx-llab-hack.js
180 lines (160 loc) · 5.85 KB
/
edx-llab-hack.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
This page is a bunch of extracted changes needed to make edX pages
work like llab.
*/
var llab = {};
llab.paths = {};
llab.paths.css_files = {};
// Extracted from loader.js
// ADDITIONAL LIBRARIES
function getTag(name, src, type, onload) {
var tag = document.createElement(name);
if (src.substring(0, 2) === "//") {
// external server, carry on..
} else if (src.substring(0,1) === "/") {
// root on this server
src = window.location.href.replace(window.location.pathname, src);
} else {
// relative link
src = llab.pathToLlab + src;
}
var link = name === 'link' ? 'href' : 'src';
tag[link] = src;
tag.type = type;
tag.onload = onload;
tag.async = true;
return tag;
}
// Syntax Highlighting support
// Note this are different paths than llab
llab.paths.syntax_highlights = "//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js";
llab.paths.css_files.syntax_highlights = "//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow-night-blue.min.css";
// Math / LaTeX rendering
llab.paths.math_katex_js = "//cdnjs.cloudflare.com/ajax/libs/KaTeX/0.8.3/katex.min.js";
llab.paths.css_files.math_katex_css = "//cdnjs.cloudflare.com/ajax/libs/KaTeX/0.8.3/katex.min.css";
//
// Extracted from llab/library
llab.snapRunURLBase = "https://snap.berkeley.edu/snapsource/snap.html#open:";
llab.getSnapRunURL = function(targeturl) {
if (!targeturl) { return ''; }
var finalurl = llab.snapRunURLBase,
edc_gh = 'https://bjc.edc.org/bjc-r/',
url_parts = targeturl.split('/'),
xmlPath = url_parts[url_parts.length - 1].replace(/_/g, '/');
return finalurl + edc_gh + xmlPath;
};
// End library
// Extracted from llab/curriculum.js
/** A prelimary API for defining loading additional content based on triggers.
* @{param} array TRIGGERS is an array of {trigger, callback} pairs.
* a `trigger` is currently a CSS selector that gets passed to $ to see if any
* of those elements are on the current page. If the elements are found then a
* `callback` is called with no arguments.
* TODO: Cleanup and test this code.
* TODO: Explore ideas for better trigger options?
*/
llab.additionalSetup = function(triggers) {
var items;
triggers.forEach(function (obj) {
if (obj.trigger && obj.function) {
items = $(obj.trigger);
if (items.length) {
obj.function.call(null);
}
}
});
}
/** Import the required JS and CSS for Code highlighting.
* TODO: Abstract this away into its own function
*/
llab.codeHighlightSetup = function () {
var cssFile, jsFile, css, js;
cssFile = llab.paths.css_files.syntax_highlights;
jsFile = llab.paths.syntax_highlights;
css = getTag('link', cssFile, 'text/css');
css.rel = "stylesheet";
js = getTag('script', jsFile, 'text/javascript');
// onload function
$(js).attr({'onload': 'llab.highlightSyntax()'});
// Using $ to append to head causes onload not to be fired...
document.head.appendChild(css);
document.head.appendChild(js);
}
// Call The Functions to HighlightJS to render
llab.highlightSyntax = function() {
// TODO: PUT THESE CLASSES SOMEWHERE
$('pre code').each(function(i, block) {
// Trim the extra whitespace in HTML files.
block.innerHTML = block.innerHTML.trim();
if (typeof hljs !== 'undefined') {
hljs.highlightBlock(block);
}
});
}
/** Import the required JS and CSS for LaTeX Code.
* TODO: Abstract this away into its own function
*/
llab.mathDisplaySetup = function () {
var cssFile, jsFile, css, js;
cssFile = llab.paths.css_files.math_katex_css;
jsFile = llab.paths.math_katex_js;
css = getTag('link', cssFile, 'text/css');
css.rel = "stylesheet";
js = getTag('script', jsFile, 'text/javascript');
// onload function
$(js).attr({'onload': 'llab.displayMathDivs()'});
// Using $ to append to head causes onload not to be fired...
document.head.appendChild(css);
document.head.appendChild(js);
}
// Call the KaTeX APIS to render the LaTeX code.
llab.displayMathDivs = function () {
// TODO: Investigate caching of the selectors?
// TODO: PUT THESE CLASSES SOMEWHERE
$('.katex, .katex-inline').each(function (idx, elm) {
katex.render(elm.textContent, elm, {throwOnError: false}); //Changed .innerHTML to .textContent on 1/29/16 to get > and < to work.
});
// TODO: PUT THESE CLASSES SOMEWHERE
$('.katex-block').each(function (idx, elm) {
katex.render(elm.textContent, elm, { //Changed .innerHTML to .textContent on 1/29/16 to get > and < to work.
displayMode: true, throwOnError: false
});
});
}
// End curriculum.js
// Custom stuff:
$(document).ready(function() {
if (typeof Giffer !== 'undefined') {
Gifffer();
} else {
var gif = $('<script>').attr(
'src',
'https://cdnjs.cloudflare.com/ajax/libs/gifffer/1.5.0/gifffer.min.js'
).on('load', 'function() {Giffer(); }');
$('head').append(gif);
}
$('head').append($('<script>').attr(
{src: 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'}
));
llab.additionalSetup([
{
trigger: 'a.run',
function: function () {
$("a.run").each(function() {
$(this).attr({
"target": "_blank",
'href': llab.getSnapRunURL($(this).attr('href'))
});
});
}
},
{ // TODO: PUT THESE CLASSES SOMEWHERE
trigger: 'pre code',
function: llab.codeHighlightSetup()
},
{ // TODO: PUT THESE CLASSES SOMEWHERE
trigger: '.katex, .katex-inline, .katex-block',
function: llab.mathDisplaySetup()
}
]);
});