Path: blob/master/src/infrastructure/markup/syntax/highlighter/PhutilLexerSyntaxHighlighter.php
12242 views
<?php12final class PhutilLexerSyntaxHighlighter extends PhutilSyntaxHighlighter {34private $config = array();56public function setConfig($key, $value) {7$this->config[$key] = $value;8return $this;9}1011public function getHighlightFuture($source) {12$strip = false;13$state = 'start';14$lang = idx($this->config, 'language');1516if ($lang == 'php') {17if (strpos($source, '<?') === false) {18$state = 'php';19}20}2122$lexer = idx($this->config, 'lexer');23$tokens = $lexer->getTokens($source, $state);24$tokens = $lexer->mergeTokens($tokens);2526$result = array();27foreach ($tokens as $token) {28list($type, $value, $context) = $token;2930$data_name = null;31switch ($type) {32case 'nc':33case 'nf':34case 'na':35$data_name = $value;36break;37}3839if (strpos($value, "\n") !== false) {40$value = explode("\n", $value);41} else {42$value = array($value);43}44foreach ($value as $part) {45if (strlen($part)) {46if ($type) {47$result[] = phutil_tag(48'span',49array(50'class' => $type,51'data-symbol-context' => $context,52'data-symbol-name' => $data_name,53),54$part);55} else {56$result[] = $part;57}58}59$result[] = "\n";60}6162// Throw away the last "\n".63array_pop($result);64}6566$result = phutil_implode_html('', $result);6768return new ImmediateFuture($result);69}7071}727374