Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F94165003
PhutilDivinerSyntaxHighlighter.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
Wed, Dec 4, 10:08
Size
1 KB
Mime Type
text/x-php
Expires
Fri, Dec 6, 10:08 (1 d, 13 h)
Engine
blob
Format
Raw Data
Handle
22748670
Attached To
rPHU libphutil
PhutilDivinerSyntaxHighlighter.php
View Options
<?php
/**
* Simple syntax highlighter for the ".diviner" format, which is just Remarkup
* with a specific ruleset. This should also work alright for Remarkup.
*
* @group markup
*/
final
class
PhutilDivinerSyntaxHighlighter
{
private
$config
=
array
();
private
$replaceClass
;
public
function
setConfig
(
$key
,
$value
)
{
$this
->
config
[
$key
]
=
$value
;
return
$this
;
}
public
function
getHighlightFuture
(
$source
)
{
$source
=
phutil_escape_html
(
$source
);
// This highlighter isn't perfect but tries to do an okay job at getting
// some of the basics at least. There's lots of room for improvement.
// Highlight "@{class:...}" links to other documentation pages.
$source
=
$this
->
highlightPattern
(
'/@{([
\w
@]+?):([^}]+?)}/'
,
$source
,
'nc'
);
// Highlight "@title", "@group", etc.
$source
=
$this
->
highlightPattern
(
'/^@(
\w
+)/m'
,
$source
,
'k'
);
// Highlight bold, italic and monospace.
$source
=
$this
->
highlightPattern
(
'@
\\
*
\\
*(.+?)
\\
*
\\
*@s'
,
$source
,
's'
);
$source
=
$this
->
highlightPattern
(
'@(?<!:)//(.+?)//@s'
,
$source
,
's'
);
$source
=
$this
->
highlightPattern
(
'@##([
\s\S
]+?)##|
\B
`(.+?)`
\B
@'
,
$source
,
's'
);
// Highlight stuff that looks like headers.
$source
=
$this
->
highlightPattern
(
'/^=(.*)$/m'
,
$source
,
'nv'
);
return
new
ImmediateFuture
(
$source
);
}
private
function
highlightPattern
(
$regexp
,
$source
,
$class
)
{
$this
->
replaceClass
=
$class
;
$source
=
preg_replace_callback
(
$regexp
,
array
(
$this
,
'replacePattern'
),
$source
);
return
$source
;
}
public
function
replacePattern
(
$matches
)
{
// NOTE: The goal here is to make sure a <span> never crosses a newline.
$content
=
$matches
[
0
];
$content
=
explode
(
"
\n
"
,
$content
);
foreach
(
$content
as
$key
=>
$line
)
{
$content
[
$key
]
=
'<span class="'
.
$this
->
replaceClass
.
'">'
.
$line
.
'</span>'
;
}
return
implode
(
"
\n
"
,
$content
);
}
}
Event Timeline
Log In to Comment