Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F106807137
PhutilConsoleSyntaxHighlighter.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Mar 31, 22:41
Size
1 KB
Mime Type
text/x-php
Expires
Wed, Apr 2, 22:41 (2 d)
Engine
blob
Format
Raw Data
Handle
25279481
Attached To
rPHU libphutil
PhutilConsoleSyntaxHighlighter.php
View Options
<?php
/**
* Simple syntax highlighter for console output. We just try to highlight the
* commands so it's easier to follow transcripts.
*/
final
class
PhutilConsoleSyntaxHighlighter
{
private
$config
=
array
();
private
$replaceClass
;
public
function
setConfig
(
$key
,
$value
)
{
$this
->
config
[
$key
]
=
$value
;
return
$this
;
}
public
function
getHighlightFuture
(
$source
)
{
$in_command
=
false
;
$lines
=
explode
(
"
\n
"
,
$source
);
foreach
(
$lines
as
$key
=>
$line
)
{
$matches
=
null
;
// Parse commands like this:
//
// some/path/ $ ./bin/example # Do things
//
// ...into path, command, and comment components.
$pattern
=
'@'
.
(
$in_command
?
'()(.*?)'
:
'^(
\S
+[
\\\\
/] )?([$] .*?)'
).
'(#.*|
\\\\
)?$@'
;
if
(
preg_match
(
$pattern
,
$line
,
$matches
))
{
$lines
[
$key
]
=
hsprintf
(
'%s<span class="gp">%s</span>%s'
,
$matches
[
1
],
$matches
[
2
],
(!
empty
(
$matches
[
3
])
?
hsprintf
(
'<span class="k">%s</span>'
,
$matches
[
3
])
:
''
));
$in_command
=
(
idx
(
$matches
,
3
)
==
'
\\
'
);
}
else
{
$lines
[
$key
]
=
hsprintf
(
'<span class="go">%s</span>'
,
$line
);
}
}
$lines
=
phutil_implode_html
(
"
\n
"
,
$lines
);
return
new
ImmediateFuture
(
$lines
);
}
}
Event Timeline
Log In to Comment