Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/markup/syntax/highlighter/PhutilConsoleSyntaxHighlighter.php
12242 views
1
<?php
2
3
/**
4
* Simple syntax highlighter for console output. We just try to highlight the
5
* commands so it's easier to follow transcripts.
6
*/
7
final class PhutilConsoleSyntaxHighlighter extends Phobject {
8
9
private $config = array();
10
11
public function setConfig($key, $value) {
12
$this->config[$key] = $value;
13
return $this;
14
}
15
16
public function getHighlightFuture($source) {
17
$in_command = false;
18
$lines = explode("\n", $source);
19
foreach ($lines as $key => $line) {
20
$matches = null;
21
22
// Parse commands like this:
23
//
24
// some/path/ $ ./bin/example # Do things
25
//
26
// ...into path, command, and comment components.
27
28
$pattern =
29
'@'.
30
($in_command ? '()(.*?)' : '^(\S+[\\\\/] )?([$] .*?)').
31
'(#.*|\\\\)?$@';
32
33
if (preg_match($pattern, $line, $matches)) {
34
$lines[$key] = hsprintf(
35
'%s<span class="gp">%s</span>%s',
36
$matches[1],
37
$matches[2],
38
(!empty($matches[3])
39
? hsprintf('<span class="k">%s</span>', $matches[3])
40
: ''));
41
$in_command = (idx($matches, 3) == '\\');
42
} else {
43
$lines[$key] = hsprintf('<span class="go">%s</span>', $line);
44
}
45
}
46
$lines = phutil_implode_html("\n", $lines);
47
48
return new ImmediateFuture($lines);
49
}
50
51
}
52
53