Esempio n. 1
0
 public function add_line($line)
 {
     $len = strlen($line);
     // paragraph
     if ($line == '') {
         $this->state_switch('', '');
         // headings
     } else {
         if (preg_match('@^[-=]{4}\\s*([^-=].*)\\s*[-=]{4}\\s*$@', $line, $ma)) {
             $this->state_switch('', '');
             $this->html .= '<h4>' . $this->anchor_for($ma[1]) . $this->format_inline($ma[1]) . "</h4>\n";
         } elseif (preg_match('@^[-=]{3}\\s*([^-=].*)\\s*[-=]{3}\\s*$@', $line, $ma)) {
             $this->state_switch('', '');
             $this->html .= '<h3>' . $this->anchor_for($ma[1]) . $this->format_inline($ma[1]) . "</h3>\n";
         } elseif (preg_match('@^[-=]{2}\\s*([^-=].*)\\s*[-=]{2}\\s*$@', $line, $ma)) {
             $this->state_switch('', '');
             $this->html .= '<h2>' . $this->anchor_for($ma[1]) . $this->format_inline($ma[1]) . "</h2>\n";
             // latex
         } elseif (preg_match('@^(FORMULA): ?(.*)$@', $line, $ma)) {
             // fancy latex
             $this->state_switch('<p>', '</p>');
             $this->html .= "<img src='image/{$ma['2']}.png'>\n";
             $this->state = 'FORMULA';
         } elseif ($this->state == 'FORMULA' && preg_match("@^\\t@", $line)) {
             // ignore
             // lists etc.
         } elseif (preg_match('@^\\s*[*](.*)$@', $line, $ma)) {
             $this->state_switch('<ul>', '</ul>');
             $this->html .= '<li>' . $this->format_inline($ma[1]) . "</li>\n";
         } elseif (preg_match('@^\\s*[#](.*)$@', $line, $ma)) {
             $this->state_switch('<ol>', '</ol>');
             $this->html .= '<li>' . $this->format_inline($ma[1]) . "</li>\n";
         } elseif (preg_match('@^>\\s*--\\s*(IGNORE|HIDDEN)\\s*$@', $line)) {
             // ignore the following block
             $this->state_switch('', '');
             $this->state = 'IGNORE';
         } elseif (preg_match('@^\\]>\\s*--\\s*BLOCK[:]?\\s*(\\S+)-continue$@', $line, $ma)) {
             $this->state = '<pre class="' . $ma[1] . '-continue';
         } elseif (preg_match('@^\\]?>\\s*--\\s*BLOCK[:]?\\s*(\\S+)$@', $line, $ma)) {
             // language of the following block
             $this->state_switch('<pre class="' . htmlspecialchars($ma[1]) . '">', '</pre>');
         } elseif (preg_match('@^[>] ?(.*)$@', $line, $ma)) {
             // source code
             if ($this->state == 'IGNORE') {
                 // ignore this block, for literal Haskell files
                 return;
             }
             if (strpos($this->state, '<pre class="haskell-') === false) {
                 $this->state_switch('<pre class="haskell">', '</pre>');
             }
             if (trim($ma[1]) === '') {
                 $this->html .= "<div class='empty-line'></div>\n";
             } else {
                 $this->html .= WikiFormat::format_code($ma[1]) . "\n";
             }
         } elseif (preg_match('@^\\]> ?(.*)$@', $line, $ma)) {
             // source code, no formating
             if (strpos($this->state, '<pre class="haskell-') !== false) {
             } else {
                 $this->state_switch('<pre class="ghci">', '</pre>');
             }
             $this->html .= WikiFormat::format_code($ma[1]) . "\n";
         } elseif (preg_match('@^\\] ?(.*)$@', $line, $ma)) {
             // source code, no formating
             if ($this->state == '<pre class="ghci">') {
                 // stay in state
             } else {
                 $this->state_switch('<pre>', '</pre>');
             }
             $this->html .= htmlspecialchars($ma[1]) . "\n";
             // link lists
         } elseif (preg_match('@^(LINK|TITLE|SUBTITLE|DATE|DETAILS|ICON): ?(.*)$@', $line, $ma)) {
             $this->state_switch('<ul class="link-list">', '</ul>');
             if ($ma[1] == 'LINK') {
                 $this->render_pending_linklist();
             }
             if (isset($this->pending_linklist[$ma[1]])) {
                 $this->pending_linklist[$ma[1]] .= "\n<br>" . $ma[2];
             } else {
                 $this->pending_linklist[$ma[1]] = $ma[2];
             }
             // other
         } elseif (preg_match("@^\\s*</?(pre|ul|ol|li|div|blockquote|h2|h3|>)@", $line)) {
             $this->state_switch('', '');
             $line = preg_replace("@^<>@", "", $line);
             $this->html .= $this->format_inline($line) . "\n";
         } elseif (preg_match("@^//@", $line)) {
             // comment, ignore
         } elseif ($this->trusted && preg_match("@^INCLUDE: ?(.*)\$@", $line, $ma)) {
             // include a file
             $lines = file($ma[1]);
             while (!empty($lines) && preg_match("@:@", $lines[0])) {
                 array_shift($lines);
             }
             $this->add_lines($lines);
         } else {
             // body text
             $this->state_switch('<p>', '</p>');
             $this->html .= $this->format_inline($line) . "\n";
         }
     }
 }