Go to:
Gentoo Home
Documentation
Forums
Lists
Bugs
Planet
Store
Wiki
Get Gentoo!
Gentoo's Bugzilla – Attachment 81307 Details for
Bug 106124
Modifying the [code] tags to allow eg. [code="/etc/make.conf"]...[/code]
Home
|
New
–
[Ex]
|
Browse
|
Search
|
Privacy Policy
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
bbcode.php.patch
bbcode.php.patch (text/plain), 9.38 KB, created by
Jonathan Coome (RETIRED)
on 2006-03-04 09:45:05 UTC
(
hide
)
Description:
bbcode.php.patch
Filename:
MIME Type:
Creator:
Jonathan Coome (RETIRED)
Created:
2006-03-04 09:45:05 UTC
Size:
9.38 KB
patch
obsolete
>--- bbcode.php.fgo_orig 2006-03-04 17:05:23.885182064 +0000 >+++ bbcode.php 2006-03-04 17:07:50.173942792 +0000 >@@ -91,6 +91,7 @@ > $bbcode_tpl['quote_username_open'] = str_replace('{USERNAME}', '\\1', $bbcode_tpl['quote_username_open']); > > $bbcode_tpl['code_open'] = str_replace('{L_CODE}', $lang['Code'], $bbcode_tpl['code_open']); >+ $bbcode_tpl['code_file_open'] = str_replace('{CODE_FILE}', '\\1', $bbcode_tpl['code_file_open']); > > $bbcode_tpl['img'] = str_replace('{URL}', '\\1', $bbcode_tpl['img']); > >@@ -411,7 +412,7 @@ > $text = " " . $text; > > // [CODE] and [/CODE] for posting code (HTML, PHP, C etc etc) in your posts. >- $text = bbencode_first_pass_pda($text, $uid, '[code]', '[/code]', '', true, ''); >+ $text = bbencode_first_pass_code($text, $uid); > > // [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff. > $text = bbencode_first_pass_pda($text, $uid, '[quote]', '[/quote]', '', false, ''); >@@ -714,6 +715,197 @@ > } // bbencode_first_pass_pda() > > /** >+ * $text - The text to operate on. >+ * $uid - The UID to add to matching tags. >+ * >+ * NOTES: - this function assumes the first character of $text is a space. >+ * - every opening tag and closing tag must be of the [...] format. >+ */ >+function bbencode_first_pass_code($text, $uid) >+{ >+ $uid_length = strlen($uid); >+ $close_tag = '[/code]'; >+ $close_tag_length = 7; // strlen('[/code]'); >+ >+ // start_tag_index = array() >+ // 1 = '[code]' >+ // 2 = '[code="foo"]' >+ >+ $stack = array(); >+ >+ // Start at the 2nd char of the string, looking for opening tags. >+ $curr_pos = 1; >+ while ($curr_pos && ($curr_pos < strlen($text))) >+ { >+ $curr_pos = strpos($text, "[", $curr_pos); >+ >+ // If not found, $curr_pos will be 0, and the loop will end. >+ if ($curr_pos) >+ { >+ // We found a [. It starts at $curr_pos. >+ // check if it's a starting or ending tag. >+ $found_start = false; >+ $which_start_tag = ""; >+ $start_tag_index = -1; >+ >+ // First check for [code] >+ $possible_start = substr($text, $curr_pos, strpos($text, ']', $curr_pos + 1) - $curr_pos + 1); >+ if (0 == strcasecmp('[code]', $possible_start)) >+ { >+ $found_start = true; >+ $which_start_tag = '[code]'; >+ $start_tag_index = 1; >+ } >+ >+ // Now check for [code="foo"] >+ if (! $found_start) >+ { >+ $possible_start = substr($text, $curr_pos, strpos($text, ']', $curr_pos + 1) - $curr_pos + 1); >+ >+ // >+ // We're going to try and catch usernames with "[' characters. >+ // >+ if( preg_match('#\[code=\\\"#si', $possible_start, $match) && !preg_match('#\[code=\\\"(.*?)\\\"\]#si', $possible_start) ) >+ { >+ // OK we are in a code tag that probably contains a ] bracket. >+ // Grab a bit more of the string to hopefully get all of it.. >+ if ($close_pos = strpos($text, '"]', $curr_pos + 14)) >+ { >+ if (strpos(substr($text, $curr_pos + 14, $close_pos - ($curr_pos + 14)), '[code') === false) >+ { >+ $possible_start = substr($text, $curr_pos, $close_pos - $curr_pos + 7); >+ } >+ } >+ } >+ >+ $match_result = array(); >+ if (preg_match('/\[code=\\\\"(.*?)\\\\"\]/is', $possible_start, $match_result)) >+ { >+ $found_start = true; >+ $which_start_tag = $match_result[0]; >+ $start_tag_index = 2; >+ } >+ } >+ >+ if ($found_start) >+ { >+ // We have an opening tag. >+ // Push its position, the text we matched, and its index in the open_tag array on to the stack, and then keep going to the right. >+ $match = array("pos" => $curr_pos, "tag" => $which_start_tag, "index" => $start_tag_index); >+ array_push($stack, $match); >+ // >+ // Rather than just increment $curr_pos >+ // Set it to the ending of the tag we just found >+ // Keeps error in nested tag from breaking out >+ // of table structure.. >+ // >+ $curr_pos += strlen($possible_start); >+ } >+ else >+ { >+ // check for a closing tag.. >+ $possible_end = substr($text, $curr_pos, $close_tag_length); >+ if (0 == strcasecmp($close_tag, $possible_end)) >+ { >+ // We have an ending tag. >+ // Check if we've already found a matching starting tag. >+ if (sizeof($stack) > 0) >+ { >+ // There exists a starting tag. >+ $curr_nesting_depth = sizeof($stack); >+ // We need to do 2 replacements now. >+ $match = array_pop($stack); >+ $start_index = $match['pos']; >+ $start_tag = $match['tag']; >+ $start_length = strlen($start_tag); >+ $start_tag_index = $match['index']; >+ >+ // everything before the opening tag. >+ $before_start_tag = substr($text, 0, $start_index); >+ >+ // everything after the opening tag, but before the closing tag. >+ $between_tags = substr($text, $start_index + $start_length, $curr_pos - $start_index - $start_length); >+ if ($curr_nesting_depth == 1) >+ { >+ $code_entities_match = array('#<#', '#>#', '#"#', '#:#', '#\[#', '#\]#', '#\(#', '#\)#', '#\{#', '#\}#'); >+ $code_entities_replace = array('<', '>', '"', ':', '[', ']', '(', ')', '{', '}'); >+ $between_tags = preg_replace($code_entities_match, $code_entities_replace, $between_tags); >+ } >+ >+ // everything after the closing tag. >+ $after_end_tag = substr($text, $curr_pos + $close_tag_length); >+ >+ // Start the replacements >+ if ($curr_nesting_depth == 1) >+ { >+ // [code="foo"] replacement >+ if ($start_tag_index == 2) >+ { >+ // Yes, this is slightly weird, but I can't >+ // make preg_replace work for the life of me... >+ $matches = array(); >+ preg_match('/\[code\=\\\\"(.*?)\\\\"\]/is', $start_tag, $matches); >+ $start_tag = '[code:1:'.$uid.'=\"'.$matches[1].'\"]'; >+ } >+ else if ($start_tag_index == 1) >+ { >+ $start_tag = '[code:1:'.$uid.']'; >+ } >+ $end_tag = '[/code:1:'.$uid.']'; >+ } >+ else >+ { >+ // [code="foo"] replacement >+ if ($start_tag_index == 2) >+ { >+ $matches = array(); >+ preg_match('/\[code\=\\\\"(.*?)\\\\"\]/is', $start_tag, $matches); >+ $start_tag = '[code=\"'.$matches[1].'\"]'; >+ } >+ else >+ { >+ $start_tag = '[code]'; >+ } >+ $end_tag = '[/code]'; >+ } >+ >+ $text = $before_start_tag . $start_tag . $between_tags . $end_tag . $after_end_tag; >+ >+ // Now.. we've screwed up the indices by changing the length of the string. >+ // So, if there's anything in the stack, we want to resume searching just after it. >+ // otherwise, we go back to the start. >+ if (sizeof($stack) > 0) >+ { >+ $match = array_pop($stack); >+ $curr_pos = $match['pos']; >+// bbcode_array_push($stack, $match); >+// ++$curr_pos; >+ } >+ else >+ { >+ $curr_pos = 1; >+ } >+ } >+ else >+ { >+ // No matching start tag found. Increment pos, keep going. >+ ++$curr_pos; >+ } >+ } >+ else >+ { >+ // No starting tag or ending tag.. Increment pos, keep looping., >+ ++$curr_pos; >+ } >+ } >+ } >+ } // while >+ >+ return $text; >+ >+} // bbencode_first_pass_code() >+ >+/** > * Does second-pass bbencoding of the [code] tags. This includes > * running htmlspecialchars() over the text contained between > * any pair of [code] tags that are at the first level of >@@ -725,9 +917,12 @@ > { > global $lang; > >+ $code_start_file_html = $bbcode_tpl['code_file_open']; > $code_start_html = $bbcode_tpl['code_open']; > $code_end_html = $bbcode_tpl['code_close']; > >+ // Start off with [code] tags. >+ > // First, do all the 1st-level matches. These need an htmlspecialchars() run, > // so they have to be handled differently. > $match_count = preg_match_all("#\[code:1:$uid\](.*?)\[/code:1:$uid\]#si", $text, $matches); >@@ -758,9 +953,40 @@ > } > > // Now, do all the non-first-level matches. These are simple. >+ // WTF? There aren't any non-first-level matches - the brackets have been >+ // replaced by html entities already... > $text = str_replace("[code:$uid]", $code_start_html, $text); > $text = str_replace("[/code:$uid]", $code_end_html, $text); > >+ // Now do [code="foo"] tags >+ $match_count = preg_match_all('#\[code:1:'.$uid.'="(.*?)"\](.*?)\[/code:1:'.$uid.'\]#si', $text, $matches); >+ >+ for ($i = 0; $i < $match_count; $i++) >+ { >+ $before_replace = $matches[2][$i]; >+ $after_replace = $matches[2][$i]; >+ >+ // Replace 2 spaces with " " so non-tabbed code indents without making huge long lines. >+ $after_replace = str_replace(" ", " ", $after_replace); >+ // now Replace 2 spaces with " " to catch odd #s of spaces. >+ $after_replace = str_replace(" ", " ", $after_replace); >+ >+ // Replace tabs with " " so tabbed code indents sorta right without making huge long lines. >+ $after_replace = str_replace("\t", " ", $after_replace); >+ >+ // now Replace space occurring at the beginning of a line >+ $after_replace = preg_replace("/^ {1}/m", ' ', $after_replace); >+ >+ $str_to_match = '[code:1:'.$uid.'="'.$matches[1][$i].'"]' . $before_replace . "[/code:1:$uid]"; >+ >+ $code_start_file_html = str_replace('\\1', $matches[1][$i], $code_start_file_html); >+ $replacement = $code_start_file_html; >+ $replacement .= $after_replace; >+ $replacement .= $code_end_html; >+ >+ $text = str_replace($str_to_match, $replacement, $text); >+ } >+ > return $text; > > } // bbencode_second_pass_code() >@@ -1005,4 +1231,4 @@ > return ( strlen($a['code']) > strlen($b['code']) ) ? -1 : 1; > } > >-?> >\ No newline at end of file >+?>
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 106124
: 81307 |
81308