-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdoc-uri.js
71 lines (62 loc) · 2.32 KB
/
doc-uri.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
//noinspection JSLint
var g = typeof global === 'undefined' ? window : global;
//noinspection JSLint,JSHint,ThisExpressionReferencesGlobalObjectJS
(function (papyrus) {
require('./doc');
var spcf = papyrus.spcf;
var id4 = papyrus.id4;
var RE_URI = new RegExp(
"(?:(https?|ftps?|wss?):)" + // scheme
"(?://" +
"(?:([^/?#\\s]*)@)?" + // credentials
"((?:[^/?#:@\\s]+\\.)*[^/?#:@\\s]+)" + // domain
"(?::([0-9]+))?" + // port
")" +
"(/[^?#'\"\\s]*)?" + // path
"(?:\\?([^'\"#\\s]*))?" + // query
"(?:#(\\S*))?", // fragment
"gi"
);
papyrus.RE_URI = RE_URI;
papyrus.addFilter('.pt', parseURIs);
function parseURIs(spec, text) {
var doc = this;
if (doc.promo) { return; } //skip uri parsing
var pid = spcf.get(spec, ';');
var ids = doc.get(pid + '.id');
var new_traps = [];
var m;
var starts = {};
RE_URI.lastIndex = 0;
// find URIs in the string
while (m = RE_URI.exec(text)) {
// locate formatting change points
var from = m.index ? id4.at(ids, m.index - 1) : pid.replace(';', ':');
var till = id4.at(ids, m.index + m[0].length - 1);
new_traps.push(from, till);
// remember the URI
var start = id4.at(ids, m.index);
if (doc.get(start + '.go') != m[0]) {
doc.set(start + '.go', m[0]);
}
starts[start] = true;
}
var old_traps = doc.get(pid + '.um'); // the previous state
new_traps = new_traps.join('');
if (old_traps === new_traps) {
return; // no changes => don't touch
}
// update traps (formatting change points)
doc.walkTraps(old_traps, '#ur', spcf.rm);
doc.walkTraps(new_traps, '#ur', spcf.add);
// forget URIs that disappeared
var clr = spcf.split(old_traps), c;
while (c = clr.pop()) {
if (!starts[c]) {
doc.set(c + '.go', '');
}
}
doc.set(pid + '.um', new_traps); // record the state
doc.reset(pid + '.pw'); // redraw the paragraph
}
}(g['papyrus'] = g['papyrus'] || {}));