From 99b9feb449578c28064bd630b429e89d5aa4e47a Mon Sep 17 00:00:00 2001 From: Joe Passavanti Date: Thu, 2 Jan 2014 10:45:31 -0800 Subject: [PATCH] directory: add template option closes #982 closes #990 --- lib/middleware/directory.js | 21 +++++++++++++++++---- test/directory.js | 19 +++++++++++++++++++ test/shared/template.html | 14 ++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 test/shared/template.html diff --git a/lib/middleware/directory.js b/lib/middleware/directory.js index ac8ea7a5c..5ccff4374 100644 --- a/lib/middleware/directory.js +++ b/lib/middleware/directory.js @@ -30,6 +30,12 @@ var Negotiator = require('negotiator'); var cache = {}; +/*! + * Default template. + */ + +var defaultTemplate = join(__dirname, '..', 'public', 'directory.html'); + /** * Media types and the map for content negotiation. */ @@ -56,6 +62,12 @@ var mediaType = { * - `hidden` display hidden (dot) files. Defaults to false. * - `icons` display icons. Defaults to false. * - `filter` Apply this filter function to files. Defaults to false. + * - `template` Optional path to html template. Defaults to a built-in template. + * The following tokens are replaced: + * - `{directory}` with the name of the directory. + * - `{files}` with the HTML of an unordered list of file links. + * - `{linked-path}` with the HTML of a link to the directory. + * - `{style}` with the built-in CSS and embedded images. * * @param {String} root * @param {Object} options @@ -72,7 +84,8 @@ exports = module.exports = function directory(root, options){ , icons = options.icons , view = options.view || 'tiles' , filter = options.filter - , root = normalize(root + sep); + , root = normalize(root + sep) + , template = options.template || defaultTemplate; return function directory(req, res, next) { if ('GET' != req.method && 'HEAD' != req.method) return next(); @@ -110,7 +123,7 @@ exports = module.exports = function directory(root, options){ // not acceptable if (!type) return next(utils.error(406)); - exports[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view); + exports[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template); }); }); }; @@ -120,8 +133,8 @@ exports = module.exports = function directory(root, options){ * Respond with text/html. */ -exports.html = function(req, res, files, next, dir, showUp, icons, path, view){ - fs.readFile(__dirname + '/../public/directory.html', 'utf8', function(err, str){ +exports.html = function(req, res, files, next, dir, showUp, icons, path, view, template){ + fs.readFile(template, 'utf8', function(err, str){ if (err) return next(err); fs.readFile(__dirname + '/../public/style.css', 'utf8', function(err, style){ if (err) return next(err); diff --git a/test/directory.js b/test/directory.js index a121f35b1..b6864527d 100644 --- a/test/directory.js +++ b/test/directory.js @@ -118,6 +118,25 @@ describe('directory()', function(){ }); }); + describe('when setting a custom template', function () { + var app = connect(connect.directory('test/fixtures', {'template': __dirname + '/shared/template.html'})); + + it('should respond with file list and testing template sentence', function (done) { + app.request() + .get('/') + .set('Accept', 'text/html') + .end(function(res){ + res.should.be.html; + res.body.should.include(' + + + listing directory {directory} + + + +

This is the test template

+
+

{linked-path}

+ {files} +
+ +