cvs: pear /Text_Wiki/Text/Wiki/Render/Docbook Heading.php

From: Date: Mon, 06 Feb 2006 20:30:14 +0000
Subject: cvs: pear /Text_Wiki/Text/Wiki/Render/Docbook Heading.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-38024@lists.php.net to get a copy of this message
toggg Mon Feb 6 20:30:14 2006 UTC Modified files: /pear/Text_Wiki/Text/Wiki/Render/Docbook Heading.php Log: Makes the sectioning configurable and the section levels sequential (a special dedicace for mj :) ) http://cvs.php.net/viewcvs.cgi/pear/Text_Wiki/Text/Wiki/Render/Docbook/Heading.php?r1=1.1&r2=1.2&diff_format=u Index: pear/Text_Wiki/Text/Wiki/Render/Docbook/Heading.php diff -u pear/Text_Wiki/Text/Wiki/Render/Docbook/Heading.php:1.1 pear/Text_Wiki/Text/Wiki/Render/Docbook/Heading.php:1.2 --- pear/Text_Wiki/Text/Wiki/Render/Docbook/Heading.php:1.1 Fri Feb 3 18:49:34 2006 +++ pear/Text_Wiki/Text/Wiki/Render/Docbook/Heading.php Mon Feb 6 20:30:14 2006 @@ -9,7 +9,7 @@ * @package Text_Wiki_Docbook * @author bertrand Gugger <bertrand@toggg.com> * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 - * @version CVS: $Id: Heading.php,v 1.1 2006/02/03 18:49:34 toggg Exp $ + * @version CVS: $Id: Heading.php,v 1.2 2006/02/06 20:30:14 toggg Exp $ * @link http://pear.php.net/package/Text_Wiki_Docbook */ @@ -25,19 +25,160 @@ */ class Text_Wiki_Render_Docbook_Heading extends Text_Wiki_Render { + /** + * Configuration keys for this rule + * 'sections' => array of strings, ordered list of sectioning tags, + * the first (mandatory) one will apply to the whole doc + * 'section_after' => string, the section optionnaly used for next, non terminal levels + * 'section_end' => string, the section optionnaly used for the terminal level + * + * @access public + * @var array 'config-key' => mixed config-value + */ + var $conf = array( + 'sections' => array('sect1', 'sect2', 'sect3', 'sect4', 'sect5'), + 'section_after' => 'section', + 'section_final' => 'simplesect' + ); + + /** + * Current level + * + * @access private + * @var int current section level + */ + var $_level = 0; + + /** + * Parsed heading levels stack + * + * @access private + * @var array of int parsed heading levels stack + */ + var $_stack = array(-1); + + /** + * Parsed heading ids stack + * + * @access private + * @var array of int parsed heading ids stack + */ + var $_id = array(-1); + + /** + * Final sectioning to apply + * + * @access private + * @var array of string section tags + */ + var $_section = array(); + + /** + * Constructor. + * We override the constructor to pre-process the heading tokens + * - to correct levels as sequential + * - mark the terminal ones + * - prepare the actual sections to be used + * + * @param object &$obj the base conversion handler + * @return The render object + * @access public + */ + function Text_Wiki_Render_Docbook_Heading(&$obj) + { + parent::Text_Wiki_Render($obj); + $max = 0; + foreach ($this->wiki->getTokens('Heading') as $key => $val) { + if ($val[1]['type'] == 'start') { + // preceding branch finished ? + if ($val[1]['level'] <= $this->_stack[$this->_level]) { + $this->wiki->tokens[$this->_id[$this->_level]][1]['terminal'] = true; + --$this->_level; + } + // more finished ? + while ($val[1]['level'] <= $this->_stack[$this->_level]) { + --$this->_level; + } + // starting section + $this->_stack[++$this->_level] = $val[1]['level']; + $this->_id[$this->_level] = $key; + if ($this->_level > $max) { + $max = $this->_level; + } + } + // adjust level sequentially + $val[1]['level'] = $this->_level; + $this->wiki->setToken($key, 'Heading', $val[1]); + } + // finish last branch + if ($this->_level > 0) { + $this->wiki->tokens[$this->_id[$this->_level]][1]['terminal'] = true; + } + // set global sections for process closure + $sections = $this->getConf('sections', array('')); + $this->wiki->source = + // will produce one only if not blank section + $this->wiki->addToken($this->rule, + array('type' => 'start', + 'level' => 0, + 'id' => 'global', + 'text' => '')) . + $this->wiki->addToken($this->rule, + array('type' => 'end', + 'level' => 0)) . + $this->wiki->source . + // will produce nothing but the closure of preceding sections + $this->wiki->addToken($this->rule, + array('type' => 'start', + 'level' => -1)); + // prepare final sectioning + if ( ! ($after = $this->getConf('section_after', ''))) { + $after = $sections[count($sections) - 1]; + } + for ($i = 0; $i <= $max; $i++) { + if ($i < count($sections)) { + $this->_section[$i] = $sections[$i]; + } else { + $this->_section[$i] = $after; + } + } + // clean stack + $this->_stack = array(); + $this->_level = -1; + } + + /** + * Replace start heading tokens with closure of preceding sections + * and opening corresponding section if not blank or the last level -1 one + * + * @param array $option the token options + * @return string the replacement text + * @access public + */ function token($options) { - // get nice variable names (id, type, level) + // get nice variable names (id, type, level, terminal) + $terminal = false; extract($options); + if ($terminal) { + $terminal = $this->getConf('section_final', ''); + } - switch($type) { - case 'start': - return $level < 6 ? '<sect'. $level . ' xml:id="' . $id . '">' . "\n" : - "<simplesect>\n"; - - case 'end': - return $level < 6 ? '</sect'. $level . ">\n" : "</simplesect>\n"; + if ($type == 'end') { + return $this->_stack[$this->_level] ? "</title>\n" : ''; + } + $output = ''; + // sections to finish ? + while ($this->_level >= 0 && $level <= $this->_level) { + $output .= $this->_stack[$this->_level] ? + '</' . $this->_stack[$this->_level] . ">\n" : ''; + --$this->_level; } + $this->_stack[++$this->_level] = $level < 0 ? '' : + ($terminal ? $terminal : $this->_section[$level]); + return $output . ($this->_stack[$this->_level] ? + '<'. $this->_stack[$this->_level] . ' xml:id="' . $id . + "\">\n<title>" : ''); } } ?>

« previous php.pear.cvs (#38024) next »