Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tysoncadenhead committed Nov 20, 2012
0 parents commit 3c2a5a1
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
node_modules/
.idea/
index.html
.DS_Store
.anvil/
plugins/anvil.*
6 changes: 6 additions & 0 deletions .npmignore
@@ -0,0 +1,6 @@
node_modules/
.idea/
index.html
.DS_Store
.anvil/
plugins/anvil.*
Empty file added README.md
Empty file.
Empty file added build.json
Empty file.
107 changes: 107 additions & 0 deletions lib/plugin.js
@@ -0,0 +1,107 @@
/*global require, console, module */

// Require Mustache. This task depends on mustache. If you don't have it, run "npm install mustache" in your terminal.
var mustache = require("mustache");

var pluginFactory = function( _, anvil ) {

'use strict';

return anvil.plugin( {

name: "anvil.mustache",

// Activity list: "identify", "pull", "combine", "pre-process","compile", "post-process", "push", "test"
activity: "post-process",

// The config data is used to pass into each of the mustache templates
config: {
data: {},
formats: ["md", "html", "handelbars", "hb", "mustache"]
},

// Tests to see if the file should be mustached
isMustache: function (file) {

var self = this,
isMustache = false;

// Loop over all of the formats. If the file is one of the mustache formats, return true
_.each(self.config.formats, function (format) {
if (file.name.indexOf('.' + format) !== -1) {
isMustache = true;
}
});

return isMustache;

},

// Compiles individual files
compileFile: function (file) {

var self = this;

// If the file is a html, handelbars or mustache file, process it
if (self.isMustache(file)) {

// This is the temp file
file.workingFile = file.workingPath + "/" + file.name;

// Read the temp file
anvil.fs.read(file.workingFile, function (html) {

// Markdown screws up some mustache partial include tags... this should fix it
html = html.replace(/\{\{>/g, '{{>');

// Render the template with mustache
file.template = mustache.render(html, self.config.data, self.config.data.partials);

// Re-write the temp file
anvil.fs.write(file.workingFile, file.template, function () {

// Report the compilation to the command line
console.log(" mustached " + (file.relativePath + '/' + file.name).replace('//', '/'));

});

});

}

},

// Compiles individual partials
compilePartial: function (partial, index) {

var self = this;

// Read the partial
anvil.fs.read(anvil.config.working + '/' + partial, function (html) {
self.config.data.partials[index] = html;
});

},

// Run all the things...
run: function( done ) {

var self = this;

// Default the partials
this.config.data.partials = _.extend({}, this.config.data.partials);

// Loop over the partials and add them as HTML
_.each(this.config.data.partials, self.compilePartial);

// Loop over all of the files
_.each(anvil.project.files, self.compileFile);

done();

}

} );
};

module.exports = pluginFactory;
28 changes: 28 additions & 0 deletions package.json
@@ -0,0 +1,28 @@
{
"author": {
"name": "Tyson Cadenhead",
"email": "tcadenhead@appendto.com",
"url": "http://appendto.com"
},
"name": "anvil.mustache",
"description": "Mustache plugin for anvil.js",
"version": "0.0.1",
"repository": {
"type": "git",
"url": "git://github.com/tysoncadenhead/anvil.mustache.git"
},
"engines": {
"node": "~0.8.3"
},
"main": "./lib/plugin.js",
"dependencies": {
"mustache": ">=0.7.0"
},
"devDependencies": {
"anvil.js": ">=0.8.4"
},
"optionalDependencies": {},
"readme": "## Anvil Mustache Plugin\n\nThis plugin requires anvil.js version 0.8.* or greater.\n\n## Installation\n\n\tanvil install anvil.mustache",
"_id": "anvil.mustache@0.0.1",
"_from": "anvil.mustache"
}
107 changes: 107 additions & 0 deletions src/plugin.js
@@ -0,0 +1,107 @@
/*global require, console, module */

// Require Mustache. This task depends on mustache. If you don't have it, run "npm install mustache" in your terminal.
var mustache = require("mustache");

var pluginFactory = function( _, anvil ) {

'use strict';

return anvil.plugin( {

name: "anvil.mustache",

// Activity list: "identify", "pull", "combine", "pre-process","compile", "post-process", "push", "test"
activity: "post-process",

// The config data is used to pass into each of the mustache templates
config: {
data: {},
formats: ["md", "html", "handelbars", "hb", "mustache"]
},

// Tests to see if the file should be mustached
isMustache: function (file) {

var self = this,
isMustache = false;

// Loop over all of the formats. If the file is one of the mustache formats, return true
_.each(self.config.formats, function (format) {
if (file.name.indexOf('.' + format) !== -1) {
isMustache = true;
}
});

return isMustache;

},

// Compiles individual files
compileFile: function (file) {

var self = this;

// If the file is a html, handelbars or mustache file, process it
if (self.isMustache(file)) {

// This is the temp file
file.workingFile = file.workingPath + "/" + file.name;

// Read the temp file
anvil.fs.read(file.workingFile, function (html) {

// Markdown screws up some mustache partial include tags... this should fix it
html = html.replace(/\{\{>/g, '{{>');

// Render the template with mustache
file.template = mustache.render(html, self.config.data, self.config.data.partials);

// Re-write the temp file
anvil.fs.write(file.workingFile, file.template, function () {

// Report the compilation to the command line
console.log(" mustached " + (file.relativePath + '/' + file.name).replace('//', '/'));

});

});

}

},

// Compiles individual partials
compilePartial: function (partial, index) {

var self = this;

// Read the partial
anvil.fs.read(anvil.config.working + '/' + partial, function (html) {
self.config.data.partials[index] = html;
});

},

// Run all the things...
run: function( done ) {

var self = this;

// Default the partials
this.config.data.partials = _.extend({}, this.config.data.partials);

// Loop over the partials and add them as HTML
_.each(this.config.data.partials, self.compilePartial);

// Loop over all of the files
_.each(anvil.project.files, self.compileFile);

done();

}

} );
};

module.exports = pluginFactory;

0 comments on commit 3c2a5a1

Please sign in to comment.