blob: 1fc7a081977276338aea1c45b01d9ae78d564f94 (
plain)
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
|
languagePluginLoader.then(() => {
// pyodide is now ready to use...
pyodide.loadPackage('Pygments').then(() => {
pyodide.runPython('import pygments.lexers, pygments.formatters.html, pygments.styles');
let qvars = getQueryVariables();
var lexerlist = pyodide.runPython('list(pygments.lexers.get_all_lexers())');
var sel = document.getElementById("lang");
for (lex of lexerlist) {
if (lex[1][0] === undefined) {
continue;
}
var opt = document.createElement("option");
opt.text = lex[0];
opt.value = lex[1][0];
sel.add(opt);
if (lex[1].indexOf(qvars['lexer']) >= 0) {
opt.selected = true;
}
}
var stylelist = pyodide.runPython('list(pygments.styles.get_all_styles())');
var sel = document.getElementById("style");
for (sty of stylelist) {
if (sty != "default") {
var opt = document.createElement("option");
opt.text = sty;
opt.value = sty;
sel.add(opt);
}
}
document.getElementById("hlbtn").disabled = false;
document.getElementById("loading").style.display = "none";
if (qvars['code'] !== undefined) {
document.getElementById("code").value = qvars['code'];
highlight();
}
});
});
function getQueryVariables() {
var query = window.location.search.substring(1);
var vars = query.split('&');
var var_obj = {};
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
var_obj[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return var_obj;
}
function new_file() {
pyodide.globals['fname'] = document.getElementById("file").files[0].name;
var alias = pyodide.runPython('pygments.lexers.find_lexer_class_for_filename(fname).aliases[0]');
var sel = document.getElementById("lang");
for (var i = 0; i < sel.length; i++) {
if (sel.options[i].value == alias) {
sel.selectedIndex = i;
reset_err_hl();
break;
}
}
}
function reset_err_hl() {
document.getElementById("aroundlang").style.backgroundColor = null;
}
function highlight() {
var select = document.getElementById("lang");
var alias = select.options.item(select.selectedIndex).value
if (alias == "") {
document.getElementById("aroundlang").style.backgroundColor = "#ffcccc";
return;
}
pyodide.globals['alias'] = alias;
var select = document.getElementById("style");
pyodide.globals['style'] = select.options.item(select.selectedIndex).value;
pyodide.runPython('lexer = pygments.lexers.get_lexer_by_name(alias)');
pyodide.runPython('fmter = pygments.formatters.html.HtmlFormatter(noclasses=True, style=style)');
var file = document.getElementById("file").files[0];
if (file) {
file.arrayBuffer().then(function(buf) {
pyodide.globals['code_mem'] = buf;
pyodide.runPython('code = bytes(code_mem)');
document.getElementById("copy_btn").style.display = "none";
highlight_now();
});
} else {
var code = document.getElementById("code").value;
pyodide.globals['code'] = code;
var link = document.location.origin + document.location.pathname +
"?lexer=" + encodeURIComponent(alias) + "&code=" + encodeURIComponent(code);
document.getElementById("copy_field").value = link;
document.getElementById("copy_btn").style.display = "";
highlight_now();
}
}
function highlight_now() {
var out = document.getElementById("hlcode");
out.innerHTML = pyodide.runPython('pygments.highlight(code, lexer, fmter)');
document.location.hash = "#try";
document.getElementById("hlcodedl").style.display = "block";
}
function copy_link() {
var copy_field = document.getElementById("copy_field");
copy_field.select();
copy_field.setSelectionRange(0, 99999);
document.execCommand("copy");
}
function download_code() {
var filename = "highlighted.html";
var hlcode = document.getElementById("hlcode").innerHTML;
var blob = new Blob([hlcode], {type: 'text/html'});
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, filename);
}
else{
var elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
window.URL.revokeObjectURL(elem.href);
}
}
|