Skip to content

Instantly share code, notes, and snippets.

@shikhalev
Last active December 22, 2015 02:49
Show Gist options
  • Save shikhalev/6406469 to your computer and use it in GitHub Desktop.
Save shikhalev/6406469 to your computer and use it in GitHub Desktop.
Немного дзынь-буддизму
var Tetrao = {
BLOCKS : [
'p', 'blockquote', 'div', 'address'
],
LINE_WIDTH : 80,
normalize : function (s) {
s = s.replace(new RegExp(' ', 'gm'), ' ');
s = s.replace(/\s+/gm, ' ');
s = s.replace(new RegExp('<br></', 'gm'), '</');
for (var i = 0, l = Tetrao.BLOCKS.length; i < l; i++) {
s = s.replace(new RegExp('\\s+</' + Tetrao.BLOCKS[i], 'gmi'),
'</' + Tetrao.BLOCKS[i]);
s = s.replace(new RegExp('<' + Tetrao.BLOCKS[i], 'gmi'),
'\n<' + Tetrao.BLOCKS[i]);
s = s.replace(new RegExp('</' + Tetrao.BLOCKS[i] + '>', 'gmi'),
'</' + Tetrao.BLOCKS[i] + '>\n');
}
s = s.replace(/^\s+/, '').replace(/\s+$/, '');
s = s.replace(/<(\w+)(\s+(class|style)=\".+?\")+\s*>/g, '<$1>');
var result = '';
var data = s.split(/\n/);
for (var i = 0, l = data.length; i < l; i++) {
var ss = data[i].replace(/^\s+/, '').replace(/\s+$/, '');
var dd = ss.split(/\s+/);
var rr = '';
for (var ii = 0, ll = dd.length; ii < ll; ii++) {
if (rr.length + dd[ii].length + 1 > Tetrao.LINE_WIDTH) {
result = result + rr + '\n';
rr = dd[ii];
} else {
if (rr === '') {
rr = dd[ii];
} else {
rr = rr + ' ' + dd[ii];
}
}
}
result = result + rr + '\n';
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment