templatizer
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -6,2 +6,3 @@ var templatizer = require('../templatizer'), | ||
templatizer(path.resolve(__dirname, '../templates'), path.resolve(__dirname, '../test/demo_output_no_mixins.js'), true); | ||
templatizer([path.resolve(__dirname, '../templates'), path.resolve(__dirname, '../templates2')], path.resolve(__dirname, '../test/demo_output_multiple_dirs.js')); | ||
templatizer(path.resolve(__dirname, '../templates'), path.resolve(__dirname, '../demo_output.js')); |
{ | ||
"name": "templatizer", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
@@ -5,0 +5,0 @@ "contributors": [ |
@@ -10,6 +10,10 @@ var jade = require('jade'); | ||
module.exports = function (templateDirectory, outputFile, dontTransformMixins) { | ||
module.exports = function (templateDirectories, outputFile, dontTransformMixins) { | ||
if (typeof templateDirectories === "string") { | ||
templateDirectories = [templateDirectories]; | ||
} | ||
var parentObjName = 'exports'; // This is just to use in multiple places | ||
var folders = []; | ||
var templates = []; | ||
var _readTemplates = []; | ||
var isWindows = process.platform === 'win32'; | ||
@@ -22,3 +26,3 @@ var pathSep = path.sep || (isWindows ? '\\' : '/'); | ||
]; | ||
var contents = walkdir.sync(templateDirectory); | ||
var jadeRuntime = fs.readFileSync(_.find(placesToLook, fs.existsSync)).toString(); | ||
@@ -34,14 +38,24 @@ var output = [ | ||
contents.forEach(function (file) { | ||
var item = file.replace(templateDirectory, '').slice(1); | ||
if (path.extname(item) === '' && path.basename(item).charAt(0) !== '.') { | ||
folders.push(item); | ||
} else if (path.extname(item) === '.jade') { | ||
templates.push(item); | ||
} | ||
}); | ||
templateDirectories.forEach(function (templateDirectory) { | ||
var contents = walkdir.sync(templateDirectory); | ||
folders = _.sortBy(folders, function (folder) { | ||
var arr = folder.split(pathSep); | ||
return arr.length; | ||
contents.forEach(function (file) { | ||
var item = file.replace(templateDirectory, '').slice(1); | ||
if (path.extname(item) === '' && path.basename(item).charAt(0) !== '.') { | ||
if (folders.indexOf(item) === -1) folders.push(item); | ||
} else if (path.extname(item) === '.jade') { | ||
// Throw an err if we are about to override a template | ||
if (_readTemplates.indexOf(item) > -1) { | ||
throw new Error(item + ' from ' + templateDirectory + '/' + item + ' already exists in ' + templates[_readTemplates.indexOf(item)]); | ||
} | ||
_readTemplates.push(item); | ||
templates.push(templateDirectory + '/' + item); | ||
} | ||
}); | ||
folders = _.sortBy(folders, function (folder) { | ||
var arr = folder.split(pathSep); | ||
return arr.length; | ||
}); | ||
}); | ||
@@ -56,17 +70,20 @@ | ||
templates.forEach(function (file) { | ||
var name = path.basename(file, '.jade'); | ||
templates.forEach(function (item) { | ||
var name = path.basename(item, '.jade'); | ||
var dirString = function () { | ||
var dirname = path.dirname(file); | ||
var itemTemplateDir = _.find(templateDirectories, function (templateDirectory) { | ||
return item.indexOf(templateDirectory + '/') === 0; | ||
}); | ||
var dirname = path.dirname(item).replace(itemTemplateDir, ''); | ||
if (dirname === '.') return name; | ||
var arr = dirname.split(pathSep); | ||
if (dirname === '.') return name; | ||
arr.push(name); | ||
return arr.join('.'); | ||
return _.compact(arr).join('.'); | ||
}(); | ||
var fullPath = templateDirectory + '/' + file; | ||
var template = beautify(jade.compile(fs.readFileSync(fullPath), { | ||
var mixinOutput = ''; | ||
var template = beautify(jade.compile(fs.readFileSync(item, 'utf-8'), { | ||
client: true, | ||
compileDebug: false, | ||
pretty: false, | ||
filename: fullPath | ||
filename: item | ||
}).toString()); | ||
@@ -79,4 +96,4 @@ var astResult = jadeAst.getMixins({ | ||
}); | ||
var mixinOutput = astResult.mixins; | ||
mixinOutput = astResult.mixins; | ||
if (!dontTransformMixins) template = astResult.template; | ||
@@ -83,0 +100,0 @@ |
@@ -1,2 +0,2 @@ | ||
/* globals test, ok, templatizer, templatizer_unaltered */ | ||
/* globals test, ok, templatizer, templatizer_unaltered, templatizer_multiple_dirs */ | ||
@@ -57,2 +57,12 @@ var data = { | ||
test("Test multiple dirs", function () { | ||
ok(templatizer_multiple_dirs.hasOwnProperty('test')); | ||
ok(!templatizer.hasOwnProperty('test')); | ||
ok(templatizer_multiple_dirs.otherfolder.hasOwnProperty('othertweet')); | ||
ok(templatizer_multiple_dirs.otherfolder.hasOwnProperty('othertweet2')); | ||
ok(templatizer.otherfolder.hasOwnProperty('othertweet')); | ||
ok(!templatizer.otherfolder.hasOwnProperty('othertweet2')); | ||
}); | ||
test("Test altered vs unaltered mixins", function () { | ||
@@ -59,0 +69,0 @@ var users = templatizer.usersMixins({users: data.users}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
128065
29
3535
6