Skip to content

Commit

Permalink
directory: add template option
Browse files Browse the repository at this point in the history
closes #982
closes #990
  • Loading branch information
gottaloveit authored and dougwilson committed Jan 3, 2014
1 parent 3866d50 commit 99b9feb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/middleware/directory.js
Expand Up @@ -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.
*/
Expand All @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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);
});
});
};
Expand All @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions test/directory.js
Expand Up @@ -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('<a href="/g%23%20%253%20o%20%252525%20%2537%20dir"');
res.body.should.include('<a href="/users"');
res.body.should.include('<a href="/file%20%231.txt"');
res.body.should.include('<a href="/todo.txt"');
res.body.should.include('This is the test template');
done();
});
});
});

describe('when set with trailing slash', function () {
var app = connect(connect.directory('test/fixtures/'));

Expand Down
14 changes: 14 additions & 0 deletions test/shared/template.html
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>listing directory {directory}</title>
<style>{style}</style>
</head>
<body class="directory">
<h1>This is the test template</h1>
<div id="wrapper">
<h1>{linked-path}</h1>
{files}
</div>
</body>
</html>

1 comment on commit 99b9feb

@jonathanong
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nice!

Please sign in to comment.