11<?php
22if (!defined ('__TYPECHO_ROOT_DIR__ ' )) exit ;
3+
34/**
45 * Table Of Contents 自动文章目录
56 *
@@ -33,7 +34,9 @@ public static function activate()
3334 * @return void
3435 * @throws Typecho_Plugin_Exception
3536 */
36- public static function deactivate (){}
37+ public static function deactivate ()
38+ {
39+ }
3740
3841 /**
3942 * 获取插件配置面板
@@ -54,7 +57,9 @@ public static function config(Typecho_Widget_Helper_Form $form)
5457 * @param Typecho_Widget_Helper_Form $form
5558 * @return void
5659 */
57- public static function personalConfig (Typecho_Widget_Helper_Form $ form ){}
60+ public static function personalConfig (Typecho_Widget_Helper_Form $ form )
61+ {
62+ }
5863
5964
6065 public static function header ()
@@ -96,7 +101,7 @@ public static function replace($content, $class, $string)
96101
97102 $ html_string = is_null ($ string ) ? $ content : $ string ;
98103
99- if ( $ class ->is ('index ' ) || $ class ->is ('search ' ) || $ class ->is ('date ' )){
104+ if ( $ class ->is ('index ' ) || $ class ->is ('search ' ) || $ class ->is ('date ' )) {
100105 return $ html_string ;
101106 }
102107
@@ -111,108 +116,107 @@ public static function replace($content, $class, $string)
111116 */
112117 public static function create_toc ($ content )
113118 {
114- preg_match_all ( '/<h([2-3])(.*)>([^<]+)<\/h[2-3]>/i ' , $ content , $ matches , PREG_SET_ORDER );
115- if (count ($ matches ) < 3 )
119+ preg_match_all ('/<h([2-3])(.*)>([^<]+)<\/h[2-3]>/i ' , $ content , $ matches , PREG_SET_ORDER );
120+ if (count ($ matches ) < 3 )
116121 return array (
117- 'toc ' => '' ,
118- 'content ' => $ content
122+ 'toc ' => '' ,
123+ 'content ' => $ content
119124 );
120125
121126 $ anchors = array ();
122- $ toc = '<div id="toc"><div id="toc-hide"><span class="am-icon-list am-btn-xs am-btn" id="toc-switch" > 显隐</span></div> ' . "\n" ;
127+ $ toc = '<div id="toc"><div id="toc-hide"><span class="am-icon-list am-btn-xs am-btn" id="toc-switch" > 显隐</span></div> ' . "\n" ;
123128
124129 $ toc .= '<p id="toc-header"> ' . _t ('文章目录 ' ) . '</p><ul id="toc-ul"> ' ;
125130 $ i = $ oder = $ prevlvl = 0 ;
126131 $ anchor_base = 'phpgao ' ;
127132
128- foreach ( $ matches as $ heading ) {
133+ foreach ($ matches as $ heading ) {
129134
130135 if ($ i == 0 )
131136 $ startlvl = $ heading [1 ];
132137
133138
134139 $ lvl = $ heading [1 ];
135140
136- $ ret = preg_match ( '/id=[ \'|"](.*)?[ \'|"]/i ' , stripslashes ($ heading [2 ]), $ anchor );
141+ $ ret = preg_match ('/id=[ \'|"](.*)?[ \'|"]/i ' , stripslashes ($ heading [2 ]), $ anchor );
137142 // 判断是否需要添加锚点
138- if ( $ ret && $ anchor [1 ] != '' ) {
139- $ anchor = stripslashes ( $ anchor [1 ] );
143+ if ($ ret && $ anchor [1 ] != '' ) {
144+ $ anchor = stripslashes ($ anchor [1 ]);
140145 $ add_id = false ;
141146 } else {
142- $ anchor = preg_replace ( '/\s+/ ' , '- ' , preg_replace ('/[^a-z\s]/ ' , '' , strtolower ( $ heading [3 ] ) ) );
147+ $ anchor = preg_replace ('/\s+/ ' , '- ' , preg_replace ('/[^a-z\s]/ ' , '' , strtolower ($ heading [3 ])) );
143148 $ add_id = true ;
144149 $ anchor = $ anchor_base . $ anchor ;
145150 }
146151
147152
148-
149153 // 如果相同,则入数组递增输出
150- if ( !in_array ( $ anchor , $ anchors ) ) {
154+ if (!in_array ($ anchor , $ anchors) ) {
151155 $ anchors [] = $ anchor ;
152156 } else {
153157 $ orig_anchor = $ anchor ;
154158 $ i = 2 ;
155- while ( in_array ( $ anchor , $ anchors ) ) {
156- $ anchor = $ orig_anchor. '- ' . $ i ;
159+ while (in_array ($ anchor , $ anchors) ) {
160+ $ anchor = $ orig_anchor . '- ' . $ i ;
157161 $ i ++;
158162 }
159163 $ anchors [] = $ anchor ;
160164 }
161165
162166 // 为文章中H元素添加锚点
163- if ( $ add_id ) {
164- if ( $ lvl == '2 ' ) {
167+ if ($ add_id ) {
168+ if ( $ lvl == '2 ' ) {
165169 $ oder ++;
166170 $ content = substr_replace ($ content , '<h ' . $ lvl . ' id=" ' . $ anchor . '" ' . $ heading [2 ] . '> ' . self ::dec2roman ($ oder ) . ' ' . $ heading [3 ] . '</h ' . $ lvl . '> ' , strpos ($ content , $ heading [0 ]), strlen ($ heading [0 ]));
167- }else {
171+ } else {
168172 $ content = substr_replace ($ content , '<h ' . $ lvl . ' id=" ' . $ anchor . '" ' . $ heading [2 ] . '> ' . $ heading [3 ] . '</h ' . $ lvl . '> ' , strpos ($ content , $ heading [0 ]), strlen ($ heading [0 ]));
169173
170174 }
171175 }
172176 //判断是否有title属性
173- $ ret = preg_match ( '/title=[ \'|"](.*)?[ \'|"]/i ' , stripslashes ( $ heading [2 ] ), $ title );
174- if ( $ ret && $ title [1 ] != '' )
175- $ title = stripslashes ( $ title [1 ] );
176- else {
177- if ( $ lvl == '2 ' ) {
177+ $ ret = preg_match ('/title=[ \'|"](.*)?[ \'|"]/i ' , stripslashes ($ heading [2 ]), $ title );
178+ if ($ ret && $ title [1 ] != '' )
179+ $ title = stripslashes ($ title [1 ]);
180+ else {
181+ if ( $ lvl == '2 ' ) {
178182 $ title = self ::dec2roman ($ oder ) . $ heading [3 ];
179- }else {
183+ } else {
180184 $ title = $ heading [3 ];
181185 }
182186 }
183187
184- $ title = trim ( strip_tags ( $ title ) );
188+ $ title = trim (strip_tags ($ title) );
185189
186190 if ($ i > 0 ) {
187191 if ($ prevlvl < $ lvl ) {
188- $ toc .= "\n" . "<ul> " . "\n" ;
192+ $ toc .= "\n" . "<ul> " . "\n" ;
189193 } else if ($ prevlvl > $ lvl ) {
190- $ toc .= '</li> ' . "\n" ;
194+ $ toc .= '</li> ' . "\n" ;
191195 while ($ prevlvl > $ lvl ) {
192- $ toc .= "</ul> " . "\n" . '</li> ' . "\n" ;
196+ $ toc .= "</ul> " . "\n" . '</li> ' . "\n" ;
193197 $ prevlvl --;
194198 }
195199 } else {
196- $ toc .= '</li> ' . "\n" ;
200+ $ toc .= '</li> ' . "\n" ;
197201 }
198202 }
199203
200204 $ j = 0 ;
201- $ toc .= '<li><a href="# ' . $ anchor. '"> ' . $ title. '</a> ' ;
205+ $ toc .= '<li><a href="# ' . $ anchor . '"> ' . $ title . '</a> ' ;
202206 $ prevlvl = $ lvl ;
203207
204208 $ i ++;
205209 }
206210
207- unset( $ anchors );
211+ unset($ anchors );
208212
209- while ( $ lvl > $ startlvl ) {
213+ while ($ lvl > $ startlvl ) {
210214 $ toc .= "\n</ol> " ;
211215 $ lvl --;
212216 }
213217
214- $ toc .= '</li> ' . "\n" ;
215- $ toc .= '</ul></div> ' . "\n" ;
218+ $ toc .= '</li> ' . "\n" ;
219+ $ toc .= '</ul></div> ' . "\n" ;
216220
217221 return $ toc . $ content ;
218222 }
@@ -222,43 +226,40 @@ public static function create_toc_with_dom($content)
222226 {
223227 require_once 'simple_html_dom.php ' ;
224228
225- $ html = str_get_html ($ content );
229+ $ html = str_get_html ($ content, 1 , 1 , ' UTF-8 ' , false );
226230
227231 $ 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"> ' ;
228232 $ toc .= '' ;
229233 $ last_level = 0 ;
230234 $ count_h2 = 0 ;
231235 $ new = $ html ->find ('h2,h3 ' );
232236
233- if (count ($ new ) < 3 ) return $ content ;
234-
235- foreach ($ new as $ h ){
236-
237-
237+ if (count ($ new ) < 3 ) return $ content ;
238238
239+ foreach ($ new as $ h ) {
239240 $ innerTEXT = trim ($ h ->innertext );
240- $ id = str_replace (' ' ,'_ ' ,$ innerTEXT );
241+ $ id = str_replace (' ' , '_ ' , $ innerTEXT );
241242 $ level = intval ($ h ->tag [1 ]);
242243
243- if ($ level == 2 ){
244+ if ($ level == 2 ) {
244245 $ count_h2 ++;
245246 $ innerTEXT = self ::dec2roman ($ count_h2 ) . ' ' . $ innerTEXT ;
246247 }
247- $ h ->id = $ id ; // add id attribute so we can jump to this element
248+ $ h ->id = $ id ; // add id attribute so we can jump to this element
248249
249- $ h ->innertext = $ innerTEXT ; // add id attribute so we can jump to this element
250+ $ h ->innertext = $ innerTEXT ; // add id attribute so we can jump to this element
250251
251252
252- if ($ level > $ last_level )
253+ if ($ level > $ last_level )
253254 // add class
254255 $ toc .= '<ol> ' ;
255- else {
256+ else {
256257 $ toc .= str_repeat ('</li></ol> ' , $ last_level - $ level );
257258 $ toc .= '</li> ' ;
258259 }
259- if ($ level >= $ last_level ){
260+ if ($ level >= $ last_level ) {
260261 $ toc .= "<li class='toc-level $ level'><a href='# {$ id }'> {$ innerTEXT }</a> " ;
261- }else {
262+ } else {
262263 $ toc .= "<li><a href='# {$ id }'> {$ innerTEXT }</a> " ;
263264 }
264265
@@ -269,7 +270,7 @@ public static function create_toc_with_dom($content)
269270 $ toc .= str_repeat ('</li></ol> ' , $ last_level );
270271 $ toc .= '</div></div> ' ;
271272
272- return $ toc . "<hr> " . $ html ->save ();
273+ return $ toc . "\n\n\n\n <!- toc end -> \n\n <hr> " . $ html ->save ();
273274
274275 }
275276
@@ -283,24 +284,23 @@ public static function dec2roman($f)
283284 {
284285 $ old_k = '' ;
285286 // Return false if either $f is not a real number, $f is bigger than 3999 or $f is lower or equal to 0:
286- if (!is_numeric ($ f ) || $ f > 3999 || $ f <= 0 ) return false ;
287+ if (!is_numeric ($ f ) || $ f > 3999 || $ f <= 0 ) return false ;
287288
288289 // Define the roman figures:
289290 $ roman = array ('M ' => 1000 , 'D ' => 500 , 'C ' => 100 , 'L ' => 50 , 'X ' => 10 , 'V ' => 5 , 'I ' => 1 );
290291
291292 // Calculate the needed roman figures:
292- foreach ($ roman as $ k => $ v ) if (($ amount [$ k ] = floor ($ f / $ v )) > 0 ) $ f -= $ amount [$ k ] * $ v ;
293+ foreach ($ roman as $ k => $ v ) if (($ amount [$ k ] = floor ($ f / $ v )) > 0 ) $ f -= $ amount [$ k ] * $ v ;
293294
294295 // Build the string:
295296 $ return = '' ;
296- foreach ($ amount as $ k => $ v )
297- {
297+ foreach ($ amount as $ k => $ v ) {
298298 $ return .= $ v <= 3 ? str_repeat ($ k , $ v ) : $ k . $ old_k ;
299299 $ old_k = $ k ;
300300 }
301301
302302 // Replace some spacial cases and return the string:
303- return str_replace (array ('VIV ' ,'LXL ' ,'DCD ' ), array ('IX ' ,'XC ' ,'CM ' ), $ return . '. ' );
303+ return str_replace (array ('VIV ' , 'LXL ' , 'DCD ' ), array ('IX ' , 'XC ' , 'CM ' ), $ return . '. ' );
304304 }
305305
306306}
0 commit comments