function _filter_autop in Drupal 4
Same name and namespace in other branches
- 8 core/modules/filter/filter.module \_filter_autop()
- 5 modules/filter/filter.module \_filter_autop()
- 6 modules/filter/filter.module \_filter_autop()
- 7 modules/filter/filter.module \_filter_autop()
- 9 core/modules/filter/filter.module \_filter_autop()
Convert line breaks into <p> and <br> in an intelligent fashion. Based on: http://photomatt.net/scripts/autop
1 call to _filter_autop()
- filter_filter in modules/
filter.module - Implementation of hook_filter(). Contains a basic set of essential filters.
File
- modules/
filter.module, line 1047 - Framework for handling filtering of content.
Code
function _filter_autop($text) {
// All block level tags
$block = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|p|h[1-6])';
// Split at <pre>, <script>, <style> and </pre>, </script>, </style> tags.
// We don't apply any processing to the contents of these tags to avoid messing
// up code. We look for matched pairs and allow basic nesting. For example:
// "processed <pre> ignored <script> ignored </script> ignored </pre> processed"
$chunks = preg_split('@(</?(?:pre|script|style)[^>]*>)@i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
// Note: PHP ensures the array consists of alternating delimiters and literals
// and begins and ends with a literal (inserting NULL as required).
$ignore = false;
$ignoretag = '';
$output = '';
foreach ($chunks as $i => $chunk) {
if ($i % 2) {
// Opening or closing tag?
$open = $chunk[1] != '/';
list($tag) = split('[ >]', substr($chunk, 2 - $open), 2);
if (!$ignore) {
if ($open) {
$ignore = true;
$ignoretag = $tag;
}
}
else {
if (!$open && $ignoretag == $tag) {
$ignore = false;
$ignoretag = '';
}
}
}
else {
if (!$ignore) {
$chunk = preg_replace('|\\n*$|', '', $chunk) . "\n\n";
// just to make things a little easier, pad the end
$chunk = preg_replace('|<br />\\s*<br />|', "\n\n", $chunk);
$chunk = preg_replace('!(<' . $block . '[^>]*>)!', "\n\$1", $chunk);
// Space things out a little
$chunk = preg_replace('!(</' . $block . '>)!', "\$1\n\n", $chunk);
// Space things out a little
$chunk = preg_replace("/\n\n+/", "\n\n", $chunk);
// take care of duplicates
$chunk = preg_replace('/\\n?(.+?)(?:\\n\\s*\\n|\\z)/s', "<p>\$1</p>\n", $chunk);
// make paragraphs, including one at the end
$chunk = preg_replace('|<p>\\s*</p>\\n|', '', $chunk);
// under certain strange conditions it could create a P of entirely whitespace
$chunk = preg_replace("|<p>(<li.+?)</p>|", "\$1", $chunk);
// problem with nested lists
$chunk = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote\$1><p>", $chunk);
$chunk = str_replace('</blockquote></p>', '</p></blockquote>', $chunk);
$chunk = preg_replace('!<p>\\s*(</?' . $block . '[^>]*>)!', "\$1", $chunk);
$chunk = preg_replace('!(</?' . $block . '[^>]*>)\\s*</p>!', "\$1", $chunk);
$chunk = preg_replace('|(?<!<br />)\\s*\\n|', "<br />\n", $chunk);
// make line breaks
$chunk = preg_replace('!(</?' . $block . '[^>]*>)\\s*<br />!', "\$1", $chunk);
$chunk = preg_replace('!<br />(\\s*</?(?:p|li|div|th|pre|td|ul|ol)>)!', '$1', $chunk);
$chunk = preg_replace('/&([^#])(?![A-Za-z0-9]{1,8};)/', '&$1', $chunk);
}
}
$output .= $chunk;
}
return $output;
}