Add browser version.

This commit is contained in:
Ruben Vermeersch 2013-12-20 09:31:12 +01:00
parent 75818a5b4a
commit 363ed2c791
6 changed files with 363 additions and 3 deletions

4
.npmignore Normal file
View File

@ -0,0 +1,4 @@
/node_modules
/dist
/Gruntfile.coffee
/test

View File

@ -1,9 +1,15 @@
module.exports = (grunt) -> module.exports = (grunt) ->
@loadNpmTasks('grunt-browserify')
@loadNpmTasks('grunt-contrib-clean')
@loadNpmTasks('grunt-contrib-jshint') @loadNpmTasks('grunt-contrib-jshint')
@loadNpmTasks('grunt-contrib-uglify')
@loadNpmTasks('grunt-contrib-watch') @loadNpmTasks('grunt-contrib-watch')
@loadNpmTasks('grunt-mocha-cli') @loadNpmTasks('grunt-mocha-cli')
@initConfig @initConfig
clean:
dist: ['dist']
jshint: jshint:
all: [ 'lib/*.js', 'test/*.js' ] all: [ 'lib/*.js', 'test/*.js' ]
options: options:
@ -23,6 +29,18 @@ module.exports = (grunt) ->
options: options:
reporter: 'spec' reporter: 'spec'
browserify:
dist:
files:
'dist/pofile.js': ['lib/po.js']
options:
alias: 'lib/po.js:pofile'
uglify:
dist:
files:
'dist/pofile.min.js': 'dist/pofile.js'
@registerTask 'default', ['test'] @registerTask 'default', ['test']
@registerTask 'build', ['jshint'] @registerTask 'build', ['clean', 'jshint', 'browserify', 'uglify']
@registerTask 'test', ['build', 'mochacli'] @registerTask 'test', ['build', 'mochacli']

24
bower.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "pofile",
"version": "0.1.1",
"authors": [
"Ruben Vermeersch <ruben@rocketeer.be>"
],
"description": "Parse and serialize Gettext PO files.",
"main": "dist/pofile.js",
"keywords": [
"i18n",
"l10n",
"gettext",
"mo",
"po"
],
"license": "MIT",
"ignore": [
"**/.*",
"lib",
"node_modules",
"bower_components",
"test"
]
}

309
dist/pofile.js vendored Normal file
View File

@ -0,0 +1,309 @@
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})({"GPUdqu":[function(require,module,exports){
var fs = require('fs'),
isArray = require('lodash.isarray');
function trim(string) {
return string.replace(/^\s+|\s+$/g, '');
}
var PO = function () {
this.comments = [];
this.headers = {};
this.items = [];
};
PO.prototype.save = function (filename, callback) {
fs.writeFile(filename, this.toString(), callback);
};
PO.prototype.toString = function () {
var lines = [],
that = this;
if (this.comments) {
this.comments.forEach(function (comment) {
lines.push('# ' + comment);
});
}
lines.push('msgid ""');
lines.push('msgstr ""');
var keys = Object.keys(this.headers);
keys.forEach(function (key) {
lines.push('"' + key + ': ' + that.headers[key] + '\\n"');
});
lines.push('');
this.items.forEach(function (item) {
lines.push(item.toString());
lines.push('');
});
return lines.join("\n");
};
PO.load = function (filename, callback) {
fs.readFile(filename, 'utf-8', function (err, data) {
if (err) {
return callback(err);
}
var po = PO.parse(data);
callback(null, po);
});
};
PO.parse = function (data) {
//support both unix and windows newline formats.
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/);
po.headers = {
'Project-Id-Version': '',
'Report-Msgid-Bugs-To': '',
'POT-Creation-Date': '',
'PO-Revision-Date': '',
'Last-Translator': '',
'Language': '',
'Language-Team': '',
'Content-Type': '',
'Content-Transfer-Encoding': '',
'Plural-Forms': '',
};
headers.split(/\n/).forEach(function (header) {
if (header.match(/^#/)) {
po.comments.push(header.replace(/^#\s*/, ''));
}
if (header.match(/^"/)) {
header = header.trim().replace(/^"/, '').replace(/\\n"$/, '');
var p = header.split(/:/),
name = p.shift().trim(),
value = p.join(':').trim();
po.headers[name] = value;
}
});
var item = new PO.Item(),
context = null,
plural = 0;
function finish() {
if (item.msgid.length > 0) {
po.items.push(item);
item = new PO.Item();
}
}
function extract(string) {
string = trim(string);
string = string.replace(/^[^"]*"|"$/g, '');
string = string.replace(/\\"/g, '"');
string = string.replace(/\\\\/g, '\\');
return string;
}
while (lines.length > 0) {
var line = trim(lines.shift()),
add = false;
if (line.match(/^#:/)) { // Reference
finish();
item.references.push(trim(line.replace(/^#:/, '')));
}
else if (line.match(/^#,/)) { // Flags
finish();
var flags = trim(line.replace(/^#,/, '')).split(",");
for (var i = 0; i < flags.length; i++) {
item.flags[flags[i]] = true;
}
}
else if (line.match(/^#/)) { // Comment
finish();
item.comments.push(trim(line.replace(/^#/, '')));
}
else if (line.match(/^msgid_plural/)) { // Plural form
item.msgid_plural = extract(line);
context = 'msgid_plural';
}
else if (line.match(/^msgid/)) { // Original
finish();
item.msgid = extract(line);
context = 'msgid';
}
else if (line.match(/^msgstr/)) { // Translation
var m = line.match(/^msgstr\[(\d+)\]/);
plural = m && m[1] ? parseInt(m[1]) : 0;
item.msgstr[plural] = extract(line);
context = 'msgstr';
}
else { // Probably multiline string or blank
if (line.length > 0) {
if (context === 'msgstr') {
item.msgstr[plural] += extract(line);
}
else if (context === 'msgid') {
item.msgid += extract(line);
}
else if (context === 'msgid_plural') {
item.msgid_plural += extract(line);
}
}
}
}
finish();
return po;
};
PO.Item = function () {
this.msgid = '';
this.references = [];
this.msgid_plural = null;
this.msgstr = [];
this.comments = [];
this.flags = {};
};
PO.Item.prototype.toString = function () {
var lines = [],
that = this;
var _process = function (keyword, text, i) {
var lines = [],
parts = text.split(/\n/),
index = typeof i !== 'undefined' ? '[' + i + ']' : '';
if (parts.length > 1) {
lines.push(keyword + index + ' ""');
parts.forEach(function (part) {
lines.push('"' + part + '"');
});
}
else {
lines.push(keyword + index + ' "' + text + '"');
}
return lines;
};
if (this.references.length > 0) {
this.references.forEach(function (ref) {
lines.push('#: ' + ref);
});
}
var flags = Object.keys(this.flags);
if (flags.length > 0) {
lines.push('#, ' + flags.join(","));
}
['msgid', 'msgid_plural', 'msgstr'].forEach(function (keyword) {
var text = that[keyword];
if (text != null) {
if (isArray(text) && text.length > 1) {
text.forEach(function (t, i) {
lines = lines.concat(_process(keyword, t, i));
});
}
else {
text = isArray(text) ? text.join() : text;
lines = lines.concat(_process(keyword, text));
}
}
});
return lines.join("\n");
};
module.exports = PO;
},{"fs":3,"lodash.isarray":4}],"pofile":[function(require,module,exports){
module.exports=require('GPUdqu');
},{}],3:[function(require,module,exports){
},{}],4:[function(require,module,exports){
/**
* Lo-Dash 2.4.1 (Custom Build) <http://lodash.com/>
* Build: `lodash modularize modern exports="npm" -o ./npm/`
* Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
* Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
* Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
* Available under MIT license <http://lodash.com/license>
*/
var isNative = require('lodash._isnative');
/** `Object#toString` result shortcuts */
var arrayClass = '[object Array]';
/** Used for native method references */
var objectProto = Object.prototype;
/** Used to resolve the internal [[Class]] of values */
var toString = objectProto.toString;
/* Native method shortcuts for methods with the same name as other `lodash` methods */
var nativeIsArray = isNative(nativeIsArray = Array.isArray) && nativeIsArray;
/**
* Checks if `value` is an array.
*
* @static
* @memberOf _
* @type Function
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is an array, else `false`.
* @example
*
* (function() { return _.isArray(arguments); })();
* // => false
*
* _.isArray([1, 2, 3]);
* // => true
*/
var isArray = nativeIsArray || function(value) {
return value && typeof value == 'object' && typeof value.length == 'number' &&
toString.call(value) == arrayClass || false;
};
module.exports = isArray;
},{"lodash._isnative":5}],5:[function(require,module,exports){
/**
* Lo-Dash 2.4.1 (Custom Build) <http://lodash.com/>
* Build: `lodash modularize modern exports="npm" -o ./npm/`
* Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
* Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
* Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
* Available under MIT license <http://lodash.com/license>
*/
/** Used for native method references */
var objectProto = Object.prototype;
/** Used to resolve the internal [[Class]] of values */
var toString = objectProto.toString;
/** Used to detect if a method is native */
var reNative = RegExp('^' +
String(toString)
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
.replace(/toString| for [^\]]+/g, '.*?') + '$'
);
/**
* Checks if `value` is a native function.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is a native function, else `false`.
*/
function isNative(value) {
return typeof value == 'function' && reNative.test(value);
}
module.exports = isNative;
},{}]},{},["GPUdqu"])

1
dist/pofile.min.js vendored Normal file
View File

@ -0,0 +1 @@
require=function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};b[g][0].call(j.exports,function(a){var c=b[g][1][a];return e(c?c:a)},j,j.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({GPUdqu:[function(a,b){function c(a){return a.replace(/^\s+|\s+$/g,"")}var d=a("fs"),e=a("lodash.isarray"),f=function(){this.comments=[],this.headers={},this.items=[]};f.prototype.save=function(a,b){d.writeFile(a,this.toString(),b)},f.prototype.toString=function(){var a=[],b=this;this.comments&&this.comments.forEach(function(b){a.push("# "+b)}),a.push('msgid ""'),a.push('msgstr ""');var c=Object.keys(this.headers);return c.forEach(function(c){a.push('"'+c+": "+b.headers[c]+'\\n"')}),a.push(""),this.items.forEach(function(b){a.push(b.toString()),a.push("")}),a.join("\n")},f.load=function(a,b){d.readFile(a,"utf-8",function(a,c){if(a)return b(a);var d=f.parse(c);b(null,d)})},f.parse=function(a){function b(){j.msgid.length>0&&(e.items.push(j),j=new f.Item)}function d(a){return a=c(a),a=a.replace(/^[^"]*"|"$/g,""),a=a.replace(/\\"/g,'"'),a=a.replace(/\\\\/g,"\\")}a=a.replace(/\r\n/g,"\n");var e=new f,g=a.split(/\n\n/),h=g.shift(),i=g.join("\n").split(/\n/);e.headers={"Project-Id-Version":"","Report-Msgid-Bugs-To":"","POT-Creation-Date":"","PO-Revision-Date":"","Last-Translator":"",Language:"","Language-Team":"","Content-Type":"","Content-Transfer-Encoding":"","Plural-Forms":""},h.split(/\n/).forEach(function(a){if(a.match(/^#/)&&e.comments.push(a.replace(/^#\s*/,"")),a.match(/^"/)){a=a.trim().replace(/^"/,"").replace(/\\n"$/,"");var b=a.split(/:/),c=b.shift().trim(),d=b.join(":").trim();e.headers[c]=d}});for(var j=new f.Item,k=null,l=0;i.length>0;){var m=c(i.shift());if(m.match(/^#:/))b(),j.references.push(c(m.replace(/^#:/,"")));else if(m.match(/^#,/)){b();for(var n=c(m.replace(/^#,/,"")).split(","),o=0;o<n.length;o++)j.flags[n[o]]=!0}else if(m.match(/^#/))b(),j.comments.push(c(m.replace(/^#/,"")));else if(m.match(/^msgid_plural/))j.msgid_plural=d(m),k="msgid_plural";else if(m.match(/^msgid/))b(),j.msgid=d(m),k="msgid";else if(m.match(/^msgstr/)){var p=m.match(/^msgstr\[(\d+)\]/);l=p&&p[1]?parseInt(p[1]):0,j.msgstr[l]=d(m),k="msgstr"}else m.length>0&&("msgstr"===k?j.msgstr[l]+=d(m):"msgid"===k?j.msgid+=d(m):"msgid_plural"===k&&(j.msgid_plural+=d(m)))}return b(),e},f.Item=function(){this.msgid="",this.references=[],this.msgid_plural=null,this.msgstr=[],this.comments=[],this.flags={}},f.Item.prototype.toString=function(){var a=[],b=this,c=function(a,b,c){var d=[],e=b.split(/\n/),f="undefined"!=typeof c?"["+c+"]":"";return e.length>1?(d.push(a+f+' ""'),e.forEach(function(a){d.push('"'+a+'"')})):d.push(a+f+' "'+b+'"'),d};this.references.length>0&&this.references.forEach(function(b){a.push("#: "+b)});var d=Object.keys(this.flags);return d.length>0&&a.push("#, "+d.join(",")),["msgid","msgid_plural","msgstr"].forEach(function(d){var f=b[d];null!=f&&(e(f)&&f.length>1?f.forEach(function(b,e){a=a.concat(c(d,b,e))}):(f=e(f)?f.join():f,a=a.concat(c(d,f))))}),a.join("\n")},b.exports=f},{fs:3,"lodash.isarray":4}],pofile:[function(a,b){b.exports=a("GPUdqu")},{}],3:[function(){},{}],4:[function(a,b){var c=a("lodash._isnative"),d="[object Array]",e=Object.prototype,f=e.toString,g=c(g=Array.isArray)&&g,h=g||function(a){return a&&"object"==typeof a&&"number"==typeof a.length&&f.call(a)==d||!1};b.exports=h},{"lodash._isnative":5}],5:[function(a,b){function c(a){return"function"==typeof a&&f.test(a)}var d=Object.prototype,e=d.toString,f=RegExp("^"+String(e).replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/toString| for [^\]]+/g,".*?")+"$");b.exports=c},{}]},{},["GPUdqu"]);

View File

@ -1,6 +1,6 @@
{ {
"name": "node-po", "name": "node-po",
"description": "Simple library for loading and saving Gettext PO files.", "description": "Parse and serialize Gettext PO files.",
"version": "0.1.1", "version": "0.1.1",
"author": "Mike Holly", "author": "Mike Holly",
"homepage": "http://github.com/mikejholly/node-po", "homepage": "http://github.com/mikejholly/node-po",
@ -23,7 +23,11 @@
"grunt": "~0.4.2", "grunt": "~0.4.2",
"grunt-contrib-watch": "~0.5.3", "grunt-contrib-watch": "~0.5.3",
"grunt-contrib-jshint": "~0.7.2", "grunt-contrib-jshint": "~0.7.2",
"grunt-mocha-cli": "~1.4.0" "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"
}, },
"dependencies": { "dependencies": {
"lodash.isarray": "~2.4.1" "lodash.isarray": "~2.4.1"