@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at recaptime-dev/main 72 lines 1.6 kB view raw
1<?php 2 3final class PhutilLexerSyntaxHighlighter extends PhutilSyntaxHighlighter { 4 5 private $config = array(); 6 7 public function setConfig($key, $value) { 8 $this->config[$key] = $value; 9 return $this; 10 } 11 12 public function getHighlightFuture($source) { 13 $strip = false; 14 $state = 'start'; 15 $lang = idx($this->config, 'language'); 16 17 if ($lang == 'php') { 18 if (strpos($source, '<?') === false) { 19 $state = 'php'; 20 } 21 } 22 23 $lexer = idx($this->config, 'lexer'); 24 $tokens = $lexer->getTokens($source, $state); 25 $tokens = $lexer->mergeTokens($tokens); 26 27 $result = array(); 28 foreach ($tokens as $token) { 29 list($type, $value, $context) = $token; 30 31 $data_name = null; 32 switch ($type) { 33 case 'nc': 34 case 'nf': 35 case 'na': 36 $data_name = $value; 37 break; 38 } 39 40 if (strpos($value, "\n") !== false) { 41 $value = explode("\n", $value); 42 } else { 43 $value = array($value); 44 } 45 foreach ($value as $part) { 46 if (strlen($part)) { 47 if ($type) { 48 $result[] = phutil_tag( 49 'span', 50 array( 51 'class' => $type, 52 'data-symbol-context' => $context, 53 'data-symbol-name' => $data_name, 54 ), 55 $part); 56 } else { 57 $result[] = $part; 58 } 59 } 60 $result[] = "\n"; 61 } 62 63 // Throw away the last "\n". 64 array_pop($result); 65 } 66 67 $result = phutil_implode_html('', $result); 68 69 return new ImmediateFuture($result); 70 } 71 72}