Beispiel #1
0
 static function filter($text = '')
 {
     $text = preg_replace_callback('/<codeblock([^>]*)>(.*)<\\/codeblock>/Usm', array(__CLASS__, '_escapeBlockContentCB'), $text);
     $tokens = gb_tokenize_html($text);
     $out = '';
     $depth = 0;
     $block = '';
     $tag = '';
     foreach ($tokens as $token) {
         if (substr($token, 0, 10) === '<codeblock') {
             $depth++;
             if ($depth === 1) {
                 # code block just started
                 $tag = $token;
                 continue;
             }
         } elseif (substr($token, 0, 12) === '</codeblock>') {
             $depth--;
             if ($depth < 0) {
                 gb::log(LOG_WARNING, 'stray </codeblock> messing up a code block');
                 $depth = 0;
             }
             if ($depth === 0) {
                 # code block ended
                 if ($block) {
                     $block = base64_decode($block);
                     $lang = '';
                     # find lang, if any
                     if (preg_match('/[\\s\\t ]+lang=("[^"]*"|\'[^\']*\')[\\s\\t ]*/', $tag, $m, PREG_OFFSET_CAPTURE)) {
                         $lang = trim($m[1][0], '"\'');
                         $end = substr($tag, $m[0][1] + strlen($m[0][0]));
                         $tag = substr($tag, 0, $m[0][1]) . ($end === '>' ? '>' : ' ' . $end);
                     }
                     # add CSS class name
                     $extra_cssclass = '';
                     if (preg_match('/class="([^"]+)"/', $tag, $m)) {
                         $extra_cssclass = $m[1];
                     }
                     # remove first and last line break if present
                     if ($block[0] === "\n") {
                         $block = substr($block, 1);
                     }
                     if ($block[strlen($block) - 1] === "\n") {
                         $block = substr($block, 0, -1);
                     }
                     # expand tabs
                     if (self::$conf['tabsize']) {
                         $block = strtr($block, array("\t" => str_repeat(' ', self::$conf['tabsize'])));
                     }
                     # append block to output
                     $out .= self::highlight($block, $lang, $extra_cssclass);
                     # clear block
                     $block = '';
                 }
                 continue;
             }
         }
         # in codeblock or not?
         if ($depth) {
             $block .= $token;
         } else {
             $out .= $token;
         }
     }
     return $out;
 }
Beispiel #2
0
function gb_uri_to_html_link($text)
{
    $tokens = gb_tokenize_html($text);
    $out = '';
    foreach ($tokens as $token) {
        if (isset($token[0]) && $token[0] !== '<' && $token[0] !== '[') {
            $words = preg_split('/([\\x00-\\x20\\x7f\\x60"\'<>\\\\\\]\\[\\^]+)/', $token, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
            $token = '';
            foreach ($words as $word) {
                $word = preg_replace('/^[a-zA-Z][a-zA-Z0-9\\.+-]*:.+$/', '<a href="$0">$0</a>', $word);
                $token .= $word;
            }
        }
        $out .= $token;
    }
    return $out;
}