Skip to content

Commit 4f11ab0

Browse files
author
AJ ONeal
committed
change inline templates to strings and regex replace
1 parent fdf34d0 commit 4f11ab0

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

index.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ var cache = {};
4646
*/
4747

4848
var defaultTemplate = join(__dirname, 'public', 'directory.html');
49+
var templates = {
50+
html: {
51+
list: '<ul id="files" class="view-{view}">',
52+
header: '<li class="header">'
53+
+ '<span class="name">Name</span>'
54+
+ '<span class="size">Size</span>'
55+
+ '<span class="date">Modified</span>'
56+
+ '</li>',
57+
item: '<li><a href="{path}" class="{classes}" title="{file.name}">'
58+
+ '<span class="name">{file.name}</span>'
59+
+ '<span class="size">{file.size}</span>'
60+
+ '<span class="date">{file.lastModified}</span>'
61+
+ '</a></li>'
62+
}
63+
}
4964

5065
/*!
5166
* Stylesheet.
@@ -259,13 +274,8 @@ serveIndex.plain = function _plain(req, res, directory, nodes) {
259274
*/
260275

261276
function createHtmlFileList(files, dirname, useIcons, view) {
262-
var html = '<ul id="files" class="view-' + escapeHtml(view) + '">'
263-
+ (view === 'details' ? (
264-
'<li class="header">'
265-
+ '<span class="name">Name</span>'
266-
+ '<span class="size">Size</span>'
267-
+ '<span class="date">Modified</span>'
268-
+ '</li>') : '');
277+
var html = templates.html.list.replace(/{view}/g, view)
278+
+ (view === 'details' ? templates.html.header : '');
269279

270280
html += files.map(function (file) {
271281
var classes = [];
@@ -299,14 +309,12 @@ function createHtmlFileList(files, dirname, useIcons, view) {
299309
? file.size
300310
: '';
301311

302-
return '<li><a href="'
303-
+ escapeHtml(normalizeSlashes(normalize(path.join('/'))))
304-
+ '" class="' + escapeHtml(classes.join(' ')) + '"'
305-
+ ' title="' + escapeHtml(file.name) + '">'
306-
+ '<span class="name">' + escapeHtml(file.name) + '</span>'
307-
+ '<span class="size">' + escapeHtml(size) + '</span>'
308-
+ '<span class="date">' + escapeHtml(date) + '</span>'
309-
+ '</a></li>';
312+
return templates.html.item
313+
.replace(/{path}/g, escapeHtml(normalizeSlashes(normalize(path.join('/')))))
314+
.replace(/{classes}/g, escapeHtml(classes.join(' ')))
315+
.replace(/{file\.name}/g, escapeHtml(file.name))
316+
.replace(/{file\.size}/g, escapeHtml(size))
317+
.replace(/{file\.lastModified/g, escapeHtml(date))
310318
}).join('\n');
311319

312320
html += '</ul>';
@@ -325,10 +333,10 @@ function createHtmlRender(template) {
325333
if (err) return callback(err);
326334

327335
var body = str
328-
.replace(/\{style\}/g, locals.style.concat(iconStyle(locals.fileList, locals.displayIcons)))
329-
.replace(/\{files\}/g, createHtmlFileList(locals.fileList, locals.directory, locals.displayIcons, locals.viewName))
330-
.replace(/\{directory\}/g, escapeHtml(locals.directory))
331-
.replace(/\{linked-path\}/g, htmlPath(locals.directory));
336+
.replace(/{style}/g, locals.style.concat(iconStyle(locals.fileList, locals.displayIcons)))
337+
.replace(/{files}/g, createHtmlFileList(locals.fileList, locals.directory, locals.displayIcons, locals.viewName))
338+
.replace(/{directory}/g, escapeHtml(locals.directory))
339+
.replace(/{linked-path}/g, htmlPath(locals.directory))
332340

333341
callback(null, body);
334342
});

0 commit comments

Comments
 (0)