moved View and Partial into their own files
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 8dc341d · Browse files · 3 files·+173 added−132 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+173 added−132 removed
- other:+173 added−132 removed
Provenance
- Commit
065c37c- Tree
fbf6297- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
8dc341d
3 files changed+173 added−132 removed
added:lib/express/view/partial.js+61 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
/*! * Express - view - Partial * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca> * MIT Licensed */
/** * Module dependencies. */
var View = require('./view') , basename = require('path').basename;
/** * Initialize a new `Partial` for the given `view` and `options`. * * @param {String} view * @param {Object} options * @api private */
var Partial = exports = module.exports = function Partial(view, options) { options = options || {}; View.call(this, view, options); this.path = this.dirname + '/_' + basename(this.path);};
/** * Inherit from `View.prototype`. */
Partial.prototype.__proto__ = View.prototype;
/** * Resolve partial object name from the view path. * * Examples: * * "user.ejs" becomes "user" * "forum thread.ejs" becomes "forumThread" * "forum/thread/post.ejs" becomes "post" * "blog-post.ejs" becomes "blogPost" * * @return {String} * @api private */
exports.resolveObjectName = function(view){ // TODO: cache return view .split('/') .slice(-1)[0] .split('.')[0] .replace(/[^a-zA-Z0-9 ]+/g, ' ') .split(/ +/).map(function(word, i){ return i ? word[0].toUpperCase() + word.substr(1) : word; }).join('');};No newline at end of fileadded:lib/express/view/view.js+106 added
1+2+/*!3+ * Express - View4+ * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5+ * MIT Licensed6+ */7+8+/**9+ * Module dependencies.10+ */11+12+var extname = require('path').extname13+ , dirname = require('path').dirname14+ , basename = require('path').basename15+ , fs = require('fs');16+17+/**18+ * Memory cache.19+ */20+21+var cache = {};22+23+/**24+ * Initialize a new `View` with the given `view` path and `options`.25+ *26+ * @param {String} view27+ * @param {Object} options28+ * @api private29+ */30+31+var View = exports = module.exports = function View(view, options) {32+ options = options || {};33+ // TODO: memoize basename etc34+ this.view = view;35+ this.root = options.root;36+ this.relative = false !== options.relative;37+ this.defaultEngine = options.defaultEngine;38+ this.parent = options.parentView;39+ this.basename = basename(view);40+ this.engine = this.resolveEngine();41+ this.extension = '.' + this.engine;42+ this.path = this.resolvePath();43+ this.dirname = dirname(this.path);44+};45+46+/**47+ * Resolve view engine.48+ *49+ * @return {String}50+ * @api private51+ */52+53+View.prototype.resolveEngine = function(){54+ // Explicit55+ if (~this.basename.indexOf('.')) return extname(this.basename).substr(1);56+ // Inherit from parent57+ if (this.parent) return this.parent.engine;58+ // Default59+ return this.defaultEngine;60+};61+62+/**63+ * Resolve view path.64+ *65+ * @return {String}66+ * @api private67+ */68+69+View.prototype.resolvePath = function(){70+ var path = this.view;71+ // Implicit engine72+ if (!~this.basename.indexOf('.')) path += this.extension;73+ // Absolute74+ if ('/' == path[0]) return path;75+ // Relative to parent76+ if (this.relative && this.parent) return this.parent.dirname + '/' + path;77+ // Relative to root78+ return this.root79+ ? this.root + '/' + path80+ : path;81+};82+83+/**84+ * Get view contents. This is a one-time hit, so we85+ * can afford to be sync.86+ *87+ * @return {String}88+ * @api public89+ */90+91+View.prototype.__defineGetter__('contents', function(){92+ return fs.readFileSync(this.path, 'utf8');93+});94+95+/**96+ * Get template engine api, cache exports to reduce97+ * require() calls.98+ *99+ * @return {Object}100+ * @api public101+ */102+103+View.prototype.__defineGetter__('templateEngine', function(){104+ var ext = this.extension;105+ return cache[ext] || (cache[ext] = require(this.engine));106+});modified:lib/express/view.js+6 added-132 removed
1 2 /*!3- * Express - View3+ * Express - view4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */7 5 unmodified lines13 , dirname = require('path').dirname14 , basename = require('path').basename15 , utils = require('connect').utils16 , clone = require('./utils').clone17+ , View = require('./view/view')18+ , Partial = require('./view/partial')19 , merge = utils.merge20 , http = require('http')19- , fs = require('fs')21 , mime = utils.mime;22 23 /**24 * Memory cache.3 unmodified lines28 29 var cache = {};30 31 /**31- * Initialize a new `View` with the given `view` path and `options`.32- *33- * @param {String} view34- * @param {Object} options35- * @api private32+ * Expose constructors.33 */34 38-var View = exports = module.exports = function View(view, options) {39- options = options || {};40- // TODO: memoize basename etc41- this.view = view;42- this.root = options.root;43- this.relative = false !== options.relative;44- this.defaultEngine = options.defaultEngine;45- this.parent = options.parentView;46- this.basename = basename(view);47- this.engine = this.resolveEngine();48- this.extension = '.' + this.engine;49- this.path = this.resolvePath();50- this.dirname = dirname(this.path);51-};35+exports = module.exports = View;36+exports.Partial = Partial;37 38 /**54- * Resolve view engine.55- *56- * @return {String}57- * @api private58- */59-60-View.prototype.resolveEngine = function(){61- // Explicit62- if (~this.basename.indexOf('.')) return extname(this.basename).substr(1);63- // Inherit from parent64- if (this.parent) return this.parent.engine;65- // Default66- return this.defaultEngine;67-};68-69-/**70- * Resolve view path.71- *72- * @return {String}73- * @api private74- */75-76-View.prototype.resolvePath = function(){77- var path = this.view;78- // Implicit engine79- if (!~this.basename.indexOf('.')) path += this.extension;80- // Absolute81- if ('/' == path[0]) return path;82- // Relative to parent83- if (this.relative && this.parent) return this.parent.dirname + '/' + path;84- // Relative to root85- return this.root86- ? this.root + '/' + path87- : path;88-};89-90-/**91- * Get view contents. This is a one-time hit, so we92- * can afford to be sync.93- *94- * @return {String}95- * @api public96- */97-98-View.prototype.__defineGetter__('contents', function(){99- return fs.readFileSync(this.path, 'utf8');100-});101-102-/**103- * Get template engine api, cache exports to reduce104- * require() calls.105- *106- * @return {Object}107- * @api public108- */109-110-View.prototype.__defineGetter__('templateEngine', function(){111- var ext = this.extension;112- return cache[ext]113- || (cache[ext] = require(this.engine));114-});115-116-/**117- * Initialize a new `Partial` for the given `view` and `options`.118- *119- * @param {String} view120- * @param {Object} options121- * @api private122- */123-124-var Partial = exports.Partial = function Partial(view, options) {125- options = options || {};126- View.call(this, view, options);127- this.path = this.dirname + '/_' + basename(this.path);128-};129-130-/**131- * Inherit from `View.prototype`.132- */133-134-Partial.prototype.__proto__ = View.prototype;135-136-/**137- * Resolve partial object name from the view path.138- *139- * Examples:140- *141- * "user.ejs" becomes "user"142- * "forum thread.ejs" becomes "forumThread"143- * "forum/thread/post.ejs" becomes "post"144- * "blog-post.ejs" becomes "blogPost"145- *146- * @return {String}147- * @api private148- */149-150-Partial.resolveObjectName = function(view){151- // TODO: cache152- return view153- .split('/')154- .slice(-1)[0]155- .split('.')[0]156- .replace(/[^a-zA-Z0-9 ]+/g, ' ')157- .split(/ +/).map(function(word, i){158- return i159- ? word[0].toUpperCase() + word.substr(1)160- : word;161- }).join('');162-};163-164-/**39 * Register the given template engine `exports`40 * as `ext`. For example we may wish to map ".html"41 * files to jade:42 *231 unmodified lines