Comparing version 0.2.8 to 0.2.9
127
lib/po.js
@@ -1,3 +0,3 @@ | ||
var fs = require('fs'), | ||
isArray = require('lodash.isarray'); | ||
var fs = require('fs'); | ||
var isArray = require('lodash.isarray'); | ||
@@ -19,4 +19,3 @@ function trim(string) { | ||
PO.prototype.toString = function () { | ||
var lines = [], | ||
that = this; | ||
var lines = []; | ||
@@ -33,4 +32,5 @@ if (this.comments) { | ||
var keys = Object.keys(this.headers); | ||
var self = this; | ||
keys.forEach(function (key) { | ||
lines.push('"' + key + ': ' + that.headers[key] + '\\n"'); | ||
lines.push('"' + key + ': ' + self.headers[key] + '\\n"'); | ||
}); | ||
@@ -45,3 +45,3 @@ | ||
return lines.join("\n"); | ||
return lines.join('\n'); | ||
}; | ||
@@ -62,6 +62,6 @@ | ||
data = data.replace(/\r\n/g, '\n'); | ||
var po = new PO(), | ||
sections = data.split(/\n\n/), | ||
headers = sections.shift(), | ||
lines = sections.join("\n").split(/\n/); | ||
var po = new PO(); | ||
var sections = data.split(/\n\n/); | ||
var headers = sections.shift(); | ||
var lines = sections.join('\n').split(/\n/); | ||
@@ -98,5 +98,5 @@ po.headers = { | ||
header = header.trim().replace(/^"/, '').replace(/\\n"$/, ''); | ||
var p = header.split(/:/), | ||
name = p.shift().trim(), | ||
value = p.join(':').trim(); | ||
var p = header.split(/:/); | ||
var name = p.shift().trim(); | ||
var value = p.join(':').trim(); | ||
po.headers[name] = value; | ||
@@ -106,13 +106,17 @@ } | ||
var item = new PO.Item(), | ||
context = null, | ||
plural = 0, | ||
obsolete = false; | ||
var item = new PO.Item(); | ||
var context = null; | ||
var plural = 0; | ||
var obsoleteCount = 0; | ||
var noCommentLineCount = 0; | ||
function finish() { | ||
if (item.msgid.length > 0) { | ||
if (obsoleteCount >= noCommentLineCount) { | ||
item.obsolete = true; | ||
} | ||
obsoleteCount = 0; | ||
noCommentLineCount = 0; | ||
po.items.push(item); | ||
item = new PO.Item(); | ||
item.obsolete = obsolete; | ||
obsolete = false; | ||
} | ||
@@ -130,10 +134,12 @@ } | ||
while (lines.length > 0) { | ||
var line = trim(lines.shift()), | ||
add = false; | ||
var line = trim(lines.shift()); | ||
var lineObsolete = false; | ||
var add = false; | ||
if (line.match(/^#\~/)) { // Obsolete item | ||
obsolete = true; | ||
//only remove the obsolte comment mark, here | ||
//might be, this is a new item, so | ||
//only remember, this line is marked obsolete, count after line is parsed | ||
line = trim(line.substring(2)); | ||
} else { | ||
obsolete = false; | ||
lineObsolete = true; | ||
} | ||
@@ -144,28 +150,24 @@ | ||
item.references.push(trim(line.replace(/^#:/, ''))); | ||
} | ||
else if (line.match(/^#,/)) { // Flags | ||
} else if (line.match(/^#,/)) { // Flags | ||
finish(); | ||
var flags = trim(line.replace(/^#,/, '')).split(","); | ||
var flags = trim(line.replace(/^#,/, '')).split(','); | ||
for (var i = 0; i < flags.length; i++) { | ||
item.flags[flags[i]] = true; | ||
} | ||
} | ||
else if (line.match(/^#\s+/)) { // Translator comment | ||
} else if (line.match(/^#\s+/)) { // Translator comment | ||
finish(); | ||
item.comments.push(trim(line.replace(/^#\s+/, ''))); | ||
} | ||
else if (line.match(/^#\./)) { // Extracted comment | ||
} else if (line.match(/^#\./)) { // Extracted comment | ||
finish(); | ||
item.extractedComments.push(trim(line.replace(/^#\./, ''))); | ||
} | ||
else if (line.match(/^msgid_plural/)) { // Plural form | ||
} else if (line.match(/^msgid_plural/)) { // Plural form | ||
item.msgid_plural = extract(line); | ||
context = 'msgid_plural'; | ||
} | ||
else if (line.match(/^msgid/)) { // Original | ||
noCommentLineCount++; | ||
} else if (line.match(/^msgid/)) { // Original | ||
finish(); | ||
item.msgid = extract(line); | ||
context = 'msgid'; | ||
} | ||
else if (line.match(/^msgstr/)) { // Translation | ||
noCommentLineCount++; | ||
} else if (line.match(/^msgstr/)) { // Translation | ||
var m = line.match(/^msgstr\[(\d+)\]/); | ||
@@ -175,16 +177,15 @@ plural = m && m[1] ? parseInt(m[1]) : 0; | ||
context = 'msgstr'; | ||
} | ||
else if (line.match(/^msgctxt/)) { // Context | ||
noCommentLineCount++; | ||
} else if (line.match(/^msgctxt/)) { // Context | ||
finish(); | ||
item.msgctxt = extract(line); | ||
} | ||
else { // Probably multiline string or blank | ||
noCommentLineCount++; | ||
} else { // Probably multiline string or blank | ||
if (line.length > 0) { | ||
noCommentLineCount++; | ||
if (context === 'msgstr') { | ||
item.msgstr[plural] += extract(line); | ||
} | ||
else if (context === 'msgid') { | ||
} else if (context === 'msgid') { | ||
item.msgid += extract(line); | ||
} | ||
else if (context === 'msgid_plural') { | ||
} else if (context === 'msgid_plural') { | ||
item.msgid_plural += extract(line); | ||
@@ -194,2 +195,7 @@ } | ||
} | ||
if (lineObsolete) { | ||
// Count obsolete lines for this item | ||
obsoleteCount++; | ||
} | ||
} | ||
@@ -214,4 +220,4 @@ finish(); | ||
PO.Item.prototype.toString = function () { | ||
var lines = [], | ||
that = this; | ||
var lines = []; | ||
var self = this; | ||
@@ -225,5 +231,5 @@ // reverse what extract(string) method during PO.parse does | ||
var _process = function (keyword, text, i) { | ||
var lines = [], | ||
parts = text.split(/\n/), | ||
index = typeof i !== 'undefined' ? '[' + i + ']' : ''; | ||
var lines = []; | ||
var parts = text.split(/\n/); | ||
var index = typeof i !== 'undefined' ? '[' + i + ']' : ''; | ||
if (parts.length > 1) { | ||
@@ -234,4 +240,3 @@ lines.push(keyword + index + ' ""'); | ||
}); | ||
} | ||
else { | ||
} else { | ||
lines.push(keyword + index + ' "' + _escape(text) + '"'); | ||
@@ -259,16 +264,16 @@ } | ||
if (flags.length > 0) { | ||
lines.push('#, ' + flags.join(",")); | ||
lines.push('#, ' + flags.join(',')); | ||
} | ||
var mkObsolete = this.obsolete ? '#~ ' : ''; | ||
['msgctxt', 'msgid', 'msgid_plural', 'msgstr'].forEach(function (keyword) { | ||
var text = that[keyword]; | ||
var text = self[keyword]; | ||
if (text != null) { | ||
if (isArray(text) && text.length > 1) { | ||
text.forEach(function (t, i) { | ||
lines = lines.concat(_process(keyword, t, i)); | ||
lines = lines.concat(mkObsolete + _process(keyword, t, i)); | ||
}); | ||
} | ||
else { | ||
} else { | ||
text = isArray(text) ? text.join() : text; | ||
lines = lines.concat(_process(keyword, text)); | ||
lines = lines.concat(mkObsolete + _process(keyword, text)); | ||
} | ||
@@ -278,9 +283,5 @@ } | ||
if (this.obsolete) { | ||
return "#~ " + lines.join("\n#~ "); | ||
} else { | ||
return lines.join("\n"); | ||
} | ||
return lines.join('\n'); | ||
}; | ||
module.exports = PO; |
{ | ||
"name": "pofile", | ||
"description": "Parse and serialize Gettext PO files.", | ||
"version": "0.2.8", | ||
"version": "0.2.9", | ||
"author": { | ||
@@ -38,11 +38,12 @@ "name": "Ruben Vermeersch", | ||
"devDependencies": { | ||
"browserify": "~3.11.1", | ||
"grunt": "~0.4.2", | ||
"grunt-contrib-watch": "~0.5.3", | ||
"grunt-browserify": "~1.3.0", | ||
"grunt-bump": "0.0.13", | ||
"grunt-contrib-clean": "~0.5.0", | ||
"grunt-contrib-jshint": "~0.7.2", | ||
"grunt-mocha-cli": "~1.4.0", | ||
"grunt-contrib-uglify": "~0.2.7", | ||
"browserify": "~3.11.1", | ||
"grunt-browserify": "~1.3.0", | ||
"grunt-contrib-clean": "~0.5.0", | ||
"grunt-bump": "0.0.13" | ||
"grunt-contrib-watch": "~0.5.3", | ||
"grunt-jscs-checker": "^0.5.1", | ||
"grunt-mocha-cli": "~1.4.0" | ||
}, | ||
@@ -49,0 +50,0 @@ "dependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16500
6
272
10