This mod will convert single enter into <br />, double into a <p> ... </p> set.
It will store xhtml in the db for display in the browser.
In the editor, none of these tags will be returned for view, but will be stripped out.
Any extra markup (h1-6, span, div, etc) will not be stripped at editor view.
NOTE: the original is not my code (I'm not that clever). Please leave the credit statement in tact.
Functions affected.
processing()
form_articles()
js()
plus 2 additional functions.
processing()
locate
$text = clean($_POST['text']);
and make like
$text = clean(convertTags($_POST['text']));// converts various newlines (\n) to either <p> or <br />
form_articles()
in the text area for entry
this line
echo html_input('textarea', 'text', 'txt', $frm_text, l('text'), '', '', '', '', '', '2', '100', '', '', '');
to be
echo html_input('textarea', 'text', 'txt', remove_tags($frm_text), l('text'), '', '', '', '', '', '2', '100', '', '', '');
js() - replace the // generate preview function updatePreview() with
// generate preview
function updatePreview() {
if (document.getElementById('txt')) {
var body = document.getElementById('txt').value;
body = body.replace(/(\r\n\r\n|\r\r|\n\n)/g, "<p>");
body = body.replace(/(\r\n|\r|\n)/g, "<br />");
document.getElementById('preview').innerHTML = body;
}
}
2 new functions.
//Editor functions
// convertTags converts \n etc to either <p> or <br /> without
// breaking other html formatting, ready for writing to db.
function convertTags($pee, $br = 1) {// CODE COURTEOUSLY BY http://ma.tt/scripts/autop/
//locates newlines, and places in <p> ... </p> and <br /> tags
$pee = $pee . "\n"; // just to make things a little easier, pad the end
$pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
// Space things out a little
$allblocks = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr)';
$pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
$pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
$pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
if ( strpos($pee, '<object') !== false ) {
$pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
$pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
}
$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
$pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end
$pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
$pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee);
$pee = preg_replace( '|<p>|', "$1<p>", $pee );
$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
$pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
$pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
if ($br) {
$pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', create_function('$matches', 'return str_replace("\n", "<WPPreserveNewline />", $matches[0]);'), $pee);
$pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
$pee = str_replace('<WPPreserveNewline />', "\n", $pee);
}
$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
$pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
if (strpos($pee, '<pre') !== false)
$pee = preg_replace_callback('!(<pre.*?>)(.*?)</pre>!is', 'clean_pre', $pee );
$pee = preg_replace( "|\n</p>$|", '</p>', $pee );
return $pee;
}
// remove_tags removes P and BR tags written to db, and replaces them with newline,
// for display in editor.
function remove_tags($text){//removes P and BR tags written to db, and replaces them.
$text = str_replace("<p>","",str_replace('<br />',"",str_replace("</p>","\r\n",str_replace('<p></p>',"",$text))));
return $text;
}