Skip to content

Instantly share code, notes, and snippets.

@vikynandha-zz
Created May 30, 2014 11:28
Show Gist options
  • Select an option

  • Save vikynandha-zz/d3c7adb66791a4d5b996 to your computer and use it in GitHub Desktop.

Select an option

Save vikynandha-zz/d3c7adb66791a4d5b996 to your computer and use it in GitHub Desktop.
Grunt task to convert underscore templates to jade
'use strict';
module.exports = function(grunt) {
var cheerio = require("cheerio"),
_ = require("underscore"),
jadeWrapIndent = function(renderedContent, indentLevel) {
var indentation=[];
for(var i=0;i<indentLevel;i++){
indentation.push(' ');
}
return indentation.join('') + renderedContent;
},
jadeComment = function(content) {
return '// ' + content;
},
jadePipedText = function(content) {
return '| ' + content;
},
jadeTag = function(tag, attribs, content) {
var attrStr='', tagAddition='', renderedTag='', attrs = [];
_(attribs).each(function(val, key){
if(key=='class') {
val && (tagAddition += '.' + val.split(' ').join('.'));
} else if( key=='id') {
tagAddition += '#' + val;
} else {
attrs.push(key + '=\"' + val + '\"');
}
});
attrStr = attrs.join(', ');
renderedTag += (tag=='div'?'':tag) + tagAddition + (attrStr ?
'('+ attrStr + ')' : '');
return renderedTag;
},
process = function(tagTree, indentLevel, list) {
//grunt.log.writeln(tagTree.name);
list.push(jadeWrapIndent(
jadeTag(tagTree.name, tagTree.attribs,
tagTree.data), indentLevel));
tagTree.children.forEach(function(value, index){
//grunt.log.writeln(value.type, Object.keys(value), value.data);
if(value.type==='tag'){
process(value, indentLevel+1, list);
} else if( value.type=='text'){
if(value.data.trim())
list.push(jadeWrapIndent(jadePipedText(
value.data.trim()), indentLevel+1));
} else if(value.type=='comment') {
list.push(jadeWrapIndent(jadeComment(value.data),
indentLevel+1));
}
});
return list;
};
grunt.registerMultiTask('j2j', 'Convert JST templates to jade', function() {
var self = this;
this.filesSrc.forEach(function(jst_file){
grunt.log.writeln('//- ', jst_file);
var content = grunt.file.read(jst_file),
compiled = '',
cleanedContent = content
// Variable interpolation syntax
.replace(/<%- *([^ ]+) *%>/g, function(all_match, group) {
return '#{' + group.replace( /^rc\./, '' ) + '}';
})
.replace(/<%/g, '<!-- <%')
.replace(/%>/g, '%> -->'),
parsedContent = cheerio.load(cleanedContent);
parsedContent.root().children().each(function(index, value){
// grunt.log.writeln(value);
compiled += process(value, 0, []).join('\n') + '\n';
});
// variables assigned to HTML attributes
compiled = compiled.replace(/\="#{([^}]+)}"/g, function(all, group) {
return '=' + group;
}).replace(/\n( *)\| #{([^}]+)}/g, function(all_match, group1, group2) {
return '\n' + group1 + '= ' + group2;
}).replace(/\n( *)\/\/ *<% if \( ([^)]+) \) { %>/g, function(all_match, group1, group2) {
return '\n' + group1 + ' if ' + group2;
}).replace(/([^a-zA-Z_])rc\./g, '$1')
.replace(/ +\n/g, '\n');
grunt.file.write(
jst_file.replace(/\.html$/,'.jade').replace(/^jst\//, 'jade/'),
compiled
);
}, this);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment