public function testFilenameGreediness() { $names = array('x.php' => 'php', '/x.php' => 'php', 'x.y.php' => 'php', '/x.y/z.php' => 'php', '/x.php/' => null); $engine = new PhutilDefaultSyntaxHighlighterEngine(); foreach ($names as $path => $language) { $detect = $engine->getLanguageFromFilename($path); $this->assertEqual($language, $detect, pht('Language detect for %s', $path)); } }
public static function newEngine() { $engine = new PhutilDefaultSyntaxHighlighterEngine(); $config = array('pygments.enabled' => PhabricatorEnv::getEnvConfig('pygments.enabled'), 'filename.map' => PhabricatorEnv::getEnvConfig('syntax.filemap')); foreach ($config as $key => $value) { $engine->setConfig($key, $value); } return $engine; }
public function markupText($text) { if (preg_match('/^```/', $text)) { // If this is a ```-style block, trim off the backticks. $text = preg_replace('/```\\s*$/', '', substr($text, 3)); } $lines = explode("\n", $text); $lang = nonempty($this->getEngine()->getConfig('phutil.codeblock.language-default'), 'php'); $aux_class = ''; do { $first_line = reset($lines); $matches = null; if (preg_match('/^\\s{2,}lang\\s*=\\s*(.*)$/i', $first_line, $matches)) { $lang = $matches[1]; array_shift($lines); continue; } if (preg_match('/^\\s{2,}COUNTEREXAMPLE$/i', $first_line, $matches)) { $aux_class = ' remarkup-counterexample'; array_shift($lines); continue; } } while (false); // Normalize the text back to a 0-level indent. $min_indent = 80; foreach ($lines as $line) { for ($ii = 0; $ii < strlen($line); $ii++) { if ($line[$ii] != ' ') { $min_indent = min($ii, $min_indent); break; } } } if ($min_indent) { $indent_string = str_repeat(' ', $min_indent); $text = preg_replace('/^' . $indent_string . '/m', '', implode("\n", $lines)); } $engine = new PhutilDefaultSyntaxHighlighterEngine(); $engine->setConfig('pygments.enabled', $this->getEngine()->getConfig('pygments.enabled')); return '<code class="remarkup-code' . $aux_class . '">' . $engine->highlightSource($lang, $text) . '</code>'; }