plugins/GeSHi.php
changeset 4 97d85c22ced7
parent 2 9e3258dfae15
child 5 bfe00fc686f2
equal deleted inserted replaced
3:f3e2bbbd2155 4:97d85c22ced7
    45 $plugins->attachHook('render_sanitize_pre', 'geshi_strip_code($text, $geshi_code_blocks, $random_id);');
    45 $plugins->attachHook('render_sanitize_pre', 'geshi_strip_code($text, $geshi_code_blocks, $random_id);');
    46 $plugins->attachHook('render_sanitize_post', 'geshi_restore_code($text, $geshi_code_blocks, $random_id);');
    46 $plugins->attachHook('render_sanitize_post', 'geshi_restore_code($text, $geshi_code_blocks, $random_id);');
    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
       
    51   $nw = preg_match_all('#<nowiki>(.*?)<\/nowiki>#is', $text, $nowiki);
       
    52     
       
    53   for ( $i = 0; $i < $nw; $i++ )
       
    54   {
       
    55     $text = str_replace('<nowiki>'.$nowiki[1][$i].'</nowiki>', '{NOWIKI:'.$random_id.':'.$i.'}', $text);
       
    56   }
       
    57   
    50   global $geshi_supported_formats;
    58   global $geshi_supported_formats;
    51   $codeblocks = array();
    59   $codeblocks = array();
    52   $sf = '(' . implode('|', $geshi_supported_formats) . ')';
    60   $sf = '(' . implode('|', $geshi_supported_formats) . ')';
    53   $regexp = '/<(code|source) (?:type|lang)="?' . $sf . '"?>(.*?)<\/\\1>/s';
    61   $regexp = '/<(code|source) (?:type|lang)="?' . $sf . '"?>(.*?)<\/\\1>/s';
    54   preg_match_all($regexp, $text, $matches);
    62   preg_match_all($regexp, $text, $matches);
    65         'match' => $match,
    73         'match' => $match,
    66         'lang' => $matches[2][$i],
    74         'lang' => $matches[2][$i],
    67         'code' => $matches[3][$i]
    75         'code' => $matches[3][$i]
    68       );
    76       );
    69     $text = str_replace_once($match, "{GESHI_BLOCK:$i:$random_id}", $text);
    77     $text = str_replace_once($match, "{GESHI_BLOCK:$i:$random_id}", $text);
       
    78   }
       
    79   
       
    80   // Reinsert <nowiki> sections
       
    81   for ( $i = 0; $i < $nw; $i++ )
       
    82   {
       
    83     $text = str_replace('{NOWIKI:'.$random_id.':'.$i.'}', $nowiki[1][$i], $text);
    70   }
    84   }
    71 }
    85 }
    72 
    86 
    73 function geshi_restore_code(&$text, &$codeblocks, $random_id)
    87 function geshi_restore_code(&$text, &$codeblocks, $random_id)
    74 {
    88 {