Commits

Kazunori Ninomiya committed 1ecbf58

Add task.

  • Participants
  • Parent commits 18cbbc0

Comments (0)

Files changed (9)

+.DS_Store
+node_modules/*
+!.gitkeep

File Gruntfile.js

+'use strict';
+
+module.exports = function(grunt)
+{
+  grunt.initConfig({
+    ect: {
+      top: {
+        options: {
+          root: 'test/ect',
+        },
+        src:  'page',
+        dest: 'test/page.html',
+        variables: {
+          title : 'Hello, world!',
+          id : 'main',
+          links: [
+            { name : 'Google', url : 'http://google.com/' },
+            { name : 'Facebook', url : 'http://facebook.com/' },
+            { name : 'Twitter', url : 'http://twitter.com/' }
+          ],
+          upperHelper : function (string) {
+            return string.toUpperCase();
+          }
+        }
+      },
+    },
+  });
+
+  grunt.loadTasks('tasks');
+};

File package.json

+{
+  "name": "grunt-ect",
+  "description": "Static site compiler built around ect",
+  "version": "0.0.1",
+  "homepage": "https://bitbucket.org/2no/grunt-ect",
+  "author": {
+    "name": "2no",
+    "url": "http://www.wakuworks.com/"
+  },
+  "repository": {
+    "type": "ssh",
+    "url": "ssh://git@bitbucket.org/2no/grunt-ect.git"
+  },
+  "bugs": {
+    "url": "https://bitbucket.org/2no/grunt-ect/issues"
+  },
+  "license": "MIT",
+  "main": "Gruntfile.js",
+  "devDependencies": {
+    "ect": "~0.4.11"
+  }
+}

File tasks/ect.js

+'use strict';
+
+module.exports = function(grunt)
+{
+  var fs  = require('fs'),
+      ECT = require('ect');
+
+  grunt.registerMultiTask('ect', 'generates an html file from a ect template', function() {
+    var html, renderer,
+        data    = this.data,
+        options = data.options || {},
+        done    = this.async();
+
+    if (typeof options['watch'] !== 'undefined') {
+        delete options['watch'];
+    }
+    renderer = new ECT(options);
+
+    try {
+      html = renderer.render(data.src, data.variables || {});
+      grunt.file.write(data.dest, html);
+      grunt.log.writeln("HTML written to '"+ data.dest +"'");
+      done(true);
+    }
+    catch (e) {
+      grunt.log.error(e);
+      done(false);
+    }
+  });
+};

File test/ect/footer

+<div id="footer">
+	<div class="left">Generated by ECT</div>
+	<% content 'footer-info' %>
+</div>

File test/ect/layout

+<!DOCTYPE html>
+<html>
+	<head>
+		<title><%- @title %></title>
+	</head>
+	<body>
+		<% content %>
+		<% include 'footer' %>
+	</body>
+</html>

File test/ect/list

+<% linkHelper = (link) -> %>
+	<li><a href="<%- link.url %>"><%- link.name %></a></li>
+<% end %>
+
+<% if @links?.length : %>
+	<ul>
+		<% for link in @links : %>
+			<%- linkHelper link %>
+		<% end %>
+	</ul>
+<% else : %>
+	<p>List is empty</p>
+<% end %>

File test/ect/page

+<% extend 'layout' %>
+
+<div id="<%- @id %>">
+	<h1><%- @upperHelper @title %></h1>
+	<% include 'list' %>
+</div>
+
+<% block 'footer-info' : %>
+	<div class="right">page: main</div>
+<% end %>

File test/page.html

+<!DOCTYPE html>
+<html>
+	<head>
+		<title>Hello, world!</title>
+	</head>
+	<body>
+		
+<div id="main">
+	<h1>HELLO, WORLD!</h1>
+	
+	<ul>
+						<li><a href="http://google.com/">Google</a></li>
+						<li><a href="http://facebook.com/">Facebook</a></li>
+						<li><a href="http://twitter.com/">Twitter</a></li>
+			</ul>
+</div>
+
+		<div id="footer">
+	<div class="left">Generated by ECT</div>
+		<div class="right">page: main</div>
+</div>
+	</body>
+</html>