/** * @param Text $text * @param array $options */ public function processFencedCodeBlock(Text $text, array $options = []) { /** @noinspection PhpUnusedParameterInspection */ $text->replace('{ (?:\\n\\n|\\A) (?: ([`~]{3})[ ]* #1 fence ` or ~ ([a-zA-Z0-9]*?)? #2 language [optional] \\n+ (.*?)\\n #3 code block \\1 # matched #1 ) }smx', function (Text $w, Text $fence, Text $lang, Text $code) use($options) { $rendererOptions = []; if (!$lang->isEmpty()) { if ($options['pygments'] && class_exists('KzykHys\\Pygments\\Pygments')) { $pygments = new Pygments(); $html = $pygments->highlight($code, $lang, 'html'); return "\n\n" . $html . "\n\n"; } $rendererOptions = ['attr' => ['class' => 'prettyprint lang-' . $lang->lower()]]; } $code->escapeHtml(ENT_NOQUOTES); $this->markdown->emit('detab', array($code)); $code->replace('/\\A\\n+/', ''); $code->replace('/\\s+\\z/', ''); return "\n\n" . $this->getRenderer()->renderCodeBlock($code, $rendererOptions) . "\n\n"; }); }
/** * @param string $content * @param string $language * @param array $parameters * @return string */ public function getOutputFor($content, $language, array $parameters = []) { $parser = new Pygments(); $styles = ''; if (isset($parameters['theme'])) { $styles .= '<style>' . $parser->getCss($parameters['theme']) . '</style>'; } return $styles . $parser->highlight($content, $language, 'html'); }
public function execute() { global $wgPygmentizePath; $target = __DIR__ . '/../modules/pygments.generated.css'; $pygments = new Pygments($wgPygmentizePath); $css = "/* Stylesheet generated by updateCSS.php */\n"; $css .= $pygments->getCss('default', '.' . SyntaxHighlight_GeSHi::HIGHLIGHT_CSS_CLASS); if (file_put_contents($target, $css) === false) { $this->output("Failed to write to {$target}\n"); } else { $this->output('CSS written to ' . realpath($target) . "\n"); } }
public function execute() { global $wgPygmentizePath; function lang_filter($val) { return preg_match('/^[a-zA-Z0-9\\-_]+$/', $val); } $header = '// Generated by ' . basename(__FILE__) . "\n\n"; $pygments = new Pygments($wgPygmentizePath); $lexers = array_keys($pygments->getLexers()); sort($lexers); $code = "<?php\n" . $header . 'return ' . var_export($lexers, true) . ";\n"; $code = preg_replace('/(\\d+ \\=\\>| (?=\\())/i', '', $code); $code = preg_replace("/^ +/m", "\t", $code); file_put_contents(__DIR__ . '/../SyntaxHighlight_GeSHi.lexers.php', $code); $this->output("Updated language list written to SyntaxHighlight_GeSHi.lexers.php\n"); }
public function testGuessLexer() { $pygments = new Pygments(); $this->assertEquals('php', $pygments->guessLexer('index.php')); $this->assertEquals('go', $pygments->guessLexer('main.go')); }
/** * Highlight a code-block using a particular lexer. * * @param string $code Code to highlight. * @param string|null $lang Language name, or null to use plain markup. * @param array $args Associative array of additional arguments. * If it contains a 'line' key, the output will include line numbers. * If it includes a 'highlight' key, the value will be parsed as a * comma-separated list of lines and line-ranges to highlight. * If it contains a 'start' key, the value will be used as the line at which to * start highlighting. * If it contains a 'inline' key, the output will not be wrapped in `<div><pre/></div>`. * @return Status Status object, with HTML representing the highlighted * code as its value. */ protected static function highlight($code, $lang = null, $args = array()) { global $wgPygmentizePath; $status = new Status(); $lexer = self::getLexer($lang); if ($lexer === null && $lang !== null) { $status->warning('syntaxhighlight-error-unknown-language', $lang); } $length = strlen($code); if (strlen($code) > self::HIGHLIGHT_MAX_BYTES) { $status->warning('syntaxhighlight-error-exceeds-size-limit', $length, self::HIGHLIGHT_MAX_BYTES); $lexer = null; } if (wfShellExecDisabled() !== false) { $status->warning('syntaxhighlight-error-pygments-invocation-failure'); wfWarn('MediaWiki determined that it cannot invoke Pygments. ' . 'As a result, SyntaxHighlight_GeSHi will not perform any syntax highlighting. ' . 'See the debug log for details: ' . 'https://www.mediawiki.org/wiki/Manual:$wgDebugLogFile'); $lexer = null; } $inline = isset($args['inline']); if ($lexer === null) { if ($inline) { $status->value = htmlspecialchars(trim($code), ENT_NOQUOTES); } else { $pre = Html::element('pre', array(), $code); $status->value = Html::rawElement('div', array('class' => self::HIGHLIGHT_CSS_CLASS), $pre); } return $status; } $options = array('cssclass' => self::HIGHLIGHT_CSS_CLASS, 'encoding' => 'utf-8'); // Line numbers if (isset($args['line'])) { $options['linenos'] = 'inline'; } if ($lexer === 'php' && strpos($code, '<?php') === false) { $options['startinline'] = 1; } // Highlight specific lines if (isset($args['highlight'])) { $lines = self::parseHighlightLines($args['highlight']); if (count($lines)) { $options['hl_lines'] = implode(' ', $lines); } } // Starting line number if (isset($args['start'])) { $options['linenostart'] = $args['start']; } if ($inline) { $options['nowrap'] = 1; } $cache = wfGetMainCache(); $cacheKey = self::makeCacheKey($code, $lexer, $options); $output = $cache->get($cacheKey); if ($output === false) { try { $pygments = new Pygments($wgPygmentizePath); $output = $pygments->highlight($code, $lexer, 'html', $options); } catch (RuntimeException $e) { $status->warning('syntaxhighlight-error-pygments-invocation-failure'); wfWarn('Failed to invoke Pygments. Please check that Pygments is installed ' . 'and that $wgPygmentizePath is accurate.'); $status->value = self::highlight($code, null, $args)->getValue(); return $status; } $cache->set($cacheKey, $output); } if ($inline) { $output = trim($output); } $status->value = $output; return $status; }