Added toString method for PO

This commit is contained in:
Mike Holly 2012-01-01 17:02:19 -08:00
parent 1bae2deab0
commit ea1fcd870f

View File

@ -7,6 +7,13 @@ var PO = function() {
}; };
PO.prototype.save = function(filename, callback) { PO.prototype.save = function(filename, callback) {
fs.writeFile(filename, this.toString(), function(err){
if (err) throw err;
callback && callback();
})
};
PO.prototype.toString = function() {
var lines = ['msgid ""', 'msgstr ""'] var lines = ['msgid ""', 'msgstr ""']
, that = this; , that = this;
@ -14,18 +21,15 @@ PO.prototype.save = function(filename, callback) {
keys.forEach(function(key){ keys.forEach(function(key){
lines.push(util.format('"%s: %s\\n"', key, that.headers[key])); lines.push(util.format('"%s: %s\\n"', key, that.headers[key]));
}); });
lines.push(''); lines.push('');
this.items.forEach(function(item){ this.items.forEach(function(item){
lines.push(item.toString()); lines.push(item.toString());
lines.push(''); lines.push('');
}); });
fs.writeFile(filename, lines.join("\n"), function(err){ return lines.join("\n");
if (err) throw err;
callback && callback();
})
}; };
PO.load = function(filename, callback) { PO.load = function(filename, callback) {
@ -34,7 +38,7 @@ PO.load = function(filename, callback) {
var po = new PO var po = new PO
, parts = data.split(/\n\n/) , parts = data.split(/\n\n/)
, headers = parts.shift().split(/\n/); , headers = parts.shift().split(/\n/);
headers.forEach(function(header){ headers.forEach(function(header){
if (header.match(/^"/)) { if (header.match(/^"/)) {
header = header.trim().replace(/^"/, '').replace(/\\n"$/, ''); header = header.trim().replace(/^"/, '').replace(/\\n"$/, '');
@ -44,13 +48,13 @@ PO.load = function(filename, callback) {
po.headers[name] = value; po.headers[name] = value;
} }
}); });
parts.forEach(function(part){ parts.forEach(function(part){
if (part.length < 1) return; if (part.length < 1) return;
var item = PO.Item.parse(part); var item = PO.Item.parse(part);
po.items.push(item); po.items.push(item);
}); });
callback && callback(po); callback && callback(po);
}); });
}; };
@ -65,7 +69,7 @@ PO.Item = function() {
PO.Item.prototype.toString = function() { PO.Item.prototype.toString = function() {
var lines = [] var lines = []
, that = this; , that = this;
var _processEach = function(keyword, text, i) { var _processEach = function(keyword, text, i) {
var lines = [] var lines = []
, parts = text.split(/\n/) , parts = text.split(/\n/)
@ -81,7 +85,7 @@ PO.Item.prototype.toString = function() {
} }
return lines; return lines;
} }
if (this.references.length > 0) { if (this.references.length > 0) {
this.references.forEach(function(ref){ this.references.forEach(function(ref){
lines.push(util.format('#: %s', ref)); lines.push(util.format('#: %s', ref));
@ -101,15 +105,15 @@ PO.Item.prototype.toString = function() {
} }
}); });
}; };
return lines.join("\n"); return lines.join("\n");
}; };
PO.Item.parse = function(chunk) { PO.Item.parse = function(chunk) {
var item = new PO.Item(); var item = new PO.Item();
var parts = chunk.split(/\nmsg/); var parts = chunk.split(/\nmsg/);
var _extract = function(string) { var _extract = function(string) {
var lines = string.split(/\n/); var lines = string.split(/\n/);
var value = ''; var value = '';
@ -119,7 +123,7 @@ PO.Item.parse = function(chunk) {
}); });
return value.trim(); return value.trim();
}; };
parts.forEach(function(part){ parts.forEach(function(part){
if (part.match(/^#:/)) { if (part.match(/^#:/)) {
item.references.push(part.replace(/^#:\s/, '')); item.references.push(part.replace(/^#:\s/, ''));