|
| 1 | +<?php |
| 2 | +// vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: |
| 3 | +/** |
| 4 | + * Baseline rule class for extension into a "real" parser component. |
| 5 | + * |
| 6 | + * PHP versions 4 and 5 |
| 7 | + * |
| 8 | + * @category Text |
| 9 | + * @package Text_Wiki |
| 10 | + * @author Paul M. Jones <[email protected]> |
| 11 | + * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 |
| 12 | + * @version $Id$ |
| 13 | + * @link http://pear.php.net/package/Text_Wiki |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * Baseline rule class for extension into a "real" parser component. |
| 18 | + * |
| 19 | + * Text_Wiki_Rule classes do not stand on their own; they are called by a |
| 20 | + * Text_Wiki object, typcially in the transform() method. Each rule class |
| 21 | + * performs three main activities: parse, process, and render. |
| 22 | + * |
| 23 | + * The parse() method takes a regex and applies it to the whole block of |
| 24 | + * source text at one time. Each match is sent as $matches to the |
| 25 | + * process() method. |
| 26 | + * |
| 27 | + * The process() method acts on the matched text from the source, and |
| 28 | + * then processes the source text is some way. This may mean the |
| 29 | + * creation of a delimited token using addToken(). In every case, the |
| 30 | + * process() method returns the text that should replace the matched text |
| 31 | + * from parse(). |
| 32 | + * |
| 33 | + * @category Text |
| 34 | + * @package Text_Wiki |
| 35 | + * @author Paul M. Jones <[email protected]> |
| 36 | + * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 |
| 37 | + * @version Release: @package_version@ |
| 38 | + * @link http://pear.php.net/package/Text_Wiki |
| 39 | + */ |
| 40 | +class Text_Wiki_Parse { |
| 41 | + |
| 42 | + /** |
| 43 | + * |
| 44 | + * Configuration options for this parser rule. |
| 45 | + * |
| 46 | + * @access public |
| 47 | + * |
| 48 | + * @var string |
| 49 | + * |
| 50 | + */ |
| 51 | + |
| 52 | + public $conf = array(); |
| 53 | + |
| 54 | + /** |
| 55 | + * |
| 56 | + * Regular expression to find matching text for this rule. |
| 57 | + * |
| 58 | + * @access public |
| 59 | + * |
| 60 | + * @var string |
| 61 | + * |
| 62 | + * @see parse() |
| 63 | + * |
| 64 | + */ |
| 65 | + |
| 66 | + public $regex = null; |
| 67 | + |
| 68 | + /** |
| 69 | + * |
| 70 | + * The name of this rule for new token array elements. |
| 71 | + * |
| 72 | + * @access public |
| 73 | + * |
| 74 | + * @var string |
| 75 | + * |
| 76 | + */ |
| 77 | + |
| 78 | + public $rule = null; |
| 79 | + |
| 80 | + /** |
| 81 | + * |
| 82 | + * A reference to the calling Text_Wiki object. |
| 83 | + * |
| 84 | + * This is needed so that each rule has access to the same source |
| 85 | + * text, token set, URLs, interwiki maps, page names, etc. |
| 86 | + * |
| 87 | + * @access public |
| 88 | + * |
| 89 | + * @var object |
| 90 | + */ |
| 91 | + |
| 92 | + public $wiki = null; |
| 93 | + |
| 94 | + /** |
| 95 | + * |
| 96 | + * Constructor for this parser rule. |
| 97 | + * |
| 98 | + * @access public |
| 99 | + * |
| 100 | + * @param object &$obj The calling "parent" Text_Wiki object. |
| 101 | + * |
| 102 | + */ |
| 103 | + |
| 104 | + function Text_Wiki_Parse(&$obj) |
| 105 | + { |
| 106 | + // this allows us access to the shared source text, token |
| 107 | + // array, etc. |
| 108 | + $this->wiki =& $obj; |
| 109 | + |
| 110 | + // set the name of this rule; generally used when adding |
| 111 | + // to the tokens array. strip off the Text_Wiki_Parse_ portion. |
| 112 | + // text_wiki_parse_ |
| 113 | + // 0123456789012345 |
| 114 | + $tmp = substr(get_class($this), 16); |
| 115 | + $this->rule = ucwords(strtolower($tmp)); |
| 116 | + |
| 117 | + // override config options for the rule if specified |
| 118 | + if (isset($this->wiki->parseConf[$this->rule]) && |
| 119 | + is_array($this->wiki->parseConf[$this->rule])) { |
| 120 | + |
| 121 | + $this->conf = array_merge( |
| 122 | + $this->conf, |
| 123 | + $this->wiki->parseConf[$this->rule] |
| 124 | + ); |
| 125 | + |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * |
| 131 | + * Abstrct method to parse source text for matches. |
| 132 | + * |
| 133 | + * Applies the rule's regular expression to the source text, passes |
| 134 | + * every match to the process() method, and replaces the matched text |
| 135 | + * with the results of the processing. |
| 136 | + * |
| 137 | + * @access public |
| 138 | + * |
| 139 | + * @see Text_Wiki_Parse::process() |
| 140 | + * |
| 141 | + */ |
| 142 | + |
| 143 | + function parse() |
| 144 | + { |
| 145 | + if($this->regex){ |
| 146 | + $this->wiki->source = preg_replace_callback( |
| 147 | + $this->regex, |
| 148 | + array(&$this, 'process'), |
| 149 | + $this->wiki->source |
| 150 | + ); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * |
| 156 | + * Abstract method to generate replacements for matched text. |
| 157 | + * |
| 158 | + * @access public |
| 159 | + * |
| 160 | + * @param array $matches An array of matches from the parse() method |
| 161 | + * as generated by preg_replace_callback. $matches[0] is the full |
| 162 | + * matched string, $matches[1] is the first matched pattern, |
| 163 | + * $matches[2] is the second matched pattern, and so on. |
| 164 | + * |
| 165 | + * @return string The processed text replacement; defaults to the |
| 166 | + * full matched string (i.e., no changes to the text). |
| 167 | + * |
| 168 | + * @see Text_Wiki_Parse::parse() |
| 169 | + * |
| 170 | + */ |
| 171 | + |
| 172 | + function process(&$matches) |
| 173 | + { |
| 174 | + return $matches[0]; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * |
| 179 | + * Simple method to safely get configuration key values. |
| 180 | + * |
| 181 | + * @access public |
| 182 | + * |
| 183 | + * @param string $key The configuration key. |
| 184 | + * |
| 185 | + * @param mixed $default If the key does not exist, return this value |
| 186 | + * instead. |
| 187 | + * |
| 188 | + * @return mixed The configuration key value (if it exists) or the |
| 189 | + * default value (if not). |
| 190 | + * |
| 191 | + */ |
| 192 | + |
| 193 | + function getConf($key, $default = null) |
| 194 | + { |
| 195 | + if (isset($this->conf[$key])) { |
| 196 | + return $this->conf[$key]; |
| 197 | + } else { |
| 198 | + return $default; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * |
| 204 | + * Extract 'attribute="value"' portions of wiki markup. |
| 205 | + * |
| 206 | + * This kind of markup is typically used only in macros, but is useful |
| 207 | + * anywhere. |
| 208 | + * |
| 209 | + * The syntax is pretty strict; there can be no spaces between the |
| 210 | + * option name, the equals, and the first double-quote; the value |
| 211 | + * must be surrounded by double-quotes. You can escape characters in |
| 212 | + * the value with a backslash, and the backslash will be stripped for |
| 213 | + * you. |
| 214 | + * |
| 215 | + * @access public |
| 216 | + * |
| 217 | + * @param string $text The "attributes" portion of markup. |
| 218 | + * |
| 219 | + * @return array An associative array of key-value pairs where the |
| 220 | + * key is the option name and the value is the option value. |
| 221 | + * |
| 222 | + */ |
| 223 | + |
| 224 | + function getAttrs($text) |
| 225 | + { |
| 226 | + $tmp = explode('="', trim($text)); |
| 227 | + |
| 228 | + // basic setup |
| 229 | + $k = count($tmp) - 1; |
| 230 | + $attrs = array(); |
| 231 | + $key = null; |
| 232 | + |
| 233 | + // loop through the sections |
| 234 | + foreach ($tmp as $i => $val) { |
| 235 | + |
| 236 | + // first element is always the first key |
| 237 | + if ($i == 0) { |
| 238 | + $key = trim($val); |
| 239 | + continue; |
| 240 | + } |
| 241 | + |
| 242 | + // find the last double-quote in the value. |
| 243 | + // the part to the left is the value for the last key, |
| 244 | + // the part to the right is the next key name |
| 245 | + $pos = strrpos($val, '"'); |
| 246 | + $attrs[$key] = stripslashes(substr($val, 0, $pos)); |
| 247 | + $key = trim(substr($val, $pos+1)); |
| 248 | + |
| 249 | + } |
| 250 | + |
| 251 | + return $attrs; |
| 252 | + |
| 253 | + } |
| 254 | +} |
0 commit comments