Skip to content

Commit

Permalink
Merge pull request #9 from gwa/develop
Browse files Browse the repository at this point in the history
v0.4.0
  • Loading branch information
gwagroves authored Feb 11, 2017
2 parents f8d7cdd + 6ce37cb commit bedbba6
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 30 deletions.
24 changes: 12 additions & 12 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
jshint: {
files: {
src: [
'Gruntfile.js',
'tasks/*.js',
'<%= nodeunit.tests %>'
]
},
options: {
jshintrc: '.jshintrc'
}
},

// Before generating any new files, remove any previously-created files.
clean: {
Expand All @@ -40,6 +28,17 @@ module.exports = function(grunt) {
'tmp/wordpress.php': ['test/fixtures/**/*.twig', 'test/fixtures/wp/*.php']
}
},

wordpress_add_textdomain: {
options: {
textdomain: 'bar',
add_textdomain: true
},
files: {
'tmp/wordpress_add_textdomain.php': ['test/fixtures/**/*.twig', 'test/fixtures/wp/*.php']
}
},

wordpress_custom_function: {
options: {
textdomain: 'bar',
Expand All @@ -49,6 +48,7 @@ module.exports = function(grunt) {
'tmp/wordpress_custom_function.php': ['test/fixtures/**/*.twig', 'test/fixtures/wp/*.php']
}
},

drupal: {
options: {
style: 'drupal',
Expand Down
2 changes: 1 addition & 1 deletion LICENSE-MIT.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Great White Ark
Copyright (c) 2017 Great White Ark

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ Wordpress only: The textdomain to be parsed. gettext calls to other domains will

`gettext` (default)

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

`false` (default)

Write the textdomain to the php file. Helpful for use with checktextdomain libraries.

## 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/).
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-gettext-parser",
"description": "Extract gettext calls from WordPress / Timber templates to a single PHP file.",
"version": "0.3.2",
"description": "Extract gettext calls from Wordpress themes and twig templates to a single PHP file.",
"version": "0.4.0",
"homepage": "https://github.com/groves/gettext-parser",
"author": {
"name": "Great White Ark",
Expand All @@ -28,7 +28,6 @@
"test": "grunt test"
},
"devDependencies": {
"grunt-contrib-jshint": "^0.11.0",
"grunt-contrib-clean": "^0.5.0",
"grunt-contrib-nodeunit": "^0.3.3",
"grunt": "~0.4.5"
Expand Down
23 changes: 12 additions & 11 deletions tasks/gettext_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@
* grunt-gettext-parser
* https://github.com/gwa/grunt-gettext-parser
*
* Copyright (c) 2015 Great White Ark
* Copyright (c) 2017 Great White Ark
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {

// Please see the Grunt documentation for more information regarding task
// creation: http://gruntjs.com/creating-tasks

var PATTERN_WORDPRESS = /_[_e]\((['"])((?:(?!\1).)*)\1,\s?\1((?:(?!\1).)*)\1/g,
var PATTERN_WORDPRESS = /_[_e]\(\s?(['"])((?:(?!\1).)*)\1,\s?\1((?:(?!\1).)*\s?)\1/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.
options = this.options({
style: 'wordpress',
textdomain: null,
output_function: 'gettext'
output_function: 'gettext',
add_textdomain: false
});

// Iterate over all specified file groups.
Expand Down Expand Up @@ -55,7 +54,6 @@ module.exports = function(grunt) {
// Print a success message.
grunt.log.writeln('File "' + f.dest + '" created.');
});

});

/**
Expand All @@ -80,9 +78,9 @@ module.exports = function(grunt) {
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}

return true;
}

/**
Expand Down Expand Up @@ -118,15 +116,18 @@ module.exports = function(grunt) {
* @return {String}
*/
function formatGettextMatch(match) {
return getGettextCall(match[2]);
return getGettextCall(match[2], match[3]);
}

/**
* @param {String} slug
* @return {String}
*/
function getGettextCall(slug) {
function getGettextCall(slug, textdomain) {
if (options.add_textdomain) {
return options.output_function + "('" + slug + "', '" + textdomain + "')";
}

return options.output_function + "('" + slug + "')";
}

};
5 changes: 5 additions & 0 deletions test/expected/wordpress_add_textdomain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
gettext('foo', 'bar');
gettext('missing space', 'bar');
gettext('Double quotes', 'bar');
gettext('Headline', 'bar');
2 changes: 1 addition & 1 deletion test/fixtures/wp/wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

<h1><?php _e('Headline', 'bar'); ?></h1>

The following call has a different domain: <?php _e('plugin name', 'myplugin'); ?>
The following call has a different domain: <?php _e( 'plugin name', 'myplugin' ); ?>
12 changes: 11 additions & 1 deletion test/gettext_parser_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,22 @@ exports.gettext_parser = {
test.done();
},

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

var actual = grunt.file.read('tmp/wordpress_add_textdomain.php');
var expected = grunt.file.read('test/expected/wordpress_add_textdomain.php');
test.equal(actual, expected, 'adds the textdomain to the rendered functions.');

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.equal(actual, expected, 'writes a custom function call.');

test.done();
},
Expand Down

0 comments on commit bedbba6

Please sign in to comment.