Commits

Kazunori Ninomiya  committed 346cfe0

Support wildcard pattern. Extension of the setting method.

  • Participants
  • Parent commits 8cdd35e

Comments (0)

Files changed (3)

+Copyright (c) 2013 2no
+
+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.

File package.json

 {
   "name": "grunt-ect",
   "description": "Static site compiler built around ect",
-  "version": "0.0.1",
+  "version": "0.0.2",
   "homepage": "https://bitbucket.org/2no/grunt-ect",
   "author": {
     "name": "2no",
+    "email": "kazunori.ninomiya@gmail.com",
     "url": "http://www.wakuworks.com/"
   },
   "repository": {
-    "type": "ssh",
+    "type": "git",
     "url": "ssh://git@bitbucket.org/2no/grunt-ect.git"
   },
   "bugs": {
     "url": "https://bitbucket.org/2no/grunt-ect/issues"
   },
-  "license": "MIT",
+  "licenses": [
+    {
+      "type": "MIT",
+      "url": "https://bitbucket.org/2no/grunt-ect/blob/master/LICENSE-MIT"
+    }
+  ],
   "main": "Gruntfile.js",
   "engines": {
     "node": ">= 0.8.0"
   },
+  "scripts": {
+    "test": "grunt test"
+  },
   "dependencies": {
-    "grunt": ">=0.4.0",
     "ect": "~0.4.11"
   },
   "devDependencies": {
-    "grunt": ">=0.4.0",
-    "ect": "~0.4.11"
+    "grunt-contrib-jshint": "~0.1.1",
+    "grunt-contrib-clean": "~0.4.0",
+    "grunt-contrib-nodeunit": "~0.1.2",
+    "grunt": "~0.4.1"
+  },
+  "peerDependencies": {
+    "grunt": "~0.4.1"
   },
   "keywords": [
-    "template",
-    "ect",
-    "grunt",
     "gruntplugin"
   ]
 }

File tasks/ect.js

+/*
+ * grunt-ect
+ * https://bitbucket.org/2no/grunt-ect
+ *
+ * Copyright (c) 2013 2no
+ * Licensed under the MIT license.
+ */
+
 'use strict';
 
 module.exports = function(grunt)
 {
-  var fs  = require('fs')
-    , ECT = require('ect');
+  var ECT  = require('ect')
+    , path = require('path')
+    , _ = grunt.util._
+    ;
 
   grunt.registerMultiTask('ect', 'generates an html file from a ect template', function() {
-    var html
-      , renderer
-      , data    = this.data
-      , options = data.options || {}
-      , done    = this.async();
+    var options = this.options({
+            variables: this.data.variables || {}
+          , separator: grunt.util.linefeed
+        })
+      , done = this.async()
+      ;
 
-    if (typeof options['watch'] !== 'undefined') {
+    if (grunt.util.kindOf(options.watch) !== 'undefined') {
       delete options['watch'];
     }
+
+    grunt.verbose.writeflags(options, 'Options');
+
+    this.files.forEach(function(f) {
+      try {
+        var validFiles = removeInvalidFiles(f);
+        writeFile(f.dest, concatOutput(validFiles, options));
+      }
+      catch (e) {
+        grunt.log.error(e);
+        done(false);
+      }
+    });
+
+    done(true);
+  });
+
+  function removeInvalidFiles(files)
+  {
+    return files.src.filter(function(filepath) {
+      if (!grunt.file.exists(filepath)) {
+        grunt.log.warn('Source file "' + filepath + '" not found."');
+        return false;
+      }
+      else {
+        return true;
+      }
+    });
+  }
+
+  function concatOutput(files, options)
+  {
+    return files.map(function(filepath) {
+      return render(filepath, options);
+    }).join(grunt.util.normalizelf(options.separator));
+  }
+
+  function render(filepath, options)
+  {
+    var renderer, root;
+
+    options = _.clone(options);
+    if (grunt.util.kindOf(options.root) === 'undefined') {
+      options.root = path.dirname(filepath);
+      filepath = path.basename(filepath);
+    }
+    else {
+      root = path.join(options.root, path.sep);
+      filepath = filepath.replace(new RegExp('^' + quote(root), 'i'), '');
+    }
+
     renderer = new ECT(options);
+    return renderer.render(filepath, options.variables);
+  }
 
-    try {
-      html = renderer.render(data.src, data.variables || {});
-      grunt.file.write(data.dest, html);
-      grunt.log.writeln("HTML written to '"+ data.dest +"'");
-      done(true);
+  function warnOnEmptyFile(path)
+  {
+    grunt.log.warn('Destination (' + path + ') not written because files were empty.');
+  }
+
+  function writeFile(path, output)
+  {
+    if (output.length < 1) {
+      warnOnEmptyFile(path);
     }
-    catch (e) {
-      grunt.log.error(e);
-      done(false);
+    else {
+      grunt.file.write(path, output);
+      grunt.log.writeln('File ' + path + ' created.');
     }
-  });
+  }
+
+  function quote(str)
+  {
+    return (str + '').replace(/([.?*+\^$\[\]\\(){}|\-])/g, "\\$1");
+  }
 };