grunt-bake
Advanced tools
Comparing version 1.5.1 to 1.6.0
@@ -326,2 +326,36 @@ /* | ||
transform_params: { | ||
options: { | ||
content: { | ||
content: { | ||
"string": "Bake", | ||
"array": [ | ||
"Jim", | ||
"John", | ||
"James", | ||
"Jonathan" | ||
] | ||
} | ||
}, | ||
transforms: { | ||
repeat: function( string, times ) { | ||
return new Array( parseInt( times, 10) + 1 ).join( String( string ) ); | ||
}, | ||
replace: function( string, searchvalue, newvalue ) { | ||
return String( string ).replace( searchvalue, newvalue ); | ||
}, | ||
max: function( array, limit ) { | ||
return array.slice( 0, limit ); | ||
}, | ||
join: function( array, glue ) { | ||
return array.join( glue ); | ||
} | ||
} | ||
}, | ||
files: { | ||
"tmp/transform_params.html": "test/fixtures/transform_params.html" | ||
} | ||
}, | ||
transform_multiple: { | ||
@@ -368,2 +402,18 @@ options: { | ||
transform_foreach: { | ||
options: { | ||
content: "test/fixtures/content.json", | ||
section: "en", | ||
transforms: { | ||
max: function( array, limit ) { | ||
return ( array || [] ).slice( 0, limit ); | ||
} | ||
} | ||
}, | ||
files: { | ||
"tmp/transform_foreach.html": "test/fixtures/transform_foreach.html" | ||
} | ||
}, | ||
keep_undefined_vars: { | ||
@@ -370,0 +420,0 @@ options: { |
{ | ||
"name": "grunt-bake", | ||
"description": "Bake external includes into files to create static pages with no server-side compilation time", | ||
"version": "1.5.1", | ||
"version": "1.6.0", | ||
"homepage": "https://github.com/MathiasPaumgarten/grunt-bake", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -169,9 +169,13 @@ # grunt-bake | ||
#### options.transformGutter | ||
Type: `String` | ||
Default value: '|' | ||
Transforms support parameters like `{{myvar | replace:'A':'B'}}`. Parameters are handed into the callback as additional parameters. | ||
Sequence used to split transforms. | ||
```js | ||
transforms: { | ||
// str => content of myvar, searchvalue => 'A', newvalue => 'B' | ||
replace: function(str, searchvalue, newvalue) { | ||
return String(str).replace(searchvalue, newvalue); | ||
} | ||
} | ||
``` | ||
#### options.semanticIf | ||
@@ -670,7 +674,8 @@ Type: `Bool` | `Array` | `Function` | ||
`1.5.1` __2-9-2016__ adds @referrer attribute to _bake. | ||
`1.5.0` __2-2-2016__ adds support for _bake attribute. | ||
`1.4.1` __2-2-2016__ fixes minor bug fix #72. | ||
`1.4.0` __1-30-2016__ adds full JS support for evaluating _if. | ||
`1.3.1` __1-20-2016__ adds support for parsing values in inline variables. | ||
`1.3.0` __1-13-2016__ adds support for parsing file paths in bake tag. | ||
* `1.6.0` __2-10-2016__ adds support for parameters in transforms. Also introduces a breaking change away from transformGutter. | ||
* `1.5.1` __2-9-2016__ adds @referrer attribute to _bake. | ||
* `1.5.0` __2-2-2016__ adds support for _bake attribute. | ||
* `1.4.1` __2-2-2016__ fixes minor bug fix #72. | ||
* `1.4.0` __1-30-2016__ adds full JS support for evaluating _if. | ||
* `1.3.1` __1-20-2016__ adds support for parsing values in inline variables. | ||
* `1.3.0` __1-13-2016__ adds support for parsing file paths in bake tag. |
@@ -30,3 +30,2 @@ /* | ||
parsePattern: /\{\{\s*([^\}]+)\s*\}\}/g, | ||
transformGutter: "|", | ||
removeUndefined: true | ||
@@ -36,2 +35,8 @@ } ); | ||
// warning about removed parameter | ||
if ( options.transformGutter !== undefined ) { | ||
grunt.log.error( "Parameter `transformGutter` is no longer supported and defaults to `|`. See #71 for details." ); | ||
} | ||
// normalize basePath | ||
@@ -43,3 +48,2 @@ | ||
// normalize content | ||
@@ -55,3 +59,2 @@ | ||
// ======================= | ||
@@ -64,18 +67,10 @@ // -- DEFAULT PROCESSOR -- | ||
return template.replace( options.parsePattern, function( match, inner ) { | ||
var processed = processPlaceholder( inner, content ); | ||
// remove whitespace | ||
var transforms = inner.split( options.transformGutter ).map( function( str ) { | ||
return mout.string.trim( str ); | ||
}); | ||
// the first value is our variable key and not a transfrom | ||
var key = transforms.shift(); | ||
var resolved = resolveName( key, content ); | ||
if( resolved === undefined && !options.removeUndefined ) { | ||
if( processed === undefined && !options.removeUndefined ) { | ||
return match; | ||
} | ||
return transforms.reduce( applyTransform, resolved ); | ||
} ); | ||
return processed; | ||
}); | ||
} | ||
@@ -87,6 +82,37 @@ | ||
function processPlaceholder( placeholder, values ) { | ||
// extract transforms from placeholder | ||
var transforms = placeholder.match( transformsRegex ).map( function( str ) { | ||
// remove whitespace, otherwise transforms and variable key may not be found | ||
str = mout.string.trim( str ); | ||
// extract name of transform and transform parameters, and clear quotes | ||
var parts = str.match( paramsRegex ).map( function( str ) { | ||
return mout.string.trim( str, "'" ); | ||
}); | ||
return { | ||
name: parts[0], | ||
params: parts.slice(1) | ||
}; | ||
}); | ||
// the first value is the set that contains our variable key, and not a transfrom | ||
var key = transforms.shift().name; | ||
var resolved = resolveName( key, values ); | ||
return transforms.reduce( applyTransform, resolved ); | ||
} | ||
function applyTransform( content, transform ) { | ||
var name = transform.name; | ||
if( content === undefined ) { | ||
return; | ||
} | ||
// check if transform is registred | ||
if( ! mout.object.has( options.transforms, transform ) ) { | ||
grunt.log.error( "Unknown transform: " + transform ); | ||
if( ! mout.object.has( options.transforms, name ) ) { | ||
grunt.log.error( "Unknown transform: " + name ); | ||
@@ -97,4 +123,4 @@ return content; | ||
// check if transform is valid callback | ||
if( ! mout.lang.isFunction( options.transforms[ transform ] ) ) { | ||
grunt.log.error( "Transform is not a function: " + transform ); | ||
if( ! mout.lang.isFunction( options.transforms[ name ] ) ) { | ||
grunt.log.error( "Transform is not a function: " + name ); | ||
@@ -104,4 +130,4 @@ return content; | ||
// apply transform | ||
return options.transforms[transform].call( null, content ); | ||
// apply transform, handler is calles with signature ( variableContent, param1, param2, ..., paramN ) | ||
return options.transforms[ name ].apply( null, [ content ].concat( transform.params ) ); | ||
} | ||
@@ -121,2 +147,10 @@ | ||
// Regex to parse transforms including their parameters from placeholders | ||
var transformsRegex = /(?:'[^']*'|[^\|])+/g; | ||
// Regex to parse parameters from transforms | ||
var paramsRegex = /(?:'[^']*'|[^:])+/g; | ||
// Regex to detect array syntax. | ||
@@ -130,3 +164,3 @@ | ||
// | ||
// Regex to serach for variable names | ||
@@ -242,3 +276,3 @@ var ifRegex = /([a-z_$][0-9a-z_$@\.]*)|(?:"([^"]*)")|(?:'([^']*)')/gi; | ||
else { | ||
var array = resolveName( string, values ); | ||
var array = processPlaceholder( string, values ); | ||
if ( ! mout.lang.isArray( array ) ) array = []; | ||
@@ -334,10 +368,11 @@ | ||
var pair = inlineValues[ "_foreach" ].split( ":" ); | ||
var set = inlineValues[ "_foreach" ].split( ":" ); | ||
delete inlineValues[ "_foreach" ]; | ||
getArrayValues( pair[ 1 ], values ).forEach( function( value ) { | ||
// as transforms may contain colons, join rest of list to recreate original string | ||
getArrayValues( set.slice(1).join( ":" ), values ).forEach( function( value ) { | ||
array.push( value ); | ||
} ); | ||
return pair[ 0 ]; | ||
return set[ 0 ]; | ||
} | ||
@@ -344,0 +379,0 @@ |
@@ -32,4 +32,6 @@ "use strict"; | ||
"tmp/transform_single.html": "test/expected/transform_single.html", | ||
"tmp/transform_params.html": "test/expected/transform_params.html", | ||
"tmp/transform_multiple.html": "test/expected/transform_multiple.html", | ||
"tmp/transform_deep.html": "test/expected/transform_deep.html", | ||
"tmp/transform_foreach.html": "test/expected/transform_foreach.html", | ||
"tmp/foreach_meta.html": "test/expected/foreach_meta.html", | ||
@@ -36,0 +38,0 @@ "tmp/multiline_bake.html": "test/expected/multiline_bake.html", |
61071
91
966
680