Skip to content

Commit

Permalink
change inline templates to strings and regex replace
Browse files Browse the repository at this point in the history
  • Loading branch information
AJ ONeal committed Aug 14, 2018
1 parent fdf34d0 commit 4f11ab0
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ var cache = {};
*/

var defaultTemplate = join(__dirname, 'public', 'directory.html');
var templates = {
html: {
list: '<ul id="files" class="view-{view}">',
header: '<li class="header">'
+ '<span class="name">Name</span>'
+ '<span class="size">Size</span>'
+ '<span class="date">Modified</span>'
+ '</li>',
item: '<li><a href="{path}" class="{classes}" title="{file.name}">'
+ '<span class="name">{file.name}</span>'
+ '<span class="size">{file.size}</span>'
+ '<span class="date">{file.lastModified}</span>'
+ '</a></li>'
}
}

/*!
* Stylesheet.
Expand Down Expand Up @@ -259,13 +274,8 @@ serveIndex.plain = function _plain(req, res, directory, nodes) {
*/

function createHtmlFileList(files, dirname, useIcons, view) {
var html = '<ul id="files" class="view-' + escapeHtml(view) + '">'
+ (view === 'details' ? (
'<li class="header">'
+ '<span class="name">Name</span>'
+ '<span class="size">Size</span>'
+ '<span class="date">Modified</span>'
+ '</li>') : '');
var html = templates.html.list.replace(/{view}/g, view)
+ (view === 'details' ? templates.html.header : '');

html += files.map(function (file) {
var classes = [];
Expand Down Expand Up @@ -299,14 +309,12 @@ function createHtmlFileList(files, dirname, useIcons, view) {
? file.size
: '';

return '<li><a href="'
+ escapeHtml(normalizeSlashes(normalize(path.join('/'))))
+ '" class="' + escapeHtml(classes.join(' ')) + '"'
+ ' title="' + escapeHtml(file.name) + '">'
+ '<span class="name">' + escapeHtml(file.name) + '</span>'
+ '<span class="size">' + escapeHtml(size) + '</span>'
+ '<span class="date">' + escapeHtml(date) + '</span>'
+ '</a></li>';
return templates.html.item
.replace(/{path}/g, escapeHtml(normalizeSlashes(normalize(path.join('/')))))
.replace(/{classes}/g, escapeHtml(classes.join(' ')))
.replace(/{file\.name}/g, escapeHtml(file.name))
.replace(/{file\.size}/g, escapeHtml(size))
.replace(/{file\.lastModified/g, escapeHtml(date))
}).join('\n');

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

var body = str
.replace(/\{style\}/g, locals.style.concat(iconStyle(locals.fileList, locals.displayIcons)))
.replace(/\{files\}/g, createHtmlFileList(locals.fileList, locals.directory, locals.displayIcons, locals.viewName))
.replace(/\{directory\}/g, escapeHtml(locals.directory))
.replace(/\{linked-path\}/g, htmlPath(locals.directory));
.replace(/{style}/g, locals.style.concat(iconStyle(locals.fileList, locals.displayIcons)))
.replace(/{files}/g, createHtmlFileList(locals.fileList, locals.directory, locals.displayIcons, locals.viewName))
.replace(/{directory}/g, escapeHtml(locals.directory))
.replace(/{linked-path}/g, htmlPath(locals.directory))

callback(null, body);
});
Expand Down

0 comments on commit 4f11ab0

Please sign in to comment.