Procházet zdrojové kódy

Functionality and tests for relative path partial names through the cli

pull/563/head
kookookchoozeus před 10 roky
rodič
revize
4b43913623
7 změnil soubory, kde provedl 67 přidání a 6 odebrání
  1. +36
    -4
      bin/mustache
  2. +1
    -0
      test/_files/alt/partial_with_relative_path.mustache
  3. +1
    -0
      test/_files/cli_with_relative_partials.json
  4. +3
    -0
      test/_files/cli_with_relative_partials.mustache
  5. +2
    -0
      test/_files/cli_with_relative_partials.txt
  6. +1
    -0
      test/_files/partial_with_relative_path.mustache
  7. +23
    -2
      test/cli-test.js

+ 36
- 4
bin/mustache Zobrazit soubor

@@ -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
];
}

+ 1
- 0
test/_files/alt/partial_with_relative_path.mustache Zobrazit soubor

@@ -0,0 +1 @@
ALT

+ 1
- 0
test/_files/cli_with_relative_partials.json Zobrazit soubor

@@ -0,0 +1 @@
{}

+ 3
- 0
test/_files/cli_with_relative_partials.mustache Zobrazit soubor

@@ -0,0 +1,3 @@
{{> ./partial_with_relative_path }}

{{> ./alt/partial_with_relative_path }}

+ 2
- 0
test/_files/cli_with_relative_partials.txt Zobrazit soubor

@@ -0,0 +1,2 @@
MAIN
ALT

+ 1
- 0
test/_files/partial_with_relative_path.mustache Zobrazit soubor

@@ -0,0 +1 @@
MAIN

+ 23
- 2
test/cli-test.js Zobrazit soubor

@@ -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();
});
});
});
});

Načítá se…
Zrušit
Uložit