This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
/
wiki.js
284 lines (255 loc) · 9.25 KB
/
wiki.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
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
/**
* @prettier
*/
const url = require('url');
const request = require('request');
const util = require('./util.js');
module.exports = {
//
// Given a string, escape any quotes within it so it can be
// passed to other functions.
//
escapeQuotes: util.escapeQuotes,
// Check if the given wiki page exists.
// This was "temporarily" disabled 7 years ago!
pageExists(/*path*/) {
// Temporarily disabling this.
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=775590#c4
return true;
},
// Retrieve the content of a document for inclusion,
// optionally filtering for a single section.
//
// Doesn't support the revision parameter offered by DekiScript
//
async page(path, section, revision, show, heading, ignore_cache_control) {
var key_text = path.toLowerCase();
if (section) {
key_text += '?section' + section;
}
var key = 'kuma:include:' + key_text;
// Adjusts the visibility and heading levels of the specified HTML.
//
// The show parameter indicates whether or not the top level
// heading/title should be displayed. The heading parameter
// sets the heading level of the top level of the text to the
// specified value and adjusts all subsequent headings
// accordingly. This adjustment happens regardless of the
// value of show. The heading parameter uses the values 0-5,
// as did DekiScript, 0 represents a page header or H1, 1 -
// H2, 2 - H3 etc
function adjustHeadings(html, section, show, heading) {
if (html && heading) {
// Get header level of page or section level
var level = 1;
if (section) {
level = Number(html.match(/^<h(\d)[^>]*>/i)[1]);
}
var offset = 1 - level + Number(heading);
// Change content header levels.
// There is probably a better way of doing this...
var re;
if (offset > 0) {
for (let i = 6; i >= level; i--) {
re = new RegExp('(</?h)' + i + '([^>]*>)', 'gi');
html = html.replace(re, '$1' + (i + offset) + '$2');
}
} else if (offset < 0) {
for (let i = level; i <= 6; i++) {
re = new RegExp('(</?h)' + i + '([^>]*>)', 'gi');
html = html.replace(re, '$1' + (i + offset) + '$2');
}
}
}
if (show) {
return html;
}
// Rip out the section header
if (html) {
html = html.replace(/^<h\d[^>]*>[^<]*<\/h\d>/gi, '') + '';
}
return html;
}
var regenerate = next => {
var params = ['raw=1', 'macros=1', 'include=1'];
if (section) {
params.push('section=' + encodeURIComponent(section));
}
var opts = {
method: 'GET',
headers: {
'Cache-Control': this.env.cache_control
},
url: util.buildAbsoluteURL(path) + '?' + params.join('&')
};
try {
request(opts, function(err, resp, body) {
var result = '';
if (resp && 200 == resp.statusCode) {
result = body || '';
if (show == undefined) {
show = 0;
}
result = adjustHeadings(result, section, show, heading);
}
next(result);
});
} catch (e) {
next('');
}
};
if (ignore_cache_control) {
return await util.cacheFnIgnoreCacheControl(key, regenerate);
} else {
return await util.cacheFn(key, this.env.cache_control, regenerate);
}
},
// Returns the page object for the specified page.
async getPage(path) {
var key = 'kuma:get_page:' + path.toLowerCase();
return JSON.parse(
await util.cacheFn(key, this.env.cache_control, next => {
var opts = {
method: 'GET',
headers: {
'Cache-Control': this.env.cache_control
},
url: util.buildAbsoluteURL(path) + '$json'
};
try {
request(opts, function(err, resp, body) {
let result;
if (resp && 200 == resp.statusCode) {
result = body;
} else {
result = '{}';
}
next(result);
});
} catch (e) {
next('{}');
}
})
);
},
// Retrieve the full uri of a given wiki page.
uri(path, query) {
const parts = url.parse(this.env.url);
var out = parts.protocol + '//' + parts.host + util.preparePath(path);
if (query) {
out += '?' + query;
}
return out;
},
// Inserts a pages sub tree
// if reverse is non-zero, the sort is backward
// if ordered is true, the output is an <ol> instead of <ul>
//
// Special note: If ordered is true, pages whose locale differ from
// the current page's locale are omitted, to work around misplaced
// localizations showing up in navigation.
async tree(path, depth, self, reverse, ordered) {
// If the path ends with a slash, remove it.
if (path.substr(-1, 1) === '/') {
path = path.slice(0, -1);
}
var pages = await this.page.subpages(path, depth, self);
if (reverse == 0) {
pages.sort(alphanumForward);
} else {
pages.sort(alphanumBackward);
}
return process_array(null, pages, ordered != 0, this.env.locale);
function chunkify(t) {
var tz = [],
x = 0,
y = -1,
n = 0,
i,
j;
while ((i = (j = t.charAt(x++)).charCodeAt(0))) {
var m = i == 46 || (i >= 48 && i <= 57);
if (m !== n) {
tz[++y] = '';
n = m;
}
tz[y] += j;
}
return tz;
}
function alphanumForward(a, b) {
var aa = chunkify(a.title);
var bb = chunkify(b.title);
for (let x = 0; aa[x] && bb[x]; x++) {
if (aa[x] !== bb[x]) {
var c = Number(aa[x]),
d = Number(bb[x]);
if (c == aa[x] && d == bb[x]) {
return c - d;
} else return aa[x] > bb[x] ? 1 : -1;
}
}
return aa.length - bb.length;
}
function alphanumBackward(a, b) {
var bb = chunkify(a.title);
var aa = chunkify(b.title);
for (let x = 0; aa[x] && bb[x]; x++) {
if (aa[x] !== bb[x]) {
var c = Number(aa[x]),
d = Number(bb[x]);
if (c == aa[x] && d == bb[x]) {
return c - d;
} else return aa[x] > bb[x] ? 1 : -1;
}
}
return aa.length - bb.length;
}
function process_array(folderItem, arr, ordered, locale) {
var result = '';
var openTag = '<ul>';
var closeTag = '</ul>';
if (ordered) {
openTag = '<ol>';
closeTag = '</ol>';
}
if (arr.length) {
result += openTag;
// First add an extra item for linking to the folder's page
// (only for ordered lists)
if (folderItem != null && ordered) {
result +=
'<li><a href="' +
folderItem.url +
'">' +
util.htmlEscape(folderItem.title) +
'</a></li>';
}
// Now dive into the child items
arr.forEach(function(item) {
if (!item) {
return;
}
if (ordered && item.locale != locale) {
return;
}
result +=
'<li><a href="' +
item.url +
'">' +
util.htmlEscape(item.title) +
'</a>' +
process_array(
item,
item.subpages || [],
ordered,
locale
) +
'</li>';
});
result += closeTag;
}
return result;
}
}
};