-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewsource.html
More file actions
65 lines (51 loc) · 1.67 KB
/
viewsource.html
File metadata and controls
65 lines (51 loc) · 1.67 KB
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
<script>
onload=()=>{
let url=new URL(location.href); let file=url.searchParams.get("file"); if(!file)return;
fetch(file).then(r=>r.text()).then(init).catch(e=>console.error(e));
function element(tag,c="",p,o){let e=(p||document.body).appendChild(document.createElement(tag));if(c)e.className=c;if(o)Object.assign(e,o);return e}
function init(raw){
element("style", "", document.head, { textContent: `
*{box-sizing:border-box;margin:0;padding:0}
pre{padding:5;margin:10px;font-size:4px}
.pairs{color:purple;}
.keywords{color:teal;}
button{margin:10px;padding:5px;cursor:pointer}
@page{margin:0}
`});
function pretty(text){
let c={},m=/[a-z]+\b|[(){}[\]]|</g;
for(let [v,k] of color(text))c[v]=`<x class=${k}>${v}</x>`;
return text.replace(m,x=>x=="<"?"<":c[x]||x);
}
function* color(text){
for(let v of "(){}[]")yield [v,"pairs"];
for(let w of new Set(text.match(/[a-z]+\b/g)||[])){
try{eval("let "+w)}catch{yield [w,"keywords"]}
}
}
function render(){
let filename = file.split('/').pop();
element("button", "", document.body, {
textContent: "Main Page",
onclick: () => { location.href = "https://github.com/bacionejs/battito"; }
});
element("button", "", document.body, {
textContent: "Download",
onclick: () => {
let a = document.createElement("a");
a.href = URL.createObjectURL(new Blob([raw], {type: "text/html"}));
a.download = filename;
a.click();
}
});
let lines=pretty(raw).split('\n');
if(lines.length && lines[lines.length-1]==='')lines.pop();
element("pre", "", document.body, {
innerHTML: lines.map((l,i)=>{
return `<span style="color:gray">${String(i+1).padStart(3,'0')}: </span>${l}`;
}).join('\n')
});
}
render();
}
}</script>