-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathAdminPageFramework_TableOfContents.php
More file actions
121 lines (108 loc) · 3.71 KB
/
Copy pathAdminPageFramework_TableOfContents.php
File metadata and controls
121 lines (108 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Generates an HTML Table of Contents block.
*
* @package Admin Page Framework
* @copyright Copyright (c) 2013-2022, Michael Uno
* @author Michael Uno
* @authorurl http://michaeluno.jp
*/
/**
* Generates an HTML Table of Contents block.
*
* This parses given text and generate headings with links to the heading elements. It helps the viewer browse the vertically long contents.
* Use this class to create a help page from existent text document.
*
* <h2>Usage</h2>
* Pass text data to the first parameter and the depth of nested headings to the second parameter.
* Then use the `get()` method to retrieve the generated output.
*
* <h2>Example</h2>
* <code>
* $_oTOC = new AdminPageFramework_TableOfContents( $sText, 4 );
* $_sTOC = $_oTOC->get();
* </code>
*
* @image http://admin-page-framework.michaeluno.jp/image/utility/table_of_contents.png
* @since 3.5.0
* @package AdminPageFramework/Utility
*/
class AdminPageFramework_TableOfContents {
/**
* Sets up properties.
*
* @param string $sHTML The HTML text to parse.
* @param integer $iDepth The header number to parse.
* @param string $sTitle The heading title which appears at the beginning of the output.
*/
public function __construct( $sHTML, $iDepth=4, $sTitle='' ) {
$this->sTitle = $sTitle;
$this->sHTML = $sHTML;
$this->iDepth = $iDepth;
}
/**
* Returns the TOC block and the contents.
*
* @since 3.5.0
* @see http://www.10stripe.com/articles/automatically-generate-table-of-contents-php.php
* @return string
*/
public function get() {
return $this->getTOC()
. $this->getContents();
}
/**
* Returns only the contents.
*
* The contents will be modified as named elements need to be inserted.
* @return string
* @internal
*/
public function getContents() {
return $this->sHTML;
}
/**
* Returns only the TOC block.
* @return string
* @internal
*/
public function getTOC() {
$iDepth = $this->iDepth;
// get the headings down to the specified depth
$this->sHTML = preg_replace_callback(
'/<h[2-' . $iDepth . ']*[^>]*>.*?<\/h[2-' . $iDepth . ']>/i',
array( $this, '_replyToInsertNamedElement' ),
$this->sHTML
);
$_aOutput = array();
foreach( $this->_aMatches as $_iIndex => $_sMatch ) {
$_sMatch = is_string( $_sMatch ) ? $_sMatch : '';
$_sMatch = strip_tags( $_sMatch, '<h1><h2><h3><h4><h5><h6><h7><h8>' );
$_sMatch = preg_replace( '/<h([1-' . $iDepth . '])>/', '<li class="toc$1"><a href="#toc_' . $_iIndex . '">', $_sMatch );
$_sMatch = preg_replace( '/<\/h[1-' . $iDepth . ']>/', '</a></li>', $_sMatch );
$_aOutput[] = $_sMatch;
}
// plug the results into appropriate HTML tags
$this->sTitle = $this->sTitle
? '<p class="toc-title">' . $this->sTitle . '</p>'
: '';
return '<div class="toc">'
. $this->sTitle
. '<ul>'
. implode( PHP_EOL, $_aOutput )
. '</ul>'
. '</div>';
}
/**#@+
* @internal
*/
protected $_aMatches = array();
public function _replyToInsertNamedElement( $aMatches ) {
static $_icount = -1;
$_icount++;
$this->_aMatches[] = $aMatches[ 0 ];
return "<span class='toc_header_link' id='toc_{$_icount}'></span>" . PHP_EOL
. $aMatches[ 0 ];
}
/**#@-*/
}