Source for file toc.php
Documentation is available at toc.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Paul M. Jones <pmjones@ciaweb.net> |
// +----------------------------------------------------------------------+
// $Id: toc.php,v 1.3 2004/03/02 22:37:18 pmjones Exp $
* This class implements a Text_Wiki_Rule to find all heading tokens and
* build a table of contents. The [[toc]] tag gets replaced with a list
* of all the level-2 through level-6 headings.
* @author Paul M. Jones <pmjones@ciaweb.net>
* The regular expression used to parse the source text and find
* matches conforming to this rule. Used by the parse() method.
var $regex = "/\[\[toc\]\]/m";
* The collection of headings (text and levels).
* Custom parsing (have to process heading entries first).
* @see Text_Wiki::parse()
* Generates a replacement for the matched text. Token options are:
* 'type' => ['list_start'|'list_end'|'item_end'|'item_end'|'target']
* 'level' => The heading level (1-6).
* 'count' => Which heading this is in the list.
* @param array &$matches The array of matches from parse().
* @return string A token indicating the TOC collection point.
$output = $this->addToken(array ('type' => 'list_start'));
foreach ($this->entry as $key => $val) {
'count' => $val['count'],
$output .= $this->addToken(array ('type' => 'item_end'));
$output .= $this->addToken(array ('type' => 'list_end'));
* Finds all headings in the text and saves them in $this->entry.
$delim = $this->_wiki->delim;
// list of all TOC entries (h2, h3, etc)
// the new source text with TOC entry tokens
// when passing through the parsed source text, keep track of when
// we are in a delimited section
// when in a delimited section, capture the token key number
// pass through the parsed source text character by character
for ($i = 0; $i < $k; $i++ ) {
$char = $this->_wiki->_source {$i};
// are alredy in a delimited section?
// yes; are we ending the section?
// yes, get the replacement text for the delimited
// token number and unset the flag.
$rule = $this->_wiki->_tokens [$key][0 ];
$opts = $this->_wiki->_tokens [$key][1 ];
// is the key a start heading token
if ($rule == 'heading' &&
$opts['type'] == 'start' &&
// yes, add a TOC target link to the
'level' => $opts['level']
// ... and to the new source, before the
$newsrc .= $token . $delim . $key . $delim;
'level' => $opts['level'],
// increase the count for the next entry
// not a heading-start of 2 or deeper.
// re-add the delimited token number
// as it was in the original source.
$newsrc .= $delim . $key . $delim;
// no, add to the delimited token key number
// not currently in a delimited section.
// are we starting into a delimited section?
// yes, reset the previous key and
// no, add to the output as-is
// replace with changed source text
$this->_wiki->_source = $newsrc;
// has problems mistaking marked-up numbers for delimited tokens
// creates target tokens, retrieves heading level and text
// loop through all tokens and get headings
foreach ($this->_wiki->_tokens as $key => $val) {
// only get heading starts of level 2 or deeper
if ($val[0] == 'heading' &&
$val[1]['type'] == 'start' &&
// the level of this header
$level = $val[1]['level'];
// the text of this header
// add a toc-target link to the tokens array
$token = $this->addToken(
// put the toc target token in front of the
$start = $delim . $key . $delim;
$this->_wiki->_source = str_replace($start, $token.$start,
// increase the count for the next item
* Renders a token into text matching the requested format.
* @param array $options The "options" portion of the token (second
* @return string The text rendered from the token options.
// the prefix used for anchor names
// ... generate an anchor.
return " <a name=\"$prefix$count\"></a>";
if ($type == 'list_start') {
if ($type == 'list_end') {
if ($type == 'item_start') {
// build some indenting spaces for the text
$indent = ($level - 2 ) * 4;
// add an extra linebreak in front of heading-2
// entries (makes for nice section separations)
if ($level <= 2 && $count > 0 ) {
// create the entry line as a link to the toc anchor
return " $pad<a href=\"#$prefix$count\">";
if ($type == 'item_end') {
Documentation generated on Mon, 11 Mar 2019 10:14:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|