rewrap text

This commit is contained in:
s2
2015-04-18 00:20:42 +02:00
parent 133f073acd
commit daafe2cef4
2 changed files with 154 additions and 17 deletions

View File

@@ -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;
}
})();