From 363ed2c7919137436c6a7cf643642545a0a81132 Mon Sep 17 00:00:00 2001 From: Ruben Vermeersch Date: Fri, 20 Dec 2013 09:31:12 +0100 Subject: [PATCH] Add browser version. --- .npmignore | 4 + Gruntfile.coffee | 20 ++- bower.json | 24 ++++ dist/pofile.js | 309 +++++++++++++++++++++++++++++++++++++++++++++ dist/pofile.min.js | 1 + package.json | 8 +- 6 files changed, 363 insertions(+), 3 deletions(-) create mode 100644 .npmignore create mode 100644 bower.json create mode 100644 dist/pofile.js create mode 100644 dist/pofile.min.js diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..9abf2fc --- /dev/null +++ b/.npmignore @@ -0,0 +1,4 @@ +/node_modules +/dist +/Gruntfile.coffee +/test diff --git a/Gruntfile.coffee b/Gruntfile.coffee index 8d4387b..fbda55f 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -1,9 +1,15 @@ module.exports = (grunt) -> + @loadNpmTasks('grunt-browserify') + @loadNpmTasks('grunt-contrib-clean') @loadNpmTasks('grunt-contrib-jshint') + @loadNpmTasks('grunt-contrib-uglify') @loadNpmTasks('grunt-contrib-watch') @loadNpmTasks('grunt-mocha-cli') @initConfig + clean: + dist: ['dist'] + jshint: all: [ 'lib/*.js', 'test/*.js' ] options: @@ -23,6 +29,18 @@ module.exports = (grunt) -> options: 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 'build', ['jshint'] + @registerTask 'build', ['clean', 'jshint', 'browserify', 'uglify'] @registerTask 'test', ['build', 'mochacli'] diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..0ff4ac4 --- /dev/null +++ b/bower.json @@ -0,0 +1,24 @@ +{ + "name": "pofile", + "version": "0.1.1", + "authors": [ + "Ruben Vermeersch " + ], + "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" + ] +} diff --git a/dist/pofile.js b/dist/pofile.js new file mode 100644 index 0000000..311b7ee --- /dev/null +++ b/dist/pofile.js @@ -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 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) + * Build: `lodash modularize modern exports="npm" -o ./npm/` + * Copyright 2012-2013 The Dojo Foundation + * Based on Underscore.js 1.5.2 + * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors + * Available under MIT 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) + * Build: `lodash modularize modern exports="npm" -o ./npm/` + * Copyright 2012-2013 The Dojo Foundation + * Based on Underscore.js 1.5.2 + * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors + * Available under MIT 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"]) \ No newline at end of file diff --git a/dist/pofile.min.js b/dist/pofile.min.js new file mode 100644 index 0000000..16452bb --- /dev/null +++ b/dist/pofile.min.js @@ -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;g0&&(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;o0&&("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"]); \ No newline at end of file diff --git a/package.json b/package.json index f3f8ca0..35bc388 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-po", - "description": "Simple library for loading and saving Gettext PO files.", + "description": "Parse and serialize Gettext PO files.", "version": "0.1.1", "author": "Mike Holly", "homepage": "http://github.com/mikejholly/node-po", @@ -23,7 +23,11 @@ "grunt": "~0.4.2", "grunt-contrib-watch": "~0.5.3", "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": { "lodash.isarray": "~2.4.1"