1 | # grunt-text-replace [!['Build status'][travis_image_url]][travis_page_url]
|
2 |
|
3 | [travis_image_url]: https://api.travis-ci.org/yoniholmes/grunt-text-replace.png
|
4 | [travis_page_url]: https://travis-ci.org/yoniholmes/grunt-text-replace
|
5 |
|
6 | Replace text in files using strings, regexs or functions.
|
7 |
|
8 | ## Installation
|
9 | In your project's [gruntfile][getting_started] directory, run:
|
10 |
|
11 | ```bash
|
12 | npm install grunt-text-replace --save-dev
|
13 | ```
|
14 |
|
15 | Then add this line to your project's [gruntfile][getting_started]:
|
16 |
|
17 | ```javascript
|
18 | grunt.loadNpmTasks('grunt-text-replace');
|
19 | ```
|
20 |
|
21 | [grunt]: http://gruntjs.com/
|
22 | [getting_started]: https://github.com/gruntjs/grunt/wiki/Getting-started#the-gruntfile
|
23 |
|
24 |
|
25 | ## Usage
|
26 |
|
27 |
|
28 | ```javascript
|
29 | replace: {
|
30 | example: {
|
31 | src: ['text/*.txt'], // source files array (supports minimatch)
|
32 | dest: 'build/text/', // destination directory or file
|
33 | replacements: [{
|
34 | from: 'Red', // string replacement
|
35 | to: 'Blue'
|
36 | }, {
|
37 | from: /(f|F)(o{2,100})/g, // regex replacement ('Fooo' to 'Mooo')
|
38 | to: 'M$2'
|
39 | }, {
|
40 | from: 'Foo',
|
41 | to: function (matchedWord) { // callback replacement
|
42 | return matchedWord + ' Bar';
|
43 | }
|
44 | }]
|
45 | }
|
46 | }
|
47 | ```
|
48 |
|
49 | Here's another example using [grunt.template][grunt.template], and overwriting
|
50 | original source files:
|
51 |
|
52 | ```javascript
|
53 | replace: {
|
54 | another_example: {
|
55 | src: ['build/*.html'],
|
56 | overwrite: true, // overwrite matched source files
|
57 | replacements: [{
|
58 | from: /[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}/g,
|
59 | to: "<%= grunt.template.today('dd/mm/yyyy') %>"
|
60 | }]
|
61 | }
|
62 | }
|
63 | ```
|
64 |
|
65 |
|
66 |
|
67 | ## API reference
|
68 |
|
69 |
|
70 | ### replace
|
71 |
|
72 | *replace* is the top level task that goes in your `grunt.initConfig({})`. It is
|
73 | a [multi-task][multitask], meaning that it must contain targets, which you can
|
74 | name anything you like.
|
75 |
|
76 | [multitask]: https://github.com/gruntjs/grunt/wiki/Configuring-tasks#task-configuration-and-targets
|
77 |
|
78 |
|
79 | ### src
|
80 |
|
81 | *src* is an array of source files to be replaced, and is required.
|
82 | It supports [minimatch][minimatch] paths.
|
83 |
|
84 | [minimatch]: https://github.com/isaacs/minimatch
|
85 |
|
86 |
|
87 | ### dest
|
88 |
|
89 | *dest* is the destination for files to be replaced, and can refer to either a:
|
90 |
|
91 | - file: `'path/output.txt'`
|
92 | - directory: `'path/'`
|
93 |
|
94 | grunt-text-replace will throw an error if multiple source files are mapped to
|
95 | a single file.
|
96 |
|
97 |
|
98 |
|
99 | ### overwrite
|
100 |
|
101 | *overwrite* is used if all you need to do is overwrite existing files.
|
102 | To use it, omit *dest*, otherwise
|
103 | grunt-text-replace will throw an error. You can only use one or the other.
|
104 |
|
105 |
|
106 | ### replacements
|
107 |
|
108 | *replacements* is an array of *from* and *to* replacements. See the
|
109 | [examples](#usage) above.
|
110 |
|
111 |
|
112 | ### from
|
113 |
|
114 | *from* is the old text that you'd like replace. It can be a:
|
115 |
|
116 | - plain string: `'Red'` *matches all instances of 'Red' in file*
|
117 | - regular expression object: `/Red/g` *same as above*
|
118 |
|
119 |
|
120 | ### to
|
121 |
|
122 | *to* is the replacement. It can be a:
|
123 |
|
124 | - plain string
|
125 | - string containing a [grunt.template][grunt.template]
|
126 | - string containing regex variables `$1`, `$2`, etc
|
127 | - combination of the above
|
128 | - function where the return value will be used as the replacement text (supports
|
129 | [grunt.template][grunt.template])
|
130 |
|
131 | #### function
|
132 | Where *to* is a function, the function receives 4 parameters:
|
133 |
|
134 | 1. **matchedWord**: the matched word
|
135 | 2. **index**: an integer representing point where word was found in a text
|
136 | 3. **fullText**: the full original text
|
137 | 4. **regexMatches**: an array containing all regex matches, empty if none
|
138 | defined or found.
|
139 |
|
140 |
|
141 | ```javascript
|
142 | // Where the original source file text is: "Hello world"
|
143 |
|
144 | replacements: [{
|
145 | from: /wor(ld)/g,
|
146 | to: function (matchedWord, index, fullText, regexMatches) {
|
147 | // matchedWord: "world"
|
148 | // index: 6
|
149 | // fullText: "Hello world"
|
150 | // regexMatches: ["ld"]
|
151 | return 'planet'; //
|
152 | }
|
153 | }]
|
154 |
|
155 | // The new text will now be: "Hello planet"
|
156 | ```
|
157 |
|
158 | ### options
|
159 |
|
160 | *options* is an object, specific to a target, and the only supported option is
|
161 | *processTemplates*
|
162 |
|
163 | #### processTemplates
|
164 |
|
165 | *processTemplates* when set to false (by default it is true) switches off
|
166 | grunt.template processing within function return statements. It doesn't work for
|
167 | string replacements (ie. when the replacement is a string, not a function), as
|
168 | grunt processes templates within config string values before they are passed to
|
169 | the plugin.
|
170 |
|
171 | ```javascript
|
172 | replace: {
|
173 | prevent_templates_example: {
|
174 | src: ['text/*.txt'],
|
175 | dest: 'build/text/',
|
176 | options: {
|
177 | processTemplates: false
|
178 | },
|
179 | replacements: [{
|
180 | from: /url\(.*\)/g,
|
181 | to: function () {
|
182 | return "url(<% Don't process this template, retain the delimeters %>)";
|
183 | }
|
184 | }]
|
185 | }
|
186 | }
|
187 | ```
|
188 |
|
189 |
|
190 | [grunt.template]: https://github.com/gruntjs/grunt/wiki/grunt.template
|
191 |
|
192 | ## Road map
|
193 | Some changes I'm considering. Happy to receive suggestions for/against:
|
194 |
|
195 | - **Consolidate function parameters.** This would mean replacing the 4 existing
|
196 | function parameters 'matchedWord', 'index', 'fullText' and 'regexMatches' with a single
|
197 | 'data' object with 4 members.
|
198 | - **Source/Destination paths in function callback**. The above change makes it easier to
|
199 | add the source and destination paths as part of the data parameter in the function callback,
|
200 | which is a requested feature.
|
201 | - **Grunt 4.0 'files' and 'options'**. At some point I might move to bringing the plugin
|
202 | in alignment with the Grunt 4.0 convention of having standard 'files' and 'options' objects.
|
203 |
|
204 |
|
205 | ## Release History
|
206 | - v0.3.7 - 2013/08/26. Bumped dependencies - grunt from 0.4.0 to 0.4.1 due to [changes to path API](http://gruntjs.com/blog/2013-03-13-grunt-0.4.1-released), grunt-contrib-jshint to 0.6.3 and grunt-contrib-nodeunit to 0.2.0.
|
207 | - v0.3.6 - 2013/06/21. Updated links in docs, some of which were pointing to 404 pages.
|
208 | - v0.3.5 - 2013/06/19. Minor clean up of docs & package.json. No functional changes since 0.3.1.
|
209 | - v0.3.1 - 2013/02/18. Minor feature addition: processTemplates: false to switch off grunt templates in function return statements.
|
210 | - v0.3.0 - 2013/02/17. Updated to work in Grunt 4.0. This release is not backwards compatible with grunt 0.3.x.
|
211 | - v0.2.10 - 2012/12/21. Minor internal refactor to better support globally installed instances of grunt on some systems.
|
212 | - v0.2.9 - 2012/11/26. Fixed issue where overwrite: true was not working where multiple src files were defined.
|
213 | - v0.2.7 - 2012/11/25. Fixed issue where replacing a string globally would fail
|
214 | if regex characters were present in string. This is no longer a problem.
|
215 | - v0.2.5 - 2012/11/23. Function replacements now support grunt.template.
|
216 | - v0.2.0 - 2012/11/21. Added tests, refactored internals, strings now replace
|
217 | globally within a file, updated documentation.
|
218 | - v0.1.0 - 2012/11/12. Initial release.
|
219 |
|
220 | Patch releases will generally remain undocumented in this release history.
|
221 | I'll do so if there's enough reason for it, such as a functionality tweak, or
|
222 | significant bug fix. For more detail see the source.
|
223 |
|
224 |
|
225 |
|
226 | ## License
|
227 | Copyright (c) 2012 Jonathan Holmes
|
228 | Licensed under the MIT license.
|