JsAutoP is the JavaScript equivalent of the PHP function described at Matthew Mullenweg's website. The PHP function is a part of Wordpress code, which is used to convert new lines to HTML line breaks and paragraphs inside user entries. A similar function is also used in Drupal for the same purpose.
JsAutoP does the same text processing on the client side requiring no server requests. This makes it possible to implement very fast previewing of the content in text areas. The live preview feature in BUEditor is a good example that it reads the content of the text area and sends the text to JsAutoP function, then prints the returned HTML code inside a preview area.
function JsAutoP(s) {
if (!s || s.search(/n|r/) == -1) {
return s;
}
var X = function(x, a, b) {return x.replace(new RegExp(a, 'g'), b)};
var R = function(a, b) {return s = X(s, a, b)};
var blocks = '(table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select'
blocks += '|form|blockquote|address|math|style|script|object|input|param|p|h[1-6])';
s += '\n';
R('<br />\\s*<br />', '\n\n');
R('(<' + blocks + '[^>]*>)', '\n$1');
R('(</' + blocks + '>)', '$1\n\n');
R('\r\n|\r', '\n'); // cross-platform newlines
R('\n\n+', '\n\n');// take care of duplicates
R('\n?((.|\n)+?)\n\\s*\n', '<p>$1</p>\n');// make paragraphs
R('\n?((.|\n)+?)$', '<p>$1</p>\n');//including one at the end
R('<p>\\s*?</p>', '');// under certain strange conditions it could create a P of entirely whitespace
R('<p>(<div[^>]*>\\s*)', '$1<p>');
R('<p>([^<]+)\\s*?(</(div|address|form)[^>]*>)', '<p>$1</p>$2');
R('<p>\\s*(</?' + blocks + '[^>]*>)\\s*</p>', '$1');
R('<p>(<li.+?)</p>', '$1');// problem with nested lists
R('<p><blockquote([^>]*)>', '<blockquote$1><p>');
R('</blockquote></p>', '</p></blockquote>');
R('<p>\\s*(</?' + blocks + '[^>]*>)', '$1');
R('(</?' + blocks + '[^>]*>)\\s*</p>', '$1');
R('<(script|style)(.|\n)*?</\\1>', function(m0) {return X(m0, '\n', '<PNL>')});
R('(<br />)?\\s*\n', '<br />\n');
R('<PNL>', '\n');
R('(</?' + blocks + '[^>]*>)\\s*<br />', '$1');
R('<br />(\\s*</?(p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)', '$1');
if (s.indexOf('<pre') != -1) {
R('(<pre(.|\n)*?>)((.|\n)*?)</pre>', function(m0, m1, m2, m3) {
return X(m1, '\\\\([\'\"\\\\])', '$1') + X(X(X(m3, '<p>', '\n'), '</p>|<br />', ''), '\\\\([\'\"\\\\])', '$1') + '</pre>';
});
}
return R('\n</p>$', '</p>');
}