NewsBlur/node/node_modules/express/lib/view.js

174 lines
3.1 KiB
JavaScript
Raw Normal View History

2012-01-02 18:22:06 -08:00
/*!
2016-11-29 18:29:50 -08:00
* express
* Copyright(c) 2009-2013 TJ Holowaychuk
* Copyright(c) 2013 Roman Shtylman
* Copyright(c) 2014-2015 Douglas Christopher Wilson
2012-01-02 18:22:06 -08:00
* MIT Licensed
*/
2016-11-29 18:29:50 -08:00
'use strict';
2012-01-02 18:22:06 -08:00
/**
2016-11-29 18:29:50 -08:00
* Module dependencies.
* @private
2012-01-02 18:22:06 -08:00
*/
2016-11-29 18:29:50 -08:00
var debug = require('debug')('express:view');
var path = require('path');
var fs = require('fs');
var utils = require('./utils');
2012-01-02 18:22:06 -08:00
/**
2016-11-29 18:29:50 -08:00
* Module variables.
* @private
2012-01-02 18:22:06 -08:00
*/
2016-11-29 18:29:50 -08:00
var dirname = path.dirname;
var basename = path.basename;
var extname = path.extname;
var join = path.join;
var resolve = path.resolve;
2012-01-02 18:22:06 -08:00
/**
2016-11-29 18:29:50 -08:00
* Module exports.
* @public
2012-01-02 18:22:06 -08:00
*/
2016-11-29 18:29:50 -08:00
module.exports = View;
2012-01-02 18:22:06 -08:00
/**
2016-11-29 18:29:50 -08:00
* Initialize a new `View` with the given `name`.
2012-01-02 18:22:06 -08:00
*
* Options:
*
2016-11-29 18:29:50 -08:00
* - `defaultEngine` the default template engine name
* - `engines` template engine require() cache
* - `root` root path for view lookup
2012-01-02 18:22:06 -08:00
*
2016-11-29 18:29:50 -08:00
* @param {string} name
* @param {object} options
* @public
2012-01-02 18:22:06 -08:00
*/
2016-11-29 18:29:50 -08:00
function View(name, options) {
var opts = options || {};
2012-01-02 18:22:06 -08:00
2016-11-29 18:29:50 -08:00
this.defaultEngine = opts.defaultEngine;
this.ext = extname(name);
this.name = name;
this.root = opts.root;
2012-01-02 18:22:06 -08:00
2016-11-29 18:29:50 -08:00
if (!this.ext && !this.defaultEngine) {
throw new Error('No default engine was specified and no extension was provided.');
}
2012-01-02 18:22:06 -08:00
2016-11-29 18:29:50 -08:00
var fileName = name;
2012-01-02 18:22:06 -08:00
2016-11-29 18:29:50 -08:00
if (!this.ext) {
// get extension from default engine name
this.ext = this.defaultEngine[0] !== '.'
? '.' + this.defaultEngine
: this.defaultEngine;
2012-01-02 18:22:06 -08:00
2016-11-29 18:29:50 -08:00
fileName += this.ext;
2012-01-02 18:22:06 -08:00
}
2016-11-29 18:29:50 -08:00
if (!opts.engines[this.ext]) {
// load engine
opts.engines[this.ext] = require(this.ext.substr(1)).__express;
2012-01-02 18:22:06 -08:00
}
2016-11-29 18:29:50 -08:00
// store loaded engine
this.engine = opts.engines[this.ext];
// lookup path
this.path = this.lookup(fileName);
}
2012-01-02 18:22:06 -08:00
/**
2016-11-29 18:29:50 -08:00
* Lookup view by the given `name`
2012-01-02 18:22:06 -08:00
*
2016-11-29 18:29:50 -08:00
* @param {string} name
* @private
2012-01-02 18:22:06 -08:00
*/
2016-11-29 18:29:50 -08:00
View.prototype.lookup = function lookup(name) {
var path;
var roots = [].concat(this.root);
2012-01-02 18:22:06 -08:00
2016-11-29 18:29:50 -08:00
debug('lookup "%s"', name);
2012-01-02 18:22:06 -08:00
2016-11-29 18:29:50 -08:00
for (var i = 0; i < roots.length && !path; i++) {
var root = roots[i];
2012-01-02 18:22:06 -08:00
2016-11-29 18:29:50 -08:00
// resolve the path
var loc = resolve(root, name);
var dir = dirname(loc);
var file = basename(loc);
2012-01-02 18:22:06 -08:00
2016-11-29 18:29:50 -08:00
// resolve the file
path = this.resolve(dir, file);
2012-01-02 18:22:06 -08:00
}
2016-11-29 18:29:50 -08:00
return path;
2012-01-02 18:22:06 -08:00
};
/**
2016-11-29 18:29:50 -08:00
* Render with the given options.
2012-01-02 18:22:06 -08:00
*
2016-11-29 18:29:50 -08:00
* @param {object} options
* @param {function} callback
* @private
2012-01-02 18:22:06 -08:00
*/
2016-11-29 18:29:50 -08:00
View.prototype.render = function render(options, callback) {
debug('render "%s"', this.path);
this.engine(this.path, options, callback);
2012-01-02 18:22:06 -08:00
};
2016-11-29 18:29:50 -08:00
/**
* Resolve the file within the given directory.
*
* @param {string} dir
* @param {string} file
* @private
*/
2012-01-02 18:22:06 -08:00
2016-11-29 18:29:50 -08:00
View.prototype.resolve = function resolve(dir, file) {
var ext = this.ext;
2012-01-02 18:22:06 -08:00
2016-11-29 18:29:50 -08:00
// <path>.<ext>
var path = join(dir, file);
var stat = tryStat(path);
2012-01-02 18:22:06 -08:00
2016-11-29 18:29:50 -08:00
if (stat && stat.isFile()) {
return path;
2012-01-02 18:22:06 -08:00
}
2016-11-29 18:29:50 -08:00
// <path>/index.<ext>
path = join(dir, basename(file, ext), 'index' + ext);
stat = tryStat(path);
2012-01-02 18:22:06 -08:00
2016-11-29 18:29:50 -08:00
if (stat && stat.isFile()) {
return path;
2012-01-02 18:22:06 -08:00
}
2016-11-29 18:29:50 -08:00
};
2012-01-02 18:22:06 -08:00
/**
2016-11-29 18:29:50 -08:00
* Return a stat, maybe.
2012-01-02 18:22:06 -08:00
*
2016-11-29 18:29:50 -08:00
* @param {string} path
* @return {fs.Stats}
* @private
2012-01-02 18:22:06 -08:00
*/
2016-11-29 18:29:50 -08:00
function tryStat(path) {
debug('stat "%s"', path);
try {
return fs.statSync(path);
} catch (e) {
return undefined;
}
}