Skip to content
Browse files

Merge branch 'relative-partials' of https://github.com/pepve/Mu into …

…pepve-relative-partials

Conflicts:
	lib/mu.js
  • Loading branch information...
2 parents 69b6a6c + 4aad0f9 commit d64ecfeeb90ce020991dff8716ba0311ce3741ea @raycmorgan committed Apr 14, 2012
Showing with 77 additions and 7 deletions.
  1. +34 −7 lib/mu.js
  2. +43 −0 test/run_examples_test_no_root.js
View
41 lib/mu.js
@@ -11,9 +11,34 @@ var mu = module.exports = {};
mu.root = process.cwd();
mu.cache = {};
-mu.fs = function (filename, callback) {
- filename = filename.indexOf('/') === 0 || filename.indexOf(':\\') === 1 ? filename : path.join(mu.root, filename);
- fs.readFile(filename, 'utf8', callback);
+mu.fs = function (filename, dirname, callback) {
+ // dirname is optional, so we shift the arguments if it is omitted
+ if (!callback) {
+ callback = dirname;
+ dirname = undefined;
+ }
+
+ var filenames = [];
+ if (filename.indexOf('/') === 0 || filename.indexOf(':\\') === 1) {
+ filenames.push(filename);
+ } else {
+ filenames.push(path.join(mu.root, filename));
+ if (dirname) {
+ filenames.push(path.join(dirname, filename));
+ }
+ }
+
+ (function next() {
+ var file = filenames.shift();
+ if (file) {
+ fs.readFile(file, 'utf8', function (err, contents) {
+ if (err) next();
+ else callback(undefined, contents, file);
+ });
+ } else {
+ callback(new Error('file_not_found'));
+ }
+ }());
}
/**
@@ -24,14 +49,16 @@ mu.fs = function (filename, callback) {
* starts with a '/', the file is assumed to be absolute, else it is
* relative to mu.root.
* @param {Function(err, Parsed)} callback The function to call when the file has been compiled.
+ * @param {Object} unique
+ * @param {String} dirname Directory to use for relative filename lookups.
*/
-mu.compile = function(filename, callback, unique) {
+mu.compile = function(filename, callback, unique, dirname) {
var parsed,
unique = unique || {};
- mu.fs(filename, function (err, contents) {
+ mu.fs(filename, dirname, function (err, contents, found) {
if (err) {
- return callback(new Error('file_not_found'));
+ return callback(err);
}
parsed = parser.parse(contents);
@@ -44,7 +71,7 @@ mu.compile = function(filename, callback, unique) {
}
if (i < parsed.partials.length) {
- mu.compile(parsed.partials[i], next, unique);
+ mu.compile(parsed.partials[i], next, unique, path.dirname(found));
i++;
} else {
View
43 test/run_examples_test_no_root.js
@@ -0,0 +1,43 @@
+var assert = require('assert'),
+ fs = require('fs'),
+ path = require('path'),
+ mu = require('../lib/mu');
+
+// This tests if Mu also works without using the mu.root variable, and only supplying absolute paths.
+
+mu.root = '/tmp/nonexistant';
+
+[
+ 'boolean',
+ 'carriage_return',
+ 'comments',
+ 'complex',
+ 'deep_partial',
+ // 'delimiters',
+ 'error_not_found',
+ 'escaped',
+ 'hash_instead_of_array',
+ 'inverted',
+ 'partial',
+ 'recursion_with_same_names',
+ 'reuse_of_enumerables',
+ 'simple',
+ 'twice',
+ 'two_in_a_row',
+ 'unescaped'
+].forEach(function (name) {
+ var js = fs.readFileSync(path.join(__dirname, 'examples', name + '.js')).toString(),
+ text = fs.readFileSync(path.join(__dirname, 'examples', name + '.txt')).toString();
+
+ js = eval('(' + js + ')');
+
+ var buffer = '';
+
+ mu.compileAndRender(path.join(__dirname, 'examples', name + '.html'), js)
+ .on('data', function (c) { buffer += c.toString(); })
+ .on('end', function () {
+ console.log("Testing: " + name);
+ assert.equal(buffer, text);
+ console.log(name + ' passed\n');
+ });
+});

0 comments on commit d64ecfe

Please sign in to comment.
Something went wrong with that request. Please try again.