From 7308a4132ada3fb5bfa89d6df154133fbd048781 Mon Sep 17 00:00:00 2001 From: Patrick Sullivan Date: Mon, 27 Apr 2015 14:12:35 -0700 Subject: [PATCH] Option for custom id transforms. A custom id mapping can be provided. Examples: // do not trim the whitespace from the string ends (the current default) 'idTransform': fn(s) { return s; } // apply html whitespace trimming rules 'idTransform': fn(s) { return s.replace(/\s+/g,' '); } --- lib/extract.js | 3 ++- test/extract.js | 14 ++++++++++++++ test/fixtures/strip-custom.html | 8 ++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/strip-custom.html diff --git a/lib/extract.js b/lib/extract.js index fa41af6..acab5c5 100644 --- a/lib/extract.js +++ b/lib/extract.js @@ -64,6 +64,7 @@ var Extractor = (function () { this.options = _.extend({ startDelim: '{{', endDelim: '}}', + idTransform: function (s) { return s.trim(); }, markerName: 'gettext', markerNames: [], lineNumbers: true, @@ -97,7 +98,7 @@ var Extractor = (function () { reference = { file: reference }; } - string = string.trim(); + string = this.options.idTransform(string); if (string.length === 0) { return; diff --git a/test/extract.js b/test/extract.js index 52ce38a..3fa10d9 100644 --- a/test/extract.js +++ b/test/extract.js @@ -109,6 +109,20 @@ describe('Extract', function () { assert.deepEqual(catalog.items[0].references, ['test/fixtures/ngif.html:3']); }); + it('Can customize trimming whitespace around strings', function () { + var files = [ + 'test/fixtures/strip-custom.html' + ]; + var catalog = testExtract(files, { + idTransform: function (s) { return s.replace(/\s+/g,' '); } + }); + + assert.equal(catalog.items.length, 1); + assert.equal(catalog.items[0].msgid, ' Hello strip custom '); + assert.equal(catalog.items[0].msgstr, ''); + assert.deepEqual(catalog.items[0].references, ['test/fixtures/strip-custom.html:3']); + }); + it('Can customize delimiters', function () { var files = [ 'test/fixtures/delim.html' diff --git a/test/fixtures/strip-custom.html b/test/fixtures/strip-custom.html new file mode 100644 index 0000000..6e1a755 --- /dev/null +++ b/test/fixtures/strip-custom.html @@ -0,0 +1,8 @@ + + +

Hello + strip custom +

+ + +