From daafe2cef408d12d12cf23340e164a2285c374e7 Mon Sep 17 00:00:00 2001 From: s2 Date: Sat, 18 Apr 2015 00:20:42 +0200 Subject: [PATCH] rewrap text --- quote.js | 70 +++++++++++++++++++++++++++++++++----- tests.js | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 154 insertions(+), 17 deletions(-) diff --git a/quote.js b/quote.js index f5d57da..5efb27f 100644 --- a/quote.js +++ b/quote.js @@ -2,17 +2,16 @@ var validquotechars = ['>']; - var quoteline = function(line, maxlength, qc) { if (line.length > maxlength) { - var lastspace = line.lastIndexOf(' ', maxlength); + var lastspace = line.lastIndexOf(' ', maxlength - 1); if (lastspace != -1) { - line = line.substr(0, lastspace) + ' \n' + quoteline(line.substr(lastspace + 1), maxlength, qc); + line = line.substr(0, lastspace) + '\n' + quoteline(line.substr(lastspace + 1), maxlength, qc); } } //this must be after the code before, because this inserts a space in the string - return qc + ' ' + line; + return qc + line.trimRight(); }; @@ -24,22 +23,75 @@ length = 75; } + //cut the signature + var signPosition = text.lastIndexOf('-- '); + if (signPosition > 0) { + text = text.substr(0, signPosition); + } + var quoted = text.split('\n').map(function(v, i) { - //if this is already quoted text, return it as is + //if this is already quoted text, return it as is, + //but trim the right spaces (thunderbird does this) if (validquotechars.indexOf(v.charAt(0)) > -1) { - return v; + return quotechar + v.trimRight(); } - return quoteline(v, length, quotechar); + return quoteline(v, length, quotechar + ' '); }).join('\n'); + //remove eventual '> ' from the end of the quoted text + var bsa = quoted.split('\n'); + var fixlastlines = function() { + if (bsa[bsa.length -1] === '> ') { + bsa.splice(-1,1); + quoted = bsa.join('\n') + '\n'; + fixlastlines(); + } + }; + fixlastlines(); + return quoted; }; + + var rewrapper = function(line, maxlength) { + if (line.length > maxlength) { + var lastspace = line.lastIndexOf(' ', maxlength - 1); + if (lastspace != -1) { + line = line.substr(0, lastspace) + ' \n' + rewrapper(line.substr(lastspace + 1), maxlength); + } + } + + //this must be after the code before, because this inserts a space in the string + return line; + }; + + var rewrap = function(text, length) { + if (typeof(length) === 'undefined') { + length = 75; + } + + var rewrapped = text.split('\n').map(function(v, i) { + //if this is already quoted text, return it as is, + //but trim the right spaces (thunderbird does this) + if (validquotechars.indexOf(v.charAt(0)) > -1) { + return v.trimRight(); + } + + return rewrapper(v, length); + }).join('\n'); + + return rewrapped; + }; + + var e = { + quote: quote, + rewrap: rewrap + }; if (typeof(window) === 'undefined') { - module.exports = quote; + module.exports = e; } else { - window.quote = quote; + window.quote = e; } })(); diff --git a/tests.js b/tests.js index 79b03ad..5af1a37 100644 --- a/tests.js +++ b/tests.js @@ -1,5 +1,8 @@ -var quote = require('./quote.js'); +var quote = require('./quote.js').quote; +var rewrap = require('./quote.js').rewrap; + +//my awesome test framework var test = function(name, what, ref, c) { process.stdout.write(name); @@ -18,13 +21,15 @@ var test = function(name, what, ref, c) { process.stdout.write(' ✓'); } else { process.stdout.write(' ⛝\n'); - process.stdout.write(' ----> expected | ' + ref + '\n'); - process.stdout.write(' ----> got | ' + what + '\n'); + process.stdout.write(' ----> expected | ' + ref.replace(/ /g, '·') + '\n'); + process.stdout.write(' ----> got | ' + what.replace(/ /g, '·') + '\n'); } process.stdout.write('\n'); }; + +//quote tests test('simple short text', quote('simple short text'), '> simple short text' @@ -32,7 +37,7 @@ test('simple short text', test('an already quoted line', quote('> simple short text'), - '> simple short text' + '>> simple short text' ); test('a line longer than 75 chars with no spaces', @@ -42,7 +47,7 @@ test('a line longer than 75 chars with no spaces', test('a line longer than 75 chars with spaces', quote('1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890', 75), - '> 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 \n> 1234567890 1234567890' + '> 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890\n> 1234567890 1234567890' ); test('three lines with a hard break', @@ -54,9 +59,89 @@ test('three lines, one long line, two with a hard break in between', quote('1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890\n' + '1234567890 1234567890 1234567890 1234567890\n' + '1234567890 1234567890 1234567890 1234567890\n'), - '> 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 \n' + + '> 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890\n' + '> 1234567890 1234567890\n' + '> 1234567890 1234567890 1234567890 1234567890\n' + - '> 1234567890 1234567890 1234567890 1234567890\n' + - '> ' + '> 1234567890 1234567890 1234567890 1234567890\n' ); + +test('three lines, one long line with a soft breake, two with a hard break in between', + quote('1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 \n1234567890 1234567890\n' + + '1234567890 1234567890 1234567890 1234567890\n' + + '1234567890 1234567890 1234567890 1234567890\n'), + '> 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890\n' + + '> 1234567890 1234567890\n' + + '> 1234567890 1234567890 1234567890 1234567890\n' + + '> 1234567890 1234567890 1234567890 1234567890\n' +); + + + +var mail = + "Il 15/04/15 12:39, Fabri / Joker197cinque ha scritto:\n" + + "> minchius_maximus wrote on 15/04/2015 :\n" + + ">\n" + + ">> (finito il main e due dlc su tre)\n" + + ">\n" + + "> Abbracciamocih.\n" + + ">\n" + + "\n" + + "sisi, avevo letto che anche tu ti eri fermato al secondo...\n" + + "ma, dopo aver fatto i primi due mi ero dedicato ad un sano e buon PVP \n" + + "nella covenant del drago... quante mazzate, ma divertente...\n" + + "\n" + + "sto scholar mi sta facendo calare l'hype...\n" + + "\n" + + "-- \n" + + "minchius_maximus"; + +var correctly_quoted = + "> Il 15/04/15 12:39, Fabri / Joker197cinque ha scritto:\n" + + ">> minchius_maximus wrote on 15/04/2015 :\n" + + ">>\n" + + ">>> (finito il main e due dlc su tre)\n" + + ">>\n" + + ">> Abbracciamocih.\n" + + ">>\n" + + "> \n" + + "> sisi, avevo letto che anche tu ti eri fermato al secondo...\n" + + "> ma, dopo aver fatto i primi due mi ero dedicato ad un sano e buon PVP\n" + + "> nella covenant del drago... quante mazzate, ma divertente...\n" + + "> \n" + + "> sto scholar mi sta facendo calare l'hype...\n"; + +test('a long real email with a signature', + quote(mail), + correctly_quoted +); + + + + +//rewrap tests +test('rewrap long text', + rewrap('1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 ', 75, ''), + '1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 \n1234567890 1234567890 ' + ); + +var mailrewrap = + "Il 15/04/15 12:39, Fabri / Joker197cinque ha scritto:\n" + + "> minchius_maximus wrote on 15/04/2015 :\n" + + ">\n" + + ">> (finito il main e due dlc su tre)\n" + + ">\n" + + "> Abbracciamocih.\n" + + ">\n" + + "\n" + + "sisi, avevo letto che anche tu ti eri fermato al secondo...\n" + + "ma, dopo aver fatto i primi due mi ero dedicato ad un sano e buon PVP nella covenant del drago... quante mazzate, ma divertente...\n" + + "\n" + + "sto scholar mi sta facendo calare l'hype...\n" + + "\n" + + "-- \n" + + "minchius_maximus"; + +test('rewrap long text with something quoted before', + rewrap(mailrewrap), + mail + );