rewrap text
This commit is contained in:
72
quote.js
72
quote.js
@@ -2,17 +2,16 @@
|
|||||||
|
|
||||||
var validquotechars = ['>'];
|
var validquotechars = ['>'];
|
||||||
|
|
||||||
|
|
||||||
var quoteline = function(line, maxlength, qc) {
|
var quoteline = function(line, maxlength, qc) {
|
||||||
if (line.length > maxlength) {
|
if (line.length > maxlength) {
|
||||||
var lastspace = line.lastIndexOf(' ', maxlength);
|
var lastspace = line.lastIndexOf(' ', maxlength - 1);
|
||||||
if (lastspace != -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
|
//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;
|
length = 75;
|
||||||
}
|
}
|
||||||
|
|
||||||
var quoted = text.split('\n').map(function(v, i) {
|
//cut the signature
|
||||||
//if this is already quoted text, return it as is
|
var signPosition = text.lastIndexOf('-- ');
|
||||||
if (validquotechars.indexOf(v.charAt(0)) > -1) {
|
if (signPosition > 0) {
|
||||||
return v;
|
text = text.substr(0, signPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
return quoteline(v, length, quotechar);
|
var quoted = 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 quotechar + v.trimRight();
|
||||||
|
}
|
||||||
|
|
||||||
|
return quoteline(v, length, quotechar + ' ');
|
||||||
}).join('\n');
|
}).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;
|
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') {
|
if (typeof(window) === 'undefined') {
|
||||||
module.exports = quote;
|
module.exports = e;
|
||||||
} else {
|
} else {
|
||||||
window.quote = quote;
|
window.quote = e;
|
||||||
}
|
}
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
97
tests.js
97
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) {
|
var test = function(name, what, ref, c) {
|
||||||
process.stdout.write(name);
|
process.stdout.write(name);
|
||||||
|
|
||||||
@@ -18,13 +21,15 @@ var test = function(name, what, ref, c) {
|
|||||||
process.stdout.write(' ✓');
|
process.stdout.write(' ✓');
|
||||||
} else {
|
} else {
|
||||||
process.stdout.write(' ⛝\n');
|
process.stdout.write(' ⛝\n');
|
||||||
process.stdout.write(' ----> expected | ' + ref + '\n');
|
process.stdout.write(' ----> expected | ' + ref.replace(/ /g, '·') + '\n');
|
||||||
process.stdout.write(' ----> got | ' + what + '\n');
|
process.stdout.write(' ----> got | ' + what.replace(/ /g, '·') + '\n');
|
||||||
}
|
}
|
||||||
process.stdout.write('\n');
|
process.stdout.write('\n');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//quote tests
|
||||||
test('simple short text',
|
test('simple short text',
|
||||||
quote('simple short text'),
|
quote('simple short text'),
|
||||||
'> simple short text'
|
'> simple short text'
|
||||||
@@ -32,7 +37,7 @@ test('simple short text',
|
|||||||
|
|
||||||
test('an already quoted line',
|
test('an already quoted line',
|
||||||
quote('> simple short text'),
|
quote('> simple short text'),
|
||||||
'> simple short text'
|
'>> simple short text'
|
||||||
);
|
);
|
||||||
|
|
||||||
test('a line longer than 75 chars with no spaces',
|
test('a line longer than 75 chars with no spaces',
|
||||||
@@ -57,6 +62,86 @@ test('three lines, one long line, two with a hard break in between',
|
|||||||
'> 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890\n' +
|
'> 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890\n' +
|
||||||
'> 1234567890 1234567890\n' +
|
'> 1234567890 1234567890\n' +
|
||||||
'> 1234567890 1234567890 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
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user