-
Notifications
You must be signed in to change notification settings - Fork 23
/
renderer.js
364 lines (296 loc) · 9.31 KB
/
renderer.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
var extend = require('util-extend'),
frontMatter = require('front-matter'),
fs = require('fs'),
hljs = require('highlight.js'),
highlightActionScript = require('highlight-bigfix-actionscript'),
nunjucks = require('nunjucks'),
marked = require('marked'),
path = require('path');
hljs.registerLanguage('bigfix-relevance', require('hljs-bigfix-relevance'));
/**
* Highlight relevance code.
*/
function highlightRelevance(relevance) {
return hljs.highlight('bigfix-relevance', relevance).value;
}
/**
* Parse the contents of a {% qna %} block.
*/
function parseQNA(text) {
var qna = { question: '', answers: [], errors: [] };
text.trim().split('\n').forEach(function(line) {
if (line.indexOf('Q:') === 0) {
qna.question = highlightRelevance(line.substr(2).trim());
} else if (line.indexOf('A:') === 0) {
qna.answers.push(line.substr(2).trim());
} else if (line.indexOf('E:') === 0) {
qna.errors.push(line.substr(2).trim());
}else if (line.indexOf('T:') === 0) {
qna.type = line.substr(2).trim();
}
});
return qna;
}
/**
* Escape any special markdown characters in the example.
*/
function escapeMarkdown(html) {
return html.replace(/\*/g, '*').replace(/_/g, '_');
}
/**
* Wrap the text in a <pre><code> block. The text is assumed to already be HTML
* escaped.
*/
function wrapCodeBlock(text) {
return '<pre><code>' + text + '</code></pre>';
}
/**
* Create markdown renderer.
*/
function createMarkdownRenderer() {
var renderer = new marked.Renderer();
// Override the 'code' function so that we can colorize relevance.
renderer.code = function(code, language) {
if (language === 'relevance') {
return wrapCodeBlock(highlightRelevance(code));
}
if (language === 'actionscript') {
return wrapCodeBlock(highlightActionScript(code));
}
hljsLangs = ['sql', 'xml', 'python', 'perl', 'js', 'javascript', 'sql'];
if (hljsLangs.indexOf(language) >= 0) {
return wrapCodeBlock(hljs.highlight(language, code).value);
}
return marked.Renderer.prototype.code.apply(this, arguments);
};
renderer.link = function(href, title, text) {
// ignore broken links such as http://<server>:52311/api
if (href.indexOf('<') >= 0) {
return text;
}
return marked.Renderer.prototype.link.apply(this, arguments);
};
var options = {
renderer: renderer
};
return function(text) {
return marked(text, options);
};
}
/**
* Create a simple nunjucks extension.
*/
function createExtension(tag, renderFunction) {
var extension = {};
extension.tags = [tag];
extension.parse = function(parser) {
var tok = parser.nextToken();
var args = parser.parseSignature(null, true);
parser.advanceAfterBlockEnd(tok.value);
var nodeList = parser.parseUntilBlocks('end' + tag);
parser.advanceAfterBlockEnd();
var templateData = nodeList.children[0].children[0];
templateData.value = renderFunction(templateData.value, args);
return nodeList;
};
return extension;
}
/**
* Create the {% qna %} extension.
*/
function createQNAExtension(templateEnv) {
return createExtension('qna', function(text) {
return escapeMarkdown(templateEnv.render('qna.html', parseQNA(text)));
});
}
/**
* Create the {% note %} extension.
*/
function createNoteExtension(templateEnv) {
return createExtension('note', function(text) {
return escapeMarkdown(templateEnv.render('note.html', {
content: marked(text)
}));
});
}
/**
* Create the {% section %} extension.
*/
function createSectionExtension(templateEnv) {
return createExtension('section', function(text) {
return escapeMarkdown(templateEnv.render('section.html', {
content: marked(text)
}));
});
}
/**
* Create the {% evaluator %} extension.
*/
function createEvaluatorExtension(templateEnv) {
return createExtension('evaluator', function(text) {
return escapeMarkdown(templateEnv.render('evaluator.html', {
content: text
}));
});
}
/**
* Create the {% collapse %} extension.
*/
function createCollapseExtension(templateEnv) {
return createExtension('collapse', function(text, args) {
var title = "";
if (args.children && args.children.length) {
title = args.children[0].value;
}
return escapeMarkdown(templateEnv.render('collapse.html', {
content: text,
title: title
}));
});
}
/**
* Create the {% collapse %} extension.
*/
function createRestApiExtension(templateEnv) {
return createExtension('restapi', function(text, args) {
var url = "";
if (args.children && (args.children.length == 3)) {
url = args.children[0].value;
method = args.children[1].value;
title = args.children[2].value;
} else {
throw new Error("Invalid number of arguments for restapi template. Excepting 3 arguments.")
}
return escapeMarkdown(templateEnv.render('restapi.html', {
content: marked(text),
url: url,
method: method,
title: title
}));
});
}
/**
* The linkType filter: {{ 'foo' | linkType }}
*/
function linkType(typeName) {
var href ='/relevance/reference/' + typeName.replace(/ /g, '-') + '.html';
var link = '<a href="' + href + '" target="_blank">' + typeName + '</a>';
return new nunjucks.runtime.SafeString(link);
}
/**
* Create the nunjucks environment and add our extensions.
*/
function createTemplateEnv(templatesDir) {
var loader = new nunjucks.FileSystemLoader(templatesDir, { watch: false });
var templateEnv = new nunjucks.Environment(loader, { autoescape: true });
templateEnv.addExtension('QNAExtension', createQNAExtension(templateEnv));
templateEnv.addExtension('NoteExtension', createNoteExtension(templateEnv));
templateEnv.addExtension('SectionExtension', createSectionExtension(templateEnv));
templateEnv.addExtension('EvaluatorExtension', createEvaluatorExtension(templateEnv));
templateEnv.addExtension('CollapseExtension', createCollapseExtension(templateEnv));
templateEnv.addExtension('RESTAPIExtension', createRestApiExtension(templateEnv));
templateEnv.addFilter('linkType', linkType);
return templateEnv;
}
/**
* Read the front-matter for a file, and combine it with any defaults.
*/
function getFileData(sourcePath, defaults) {
var parsed = frontMatter(fs.readFileSync(sourcePath).toString());
var data = extend(extend({}, defaults), parsed.attributes);
data.content = parsed.body;
return data;
}
/**
* Return whether the document 'location' matches the nav 'href'.
*/
function locationMatches(location, href) {
if (location === href) {
return true;
}
return (location.indexOf(href) === 0) && (location[href.length - 1] === '/');
}
/**
* Render the side navigation for 'location'.
*/
function renderSideNav(nav, location, templateEnv) {
var items = [];
nav.forEach(function(child) {
var item = {
name: child.name,
href: child.href
};
if (locationMatches(location, child.href)) {
if (child.children) {
item.children = renderSideNav(child.children, location, templateEnv);
}
if (location === child.href) {
item.selected = 'selected';
}
}
items.push(item);
});
return templateEnv.render('nav.html', { items: items });
}
/**
* Normalize the file location.
*/
function normalizeLocation(location) {
// Add leading slash
if (location[0] !== '/') {
location = '/' + location;
}
// Strip trailing 'index.html'
var match = location.match(/(.*\/)index.html$/);
if (match) {
location = match[1];
}
return location;
}
module.exports = function(templatesDir) {
var renderMarkdown = createMarkdownRenderer();
var templateEnv = createTemplateEnv(templatesDir);
var formats = {};
formats['.md'] = function(location, sourcePath, defaults) {
var fileData = getFileData(sourcePath, defaults);
fileData.content = templateEnv.renderString(fileData.content, fileData);
fileData.content = renderMarkdown(fileData.content);
if (fileData.nav) {
fileData.sideNav =
renderSideNav(fileData.nav, normalizeLocation(location), templateEnv);
}
return templateEnv.render(fileData.layout + '.html', fileData);
};
formats['.html'] = function(location, sourcePath, defaults) {
var fileData = getFileData(sourcePath, defaults);
fileData.content = templateEnv.renderString(fileData.content, fileData);
if (fileData.nav) {
fileData.sideNav =
renderSideNav(fileData.nav, normalizeLocation(location), templateEnv);
}
return templateEnv.render(fileData.layout + '.html', fileData);
};
var renderer = {};
renderer.renderSideNav = function(location, fileData) {
return renderSideNav(fileData.nav, normalizeLocation(location),
templateEnv);
};
renderer.renderMarkdown = function(text) {
return renderMarkdown(templateEnv.renderString(text));
};
renderer.renderTemplate = function(template, data) {
return templateEnv.render(template + '.html', data);
};
renderer.renderFile = function(location, sourcePath, defaults) {
var extension = path.extname(sourcePath);
var renderer = formats[extension];
if (!renderer) {
throw new Error('Cannot render file of type ' + extension);
}
return renderer(location, sourcePath, defaults);
};
renderer.canRenderFile = function(sourcePath) {
return formats[path.extname(sourcePath)] !== undefined;
};
return renderer;
};