From 4b4391362353c62c47a2bd6048ee1318972b37a2 Mon Sep 17 00:00:00 2001 From: kookookchoozeus Date: Tue, 26 Apr 2016 11:36:58 +0100 Subject: [PATCH] Functionality and tests for relative path partial names through the cli --- bin/mustache | 40 +++++++++++++++++-- .../alt/partial_with_relative_path.mustache | 1 + test/_files/cli_with_relative_partials.json | 1 + .../cli_with_relative_partials.mustache | 3 ++ test/_files/cli_with_relative_partials.txt | 2 + .../partial_with_relative_path.mustache | 1 + test/cli-test.js | 25 +++++++++++- 7 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 test/_files/alt/partial_with_relative_path.mustache create mode 100644 test/_files/cli_with_relative_partials.json create mode 100644 test/_files/cli_with_relative_partials.mustache create mode 100644 test/_files/cli_with_relative_partials.txt create mode 100644 test/_files/partial_with_relative_path.mustache diff --git a/bin/mustache b/bin/mustache index 430e8b937..b7ded43f4 100755 --- a/bin/mustache +++ b/bin/mustache @@ -78,7 +78,13 @@ function readPartials (cb) { var partialPath = partialsPaths.pop(); var partial = fs.createReadStream(partialPath); streamToStr(partial, function onDone (str) { - partials[getPartialName(partialPath)] = str; + + var keysArray = getPartialNames(partialPath); + + keysArray.forEach(function addPartialNames (key) { + partials[key] = str; + }); + readPartials(cb); }); } @@ -131,6 +137,32 @@ function hasVersionArg () { }); } -function getPartialName (filename) { - return path.basename(filename, '.mustache'); -} +function getPartialNames (filename) { + // get path relative to the template file + // in order to use e.g. {{> ../common/footer }} + + // before, {{> footer }} used the -p file whose streamToStr happened to + // finish first, as seen above in readPartials + + // this can be extended to the mustache API by using the relative path as + // the key in the partials object + // e.g. mustache.render(template, view, { '../common/footer': '...' }) + var relativePath = path + .relative(templateArg, filename) + // without this, you would have to use {{> ..\\common\\footer }} for windows + // and {{> ../common/footer }} for *nix + .replace(/\\{1,2}/g, '/') + // since path.relative shows paths relative to the file, and not the + // directory (i.e. what everyone is used to), change the reference to the + // current directory from '../' to './' + .replace(/^\.\.\//, './') + // obviously if we want parents of the current directory, we want '../../' + // as opposed to './../../', so this replace fixes that + .replace(/^\.\/\.\.\//, '../') + .replace('.mustache', ''); + + return [ + path.basename(filename, '.mustache'), + relativePath + ]; +} \ No newline at end of file diff --git a/test/_files/alt/partial_with_relative_path.mustache b/test/_files/alt/partial_with_relative_path.mustache new file mode 100644 index 000000000..111de78bd --- /dev/null +++ b/test/_files/alt/partial_with_relative_path.mustache @@ -0,0 +1 @@ +ALT \ No newline at end of file diff --git a/test/_files/cli_with_relative_partials.json b/test/_files/cli_with_relative_partials.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/test/_files/cli_with_relative_partials.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/_files/cli_with_relative_partials.mustache b/test/_files/cli_with_relative_partials.mustache new file mode 100644 index 000000000..fd80b91a2 --- /dev/null +++ b/test/_files/cli_with_relative_partials.mustache @@ -0,0 +1,3 @@ +{{> ./partial_with_relative_path }} + +{{> ./alt/partial_with_relative_path }} \ No newline at end of file diff --git a/test/_files/cli_with_relative_partials.txt b/test/_files/cli_with_relative_partials.txt new file mode 100644 index 000000000..0a9888f8e --- /dev/null +++ b/test/_files/cli_with_relative_partials.txt @@ -0,0 +1,2 @@ +MAIN +ALT \ No newline at end of file diff --git a/test/_files/partial_with_relative_path.mustache b/test/_files/partial_with_relative_path.mustache new file mode 100644 index 000000000..3a8c1686b --- /dev/null +++ b/test/_files/partial_with_relative_path.mustache @@ -0,0 +1 @@ +MAIN \ No newline at end of file diff --git a/test/cli-test.js b/test/cli-test.js index 1e78eea53..0abf67fdb 100644 --- a/test/cli-test.js +++ b/test/cli-test.js @@ -6,6 +6,7 @@ var child_process = require('child_process'); var _files = path.join(__dirname, '_files'); var cliTxt = path.resolve(_files, 'cli.txt'); var cliPartialsTxt = path.resolve(_files, 'cli_with_partials.txt'); +var cliRelativePartialsTxt = path.resolve(_files, 'cli_with_relative_partials.txt'); var moduleVersion = require('../package').version; function changeForOS(command) { @@ -140,5 +141,25 @@ describe('Mustache CLI', function () { done(); }); }); - }) -}); + }); + + describe('with relative partials', function () { + before(function(done) { + fs.readFile(cliRelativePartialsTxt, function onFsEnd(err, data) { + if (err) return done(err); + + expectedOutput = data.toString(); + done(); + }); + }); + + it('selects the file described with the relative path', function (done) { + exec(changeForOS('bin/mustache -p test/_files/alt/partial_with_relative_path.mustache -p test/_files/partial_with_relative_path.mustache test/_files/cli_with_relative_partials.json test/_files/cli_with_relative_partials.mustache'), function (err, stdout, stderr) { + assert.equal(err, null); + assert.equal(stderr, ''); + assert.equal(stdout, expectedOutput); + done(); + }); + }); + }); +}); \ No newline at end of file