Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for handling multiple filters that seemed to be missing. #136

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var _ = require('lodash');
var escapeRegex = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;
var noContext = '$$noContext';

function mkAttrRegex(startDelim, endDelim, attribute) {
function mkAttrRegex(startDelim, endDelim, attributes) {
var start = startDelim.replace(escapeRegex, '\\$&');
var end = endDelim.replace(escapeRegex, '\\$&');

Expand All @@ -20,11 +20,11 @@ function mkAttrRegex(startDelim, endDelim, attribute) {
start += '(?:\\s*\\:\\:\\s*)?';
}

if (!_.isString(attribute) || attribute.length === 0) {
attribute = 'translate';
if (typeof attributes === 'undefined') {
attributes = ['translate'];
}

return new RegExp(start + '\\s*(\'|"|"|')(.*?)\\1\\s*\\|\\s*' + attribute + '\\s*(' + end + '|\\|)', 'g');
return new RegExp(start + '\\s*(\'|"|"|')(.*?)\\1\\s*\\|\\s*(' + attributes.join('|') + ')\\s*(' + end + '|\\|)', 'g');
}

function localeCompare(a, b) {
Expand Down Expand Up @@ -92,8 +92,8 @@ var Extractor = (function () {
this.options.attributes.unshift(this.options.attribute);

this.strings = {};
this.attrRegex = mkAttrRegex(this.options.startDelim, this.options.endDelim, this.options.attribute);
this.noDelimRegex = mkAttrRegex('', '', this.options.attribute);
this.attrRegex = mkAttrRegex(this.options.startDelim, this.options.endDelim, this.options.attributes);
this.noDelimRegex = mkAttrRegex('', '', this.options.attributes);
}

Extractor.isValidStrategy = function (strategy) {
Expand Down
17 changes: 17 additions & 0 deletions test/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,21 @@ describe('Extract', function () {
assert.equal(catalog.items[1].msgid, 'Hello!');
assert.equal(catalog.items[1].msgstr, '');
});

it('Should extract multiple filters', function () {
var files = [
'test/fixtures/multiple-filters.html'
];
var catalog = testExtract(files, {
attributes: ['normalTranslate', 'specialTranslate']
});

assert.equal(catalog.items.length, 2);

assert.equal(catalog.items[0].msgid, 'Normal translate');
assert.equal(catalog.items[0].msgstr, '');

assert.equal(catalog.items[1].msgid, 'Special translate');
assert.equal(catalog.items[1].msgstr, '');
});
});
36 changes: 18 additions & 18 deletions test/extract_regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Extract: Filter regex', function () {
var hit = false;

while (matches = regex.exec('{{\'Hello\'|translate}}')) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
hit = true;
}
Expand All @@ -27,7 +27,7 @@ describe('Extract: Filter regex', function () {
var hit = false;

while (matches = regex.exec('{{\'Hello\'|translate|lowercase}}')) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
hit = true;
}
Expand All @@ -39,7 +39,7 @@ describe('Extract: Filter regex', function () {
var hit = false;

while (matches = regex.exec('{{"Hello"|translate}}')) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
hit = true;
}
Expand All @@ -51,7 +51,7 @@ describe('Extract: Filter regex', function () {
var hit = false;

while (matches = regex.exec('{{\'Hello\'|translate|lowercase}}')) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
hit = true;
}
Expand All @@ -64,10 +64,10 @@ describe('Extract: Filter regex', function () {

while (matches = regex.exec('{{\'Hello\'|translate}} {{"Second"|translate}}')) {
if (hit === 0) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
} else if (hit === 1) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Second');
}
hit++;
Expand All @@ -81,10 +81,10 @@ describe('Extract: Filter regex', function () {

while (matches = regex.exec('{{\'Hello\'|translate|lowercase}} {{"Second"|translate|uppercase}}')) {
if (hit === 0) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
} else if (hit === 1) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Second');
}
hit++;
Expand All @@ -98,10 +98,10 @@ describe('Extract: Filter regex', function () {

while (matches = regex.exec('{{\'Hello\'|translate}} {{"Second"|translate}}')) {
if (hit === 0) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
} else if (hit === 1) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Second');
}
hit++;
Expand All @@ -115,10 +115,10 @@ describe('Extract: Filter regex', function () {

while (matches = regex.exec('{{\'Hello\'|translate}} {{"Second"|translate|lowercase}}')) {
if (hit === 0) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
} else if (hit === 1) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Second');
}
hit++;
Expand All @@ -131,7 +131,7 @@ describe('Extract: Filter regex', function () {
var hit = false;

while (matches = regex.exec('{{ "Hello" | translate }}')) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
hit = true;
}
Expand All @@ -143,7 +143,7 @@ describe('Extract: Filter regex', function () {
var hit = false;

while (matches = regex.exec('{{ "Hello" | translate | lowercase }}')) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
hit = true;
}
Expand All @@ -156,7 +156,7 @@ describe('Extract: Filter regex', function () {
var hit = false;

while (matches = regex.exec('[[\'Hello\'|translate]]')) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
hit = true;
}
Expand All @@ -169,7 +169,7 @@ describe('Extract: Filter regex', function () {
var hit = false;

while (matches = regex.exec('[[\'Hello\'|translate|lowercase]]')) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
hit = true;
}
Expand All @@ -182,7 +182,7 @@ describe('Extract: Filter regex', function () {
var hit = false;

while (matches = regex.exec('\'Hello\' | translate')) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
hit = true;
}
Expand All @@ -195,7 +195,7 @@ describe('Extract: Filter regex', function () {
var hit = false;

while (matches = regex.exec('\'Hello\' | translate | lowercase')) {
assert.equal(matches.length, 4);
assert.equal(matches.length, 5);
assert.equal(matches[2], 'Hello');
hit = true;
}
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/multiple-filters.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<input type="text" placeholder="{{ 'Normal translate' | normalTranslate }}" />
<span>{{ 'Special translate' | specialTranslate }}</span>
</body>
</html>