Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F92595957
PhutilLexerSyntaxHighlighter.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
Thu, Nov 21, 20:19
Size
1 KB
Mime Type
text/x-php
Expires
Sat, Nov 23, 20:19 (2 d)
Engine
blob
Format
Raw Data
Handle
22466379
Attached To
rPHU libphutil
PhutilLexerSyntaxHighlighter.php
View Options
<?php
/**
* @group markup
*/
final
class
PhutilLexerSyntaxHighlighter
extends
PhutilSyntaxHighlighter
{
private
$config
=
array
();
public
function
setConfig
(
$key
,
$value
)
{
$this
->
config
[
$key
]
=
$value
;
return
$this
;
}
public
function
getHighlightFuture
(
$source
)
{
$strip
=
false
;
$state
=
'start'
;
$lang
=
idx
(
$this
->
config
,
'language'
);
if
(
$lang
==
'php'
)
{
if
(
strpos
(
$source
,
'<?'
)
===
false
)
{
$state
=
'php'
;
}
}
$lexer
=
idx
(
$this
->
config
,
'lexer'
);
$tokens
=
$lexer
->
getTokens
(
$source
,
$state
);
$tokens
=
$lexer
->
mergeTokens
(
$tokens
);
$result
=
array
();
foreach
(
$tokens
as
$token
)
{
list
(
$type
,
$value
,
$context
)
=
$token
;
$value
=
phutil_escape_html
(
$value
);
$data_name
=
null
;
switch
(
$type
)
{
case
'nc'
:
case
'nf'
:
case
'na'
:
$data_name
=
' data-symbol-name="'
.
$value
.
'"'
;
break
;
}
$data_context
=
null
;
if
(
$context
)
{
$context
=
phutil_escape_html
(
$context
);
$data_context
=
' data-symbol-context="'
.
$context
.
'"'
;
}
$class_name
=
null
;
if
(
$type
)
{
$class_name
=
' class="'
.
$type
.
'"'
;
}
if
(
strpos
(
$value
,
"
\n
"
)
!==
false
)
{
$value
=
explode
(
"
\n
"
,
$value
);
}
else
{
$value
=
array
(
$value
);
}
foreach
(
$value
as
$part
)
{
if
(
strlen
(
$part
))
{
if
(
$class_name
)
{
$result
[]
=
'<span'
.
$class_name
.
$data_context
.
$data_name
.
'>'
.
$part
.
'</span>'
;
}
else
{
$result
[]
=
$part
;
}
}
$result
[]
=
"
\n
"
;
}
// Throw away the last "\n".
array_pop
(
$result
);
}
$result
=
implode
(
''
,
$result
);
return
new
ImmediateFuture
(
$result
);
}
}
Event Timeline
Log In to Comment