gettext-swig
Advanced tools
Comparing version 0.1.1 to 0.2.0
54
index.js
var newline = /\r?\n|\r/g, | ||
escapeRegExp = function (string) { | ||
// source: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions | ||
return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1"); | ||
}, | ||
escapeRegExp = function (str) { | ||
// source: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions | ||
return str.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1"); | ||
}, | ||
trim = function (str) { | ||
return str.replace(/^\s+|\s+$/g, ''); | ||
}, | ||
trimQuotes = function (str) { | ||
return str.replace(/^['"]/g, '').replace(/['"]$/g, ''); | ||
}, | ||
paramPattern = new RegExp('([\'"][^\'"]*[\'"]|[^\'", ]+)', 'g'); | ||
return str.replace(/^['"]|['"]$/g, ''); | ||
}, | ||
isQuote = function (chr) { | ||
return /['"]/.test(chr); | ||
}, | ||
groupParams = function (result, part) { | ||
if (result.length > 0) { | ||
var last = result[result.length - 1], | ||
firstChar = last[0], | ||
lastChar = last[last.length - 1]; | ||
if (isQuote(firstChar) && (!isQuote(lastChar) || last[last.length - 2] === '\\')) { | ||
// merge with previous | ||
result[result.length - 1] += ',' + part; | ||
} else { | ||
result.push(part); | ||
} | ||
} else { | ||
result.push(part); | ||
} | ||
return result; | ||
}; | ||
/** | ||
@@ -17,6 +40,6 @@ * Constructor | ||
keywordSpec = keywordSpec || { | ||
_: [0], | ||
gettext: [0], | ||
ngettext: [0, 1] | ||
}; | ||
_: [0], | ||
gettext: [0], | ||
ngettext: [0, 1] | ||
}; | ||
@@ -29,3 +52,3 @@ if (typeof keywordSpec !== 'object') { | ||
this.expressionPattern = new RegExp([ | ||
'{{ +', | ||
'{{ *', | ||
'(' + Object.keys(keywordSpec).map(escapeRegExp).join('|') + ')', | ||
@@ -35,4 +58,4 @@ '\\(', | ||
'\\)', | ||
' +}}' | ||
].join(''), 'g'); | ||
' *}}' | ||
].join(''), 'g'); | ||
} | ||
@@ -55,4 +78,5 @@ | ||
keyword = match[1]; | ||
params = match[2].match(paramPattern).map(trimQuotes); | ||
params = match[2].split(',').reduce(groupParams, []).map(trim).map(trimQuotes); | ||
msgid = params[this.keywordSpec[keyword][0]]; | ||
@@ -59,0 +83,0 @@ |
{ | ||
"name": "gettext-swig", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Extract translatable strings from Swig templates", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
5159
78