Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F92864217
PhutilRemarkupEngineRemarkupHeaderBlockRule.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
Sun, Nov 24, 08:10
Size
2 KB
Mime Type
text/x-php
Expires
Tue, Nov 26, 08:10 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
22530536
Attached To
rPHU libphutil
PhutilRemarkupEngineRemarkupHeaderBlockRule.php
View Options
<?php
/**
* @group markup
*/
final
class
PhutilRemarkupEngineRemarkupHeaderBlockRule
extends
PhutilRemarkupEngineBlockRule
{
const
KEY_HEADER_TOC
=
'headers.toc'
;
public
function
getBlockPattern
()
{
return
'/^(={1,5})(.*?)
\\
1?
\s
*$/'
;
}
public
function
shouldMergeBlocks
()
{
return
false
;
}
public
function
markupText
(
$text
)
{
$text
=
trim
(
$text
);
$level
=
1
;
for
(
$ii
=
0
;
$ii
<
min
(
5
,
strlen
(
$text
));
$ii
++)
{
if
(
$text
[
$ii
]
==
'='
)
{
++
$level
;
}
else
{
break
;
}
}
$text
=
trim
(
$text
,
' ='
);
$engine
=
$this
->
getEngine
();
$use_anchors
=
$engine
->
getConfig
(
'header.generate-toc'
);
$anchor
=
null
;
if
(
$use_anchors
)
{
$anchor
=
$this
->
generateAnchor
(
$level
-
1
,
$text
);
}
return
'<h'
.
$level
.
'>'
.
$anchor
.
$this
->
applyRules
(
$text
).
'</h'
.
$level
.
'>'
;
}
private
function
generateAnchor
(
$level
,
$text
)
{
$anchor
=
strtolower
(
$text
);
$anchor
=
preg_replace
(
'/[^a-z0-9]/'
,
'-'
,
$anchor
);
$anchor
=
preg_replace
(
'/--+/'
,
'-'
,
$anchor
);
$anchor
=
trim
(
$anchor
,
'-'
);
$anchor
=
substr
(
$anchor
,
0
,
24
);
$anchor
=
trim
(
$anchor
,
'-'
);
$base
=
$anchor
;
$key
=
self
::
KEY_HEADER_TOC
;
$engine
=
$this
->
getEngine
();
$anchors
=
$engine
->
getTextMetadata
(
$key
,
array
());
$suffix
=
1
;
while
(!
strlen
(
$anchor
)
||
isset
(
$anchors
[
$anchor
]))
{
$anchor
=
$base
.
'-'
.
$suffix
;
$anchor
=
trim
(
$anchor
,
'-'
);
$suffix
++;
}
$anchors
[
$anchor
]
=
array
(
$level
,
$text
);
$engine
->
setTextMetadata
(
$key
,
$anchors
);
return
phutil_render_tag
(
'a'
,
array
(
'name'
=>
$anchor
,
),
''
);
}
public
static
function
renderTableOfContents
(
PhutilRemarkupEngine
$engine
)
{
$key
=
self
::
KEY_HEADER_TOC
;
$anchors
=
$engine
->
getTextMetadata
(
$key
,
array
());
if
(
count
(
$anchors
)
<
2
)
{
// Don't generate a TOC if there are no headers, or if there's only
// one header (since such a TOC would be silly).
return
null
;
}
$depth
=
0
;
$toc
=
array
();
foreach
(
$anchors
as
$anchor
=>
$info
)
{
list
(
$level
,
$name
)
=
$info
;
while
(
$depth
<
$level
)
{
$toc
[]
=
'<ul>'
;
$depth
++;
}
while
(
$depth
>
$level
)
{
$toc
[]
=
'</ul>'
;
$depth
--;
}
$toc
[]
=
phutil_render_tag
(
'li'
,
array
(),
phutil_render_tag
(
'a'
,
array
(
'href'
=>
'#'
.
$anchor
,
),
phutil_escape_html
(
$name
)));
}
while
(
$depth
>
0
)
{
$toc
[]
=
'</ul>'
;
$depth
--;
}
return
implode
(
"
\n
"
,
$toc
);
}
}
Event Timeline
Log In to Comment