Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f6119a57af | ||
|
|
6c09df93a2 | ||
|
|
1bf498ecf7 | ||
|
|
71bb04f046 | ||
|
|
ba9a2db453 | ||
|
|
fbe773c636 | ||
|
|
7ceda82794 | ||
|
|
d1be0f51b0 | ||
|
|
869f763d80 | ||
|
|
b9394176b1 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "pofile",
|
"name": "pofile",
|
||||||
"version": "0.3.0",
|
"version": "1.0.2",
|
||||||
"authors": [
|
"authors": [
|
||||||
"Ruben Vermeersch <ruben@rocketeer.be>"
|
"Ruben Vermeersch <ruben@rocketeer.be>"
|
||||||
],
|
],
|
||||||
|
|||||||
33
dist/pofile.js
vendored
33
dist/pofile.js
vendored
@@ -1,4 +1,6 @@
|
|||||||
require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"W8CkM0":[function(require,module,exports){
|
require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"pofile":[function(require,module,exports){
|
||||||
|
module.exports=require('W8CkM0');
|
||||||
|
},{}],"W8CkM0":[function(require,module,exports){
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var isArray = require('lodash.isarray');
|
var isArray = require('lodash.isarray');
|
||||||
|
|
||||||
@@ -8,6 +10,7 @@ function trim(string) {
|
|||||||
|
|
||||||
var PO = function () {
|
var PO = function () {
|
||||||
this.comments = [];
|
this.comments = [];
|
||||||
|
this.extractedComments = [];
|
||||||
this.headers = {};
|
this.headers = {};
|
||||||
this.items = [];
|
this.items = [];
|
||||||
};
|
};
|
||||||
@@ -24,6 +27,11 @@ PO.prototype.toString = function () {
|
|||||||
lines.push('# ' + comment);
|
lines.push('# ' + comment);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (this.extractedComments) {
|
||||||
|
this.extractedComments.forEach(function (comment) {
|
||||||
|
lines.push('#. ' + comment);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
lines.push('msgid ""');
|
lines.push('msgid ""');
|
||||||
lines.push('msgstr ""');
|
lines.push('msgstr ""');
|
||||||
@@ -59,7 +67,17 @@ PO.parse = function (data) {
|
|||||||
data = data.replace(/\r\n/g, '\n');
|
data = data.replace(/\r\n/g, '\n');
|
||||||
var po = new PO();
|
var po = new PO();
|
||||||
var sections = data.split(/\n\n/);
|
var sections = data.split(/\n\n/);
|
||||||
var headers = sections.shift();
|
var headers = [];
|
||||||
|
//everything until the first 'msgid ""' is considered header
|
||||||
|
while (sections[0] && (headers.length === 0 || headers[headers.length - 1].indexOf('msgid ""') < 0)) {
|
||||||
|
if (sections[0].match(/msgid "[^"]/)) {
|
||||||
|
//found first real string, adding a dummy header item
|
||||||
|
headers.push('msgid ""');
|
||||||
|
} else {
|
||||||
|
headers.push(sections.shift());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
headers = headers.join('\n');
|
||||||
var lines = sections.join('\n').split(/\n/);
|
var lines = sections.join('\n').split(/\n/);
|
||||||
|
|
||||||
po.headers = {
|
po.headers = {
|
||||||
@@ -87,10 +105,11 @@ PO.parse = function (data) {
|
|||||||
acc.push(line);
|
acc.push(line);
|
||||||
return acc;
|
return acc;
|
||||||
}, []).forEach(function (header) {
|
}, []).forEach(function (header) {
|
||||||
if (header.match(/^#/)) {
|
if (header.match(/^#\./)) {
|
||||||
|
po.extractedComments.push(header.replace(/^#\.\s*/, ''));
|
||||||
|
} else if (header.match(/^#/)) {
|
||||||
po.comments.push(header.replace(/^#\s*/, ''));
|
po.comments.push(header.replace(/^#\s*/, ''));
|
||||||
}
|
} else if (header.match(/^"/)) {
|
||||||
if (header.match(/^"/)) {
|
|
||||||
header = header.trim().replace(/^"/, '').replace(/\\n"$/, '');
|
header = header.trim().replace(/^"/, '').replace(/\\n"$/, '');
|
||||||
var p = header.split(/:/);
|
var p = header.split(/:/);
|
||||||
var name = p.shift().trim();
|
var name = p.shift().trim();
|
||||||
@@ -322,9 +341,7 @@ PO.Item.prototype.toString = function () {
|
|||||||
|
|
||||||
module.exports = PO;
|
module.exports = PO;
|
||||||
|
|
||||||
},{"fs":3,"lodash.isarray":4}],"pofile":[function(require,module,exports){
|
},{"fs":3,"lodash.isarray":4}],3:[function(require,module,exports){
|
||||||
module.exports=require('W8CkM0');
|
|
||||||
},{}],3:[function(require,module,exports){
|
|
||||||
|
|
||||||
},{}],4:[function(require,module,exports){
|
},{}],4:[function(require,module,exports){
|
||||||
/**
|
/**
|
||||||
|
|||||||
2
dist/pofile.min.js
vendored
2
dist/pofile.min.js
vendored
File diff suppressed because one or more lines are too long
25
lib/po.js
25
lib/po.js
@@ -7,6 +7,7 @@ function trim(string) {
|
|||||||
|
|
||||||
var PO = function () {
|
var PO = function () {
|
||||||
this.comments = [];
|
this.comments = [];
|
||||||
|
this.extractedComments = [];
|
||||||
this.headers = {};
|
this.headers = {};
|
||||||
this.items = [];
|
this.items = [];
|
||||||
};
|
};
|
||||||
@@ -23,6 +24,11 @@ PO.prototype.toString = function () {
|
|||||||
lines.push('# ' + comment);
|
lines.push('# ' + comment);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (this.extractedComments) {
|
||||||
|
this.extractedComments.forEach(function (comment) {
|
||||||
|
lines.push('#. ' + comment);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
lines.push('msgid ""');
|
lines.push('msgid ""');
|
||||||
lines.push('msgstr ""');
|
lines.push('msgstr ""');
|
||||||
@@ -58,7 +64,17 @@ PO.parse = function (data) {
|
|||||||
data = data.replace(/\r\n/g, '\n');
|
data = data.replace(/\r\n/g, '\n');
|
||||||
var po = new PO();
|
var po = new PO();
|
||||||
var sections = data.split(/\n\n/);
|
var sections = data.split(/\n\n/);
|
||||||
var headers = sections.shift();
|
var headers = [];
|
||||||
|
//everything until the first 'msgid ""' is considered header
|
||||||
|
while (sections[0] && (headers.length === 0 || headers[headers.length - 1].indexOf('msgid ""') < 0)) {
|
||||||
|
if (sections[0].match(/msgid "[^"]/)) {
|
||||||
|
//found first real string, adding a dummy header item
|
||||||
|
headers.push('msgid ""');
|
||||||
|
} else {
|
||||||
|
headers.push(sections.shift());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
headers = headers.join('\n');
|
||||||
var lines = sections.join('\n').split(/\n/);
|
var lines = sections.join('\n').split(/\n/);
|
||||||
|
|
||||||
po.headers = {
|
po.headers = {
|
||||||
@@ -86,10 +102,11 @@ PO.parse = function (data) {
|
|||||||
acc.push(line);
|
acc.push(line);
|
||||||
return acc;
|
return acc;
|
||||||
}, []).forEach(function (header) {
|
}, []).forEach(function (header) {
|
||||||
if (header.match(/^#/)) {
|
if (header.match(/^#\./)) {
|
||||||
|
po.extractedComments.push(header.replace(/^#\.\s*/, ''));
|
||||||
|
} else if (header.match(/^#/)) {
|
||||||
po.comments.push(header.replace(/^#\s*/, ''));
|
po.comments.push(header.replace(/^#\s*/, ''));
|
||||||
}
|
} else if (header.match(/^"/)) {
|
||||||
if (header.match(/^"/)) {
|
|
||||||
header = header.trim().replace(/^"/, '').replace(/\\n"$/, '');
|
header = header.trim().replace(/^"/, '').replace(/\\n"$/, '');
|
||||||
var p = header.split(/:/);
|
var p = header.split(/:/);
|
||||||
var name = p.shift().trim();
|
var name = p.shift().trim();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "pofile",
|
"name": "pofile",
|
||||||
"description": "Parse and serialize Gettext PO files.",
|
"description": "Parse and serialize Gettext PO files.",
|
||||||
"version": "0.3.0",
|
"version": "1.0.2",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "Ruben Vermeersch",
|
"name": "Ruben Vermeersch",
|
||||||
"email": "ruben@savanne.be",
|
"email": "ruben@savanne.be",
|
||||||
|
|||||||
1
test/fixtures/big.po
vendored
1
test/fixtures/big.po
vendored
@@ -1,4 +1,5 @@
|
|||||||
# French translation of Link (6.x-2.9)
|
# French translation of Link (6.x-2.9)
|
||||||
|
|
||||||
# Copyright (c) 2011 by the French translation team
|
# Copyright (c) 2011 by the French translation team
|
||||||
#
|
#
|
||||||
msgid ""
|
msgid ""
|
||||||
|
|||||||
1
test/fixtures/comment.po
vendored
1
test/fixtures/comment.po
vendored
@@ -1,6 +1,7 @@
|
|||||||
# French translation of Link (6.x-2.9)
|
# French translation of Link (6.x-2.9)
|
||||||
# Copyright (c) 2011 by the French translation team
|
# Copyright (c) 2011 by the French translation team
|
||||||
#
|
#
|
||||||
|
#. extracted from test
|
||||||
msgid ""
|
msgid ""
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: Link (6.x-2.9)\n"
|
"Project-Id-Version: Link (6.x-2.9)\n"
|
||||||
|
|||||||
8
test/fixtures/no_header.po
vendored
Normal file
8
test/fixtures/no_header.po
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# some comment
|
||||||
|
|
||||||
|
msgid "First id, no header"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
msgid "A second string"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
@@ -28,3 +28,46 @@ describe('Headers', function () {
|
|||||||
assert.equal(Object.keys(po.headers).length, 12);
|
assert.equal(Object.keys(po.headers).length, 12);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('PO files with no headers', function () {
|
||||||
|
|
||||||
|
it('Parses an empty string', function () {
|
||||||
|
var po = PO.parse('');
|
||||||
|
assert.notEqual(po, null);
|
||||||
|
// all headers should be empty
|
||||||
|
for (var key in po.headers) {
|
||||||
|
assert.equal(po.headers[key], '');
|
||||||
|
}
|
||||||
|
assert.equal(po.items.length, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Parses a minimal example', function () {
|
||||||
|
var po = PO.parse('msgid "minimal PO"\nmsgstr ""');
|
||||||
|
assert.notEqual(po, null);
|
||||||
|
// all headers should be empty
|
||||||
|
for (var key in po.headers) {
|
||||||
|
assert.equal(po.headers[key], '');
|
||||||
|
}
|
||||||
|
assert.equal(po.items.length, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('advanced example', function () {
|
||||||
|
var po;
|
||||||
|
|
||||||
|
before(function (done) {
|
||||||
|
PO.load(__dirname + '/fixtures/no_header.po', function (err, result) {
|
||||||
|
assert.equal(err, null);
|
||||||
|
po = result;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Parses the po file', function () {
|
||||||
|
assert.notEqual(po, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Finds all items', function () {
|
||||||
|
assert.equal(po.items.length, 2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -61,6 +61,9 @@ describe('Parse', function () {
|
|||||||
assert.notEqual(po, null);
|
assert.notEqual(po, null);
|
||||||
assert.equal(po.items.length, 2);
|
assert.equal(po.items.length, 2);
|
||||||
|
|
||||||
|
assert.equal(po.extractedComments.length, 1);
|
||||||
|
assert.equal(po.extractedComments[0], 'extracted from test');
|
||||||
|
|
||||||
var item = po.items[0];
|
var item = po.items[0];
|
||||||
assert.equal(item.msgid, 'Title, as plain text');
|
assert.equal(item.msgid, 'Title, as plain text');
|
||||||
assert.equal(item.msgstr, 'Attribut title, en tant que texte brut');
|
assert.equal(item.msgstr, 'Attribut title, en tant que texte brut');
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ describe('Write', function () {
|
|||||||
var input = fs.readFileSync(__dirname + '/fixtures/comment.po', 'utf8');
|
var input = fs.readFileSync(__dirname + '/fixtures/comment.po', 'utf8');
|
||||||
var po = PO.parse(input);
|
var po = PO.parse(input);
|
||||||
var str = po.toString();
|
var str = po.toString();
|
||||||
|
assertHasLine(str, '#. extracted from test');
|
||||||
assertHasLine(str, '#. Extracted comment');
|
assertHasLine(str, '#. Extracted comment');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user