Marko v3: Added "preserveWhitespace" as a load template/compiler option

This commit is contained in:
Patrick Steele-Idem 2016-02-09 16:02:34 -07:00
parent 5d2b43b88e
commit 4339c8dfd1
8 changed files with 41 additions and 11 deletions

View File

@ -70,13 +70,19 @@ class Compiler {
ok(this.parser, '"options.parser" is required');
}
compile(src, filename) {
compile(src, filename, options) {
ok(typeof src === 'string', '"src" argument should be a string');
ok(filename, '"filename" argument is required');
ok(typeof filename === 'string', '"filename" argument should be a string');
var context = new CompileContext(src, filename, this.builder);
if (options) {
if (options.preserveWhitespace) {
context.setPreserveWhitespace(true);
}
}
// STAGE 1: Parse the template to produce the initial AST
var ast = this.parser.parse(src, context);
context.root = ast;

View File

@ -76,14 +76,14 @@ function compileFile(filename, options, callback) {
}
try {
callback(null, compiler.compile(templateSrc, filename));
callback(null, compiler.compile(templateSrc, filename, options));
} catch(e) {
callback(e);
}
});
} else {
let templateSrc = fs.readFileSync(filename, {encoding: 'utf8'});
return compiler.compile(templateSrc, filename);
return compiler.compile(templateSrc, filename, options);
}
}
@ -105,7 +105,7 @@ function compile(src, filename, options, callback) {
if (callback) {
try {
callback(null, compiler.compile(src, filename));
callback(null, compiler.compile(src, filename, options));
} catch(e) {
callback(e);
}

View File

@ -44,7 +44,7 @@ function loadSource(templatePath, compiledSrc) {
return templateModule.exports;
}
function loadFile(templatePath) {
function loadFile(templatePath, options) {
templatePath = nodePath.resolve(cwd, templatePath);
var targetDir = nodePath.dirname(templatePath);
@ -54,7 +54,7 @@ function loadFile(templatePath) {
return require(targetFile);
}
var compiledSrc = markoCompiler.compileFile(templatePath);
var compiledSrc = markoCompiler.compileFile(templatePath, options);
// console.log('Compiled code for "' + templatePath + '":\n' + compiledSrc);
var filename = nodePath.basename(targetFile);
@ -91,10 +91,10 @@ module.exports = function load(templatePath, templateSrc, options) {
templateSrc = fs.readFileSync(templatePath, fsReadOptions);
}
var compiledSrc = markoCompiler.compile(templateSrc, templatePath);
var compiledSrc = markoCompiler.compile(templateSrc, templatePath, options);
return loadSource(templatePath, compiledSrc);
} else {
return loadFile(templatePath);
return loadFile(templatePath, options);
}
};

View File

@ -39,6 +39,9 @@
"ignore": true
}
},
"<style>": {
"preserve-whitespace": true
},
"<textarea>": {
"preserve-whitespace": true
},

View File

@ -0,0 +1,8 @@
<div>
This whitespace
should be
preserved.
preserveWhitespace is set to true
See: test.js
</div>

View File

@ -0,0 +1,8 @@
<div>
This whitespace
should be
preserved.
preserveWhitespace is set to true
See: test.js
</div>

View File

@ -0,0 +1,4 @@
exports.templateData = {};
exports.loadOptions = {
preserveWhitespace: true
};

View File

@ -18,12 +18,13 @@ describe('render', function() {
var mainPath = path.join(dir, 'test.js');
var main = fs.existsSync(mainPath) ? require(mainPath) : {};
var loadOptions = main && main.loadOptions;
if (main.checkError) {
var e;
try {
marko.load(templatePath);
marko.load(templatePath, loadOptions);
} catch(_e) {
e = _e;
}
@ -35,7 +36,7 @@ describe('render', function() {
main.checkError(e);
return '$PASS$';
} else {
var template = marko.load(templatePath);
var template = marko.load(templatePath, loadOptions);
var templateData = main.templateData || {};
var html = template.renderSync(templateData);
return html;