plugins/GeSHi.php
changeset 6 aeecc46f11c6
parent 5 bfe00fc686f2
child 7 86f0dc4ca29d
equal deleted inserted replaced
5:bfe00fc686f2 6:aeecc46f11c6
    47 
    47 
    48 function geshi_strip_code(&$text, &$codeblocks, $random_id)
    48 function geshi_strip_code(&$text, &$codeblocks, $random_id)
    49 {
    49 {
    50   // remove nowiki
    50   // remove nowiki
    51   $nw = preg_match_all('#<nowiki>(.*?)<\/nowiki>#is', $text, $nowiki);
    51   $nw = preg_match_all('#<nowiki>(.*?)<\/nowiki>#is', $text, $nowiki);
    52     
    52   
    53   for ( $i = 0; $i < $nw; $i++ )
    53   for ( $i = 0; $i < $nw; $i++ )
    54   {
    54   {
    55     $text = str_replace('<nowiki>'.$nowiki[1][$i].'</nowiki>', '{NOWIKI:'.$random_id.':'.$i.'}', $text);
    55     $text = str_replace($nowiki[0][$i], '{NOWIKI:'.$random_id.':'.$i.'}', $text);
    56   }
    56   }
    57   
    57   
    58   global $geshi_supported_formats;
    58   global $geshi_supported_formats;
    59   $codeblocks = array();
    59   $codeblocks = array();
    60   $sf = '(' . implode('|', $geshi_supported_formats) . ')';
    60   $sf = '(' . implode('|', $geshi_supported_formats) . ')';
    61   $regexp = '/<(code|source) (?:type|lang)="?' . $sf . '"?>(.*?)<\/\\1>/s';
    61   $regexp = '/<(code|source) (?:type|lang)="?' . $sf . '"?>(.*?)<\/\\1>/s';
    62   preg_match_all($regexp, $text, $matches);
    62   $count = preg_match_all($regexp, $text, $matches);
    63   
    63   
    64   // for debug
    64   // for debug
    65   /*
    65   /*
    66   if ( strstr($text, '<code type') )
    66   if ( strstr($text, '<code type') )
    67     die('processing codes: <pre>' . htmlspecialchars(print_r($matches, true)) . '</pre><pre>' . htmlspecialchars($text) . '</pre>' . htmlspecialchars($regexp));
    67     die('processing codes: <pre>' . htmlspecialchars(print_r($matches, true)) . '</pre><pre>' . htmlspecialchars($text) . '</pre>' . htmlspecialchars($regexp));
    68   */
    68   */
    69   
    69   
    70   foreach ( $matches[0] as $i => $match )
    70   if ( $count > 0 )
    71   {
    71     foreach ( $matches[0] as $i => $match )
    72     $codeblocks[$i] = array(
    72     {
    73         'match' => $match,
    73       $codeblocks[$i] = array(
    74         'lang' => $matches[2][$i],
    74           'match' => $match,
    75         'code' => $matches[3][$i]
    75           'lang' => $matches[2][$i],
    76       );
    76           'code' => $matches[3][$i]
    77     $text = str_replace_once($match, "{GESHI_BLOCK:$i:$random_id}", $text);
    77         );
    78   }
    78       $text = str_replace_once($match, "{GESHI_BLOCK:$i:$random_id}", $text);
       
    79     }
    79   
    80   
    80   // Reinsert <nowiki> sections
    81   // restore nowiki
    81   for ( $i = 0; $i < $nw; $i++ )
    82   for ( $i = 0; $i < $nw; $i++ )
    82   {
    83   {
    83     $text = str_replace('{NOWIKI:'.$random_id.':'.$i.'}', $nowiki[1][$i], $text);
    84     $text = str_replace('{NOWIKI:'.$random_id.':'.$i.'}', $nowiki[0][$i], $text);
    84   }
    85   }
    85 }
    86 }
    86 
    87 
    87 function geshi_restore_code(&$text, &$codeblocks, $random_id)
    88 function geshi_restore_code(&$text, &$codeblocks, $random_id)
    88 {
    89 {