[Pkg-javascript-commits] [node-utml] 01/02: Imported Upstream version 0.2.0~gite9f7c3d

Thorsten Alteholz alteholz at moszumanska.debian.org
Sun Jul 17 13:24:17 UTC 2016


This is an automated email from the git hooks/post-receive script.

alteholz pushed a commit to branch master
in repository node-utml.

commit b75ddedcdfbcd7b1c841a240b9057281d42ec158
Author: Thorsten Alteholz <debian at alteholz.de>
Date:   Sun Jul 17 15:24:14 2016 +0200

    Imported Upstream version 0.2.0~gite9f7c3d
---
 .npminclude                             |   2 +
 LICENSE                                 |  21 +++
 README.md                               |  54 +++++++
 examples/code/code.js                   |  20 +++
 examples/code/code.utml                 |  17 +++
 examples/looping/looping.js             |  14 ++
 examples/looping/looping.utml           |  22 +++
 examples/subtemplates/maintemplate.utml |  20 +++
 examples/subtemplates/subtemplate.utml  |   1 +
 examples/subtemplates/subtemplates.js   |  22 +++
 package.json                            |  16 +++
 utml.js                                 | 248 ++++++++++++++++++++++++++++++++
 12 files changed, 457 insertions(+)

diff --git a/.npminclude b/.npminclude
new file mode 100644
index 0000000..5cd4a57
--- /dev/null
+++ b/.npminclude
@@ -0,0 +1,2 @@
+package.json
+utml.js
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..b3ae92b
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2013 Mike Frey
+
+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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..be4667f
--- /dev/null
+++ b/README.md
@@ -0,0 +1,54 @@
+UTML: Underscore Template Markup Language
+=========================================
+
+UTML is a wrapper around the underscore.js template method to make it comply with the express web framework.
+
+## Installing
+
+Use npm:
+
+	npm install utml
+
+## Using it in your project
+
+Setting up your express app
+
+	// no need to require utml, express will do that for us
+	var app = require('express').createServer();
+	
+	// set utml as the view engine
+	app.set('view engine', 'utml');
+	
+	
+	app.get('/', function(req, res){
+		res.render('index', {
+			locals : { 
+				pageTitle : "Hello Node.js + Express + UTML!!",
+				msg : "Insert snarky message here."
+			}
+		});
+	});
+	
+	// start listening on the specified port
+	app.listen(8000);
+
+Create 'views/index.utml'
+
+	<p><%= msg %></p>
+
+Create 'views/layout.utml'
+
+	<!doctype html>
+	<html>
+		<head>
+			<title>Node Test Suite</title>
+		</head>
+		<body>
+	
+			<h1><%= pageTitle %></h1>
+	
+			<div><%= body %></div>
+	
+		</body>
+	</html>
+
diff --git a/examples/code/code.js b/examples/code/code.js
new file mode 100644
index 0000000..18577ca
--- /dev/null
+++ b/examples/code/code.js
@@ -0,0 +1,20 @@
+var utml = require('./../../utml.js');
+
+var options = { 
+  locals : { 
+    title : 'Friends', 
+    people : [
+      { name:'Mike', email:'frey.mike at gmail.com' },
+      { name:'Blago' },
+      { name:'Sara' }, 
+      { name:'Fred', email:'fred at flinstones.com' },
+      { name:'Wilma', email:'wilma at flinstones.com' }
+    ]
+  }
+};
+
+// render the main template
+utml.renderFile(__dirname + '/code.utml', options, function(err, html) {
+  if (err) throw err;
+  console.log(html);
+});
\ No newline at end of file
diff --git a/examples/code/code.utml b/examples/code/code.utml
new file mode 100644
index 0000000..de51173
--- /dev/null
+++ b/examples/code/code.utml
@@ -0,0 +1,17 @@
+<% var pageTitle = "UTML - Code"; %>
+<html>
+<head>
+  <title><%= pageTitle %></title>
+</head>
+<body>
+  <h1><%= title %></h1>
+  
+  <ul>
+    <% for(var i = 0; i < people.length; i++) { 
+    	var person = people[i]; %>
+      <li><%= person.name %> 
+	      <% if (person.email) { %><<%= person.email %>><% } %></li>
+    <% } %>
+  </ul>
+</body>
+</html>
diff --git a/examples/looping/looping.js b/examples/looping/looping.js
new file mode 100644
index 0000000..625ffab
--- /dev/null
+++ b/examples/looping/looping.js
@@ -0,0 +1,14 @@
+var utml = require('./../../utml.js');
+
+var options = { 
+  locals : { 
+    title : 'Friends', 
+    people : ['Mike', 'Blago', 'Sara', 'Fred', 'Wilma']
+  }
+};
+
+// render the main template
+utml.renderFile(__dirname + '/looping.utml', options, function(err, html) {
+  if (err) throw err;
+  console.log(html);
+});
\ No newline at end of file
diff --git a/examples/looping/looping.utml b/examples/looping/looping.utml
new file mode 100644
index 0000000..996261f
--- /dev/null
+++ b/examples/looping/looping.utml
@@ -0,0 +1,22 @@
+<html>
+<head>
+  <title>UTML - Looping</title>
+</head>
+<body>
+  <h1><%= title %></h1>
+  
+  <h2>'for' loop</h2>
+  <ul>
+    <% for(var i = 0; i < people.length; i++) { %>
+      <li><%= people[i] %></li>
+    <% } %>
+  </ul>
+  
+  <h2>Underscore's .each</h2>
+  <ul>  
+    <% _.each(people, function(name){ %>
+      <li><%= name %></li>
+    <% }); %>
+  </ul>
+</body>
+</html>
diff --git a/examples/subtemplates/maintemplate.utml b/examples/subtemplates/maintemplate.utml
new file mode 100644
index 0000000..b676516
--- /dev/null
+++ b/examples/subtemplates/maintemplate.utml
@@ -0,0 +1,20 @@
+<html>
+<head>
+  <title>UTML - Subtemplating</title>
+</head>
+<body>
+  <h1><%= title %></h1>
+
+  <ul>
+    <%
+    for(var i = 0; i < people.length; i++) {
+      print(subtemplate({ name:people[i] }));
+    }
+    
+    _.each(people, function(name){
+      print(subtemplate({ name:name }));
+    });
+    %>
+  </ul>
+</body>
+</html>
diff --git a/examples/subtemplates/subtemplate.utml b/examples/subtemplates/subtemplate.utml
new file mode 100644
index 0000000..792d30e
--- /dev/null
+++ b/examples/subtemplates/subtemplate.utml
@@ -0,0 +1 @@
+<li><%= name %><li>
\ No newline at end of file
diff --git a/examples/subtemplates/subtemplates.js b/examples/subtemplates/subtemplates.js
new file mode 100644
index 0000000..7d51776
--- /dev/null
+++ b/examples/subtemplates/subtemplates.js
@@ -0,0 +1,22 @@
+var utml = require('./../../utml.js');
+
+var options = { 
+  locals : { 
+    title : 'Friends', 
+    people : ['Mike', 'Blago', 'Sara', 'Fred', 'Wilma']
+  }
+};
+
+// compile the subtemplate
+var subtmpl = utml.compileFile(__dirname + '/subtemplate.utml', function(err, fn){
+  if (err) throw err;
+  
+  // add the subtemplate to locals
+  options.locals.subtemplate = fn;
+  
+  // render the main template
+  utml.renderFile(__dirname + '/maintemplate.utml', options, function(err, html) {
+    if (err) throw err;
+    console.log(html);
+  });
+});
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..b3966d3
--- /dev/null
+++ b/package.json
@@ -0,0 +1,16 @@
+{
+  "name" : "utml",
+  "version" : "0.2.0",
+  "description" : "Express compliant templating for underscore.js",
+  "keywords" : ["underscore", "express", "template", "templating", "engine"],
+  "files" : ["utml.js"],
+  "author" : "Mike Frey <frey.mike at gmail.com> (http://freyday.com)",
+  "license" : "MIT",
+  "main" : "./utml.js",
+  "dependencies" : { "underscore" : ">=1.1.3" },
+  "engines" : { "node" : ">=0.2.0" },
+  "repository" : {
+    "type" : "git",
+    "url" : "git://github.com/mikefrey/utml.git"
+  }
+}
diff --git a/utml.js b/utml.js
new file mode 100644
index 0000000..2025b9e
--- /dev/null
+++ b/utml.js
@@ -0,0 +1,248 @@
+/*!
+ * UTML
+ * Underscore Template Markup Language
+ * Copyright(c) 2011 Mike Frey <frey.mike at gmail.com>
+ * MIT Licensed
+ */
+
+
+/**
+ * Dependencies
+ */
+var _ = require('underscore'),
+  fs = require('fs');
+
+
+/**
+ * Module Version
+ */
+exports.version = '0.2.0';
+
+
+/**
+ * Default Options object
+ */
+
+var optionDefaults = {
+  cache:true
+};
+
+
+/**
+ * Template cache
+ */
+
+var cache = {};
+
+
+/**
+ * Clear template cache for the given 'filename', otherwise clear the whole cache
+ *
+ * @param {String} filename
+ * @api public
+ */
+
+var clearCache = exports.clearCache = function(filename) {
+  if (filename) 
+    delete cache[filename];
+  else
+    cache = {};
+}
+
+
+
+/**
+ * Get a template from the cache
+ * 
+ * @param {object} options
+ * @return {Function}
+ */
+
+function fromCache(options) {
+  var filename = options.filename;
+  if (options.cache) {
+    if (filename) {
+      if (cache[filename]) {
+        return cache[filename];
+      }
+    }
+    else {
+      throw new Error('filename is required when using the cache option');
+    }
+  }
+  return false;
+}
+
+/**
+ * Store the given fn in the cache
+ * 
+ * @param {Function} fn
+ * @param {object} options
+ */
+
+function cacheTemplate(fn, options) {
+  if (options.cache && options.filename) {
+    cache[options.filename] = fn;
+  }
+}
+
+
+
+/**
+ * Compile the given 'source' utml into a 'Function'
+ * 
+ * @param {String} source
+ * @param {Object} options
+ * @return {Function}
+ * @api public
+ */
+
+var compile = exports.compile = function(source, options) {
+  if (typeof source === 'string') {
+    var tmpl = _.template(source);
+    cacheTemplate(tmpl, options);
+    return tmpl;
+  }
+  else {
+    return source;
+  }
+}
+
+/**
+ * Compiles a utml file into a 'Function'
+ * 
+ * @param {String} path
+ * @param {object} options
+ * @param {Function} fn
+ * @return {Function}
+ * @api public
+ */
+
+var compileFile = exports.compileFile = function(path, options, fn) {
+  if (typeof options === 'function') {
+    fn = options;
+    options = _.extend({}, optionDefaults);
+  }
+  options.filename = path;
+  
+  var tmpl = fromCache(options);
+  
+  if (tmpl) {
+    return fn(null, tmpl);
+  }
+  else {
+    return fs.readFile(path, 'utf8', function(err, str) {
+      if (err) return fn(err);
+      try {
+        return fn(null, compile(str, options));
+      }
+      catch (err) {
+        return fn(err);
+      }
+    });
+  }
+}
+
+
+
+/**
+ * Render the given 'str' of utml
+ * 
+ * Options:
+ *  - 'locals'    Local variables object
+ *  - 'cache'     Compiled functions are cached, requires 'filename', default: true
+ *  - 'filename'  Used by 'cache' to key caches
+ *  - 'scope'     Function execution context
+ * 
+ * @param {String} str
+ * @param {Object} options
+ * @return {String}
+ * @api public
+ */
+
+var render = exports.render = function(str, options) {
+  var fn, locals;
+  
+  options = _.extend({}, optionDefaults, options || {});
+  locals = _.extend({}, {_:_}, options.locals || {});
+
+  if (options.filename) {
+    locals.include = include(options.filename, options);
+  }
+  
+  fn = (typeof str === 'function') && str || fromCache(options) || compile(str, options);
+  
+  if (options.scope) {
+    return fn.call(options.scope, locals);
+  }
+  else {
+    return fn(locals);
+  }
+}
+
+/**
+ * Renders the utml file at the given path
+ * 
+ * @param {String} path
+ * @param {object} options
+ * @param {Function} fn
+ * @return {String}
+ * @api public
+ */
+
+var renderFile = exports.renderFile = function(path, options, fn) {
+  if (typeof options === 'function') {
+    fn = options;
+    options = _.extend({}, optionDefaults);
+  }
+  options.filename = path;
+  
+  if (fromCache(options)) {
+    return fn(null, render('', options));
+  }
+  else {
+    compileFile(path, options, function(err, tmpl) {
+      if (err) return fn(err);
+
+      return fn(null, render(tmpl, options));
+    });
+  }
+}
+
+
+
+function include(basename, options) {
+  var path = require('path');
+
+  return function(name) {
+    var filePath = path.resolve(path.dirname(basename), name);
+    if (!path.extname(name)) {
+      filePath += '.html';
+    }
+
+    options = _.extend(options, {filename:filePath});
+
+    var tmpl = fs.readFileSync(filePath, 'utf8');
+    return render(tmpl, options);
+  }
+}
+
+
+exports.__express = exports.renderFile;
+
+
+/**
+ * Expose to require()
+ */
+
+if (require.extensions) {
+  require.extensions['.utml'] = function(module, filename) {
+    source = fs.readFileSync(filename, 'utf8');
+    module._compile(compile(source, {}), filename);
+  };
+}
+else if (require.registerExtension) {
+  require.registerExtension('.utml', function(src){
+    return compile(src, {});
+  });
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-utml.git



More information about the Pkg-javascript-commits mailing list