Source for file Toc.php
Documentation is available at Toc.php
// $Id: Toc.php,v 1.1 2004/06/06 15:44:34 pmjones Exp $
* This class implements a Text_Wiki_Parse 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()
// have to get all the heading entries before we can parse properly.
// now parse the source text for TOC entries
* Generates a replacement for the matched text.
* 'type' => ['list_start'|'list_end'|'item_start'|'item_end'|'target']
* 'level' => The heading level (1-6).
* 'count' => Which entry number 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->wiki->addToken (
array ('type' => 'list_start')
foreach ($this->entry as $key => $val) {
'count' => $val['count'],
$output .= $this->wiki->addToken ($this->rule, $options);
$output .= $this->wiki->addToken (
array ('type' => 'item_end')
$output .= $this->wiki->addToken (
$this->rule, 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
$token = $this->wiki->addToken (
'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 entire source text with changed source text
$this->wiki->source = $newsrc;
Documentation generated on Mon, 11 Mar 2019 13:51:29 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|