inside your post. Author: Yskin Author URI: http://yskin.net/ Thanks to Table of Contents Generator(http://fucoder.com/code/toc-generator/). License ============================================================================= Copyright (C) 2006 yskin (email: yskins@gmail.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ class TableOfContents { var $toc; var $stack; var $start; var $pos; function toc_filter($content) { // Return original content if no toc tag was found if ( strpos($content, '') === false ) return $content; // 类的对象将被重用,所以*必须*在filter里初始化!否则影响两次执行filter的页面,比如feed。 $this->toc = ''; $this->stack = array(); $this->start = 0; $this->pos = 0; $regex = '|]*)>(.*)|mu';//(.*?) $content = preg_replace_callback($regex, array(&$this, 'replace_heading'), $content); $this->deep_process_toc(); $content = str_replace('', $this->toc, $content); return $content; } function replace_heading($matches) { $toc = &$this->toc; $stack = &$this->stack; $start = &$this->start; $pos = &$this->pos; $level = intval($matches[1])-$start; $text = $matches[3]; if ( empty($stack) ) { $stack = array_fill(0, 6-$start+1, 0); $start = $level; $level = 0; $stack[$level] = 1; $pos = $level; $toc = '
  1. ' . $this->get_toclink($text); } elseif ( $level <= $pos) { while ( $level < $pos ) { $pos--; $toc .= '
'; } $stack[$level]++; $toc .= "\n
  • " . $this->get_toclink($text); } else { $pos++; $stack[$pos]=1; $toc .= "
    1. " . $this->get_toclink($text); } return "get_tocid() . "\"{$matches[2]}>{$matches[3]}"; } function get_tocid() { return 'toc-' . implode('-', array_slice($this->stack, 0, $this->pos + 1)); } function get_toclink($text) { return '$text"; } function deep_process_toc() { $toc = &$this->toc; for ($i = 0; $i <= $this->pos; $i++) $toc .= "
    "; $titlepaddingsize = 2; $insidepaddingsize = 8; $bordercolor = '#D0DDD0'; $bordersize = 1; $backcolor = '#F0FFF0'; $offsetsub = ($insidepaddingsize + $bordersize)*2; $tochead = <<
    Table of Contents
    EOA; $toc = $tochead . $toc . "
    \n"; } } add_filter('the_content', array(new TableOfContents, 'toc_filter'), 9); ?>