-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathscripts.js
More file actions
295 lines (276 loc) · 9.63 KB
/
Copy pathscripts.js
File metadata and controls
295 lines (276 loc) · 9.63 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
function prepareHighlight (highlightables, onClass, offClass, slide) {
if (slide === undefined)
slide = $("body .with-highlight").last();
$.each( highlightables, function( index, value ){
var elts = slide.find( "."+value );
elts
.mouseenter(function() {
elts.removeClass(offClass).addClass(onClass);
})
.mouseleave(function() {
elts.removeClass(onClass).addClass(offClass);
})
.addClass(offClass);
})
}
async function prepRep (elts, cls) {
const { default: shexParser } = await import("./dist/shexParser.es.js");
elts.each(function (idx, container) {
container = $(container);
var button = $("<button></button>");
container.append(button);
button.on("click", function (evt) {
var next = button.text() === "json" ? "shexc" : "json";
chooseRep(container, next);
});
container.attr("tabindex", "0").keydown(function (evt) {
if (evt.ctrlKey || evt.shiftKey)
return true;
return toggle(container, evt.keyCode);
})
// Parse and verify that representations emit identical schemas.
if (!container.hasClass("incomplete")) {
try {
let shexjStr = container.find("pre.json").text()
let shexj = JSON.parse(shexjStr)
let shexcStr =
"PREFIX ex: <http://schema.example/#>\n" +
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" +
"PREFIX Test: <http://shex.io/extensions/Test/>\n" +
container.find("pre.shexc").text()
if (shexcStr.match(/approvedBy/))
debugger
let shexc = shexParser.construct("http://schema.example/base").parse(shexcStr)
delete shexc.prefixes
delete shexc.base
if (!deepEquals(shexj, shexc)) {
console.dir([container.get(), shexj, shexc]);
container.addClass("rep-choice-semantics-mismatch");
}
} catch (e) {
console.dir([container.get(), e]);
container.addClass("rep-choice-parse-error");
}
}
// Verify rendered sizes line up.
if (true) {
var widths = {}, heights = {};
["json", "shexc"].forEach(c => {
chooseRep(container, c);
var pre = container.find("pre."+c);
var bbox = pre.get(0).getBoundingClientRect();
// var span = $("<span> \n</span>");
// pre.append(span);
// var w = span.width();
// var h = span.height();
// span.remove();
widths[c] = Math.round(bbox.width);
heights[c] = Math.round(bbox.height);
});
if (widths.json !== widths.shexc ||
heights.json !== heights.shexc) {
console.dir([container.get(0),
widths.json-widths.shexc,
heights.json-heights.shexc]);
button.attr("title", ""+(widths.json-widths.shexc)+
", "+(heights.json-heights.shexc));
container.addClass("rep-choice-size-mismatch");
}
}
chooseRep(container, cls);
});
return
}
function chooseRep (container, cls) {
var button = container.find("> button");
button.text(cls);
container.find("> pre").each(function (idx, child) {
child = $(child);
var shown = child.hasClass(cls);
child.addClass(shown ? "repchosen" : "rephidden");
child.removeClass(shown ? "rephidden" : "repchosen");
});
container.find("> button").text(cls);
}
function unComment (doc, content) {
// perform transformations to make it render and prettier
content = content.replace(/<!--/, '');
content = content.replace(/-->/, '');
return content ;
}
// pulls "..." strings out of content so later regexes can't be confused by
// slashes, angle brackets or keywords that happen to appear inside them.
function stashStrings (content) {
var strings = [];
content = content.replace(/"(?:[^"\\]|\\.)*"/g, function (s) {
strings.push(s);
return "@@STR" + (strings.length - 1) + "@@";
});
return { content: content, restore: function (out) {
return out.replace(/@@STR(\d+)@@/g,
(m, i) => "<span class='string'>"+strings[+i]+"</span>");
} };
}
// Crappy, single-purpose ShExC highlighter.
function highlightShExC (doc, content) {
var stashed = stashStrings(content);
var out = stashed.content
.replace(/# ([^\n]*)$/gm, s => s.replace(/</g, "@@@"))
.replace(/<([^>]*)>/g, "<span class='relativeIRI'><$1></span>")
.replace(/@@@/g, "<")
.replace(/# ([^\n]*)$/gm, s => "<span class='comment'>"+s+"</span>")
.replace(/\/(?:\\.|[^\/\n])+\//g, s => "<span class='pattern'>"+s+"</span>")
.replace(/(\b(?:CLOSED|BNODE|IRI|OR|PREFIX|BASE|LITERAL|a)\b|start=)/g,
"<span class='keyword'>$1</span>")
.replace(/\[(.*?)\]/g, "<span class='valueSet'>[$1]</span>")
return stashed.restore(out);
}
// Crappy, single-purpose Turtle highlighter.
function highlightTurtle (doc, content) {
var stashed = stashStrings(content);
var out = stashed.content
.replace(/# ([^\n]*)$/gm, s => s.replace(/</g, "@@@"))
.replace(/<([^>]*)>/g, "<span class='relativeIRI'><$1></span>")
.replace(/@@@/g, "<")
.replace(/# ([^\n]*)$/gm, s => "<span class='comment'>"+s+"</span>")
.replace(/(^|\s)(a|PREFIX|BASE)(?=\s)/gm, "$1<span class='keyword'>$2</span>")
return stashed.restore(out);
}
// Expands terse "@label@" markers (e.g. "@te1@") into the full pin span
// prose needs, so writing a reference doesn't require hand-typing
// <span class="lineno ...">. Convention (matching this document's labels):
// "S<n>", "te<n>" and "tc<n>" are schema labels; a bare "t<n>" is a data
// (triple) label. Must run before initPinHighlights so the expanded spans
// exist by the time it links same-labelled elements together. Never
// touches .line-labels or code <pre> listings - those are hand-authored.
function expandPinRefs () {
$("p, li, td, th, dd, dt").each(function () {
var el = $(this);
if (el.closest("pre, .line-labels").length) return;
var html = el.html();
if (html.indexOf("@") === -1) return;
el.html(html.replace(/@([A-Za-z]+[0-9]+)@/g, function (m, label) {
var kind = /^t[0-9]/.test(label) ? "data" : "schema";
return "<span class='lineno " + kind + " pin " + label + "'>" + label + "</span>";
}));
});
}
// Wires up hover on each .line-labels gutter so that mousing over a line
// label (e.g. "tc1") highlights that gutter chip, the matching .pin span
// in the adjacent code listing, and any prose mentions of the same label
// - all of them share a class named after the label, and prepareHighlight()
// (above) already knows how to link any set of same-classed elements
// together. Scoped to the enclosing <section> (not just the .example div)
// since prose references live outside the code/data example blocks.
function initPinHighlights () {
$(".line-labels").each(function () {
var gutter = $(this);
var scope = gutter.closest("section");
var labels = gutter.find(".lineno")
.map(function () { return $(this).text().trim(); })
.get()
.filter(function (label) { return label.length > 0; });
if (labels.length)
prepareHighlight(labels, "pin-active", "pin-inactive", scope);
});
}
function toggleGrammar () {
const labels = [
"Display grammar only", "Display semantic actions"
];
var state = labels.indexOf($("#toggleGrammar").text());
var sel = $(".grammarTable tr").not($("tr[style='vertical-align: baseline']"));
if (state === 0) {
state = 1;
sel.hide();
} else {
state = 0;
sel.show();
}
$("#toggleGrammar").text(labels[state]);
return false;
}
async function doStuff () {
await prepRep($(".repchoice"), "json");
$("#toggleGrammar").on("click", toggleGrammar);
$("body").keydown(function (evt) {
if (evt.ctrlKey || !evt.shiftKey)
return true;
return toggle($("body .repchoice"), evt.keyCode);
});
setTimeout(
() => {
document.querySelector('#toc-toggle').click();
},
1000
);
for (elt of [...document.querySelectorAll('.MUST')]) {
elt.innerText = "SHALL";
}
};
function toggle (from, key) {
var toHide, toShow;
switch (key) {
case "J".charCodeAt(0):
toHide = "shexc";
toShow = "json";
break;
case "C".charCodeAt(0):
toHide = "json";
toShow = "shexc";
break;
// case "T".charCodeAt(0):
// toHide = "repchosen";
// toShow = "rephidden";
// break;
default:
return true;
}
from.find("."+toHide).removeClass("repchosen").addClass("rephidden");
from.find("."+toShow).removeClass("rephidden").addClass("repchosen");
from.find("> button").text(toShow);
return false;
}
// from https://stackoverflow.com/questions/1068834/object-comparison-in-javascript#answer-2408334
function deepEquals (l, r) {
for (i in l) {
if(typeof r[i] === 'undefined') {
return false;
}
else if(typeof r[i] === 'object') {
if(!deepEquals(r[i], l[i])) {
return false;
}
}
else if(r[i] != l[i]) {
return false;
}
}
for(i in r) {
if(typeof l[i] === 'undefined') {
return false;
}
else if(typeof l[i] === 'object') {
if(!deepEquals(l[i], r[i])) {
return false;
}
}
else if(l[i] != r[i]) {
return false;
}
}
return true;
}
/*
let a = {foo:'bar', bar: {blub:'bla'}}
let b = {foo:'bar', bar: {blub:'blob'}}
console.log(false, deepEquals(a, b))
console.log(true, deepEquals(a, JSON.parse(JSON.stringify(a))))
console.log(true, deepEquals(b, JSON.parse(JSON.stringify(b))))
let c = {a:[]}
let d = {a:[]}
console.log(true, deepEquals(c, d))
*/