diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..59290ba --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore index b785247..0670764 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules npm-debug.log tmp +.DS_Store diff --git a/.jshintrc b/.jshintrc index 207ef1c..507f912 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,21 +1,14 @@ { + "boss": true, "curly": true, "eqeqeq": true, + "eqnull": true, "immed": true, "latedef": true, "newcap": true, "noarg": true, + "node": true, "sub": true, "undef": true, - "boss": true, - "eqnull": true, - "node": true, - "predef": [ - "describe", - "it", - "before", - "beforeEach", - "after", - "afterEach" - ] + "unused": true } diff --git a/.travis.yml b/.travis.yml index 4d7aab0..63efea3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,8 @@ +sudo: false language: node_js node_js: - - "0.10" -before_install: + - 'iojs' + - '0.12' + - '0.10' +before_script: - npm install -g grunt-cli diff --git a/README.md b/README.md index aa5c2c2..3ed5aba 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,7 @@ -# grunt-replace [![Build Status](https://secure.travis-ci.org/outaTiME/grunt-replace.png?branch=master)](http://travis-ci.org/outaTiME/grunt-replace) +# grunt-replace [![Build Status](https://travis-ci.org/outaTiME/grunt-replace.svg?branch=master)](https://travis-ci.org/outaTiME/grunt-replace) > Replace text patterns with [applause](https://github.com/outaTiME/applause). - - ## Install From NPM: @@ -42,10 +40,230 @@ module.exports = function (grunt) { ### Options -function () { - var name = 'Applause Options'; - return sections[name] || '_(Coming soon)_'; // empty + + +#### patterns +Type: `Array` + +Define patterns that will be used to replace the contents of source files. + +#### patterns.match +Type: `String|RegExp` + +Indicates the matching expression. + +If matching type is `String` we use a simple variable lookup mechanism `@@string` (in any other case we use the default regexp replace logic): + +```javascript +{ + patterns: [ + { + match: 'foo', + replacement: 'bar' // replaces "@@foo" to "bar" + } + ] +} +``` + +#### patterns.replacement or patterns.replace +Type: `String|Function|Object` + +Indicates the replacement for match, for more information about replacement check out the [String.replace]. + +You can specify a function as replacement. In this case, the function will be invoked after the match has been performed. The function's result (return value) will be used as the replacement string. + +```javascript +{ + patterns: [ + { + match: /foo/g, + replacement: function () { + return 'bar'; // replaces "foo" to "bar" + } + } + ] +} +``` + +Also supports object as replacement (we create string representation of object using [JSON.stringify]): + +```javascript +{ + patterns: [ + { + match: /foo/g, + replacement: [1, 2, 3] // replaces "foo" with string representation of "array" object + } + ] +} +``` + +[String.replace]: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace +[JSON.stringify]: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify + +#### patterns.json +Type: `Object` + +If an attribute `json` found in pattern definition we flatten the object using `delimiter` concatenation and each key–value pair will be used for the replacement (simple variable lookup mechanism and no regexp support). + +```javascript +{ + patterns: [ + { + json: { + "key": "value" // replaces "@@key" to "value" + } + } + ] +} +``` + +Also supports nested objects: + +```javascript +{ + patterns: [ + { + json: { + "key": "value", // replaces "@@key" to "value" + "inner": { // replaces "@@inner" with string representation of "inner" object + "key": "value" // replaces "@@inner.key" to "value" + } + } + } + ] +} +``` + +For deferred invocations is possible to define functions: + +```javascript +{ + patterns: [ + { + json: function (done) { + done({ + key: 'value' + }); + } + } + ] +} +``` + +#### patterns.yaml +Type: `String` + +If an attribute `yaml` found in pattern definition will be converted and then processed like [json attribute](#patternsjson). + +```javascript +{ + patterns: [ + { + yaml: 'key: value' // replaces "@@key" to "value" + } + ] +} +``` + +For deferred invocations is possible to define functions: + +```javascript +{ + patterns: [ + { + yaml: function (done) { + done('key: value'); + } + } + ] +} +``` + +#### patterns.cson +Type: `String` + +If an attribute `cson` found in pattern definition will be converted and then processed like [json attribute](#patternsjson). + +```javascript +{ + patterns: [ + { + cson: 'key: \'value\'' } + ] +} +``` + +For deferred invocations is possible to define functions: + +```javascript +{ + patterns: [ + { + cson: function (done) { + done('key: \'value\''); + } + } + ] +} +``` + +#### variables +Type: `Object` + +This is the old way to define patterns using plain object (simple variable lookup mechanism and no regexp support). You can still use this but for more control you should use the new `patterns` way. + +```javascript +{ + variables: { + 'key': 'value' // replaces "@@key" to "value" + } +} +``` + +#### prefix +Type: `String` +Default: `@@` + +The prefix added for matching (prevent bad replacements / easy way). + +> This only applies for simple variable lookup mechanism. + +#### usePrefix +Type: `Boolean` +Default: `true` + +If set to `false`, we match the pattern without `prefix` concatenation (useful when you want to lookup an simple string). + +> This only applies for simple variable lookup mechanism. + +#### preservePrefix +Type: `Boolean` +Default: `false` + +If set to `true`, we preserve the `prefix` in target. + +> This only applies for simple variable lookup mechanism and `patterns.replacement` is an string. + +#### delimiter +Type: `String` +Default: `.` + +The delimiter used to flatten when using object as replacement. + +#### preserveOrder +Type: `Boolean` +Default: `false` + +If set to `true`, we preserve the patterns definition order, otherwise these will be sorted (in ascending order) to prevent replacement issues like `head` / `header` (typo regexps will be resolved at last). + +#### detail +Type: `Boolean` +Default: `false` + +If set to `true`, return a object response with the `content` and `detail` of replace operation. + #### excludeBuiltins Type: `Boolean` @@ -407,6 +625,7 @@ replace: { ## Release History + * 2015-08-06   v0.10.0   Last [applause](https://github.com/outaTiME/applause) integration and package.json update. * 2015-08-06   v0.9.3   New pedantic option (thanks [@donkeybanana](https://github.com/donkeybanana)). Fix issue with special characters attributes ($$, $&, $`, $', $n or $nn) on JSON, YAML and CSON. * 2015-05-07   v0.9.2   Fix regression issue with empty string in replacement. * 2015-05-01   v0.9.1   Better output. diff --git a/docs/README.md b/docs/README.md index 1c1fa88..a00c73f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,9 +1,7 @@ -# grunt-replace [![Build Status](https://secure.travis-ci.org/outaTiME/grunt-replace.png?branch=master)](http://travis-ci.org/outaTiME/grunt-replace) +# grunt-replace [![Build Status](https://travis-ci.org/outaTiME/grunt-replace.svg?branch=master)](https://travis-ci.org/outaTiME/grunt-replace) > Replace text patterns with [applause](https://github.com/outaTiME/applause). - - ## Install From NPM: @@ -404,6 +402,7 @@ replace: { ## Release History + * 2015-08-06   v0.10.0   Last [applause](https://github.com/outaTiME/applause) integration and package.json update. * 2015-08-06   v0.9.3   New pedantic option (thanks [@donkeybanana](https://github.com/donkeybanana)). Fix issue with special characters attributes ($$, $&, $`, $', $n or $nn) on JSON, YAML and CSON. * 2015-05-07   v0.9.2   Fix regression issue with empty string in replacement. * 2015-05-01   v0.9.1   Better output. diff --git a/package.json b/package.json index 5faa8fb..8f676b7 100644 --- a/package.json +++ b/package.json @@ -1,25 +1,13 @@ { "name": "grunt-replace", "description": "Replace text patterns with applause.", - "version": "0.9.3", - "homepage": "http://github.com/outaTiME/grunt-replace", + "version": "0.10.0", "author": { "name": "outaTiME", "url": "http://outa.im/" }, - "repository": { - "type": "git", - "url": "http://github.com/outaTiME/grunt-replace.git" - }, - "bugs": { - "url": "http://github.com/outaTiME/grunt-replace/issues" - }, - "licenses": [ - { - "type": "MIT", - "url": "https://github.com/outaTiME/grunt-replace/blob/master/LICENSE-MIT" - } - ], + "repository": "outaTiME/grunt-replace", + "license": "MIT", "engines": { "node": ">=0.10.0" }, @@ -30,7 +18,7 @@ "dependencies": { "chalk": "^1.0.0", "lodash": "^3.1.0", - "applause": "0.4.3" + "applause": "1.0.3" }, "devDependencies": { "grunt": "^0.4.0", @@ -59,7 +47,6 @@ "flatten" ], "files": [ - "tasks", - "LICENSE-MIT" + "tasks" ] } diff --git a/scripts/generate.js b/scripts/generate.js index ded9623..ac8c852 100644 --- a/scripts/generate.js +++ b/scripts/generate.js @@ -30,12 +30,13 @@ while ((match = pattern.exec(readme)) !== null) { var Applause = require('applause'); var options = { - variables: { - 'options': function () { + patterns: [{ + match: 'options', + replacement: function () { var name = 'Applause Options'; return sections[name] || '_(Coming soon)_'; // empty } - } + }] }; var applause = Applause.create(options); var contents = fs.readFileSync('docs/README.md', 'utf8');