Path: blob/master/src/infrastructure/markup/syntax/highlighter/PhutilConsoleSyntaxHighlighter.php
12242 views
<?php12/**3* Simple syntax highlighter for console output. We just try to highlight the4* commands so it's easier to follow transcripts.5*/6final class PhutilConsoleSyntaxHighlighter extends Phobject {78private $config = array();910public function setConfig($key, $value) {11$this->config[$key] = $value;12return $this;13}1415public function getHighlightFuture($source) {16$in_command = false;17$lines = explode("\n", $source);18foreach ($lines as $key => $line) {19$matches = null;2021// Parse commands like this:22//23// some/path/ $ ./bin/example # Do things24//25// ...into path, command, and comment components.2627$pattern =28'@'.29($in_command ? '()(.*?)' : '^(\S+[\\\\/] )?([$] .*?)').30'(#.*|\\\\)?$@';3132if (preg_match($pattern, $line, $matches)) {33$lines[$key] = hsprintf(34'%s<span class="gp">%s</span>%s',35$matches[1],36$matches[2],37(!empty($matches[3])38? hsprintf('<span class="k">%s</span>', $matches[3])39: ''));40$in_command = (idx($matches, 3) == '\\');41} else {42$lines[$key] = hsprintf('<span class="go">%s</span>', $line);43}44}45$lines = phutil_implode_html("\n", $lines);4647return new ImmediateFuture($lines);48}4950}515253