Files
phabricator/src/infrastructure/markup/syntax/highlighter/PhutilLexerSyntaxHighlighter.php
epriestley f9b3e3360b Continue moving classes with no callers in libphutil or Arcanist to Phabricator
Summary: Ref T13395. Move cache classes, syntax highlighters, other markup classes, and sprite sheets to Phabricator.

Test Plan: Attempted to find any callers for any of this stuff in libphutil or Arcanist and couldn't.

Maniphest Tasks: T13395

Differential Revision: https://secure.phabricator.com/D20977
2020-02-12 13:14:04 -08:00

73 lines
1.6 KiB
PHP

<?php
final class PhutilLexerSyntaxHighlighter extends PhutilSyntaxHighlighter {
private $config = array();
public function setConfig($key, $value) {
$this->config[$key] = $value;
return $this;
}
public function getHighlightFuture($source) {
$strip = false;
$state = 'start';
$lang = idx($this->config, 'language');
if ($lang == 'php') {
if (strpos($source, '<?') === false) {
$state = 'php';
}
}
$lexer = idx($this->config, 'lexer');
$tokens = $lexer->getTokens($source, $state);
$tokens = $lexer->mergeTokens($tokens);
$result = array();
foreach ($tokens as $token) {
list($type, $value, $context) = $token;
$data_name = null;
switch ($type) {
case 'nc':
case 'nf':
case 'na':
$data_name = $value;
break;
}
if (strpos($value, "\n") !== false) {
$value = explode("\n", $value);
} else {
$value = array($value);
}
foreach ($value as $part) {
if (strlen($part)) {
if ($type) {
$result[] = phutil_tag(
'span',
array(
'class' => $type,
'data-symbol-context' => $context,
'data-symbol-name' => $data_name,
),
$part);
} else {
$result[] = $part;
}
}
$result[] = "\n";
}
// Throw away the last "\n".
array_pop($result);
}
$result = phutil_implode_html('', $result);
return new ImmediateFuture($result);
}
}