-
Notifications
You must be signed in to change notification settings - Fork 17
/
coqdocjs.js
189 lines (170 loc) · 5.85 KB
/
coqdocjs.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
181
182
183
184
185
186
187
188
189
var coqdocjs = coqdocjs || {};
(function(){
function replace(s){
var m;
if (m = s.match(/^(.+)'/)) {
return replace(m[1])+"'";
} else if (m = s.match(/^([A-Za-z]+)_?(\d+)$/)) {
return replace(m[1])+m[2].replace(/\d/g, function(d){
if (coqdocjs.subscr.hasOwnProperty(d)) {
return coqdocjs.subscr[d];
} else {
return d;
}
});
} else if (coqdocjs.repl.hasOwnProperty(s)){
return coqdocjs.repl[s]
} else {
return s;
}
}
function toArray(nl){
return Array.prototype.slice.call(nl);
}
function replInTextNodes() {
coqdocjs.replInText.forEach(function(toReplace){
toArray(document.getElementsByClassName("code")).concat(toArray(document.getElementsByClassName("inlinecode"))).forEach(function(elem){
toArray(elem.childNodes).forEach(function(node){
if (node.nodeType != Node.TEXT_NODE) return;
var fragments = node.textContent.split(toReplace);
node.textContent = fragments[fragments.length-1];
for (var k = 0; k < fragments.length - 1; ++k) {
node.parentNode.insertBefore(document.createTextNode(fragments[k]),node);
var replacement = document.createElement("span");
replacement.appendChild(document.createTextNode(toReplace));
replacement.setAttribute("class", "id");
replacement.setAttribute("type", "keyword");
node.parentNode.insertBefore(replacement, node);
}
});
});
});
}
function replNodes() {
toArray(document.getElementsByClassName("id")).forEach(function(node){
if (["var", "variable", "keyword", "notation", "definition", "inductive"].indexOf(node.getAttribute("type"))>=0){
var text = node.textContent;
var replText = replace(text);
if(text != replText) {
node.setAttribute("repl", replText);
node.setAttribute("title", text);
var hidden = document.createElement("span");
hidden.setAttribute("class", "hidden");
while (node.firstChild) {
hidden.appendChild(node.firstChild);
}
node.appendChild(hidden);
}
}
});
}
function isVernacStart(l, t){
t = t.trim();
for(var s of l){
if (t == s || t.startsWith(s+" ") || t.startsWith(s+".")){
return true;
}
}
return false;
}
function isProofStart(n){
return isVernacStart(["Proof"], n.textContent) && !isVernacStart(["Default", "Suggest"], n.previousSibling.previousSibling.textContent) ||
(isVernacStart(["Next"], n.textContent) && isVernacStart(["Obligation"], n.nextSibling.nextSibling.textContent));
}
function isProofEnd(s){
return isVernacStart(["Qed", "Admitted", "Defined", "Abort"], s);
}
function proofStatus(){
var proofs = toArray(document.getElementsByClassName("proof"));
if(proofs.length) {
for(var proof of proofs) {
if (proof.getAttribute("show") === "false") {
return "some-hidden";
}
}
return "all-shown";
}
else {
return "no-proofs";
}
}
function updateView(){
document.getElementById("toggle-proofs").setAttribute("proof-status", proofStatus());
}
function foldProofs() {
var hasCommands = true;
var nodes = document.getElementsByClassName("command");
if(nodes.length == 0) {
hasCommands = false;
console.log("no command tags found")
nodes = document.getElementsByClassName("id");
}
toArray(nodes).forEach(function(node){
if(isProofStart(node)) {
var proof = document.createElement("span");
proof.setAttribute("class", "proof");
node.parentNode.insertBefore(proof, node);
if(proof.previousSibling.nodeType === Node.TEXT_NODE)
proof.appendChild(proof.previousSibling);
while(node && !isProofEnd(node.textContent)) {
proof.appendChild(node);
node = proof.nextSibling;
}
if (proof.nextSibling) proof.appendChild(proof.nextSibling); // the Qed
if (!hasCommands && proof.nextSibling) proof.appendChild(proof.nextSibling); // the dot after the Qed
proof.addEventListener("click", function(proof){return function(e){
if (e.target.parentNode.tagName.toLowerCase() === "a")
return;
proof.setAttribute("show", proof.getAttribute("show") === "true" ? "false" : "true");
proof.setAttribute("animate", "");
updateView();
};}(proof));
proof.setAttribute("show", "false");
}
});
}
function toggleProofs(){
var someProofsHidden = proofStatus() === "some-hidden";
toArray(document.getElementsByClassName("proof")).forEach(function(proof){
proof.setAttribute("show", someProofsHidden);
proof.setAttribute("animate", "");
});
updateView();
}
function repairDom(){
// pull whitespace out of command
toArray(document.getElementsByClassName("command")).forEach(function(node){
while(node.firstChild && node.firstChild.textContent.trim() == ""){
console.log("try move");
node.parentNode.insertBefore(node.firstChild, node);
}
});
toArray(document.getElementsByClassName("id")).forEach(function(node){
node.setAttribute("type", node.getAttribute("title"));
});
toArray(document.getElementsByClassName("idref")).forEach(function(ref){
toArray(ref.childNodes).forEach(function(child){
if (["var", "variable"].indexOf(child.getAttribute("type")) > -1)
ref.removeAttribute("href");
});
});
}
function fixTitle(){
var url = "/" + window.location.pathname;
var basename = url.substring(url.lastIndexOf('/')+1, url.lastIndexOf('.'));
if (basename === "toc") {document.title = "Table of Contents";}
else if (basename === "indexpage") {document.title = "Index";}
else {document.title = basename;}
}
function postprocess(){
repairDom();
replInTextNodes()
replNodes();
foldProofs();
document.getElementById("toggle-proofs").addEventListener("click", toggleProofs);
updateView();
}
fixTitle();
document.addEventListener('DOMContentLoaded', postprocess);
coqdocjs.toggleProofs = toggleProofs;
})();