Example #1
0
 public static function format($code)
 {
     // Haskell lexer goes here
     global $haskell_lang;
     $lex = new Lexer($haskell_lang, $code);
     $out = '';
     while (!$lex->end()) {
         list($type, $match) = $lex->next();
         $is_html = false;
         if ($type == '!!!notation') {
             list($type, $next_match) = $lex->next();
             $match = substr($match, 5, -5);
             $is_html = true;
         }
         if ($type == '') {
             $out .= htmlspecialchars($match);
         } elseif ($type == '!!!') {
             $out .= substr($match, 3, -3);
         } else {
             if (!$is_html) {
                 $match = htmlspecialchars($match);
             }
             if ($type == 'keyword') {
                 $match = preg_replace('@^__keyword__@', '', $match);
             }
             if ($type == 'varid' || $type == 'varop' || $type == 'comment') {
                 $match = preg_replace('@__([[:alnum:]_]+)@', '<sub>\\1</sub>', $match);
                 $match = preg_replace('@__[{]([^}]*)[}]@', '<sub>\\1</sub>', $match);
                 $match = preg_replace('@!!!(.*?)!!!@e', 'htmlspecialchars_decode("\\1")', $match);
             }
             $out .= "<span class=\"{$type}\">{$match}</span>";
         }
     }
     return $out;
 }