* @license PHP License * @package wb * @subpackage Markup */ WBClass::load('WBMarkup_Listener'); /** * Markup Scanner Listener: Toc * * Collect headlines to create table of content when scan is completed * * @version 0.1.0 * @package wb * @subpackage Markup */ class WBMarkup_Listener_Toc extends WBMarkup_Listener { /** * configuration * @var array */ protected $config = array( 'stopat' => '', 'template' => 'default', 'cData' => '', 'tags' => array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ), 'class' => '' ); /** * */ private $run = true; /** * template engine * @var patTemplate */ private $tmpl; /** * collected nodes * @var array */ private $nodes = array(); /** * test handler on end element * */ public function onEndElement($node) { if (!$this->run) { return; } if (!empty($node['ns'])) { return; } if (!in_array($node['tag'], array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'))) { return; } // stop collecting if ($this->config['stopat'] == $node['tag']) { $this->run = false; return; } // only specified tags if (!in_array($node['tag'], $this->config['tags'])) { return; } unset($node['attributes']); $cData = $node['cData']; $cData = implode("\n", $cData); $cData = strip_tags($cData); $cData = str_replace(' ', ' ', $cData); $cData = trim($cData); $node['cData'] = $cData; $cData = substr($cData, 0, 40); $cData = urlencode($cData); $node['cData_url'] = $cData; $this->nodes[] = $node; } /** * called right after scan is complete * * @param string $content */ public function onScanComplete(&$content) { $this->config['count'] = count($this->nodes); if (is_array($this->config['cData'])) { $this->config['cData'] = implode('', $this->config['cData']); } $this->tmpl = WBClass::create('patTemplate'); $this->tmpl->readTemplatesFromInput('Wxml/converter/wb/toc/' . $this->config['template'] . '.tmpl'); $this->tmpl->addGlobalVars($this->config, 'config_'); $this->tmpl->addRows('list_entry', $this->nodes); $content = str_replace($this->config['needle'], $this->tmpl->getParsedTemplate('snippet'), $content); } }