From 8bd781070346f88f7cef55d2f83662e76c7c1305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Bacht=C3=ADk?= Date: Wed, 23 May 2018 18:25:55 +0200 Subject: [PATCH] header order --- dist/pofile.js | 22 ++++++++++++++++++++-- dist/pofile.min.js | 2 +- lib/po.js | 20 +++++++++++++++++++- test/write.js | 22 ++++++++++++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/dist/pofile.js b/dist/pofile.js index 0a73481..a31e945 100644 --- a/dist/pofile.js +++ b/dist/pofile.js @@ -1,4 +1,4 @@ -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);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o0&&(o>=p&&(l.obsolete=!0),o=0,p=0,e.items.push(l),l=new f.Item({nplurals:k}))}function c(a){return a=d(a),a=a.replace(/^[^"]*"|"$/g,""),a=a.replace(/\\([abtnvfr'"\\?]|([0-7]{3})|x([0-9a-fA-F]{2}))/g,function(a,b,c,d){if(c)return String.fromCharCode(parseInt(c,8));if(d)return String.fromCharCode(parseInt(d,16));switch(b){case"a":return"";case"b":return"\b";case"t":return"\t";case"n":return"\n";case"v":return"\v";case"f":return"\f";case"r":return"\r";default:return b}})}a=a.replace(/\r\n/g,"\n");for(var e=new f,g=a.split(/\n\n/),h=[];g[0]&&(0===h.length||h[h.length-1].indexOf('msgid ""')<0);)g[0].match(/msgid "[^"]/)?h.push('msgid ""'):h.push(g.shift());h=h.join("\n");var 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/).reduce(function(a,b){return a.merge&&(b=a.pop().slice(0,-1)+b.slice(1),delete a.merge),/^".*"$/.test(b)&&!/^".*\\n"$/.test(b)&&(a.merge=!0),a.push(b),a},[]).forEach(function(a){if(a.match(/^#\./))e.extractedComments.push(a.replace(/^#\.\s*/,""));else if(a.match(/^#/))e.comments.push(a.replace(/^#\s*/,""));else if(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=f.parsePluralForms(e.headers["Plural-Forms"]),k=j.nplurals,l=new f.Item({nplurals:k}),m=null,n=0,o=0,p=0;i.length>0;){var q=d(i.shift()),r=!1;if(q.match(/^#\~/)&&(q=d(q.substring(2)),r=!0),q.match(/^#:/))b(),l.references.push(d(q.replace(/^#:/,"")));else if(q.match(/^#,/)){b();for(var s=d(q.replace(/^#,/,"")).split(","),t=0;t0&&(p++,"msgstr"===m?l.msgstr[n]+=c(q):"msgid"===m?l.msgid+=c(q):"msgid_plural"===m?l.msgid_plural+=c(q):"msgctxt"===m&&(l.msgctxt+=c(q)));r&&o++}return b(),e},f.parsePluralForms=function(a){var b=(a||"").split(";").reduce(function(a,b){var c=b.trim(),d=c.indexOf("="),e=c.substring(0,d).trim(),f=c.substring(d+1).trim();return a[e]=f,a},{});return{nplurals:b.nplurals,plural:b.plural}},f.Item=function(a){var b=a&&a.nplurals;this.msgid="",this.msgctxt=null,this.references=[],this.msgid_plural=null,this.msgstr=[],this.comments=[],this.extractedComments=[],this.flags={},this.obsolete=!1;var c=Number(b);this.nplurals=isNaN(c)?2:c},f.Item.prototype.toString=function(){var a=[],b=this,c=function(a){return a=a.replace(/[\x07\b\t\v\f\r"\\]/g,function(a){switch(a){case"":return"\\a";case"\b":return"\\b";case"\t":return"\\t";case"\v":return"\\v";case"\f":return"\\f";case"\r":return"\\r";default:return"\\"+a}})},d=function(a,b,d){var e=[],f=b.split(/\n/),g="undefined"!=typeof d?"["+d+"]":"";return f.length>1?(e.push(a+g+' ""'),f.forEach(function(a){e.push('"'+c(a)+'"')})):e.push(a+g+' "'+c(b)+'"'),e},e=function(a,b,c){for(var e=d(a,b,c),f=1;f0&&a.push("#, "+f.join(","));var g=this.obsolete?"#~ ":"";return["msgctxt","msgid","msgid_plural","msgstr"].forEach(function(c){var f=b[c];if(null!=f){var h=!1;if(Array.isArray(f)&&(h=f.some(function(a){return a})),Array.isArray(f)&&f.length>1)f.forEach(function(b,d){var f=e(c,b,d);a=a.concat(g+f.join("\n"+g))});else if(b.msgid_plural&&"msgstr"===c&&!h)for(var i=0;i0&&(o>=p&&(l.obsolete=!0),o=0,p=0,e.items.push(l),l=new f.Item({nplurals:k}))}function c(a){return a=d(a),a=a.replace(/^[^"]*"|"$/g,""),a=a.replace(/\\([abtnvfr'"\\?]|([0-7]{3})|x([0-9a-fA-F]{2}))/g,function(a,b,c,d){if(c)return String.fromCharCode(parseInt(c,8));if(d)return String.fromCharCode(parseInt(d,16));switch(b){case"a":return"";case"b":return"\b";case"t":return"\t";case"n":return"\n";case"v":return"\v";case"f":return"\f";case"r":return"\r";default:return b}})}a=a.replace(/\r\n/g,"\n");for(var e=new f,g=a.split(/\n\n/),h=[];g[0]&&(0===h.length||h[h.length-1].indexOf('msgid ""')<0);)g[0].match(/msgid "[^"]/)?h.push('msgid ""'):h.push(g.shift());h=h.join("\n");var 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":""},e.headerOrder=[],h.split(/\n/).reduce(function(a,b){return a.merge&&(b=a.pop().slice(0,-1)+b.slice(1),delete a.merge),/^".*"$/.test(b)&&!/^".*\\n"$/.test(b)&&(a.merge=!0),a.push(b),a},[]).forEach(function(a){if(a.match(/^#\./))e.extractedComments.push(a.replace(/^#\.\s*/,""));else if(a.match(/^#/))e.comments.push(a.replace(/^#\s*/,""));else if(a.match(/^"/)){a=a.trim().replace(/^"/,"").replace(/\\n"$/,"");var b=a.split(/:/),c=b.shift().trim(),d=b.join(":").trim();e.headers[c]=d,e.headerOrder.push(c)}});for(var j=f.parsePluralForms(e.headers["Plural-Forms"]),k=j.nplurals,l=new f.Item({nplurals:k}),m=null,n=0,o=0,p=0;i.length>0;){var q=d(i.shift()),r=!1;if(q.match(/^#\~/)&&(q=d(q.substring(2)),r=!0),q.match(/^#:/))b(),l.references.push(d(q.replace(/^#:/,"")));else if(q.match(/^#,/)){b();for(var s=d(q.replace(/^#,/,"")).split(","),t=0;t0&&(p++,"msgstr"===m?l.msgstr[n]+=c(q):"msgid"===m?l.msgid+=c(q):"msgid_plural"===m?l.msgid_plural+=c(q):"msgctxt"===m&&(l.msgctxt+=c(q)));r&&o++}return b(),e},f.parsePluralForms=function(a){var b=(a||"").split(";").reduce(function(a,b){var c=b.trim(),d=c.indexOf("="),e=c.substring(0,d).trim(),f=c.substring(d+1).trim();return a[e]=f,a},{});return{nplurals:b.nplurals,plural:b.plural}},f.Item=function(a){var b=a&&a.nplurals;this.msgid="",this.msgctxt=null,this.references=[],this.msgid_plural=null,this.msgstr=[],this.comments=[],this.extractedComments=[],this.flags={},this.obsolete=!1;var c=Number(b);this.nplurals=isNaN(c)?2:c},f.Item.prototype.toString=function(){var a=[],b=this,c=function(a){return a=a.replace(/[\x07\b\t\v\f\r"\\]/g,function(a){switch(a){case"":return"\\a";case"\b":return"\\b";case"\t":return"\\t";case"\v":return"\\v";case"\f":return"\\f";case"\r":return"\\r";default:return"\\"+a}})},d=function(a,b,d){var e=[],f=b.split(/\n/),g="undefined"!=typeof d?"["+d+"]":"";return f.length>1?(e.push(a+g+' ""'),f.forEach(function(a){e.push('"'+c(a)+'"')})):e.push(a+g+' "'+c(b)+'"'),e},e=function(a,b,c){for(var e=d(a,b,c),f=1;f0&&a.push("#, "+f.join(","));var g=this.obsolete?"#~ ":"";return["msgctxt","msgid","msgid_plural","msgstr"].forEach(function(c){var f=b[c];if(null!=f){var h=!1;if(Array.isArray(f)&&(h=f.some(function(a){return a})),Array.isArray(f)&&f.length>1)f.forEach(function(b,d){var f=e(c,b,d);a=a.concat(g+f.join("\n"+g))});else if(b.msgid_plural&&"msgstr"===c&&!h)for(var i=0;i= 0); }); }); + + it('should keep the header order', function () { + var input = fs.readFileSync(__dirname + '/fixtures/big.po', 'utf8'); + var po = PO.parse(input); + var str = po.toString(); + + assertHasContiguousLines(str, [ + 'msgid ""', + 'msgstr ""', + '"Project-Id-Version: Link (6.x-2.9)\\n"', + '"POT-Creation-Date: 2011-12-31 23:39+0000\\n"', + '"PO-Revision-Date: 2013-12-17 14:21+0100\\n"', + '"Language-Team: French\\n"', + '"MIME-Version: 1.0\\n"', + '"Content-Type: text/plain; charset=UTF-8\\n"', + '"Content-Transfer-Encoding: 8bit\\n"', + '"Plural-Forms: nplurals=2; plural=(n > 1);\\n"', + '"Last-Translator: Ruben Vermeersch \\n"', + '"Language: fr\\n"', + '"X-Generator: Poedit 1.6.2\\n"', + ]); + }); });