Permalink
Browse files

first working commit

  • Loading branch information...
0 parents commit 8d603387a81dafb57212bbe0061e5f3c020053e3 @interlock committed Mar 6, 2012
Showing with 131 additions and 0 deletions.
  1. +8 −0 LICENSE
  2. +30 −0 README.md
  3. +57 −0 lib/connect-handlebars.js
  4. +36 −0 package.json
@@ -0,0 +1,8 @@
+Copyright (c) 2012 James Sapara
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
@@ -0,0 +1,30 @@
+# Introduction
+
+`connect-handlebars` is [Connect](http://www.senchalabs.org/connect/)
+middleware that pre-compiles [handlebars](http://handlebarsjs.com/)) to JS.
+ Since [express](http://expressjs.com) is built on top of Connect,
+`connect-handlebars` will allow you to compile handlebars templates with expressjs.
+
+# Installation
+
+ npm install connect-handlebars
+
+# Usage
+
+Example using express:
+
+ less = require('connect-handlebars')
+ app = require('express').createServer()
+
+ app.use("/templates.js", less("path/to/handles/templates", {
+ }));
+
+ app.listen(80);
+
+The middleware builder takes two arguments `source` and `options`
+
+* `source`: The path to the root directory of .handlebars files to compile.
+* `options`: Object of options with the following keys
+
+# Running Tests
+
@@ -0,0 +1,57 @@
+var handlebars = require('handlebars');
+var fs = require('fs');
+module.exports = function(source,options) {
+ if ( options == undefined) {
+ options = {};
+ }
+ if (!source) throw new Error('connect-handlebars requires file path');
+
+ function processTemplate(template, root, output) {
+ var path = template,
+ stat = fs.statSync(path);
+ if (stat.isDirectory()) {
+ console.log('dir %s', path);
+ fs.readdirSync(template).map(function(file) {
+ var path = template + '/' + file;
+
+ if (/\.handlebars$/.test(path) || fs.statSync(path).isDirectory()) {
+ processTemplate(path, root || template, output);
+ }
+ });
+ } else {
+ console.log('file %s', path);
+ var data = fs.readFileSync(path, 'utf8');
+
+ /*
+ var options = {
+ knownHelpers: known,
+ knownHelpersOnly: argv.o
+ };
+ */
+
+ // Clean the template name
+ if (!root) {
+ template = basename(template);
+ } else if (template.indexOf(root) === 0) {
+ template = template.substring(root.length+1);
+ }
+ template = template.replace(/\.handlebars$/, '');
+
+ output.push('templates[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n');
+ console.log(output);
+ }
+ }
+
+ return function(req,res,next) {
+ var output = [];
+ output.push('(function() {\n var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};\n');
+ processTemplate(source, source, output);
+ output.push('})();');
+ output = output.join('');
+ res.writeHead(200,{
+ 'Content-Type': 'text/javascript',
+ 'Content-Length': output.length
+ });
+ return res.end(output);
+ };
+};
@@ -0,0 +1,36 @@
+{
+ "author": "James Sapara <jsapara@gmail.com>",
+ "name": "connect-handlebars",
+ "description": "Connect middleware for Handlebars",
+ "version": "0.0.1",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/interlock/connect-handlebars.git"
+ },
+ "keywords": [
+ "Handlebars",
+ "Connect",
+ "Express",
+ "middleware"
+ ],
+ "homepage": "http://github.com/interlock/connect-handlebars",
+ "bugs": {
+ "url": "https://github.com/interlock/connect-handlebars/issues"
+ },
+ "main": "lib/connect-handlebars.js",
+ "engines": {
+ "node": "*"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/interlock/connect-lesscss/blob/master/LICENSE"
+ }
+ ],
+ "dependencies": {
+ "handlebars": "> 1.0.0"
+ },
+ "devDependencies": {
+ "connect": "*"
+ }
+}

0 comments on commit 8d60338

Please sign in to comment.