public function AddRegexMatchesBasic(CodePattern $pattern, $text)
 {
     $text = preg_replace_callback($pattern->GetPatterns(), function ($match) use($pattern) {
         $classes = $pattern->GetClasses();
         $handlers = $pattern->GetHandlers();
         $output = '';
         // reassemble components
         foreach ($pattern->GetComponents() as $name) {
             if (isset($match[$name])) {
                 $region = $match[$name];
                 // check Handlers
                 if (isset($handlers[$name])) {
                     $handler_set = $handlers[$name];
                     if (!is_array($handler_set)) {
                         $handler_set = [$handler_set];
                     }
                     // probe Handlers
                     foreach ($handler_set as $handler) {
                         if (is_string($handler)) {
                             $region_new = SyntaxHighlighter::Execute($region, [$handler => ['__probe' => true]]);
                         } else {
                             //$region_new = $this->AddRegexMatchesBasic($handler, $region);
                             //$region_new = $this->ProcessPatterns([$handler], $region);
                             $rm = new self();
                             $region_new = $rm->ProcessPatterns([$handler], $region);
                         }
                         // only use the first successful probe
                         if ($region_new !== $region) {
                             $region = $region_new;
                             break;
                         }
                     }
                 }
                 if (isset($classes[$name])) {
                     $region = $this->WrapCodeClass($region, $classes[$name]);
                 }
                 $output .= $region;
             }
         }
         if (isset($classes[NULL])) {
             $output = $this->WrapCodeClass($output, $classes[NULL]);
         }
         return $this->AddReplacedValue($output);
     }, $text);
     return $text;
 }
Exemple #2
0
    public static function Format($code, $handlers)
    {
        $showGutter = false;
        $code = htmlentities($code, ENT_NOQUOTES);
        //$code = str_replace("\t", '	', $code);
        //$code = str_replace(" ", ' ', $code);
        if ($handlers) {
            $code = SyntaxHighlighter::Execute($code, $handlers);
        }
        $code_lines = explode("\n", $code);
        $code_line_numbers = [];
        foreach ($code_lines as $i => &$line) {
            if (empty($line)) {
                $line = '​';
            }
            //$line = sprintf('<div>%1$s</div>', $line);
            $line = sprintf('<div><code>%1$s</code></div>', $line);
            //$line = sprintf('%1$s', $line);
            $code_line_numbers[] = sprintf('<div>%1$s</div>', $i + 1);
        }
        $code = implode('', $code_lines);
        //$code = implode("\n", $code_lines);
        $gutter = '';
        if ($showGutter) {
            $code_line_numbers_str = implode('', $code_line_numbers);
            $placeholder = str_repeat('&nbsp;', strlen((string) count($code_lines)));
            $gutter = <<<EOS
\t\t\t<td><div class="gutter">{$code_line_numbers_str}</div><div class="gutter-placeholder">{$placeholder}</div></td>
EOS;
        }
        $code = <<<EOS
\t\t<pre><code><table>
\t\t\t<tr>
\t\t\t\t{$gutter}
\t\t\t\t<td><div class="code">{$code}</div></td>
\t\t\t</tr>
\t\t</table></code></pre>
EOS;
        return $code;
    }