1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
<?php
/**
* The Text_reST:: class represents a parse node of a reStructuredText
* document and provides an API for parsing reStructuredText documents.
*
* $Horde: framework/Text_reST/reST.php,v 1.5.10.1 2005/01/03 12:19:17 jan Exp $
*
* Copyright 2003-2005 Jason M. Felice <jfelice@cronosys.com>
*
* See the enclosed file COPYING for license information (LGPL). If you did not
* receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Jason M. Felice <jfelice@cronosys.com>
* @version $Revision: 1.5.10.1 $
* @package Text_reST
*/
class Text_reST {
/**
* The parse node type.
*
* @access private
* @var string $_type
*/
var $_type;
/**
* An array of the parse node's children.
*
* @access private
* @var string $_children
*/
var $_children = array();
/**
* A hash of parse node properties.
*
* @access private
* @var array $_properties
*/
var $_properties = array();
/**
* Constructor.
*
* @access public
*
* @param string $type This is the node type. The default is 'Document'.
*/
function Text_reST($type = 'Document')
{
$this->_type = $type;
}
/**
* Appends a child parse node to this parse node.
*
* @access public
*
* @param string|Text_reST &$child This is the string or object child
* to append to this parse node.
*/
function appendChild(&$child)
{
$n = count($this->_children);
if (is_string($child) && $n > 0 && is_string($this->_children[$n - 1])) {
$this->_children[$n - 1] .= $child;
} elseif (is_string($child)) {
$this->_children[] = $child;
} else {
$this->_children[] = &$child;
}
}
/**
* Sets the value of a parse node property.
*
* @param string $name The property's name.
* @param string $value The property's value.
*/
function setProperty($name, $value)
{
$this->_properties[$name] = $value;
}
/**
* Retrieves the value of a parse node property.
*
* @param string $name The property's name.
*
* @return string The property's value.
*/
function getProperty($name)
{
if (!array_key_exists($name, $this->_properties)) {
return null;
}
return $this->_properties[$name];
}
/**
* Dumps this parse node and its children.
* This method is for debugging purposes.
*
* @access public
*
* @param int $level This is the indent level of this parse node.
*/
function dump($level = 0)
{
for ($i = 0; $i < $level; $i++) {
echo ' ';
}
echo $this->_type, '::';
ksort($this->_properties);
foreach ($this->_properties as $name => $value) {
echo ' ', $name, '="', preg_replace('/["\\\\]/', '\\$1', $value),
'"';
}
echo "\n";
foreach ($this->_children as $child) {
if (is_string($child)) {
for ($i = 0; $i < ($level + 1); $i++) {
echo ' ';
}
echo '"', preg_replace('/["\\\\]/', '\\$1', $child), "\"\n";
} else {
$child->dump($level + 1);
}
}
}
/**
* Parses a reStructuredText document.
*
* @static
*
* @param string $text This is the text of the document we want to parse.
*
* @return Text_reST The parsed document or PEAR_Error on failure.
*/
function &parse($text)
{
require_once dirname(__FILE__) . '/reST/Parser.php';
$parser = &new Text_reST_Parser();
return $parser->parse($text);
}
}
|