Skip to content

Commit

Permalink
Merge pull request #5 from drumba/master
Browse files Browse the repository at this point in the history
added option to override the output function
  • Loading branch information
gwagroves authored Feb 9, 2017
2 parents ee1d5d9 + 94adcfb commit 0a6b79b
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- "node"
- "iojs"
- "0.12"
install:
- npm install
21 changes: 16 additions & 5 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
jshint: {
all: [
'Gruntfile.js',
'tasks/*.js',
'<%= nodeunit.tests %>'
],
files: {
src: [
'Gruntfile.js',
'tasks/*.js',
'<%= nodeunit.tests %>'
]
},
options: {
jshintrc: '.jshintrc'
}
Expand All @@ -38,6 +40,15 @@ module.exports = function(grunt) {
'tmp/wordpress.php': ['test/fixtures/**/*.twig', 'test/fixtures/wp/*.php']
}
},
wordpress_custom_function: {
options: {
textdomain: 'bar',
output_function: 'fooMethod',
},
files: {
'tmp/wordpress_custom_function.php': ['test/fixtures/**/*.twig', 'test/fixtures/wp/*.php']
}
},
drupal: {
options: {
style: 'drupal',
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,11 @@ grunt.initConfig({

Wordpress only: The textdomain to be parsed. gettext calls to other domains will be ignored. Set to `null` to parse all text domains.

#### output_function

`gettext` (default)

The function name that is used in the generated php file.

## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"test": "grunt test"
},
"devDependencies": {
"grunt-contrib-jshint": "^0.9.2",
"grunt-contrib-jshint": "^0.11.0",
"grunt-contrib-clean": "^0.5.0",
"grunt-contrib-nodeunit": "^0.3.3",
"grunt": "~0.4.5"
Expand Down
10 changes: 6 additions & 4 deletions tasks/gettext_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ module.exports = function(grunt) {
// creation: http://gruntjs.com/creating-tasks

var PATTERN_WORDPRESS = /_[_e]\((['"])((?:(?!\1).)*)\1,\s?\1((?:(?!\1).)*)\1/g,
PATTERN_DRUPAL_TWIG = new RegExp('{{ ?([\'"])((?:(?!\\1).)*)\\1\\|t ?}}', 'g');
PATTERN_DRUPAL_TWIG = new RegExp('{{ ?([\'"])((?:(?!\\1).)*)\\1\\|t ?}}', 'g'),
options = {};

grunt.registerMultiTask('gettext_parser', 'Extract gettext calls to a single file.', function() {

// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
options = this.options({
style: 'wordpress',
textdomain: null
textdomain: null,
output_function: 'gettext'
});

// Iterate over all specified file groups.
Expand Down Expand Up @@ -124,7 +126,7 @@ module.exports = function(grunt) {
* @return {String}
*/
function getGettextCall(slug) {
return "gettext('" + slug + "')";
return options.output_function + "('" + slug + "')";
}

};
5 changes: 5 additions & 0 deletions test/expected/wordpress_custom_function.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
fooMethod('foo');
fooMethod('missing space');
fooMethod('Double quotes');
fooMethod('Headline');
14 changes: 12 additions & 2 deletions test/gettext_parser_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@ exports.gettext_parser = {
var expected = grunt.file.read('test/expected/wordpress.php');
test.equal(actual, expected, 'parses and extracts gettext calls from wordpress PHP and twig files.');

var actual = grunt.file.read('tmp/drupal.php');
var expected = grunt.file.read('test/expected/drupal.php');
actual = grunt.file.read('tmp/drupal.php');
expected = grunt.file.read('test/expected/drupal.php');
test.equal(actual, expected, 'parses and extracts gettext calls from drupal twig files.');

test.done();
},

custom_function_option: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/wordpress_custom_function.php');
var expected = grunt.file.read('test/expected/wordpress_custom_function.php');
test.equal(actual, expected, 'writes a custom function call .');

test.done();
},

};

0 comments on commit 0a6b79b

Please sign in to comment.