Index: bbcode-filter.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/bbcode/bbcode-filter.inc,v
retrieving revision 1.47
diff -u -r1.47 bbcode-filter.inc
--- bbcode-filter.inc 13 Jun 2006 19:33:56 -0000 1.47
+++ bbcode-filter.inc 22 Jun 2006 21:51:28 -0000
@@ -9,20 +9,28 @@
// Encode all script tags to prevent XSS html injection attacks
$body = preg_replace(array('#]*)>#i'), array('<script\\1>', '</script\\1>'), $body);
- // Apply line and paragraph breaks
+ // Find all [code] tags and check if they contain a newline. If we find a newline,
+ // that [code] should be rendered as a block, otherwise it will still be inline
$mode = variable_get("bbcode_paragraph_breaks_$format", 2);
- if ($mode) {
-
- // Step 1: strip preformatted code blocks from text
- $pre = array(); $i = 0;
- while ($pre_str = stristr($body, '[code]')) {
- $pre_str = substr($pre_str, 0, strpos(strtolower($pre_str), '[/code]')+7);
- $body = str_replace($pre_str, "***pRe_sTrInG$i***", $body);
- $pre[$i] = str_replace(array('<', '>'), array('<', '>'), $pre_str);
- $i++;
+ $pre = array(); $i = 0;
+ if (preg_match_all('#\[code(?::\w+)?\](.*?)\[/code(?::\w+)?\]#si', $body, $code_tags, PREG_SET_ORDER)) {
+ foreach ($code_tags as $code_tag) {
+ $code_tag[1] = str_replace(array('<', '>'), array('<', '>'), $code_tag[1]);
+ if (strpos($code_tag[1], "\n") === FALSE)
+ $body = str_replace($code_tag[0], ''. $code_tag[1] .'', $body);
+ elseif ($mode) {
+ // Strip preformatted code blocks from text during line break processing, replaced below
+ $body = str_replace($code_tag[0], "***pRe_sTrInG$i***", $body);
+ $pre[$i++] = '
'. $code_tag[1] .''; + } + else + $body = str_replace($code_tag[0], '
'. $code_tag[1] .'', $body); } - // Step 2: Apply line and paragraph breaks (skipping preformatted code) + // Apply line and paragraph breaks + if ($mode) { + + // Apply line and paragraph breaks (skipping preformatted code) if ($mode == 1) // Line breaks only $body = nl2br($body); if ($mode == 2) { // Line and paragraph breaks @@ -41,10 +49,9 @@ $body = implode("\n\n", $parts); } - // Step 3: Reinsert preformatted code blocks - $cp = count($pre)-1; - for ($i=0; $i <= $cp; $i++) - $body = str_replace("***pRe_sTrInG$i***", '
'.substr($pre[$i],6,-7).'', $body); + // Reinsert preformatted code blocks + foreach ($pre as $i => $code_tag) + $body = str_replace("***pRe_sTrInG$i***", $code_tag, $body); } // Add closing tags to prevent users from disruping your site's HTML @@ -169,20 +176,6 @@ $body); } - // Find all [code] tags and check if they contain a newline. If we find a newline, - // that [code] should be rendered as a block, otherwise it will still be inline - if (preg_match_all('#\[code(?::\w+)?\](.*?)\[/code(?::\w+)?\]#si', $body, $code_tags, PREG_SET_ORDER)) { - foreach ($code_tags as $code_tag) { - if (strpos($code_tag[1], "\n") !== FALSE) { - $code_tag[1] = str_replace(array("\n ", ' ', "\n"), array('
%s'; - } - else - $code_block = '
%s';
- $body = str_replace($code_tag[0], sprintf($code_block, $code_tag[1]), $body);
- }
- }
-
// Turns web and e-mail addresses into clickable links
if (variable_get("bbcode_make_links_$format", 1)) {