UNPKG

4.08 kBMarkdownView Raw
1# grunt-text-replace !['Build status'](https://api.travis-ci.org/yoniholmes/grunt-text-replace.png)
2*General purpose text-replacement for grunt.*
3
4This plugin allows you to replace text in files using strings, regexs or functions.
5
6
7## Installation
8In your project's [grunt.js][getting_started] directory, run:
9
10```bash
11npm install grunt-text-replace
12```
13
14Then add this line to your project's *grunt.js*:
15
16```javascript
17grunt.loadNpmTasks('grunt-text-replace');
18```
19
20[grunt]: http://gruntjs.com/
21[getting_started]: https://github.com/gruntjs/grunt/blob/master/docs/getting_started.md
22
23
24## Usage
25
26
27```javascript
28replace: {
29 example: {
30 src: ['text/*.txt'], // source files array (supports minimatch)
31 dest: 'build/text/', // destination directory or file
32 replacements: [{
33 from: 'Red', // string replacement
34 to: 'Blue'
35 }, {
36 from: /(f|F)(o{2,100})/g, // regex replacement ('Fooo' to 'Mooo')
37 to: 'M$2'
38 }, {
39 from: 'Foo',
40 to: function (matchedWord) { // callback replacement
41 return matchedWord + ' Bar';
42 }
43 }]
44 }
45}
46```
47
48Here's another example using [grunt.template][grunt.template], and overwriting
49of source files:
50
51```javascript
52replace: {
53 another_example: {
54 src: ['build/*.html'],
55 overwrite: true, // overwriting each file
56 replacements: [{
57 from: /[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}/g,
58 to: "<%= grunt.template.today('dd/mm/yyyy') %>"
59 }]
60 }
61}
62```
63
64
65
66## API reference
67
68
69### replace
70
71*replace* is the top level task that goes in your `grunt.initConfig({})`. It is
72a [multi-task][multitask], meaning that it must contain targets, which you can
73name anything you like.
74
75[multitask]: https://github.com/gruntjs/grunt/blob/master/docs/api.md#gruntregistermultitask
76
77
78### src
79
80*src* is an array of source files to be replaced, and is required.
81It supports [minimatch][minimatch] paths.
82
83[minimatch]: https://github.com/isaacs/minimatch
84
85
86### dest
87
88*dest* is the destination for files to be replaced, and can refer to either a:
89
90- file: `'path/output.txt'`
91- directory: `'path/'`
92
93grunt-text-replace will throw an error if multiple source files are mapped to
94a single file.
95
96
97
98### overwrite
99
100*overwrite* is used if all you need to do is overwrite existing files.
101To use it, omit *dest*, otherwise
102grunt-text-replace will throw an error. You can only use one or the other.
103
104
105### replacements
106
107*replacements* is an array of *from* and *to* replacements. See the
108[examples](#usage) above.
109
110
111### from
112
113*from* is the old text that you'd like replace. It can be a:
114
115- plain string: `'Red'` *matches all instances of 'Red' in file*
116- regular expression object: `/(r|R)ed/g` *same as above*
117
118
119### to
120
121*to* is the replacement. It can be a:
122
123- plain string
124- string containing a [grunt.template][grunt.template]
125- string containing regex variables `$1`, `$2`, etc
126- combination of the above
127- function where the return value will be used as the replacement text.
128
129#### function
130Where *to* is a function, the function receives 4 parameters:
131
1321. **matchedWord**: the matched word
1332. **index**: an integer representing point where word was found in a text
1343. **fullText**: the full original text
1354. **regexMatches**: an array containing all regex matches, empty if none defined or found.
136
137
138```javascript
139// Where the original source string is: "Hello world"
140
141replacements: [{
142 from: /wor(ld)/g,
143 to: function (matchedWord, index, fullText, regexMatches) {
144 // matchedWord: "world"
145 // index: 6
146 // fullText: "Hello world"
147 // regexMatches: ["ld"]
148 return 'planet'; //
149 }
150}]
151
152// The full text will now be: "Hello planet"
153```
154
155
156[grunt.template]: https://github.com/gruntjs/grunt/blob/master/docs/api_template.md
157
158
159## Release History
160- v0.2.0 - 2012/11/21. Added tests, refactored internals, strings now replace globally within a file, updated documentation.
161- v0.1.0 - 2012/11/12. Initial release.
162
163
164
165
166## License
167Copyright (c) 2012 Jonathan Holmes
168Licensed under the MIT license.
\No newline at end of file