Skip to content

Commit ee62fc7

Browse files
committed
dom parser
1 parent 5306870 commit ee62fc7

File tree

3 files changed

+1825
-32
lines changed

3 files changed

+1825
-32
lines changed

Plugin.php

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,20 @@ public static function footer()
6767
{
6868

6969
echo '<script>
70-
$(function(){
71-
$("#toc-switch").click(function(){
72-
$("#toc-ul").toggle("fast");
73-
});
74-
});
70+
window.content_index_showTocToggle = true;
71+
function content_index_toggleToc() {
72+
var tts = "显示";
73+
var tth = "隐藏";
74+
if (window.content_index_showTocToggle) {
75+
window.content_index_showTocToggle = false;
76+
document.getElementById("toc-content").style.display = "none";
77+
document.getElementById("content-index-togglelink").innerHTML = tts
78+
} else {
79+
window.content_index_showTocToggle = true;
80+
document.getElementById("toc-content").style.display = "block";
81+
document.getElementById("content-index-togglelink").innerHTML = tth
82+
}
83+
}
7584
</script>';
7685
}
7786

@@ -87,12 +96,12 @@ public static function replace($content, $class, $string)
8796

8897
$html_string = is_null($string) ? $content : $string;
8998

90-
if( $class->is('index') || $class->is('search')){
99+
if( $class->is('index') || $class->is('search') || $class->is('date')){
91100
return $html_string;
92101
}
93102

94-
$toc = self::create_toc($html_string);
95-
return $toc['toc'] . $toc['content'];
103+
$toc = self::create_toc_with_dom($html_string);
104+
return $toc;
96105
}
97106

98107
/**
@@ -205,10 +214,63 @@ public static function create_toc($content)
205214
$toc .= '</li>'."\n";
206215
$toc .= '</ul></div>'."\n";
207216

208-
return array(
209-
'toc' => $toc,
210-
'content' => $content
211-
);
217+
return $toc . $content;
218+
}
219+
220+
221+
public static function create_toc_with_dom($content)
222+
{
223+
require_once 'simple_html_dom.php';
224+
225+
$html = str_get_html($content);
226+
227+
$toc = '<div class="toc-index"><div class="toc-title">本文目录</div><span class="toc-toggle">[<a id="content-index-togglelink" href="javascript:content_index_toggleToc()">隐藏</a>]</span><div id="toc-content">';
228+
$toc .= '';
229+
$last_level = 0;
230+
$count_h2 = 0;
231+
$new = $html->find('h2,h3');
232+
233+
if(count($new) < 3) return $content;
234+
235+
foreach($new as $h){
236+
237+
238+
239+
$innerTEXT = trim($h->innertext);
240+
$id = str_replace(' ','_',$innerTEXT);
241+
$level = intval($h->tag[1]);
242+
243+
if($level == 2){
244+
$count_h2++;
245+
$innerTEXT = self::dec2roman($count_h2) . ' ' . $innerTEXT;
246+
}
247+
$h->id= $id; // add id attribute so we can jump to this element
248+
249+
$h->innertext = $innerTEXT ; // add id attribute so we can jump to this element
250+
251+
252+
if($level > $last_level)
253+
// add class
254+
$toc .= '<ol>';
255+
else{
256+
$toc .= str_repeat('</li></ol>', $last_level - $level);
257+
$toc .= '</li>';
258+
}
259+
if($level >= $last_level){
260+
$toc .= "<li class='toc-level$level'><a href='#{$id}'>{$innerTEXT}</a>";
261+
}else{
262+
$toc .= "<li><a href='#{$id}'>{$innerTEXT}</a>";
263+
}
264+
265+
266+
$last_level = $level;
267+
}
268+
269+
$toc .= str_repeat('</li></ol>', $last_level);
270+
$toc .= '</div></div>';
271+
272+
return $toc . "<hr>" . $html->save();
273+
212274
}
213275

214276

css/toc_style.css

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
1-
#toc {
1+
.toc-index {
2+
padding: 10px;
3+
border: 1px solid #ddd;
4+
background: #f8f8f8;
5+
line-height: 160%;
6+
max-width: 26%;
7+
font-size: 12px;
28
position: relative;
3-
border: 1px solid #bba;
4-
background-color: #f7f8ff;
5-
padding: 5px;
6-
margin-left: 8px;
7-
font-size: 85%;
8-
width: 15em;
9-
float: right
9+
z-index: 900;
10+
margin: 0 0 10px 10px;
11+
float: right;
12+
1013
}
1114

12-
#toc ul {
13-
margin: 0px;
14-
list-style: none
15+
.toc-title {
16+
margin: 0 0 3px 0;
17+
padding: 0;
18+
font-size: 130%;
19+
font-weight: bold
1520
}
1621

17-
#toc-ul ul li {
18-
list-style: square
22+
.toc-toggle {
23+
font-size: 9pt
1924
}
2025

21-
#toc-hide {
22-
position: absolute;
23-
top: 0;
24-
right: 0
26+
.toc-index ol {
27+
padding: 0;
28+
margin: 0;
29+
font-size: 100%;
30+
list-style: none;
2531
}
2632

27-
#toc-header {
28-
text-align: center;
29-
}
33+
.toc-level3 {
34+
margin-left: 20px;
35+
list-style-type: square;
36+
}

0 commit comments

Comments
 (0)