5 * Baseline rule class for extension into a "real" parser component.
8 * Text_Wiki_Parse_* classes do not stand on their own; they are called by a
9 * Text_Wiki object, typcially in the transform() method. Each rule class
10 * performs two separate activities: parse and process.
16 * @author Paul M. Jones <pmjones@php.net>
24 * Baseline rule class for extension into a "real" parser component.
26 * Text_Wiki_Rule classes do not stand on their own; they are called by a
27 * Text_Wiki object, typcially in the transform() method. Each rule class
28 * performs three main activities: parse, process, and render.
31 * The parse() method takes a regex and applies it to the whole block of
32 * source text at one time. Each match is sent as $matches to the
35 * The process() method acts on the matched text from the source, and
36 * then processes the source text is some way. This may mean the
37 * creation of a delimited token using addToken(). In every case, the
38 * process() method returns the text that should replace the matched text
45 * @author Paul M. Jones <pmjones@php.net>
49 class Text_Wiki_Parse
{
54 * Configuration options for this parser rule.
67 * Regular expression to find matching text for this rule.
82 * The name of this rule for new token array elements.
95 * A reference to the calling Text_Wiki object.
97 * This is needed so that each rule has access to the same source
98 * text, token set, URLs, interwiki maps, page names, etc.
110 * Constructor for this parser rule.
114 * @param object &$obj The calling "parent" Text_Wiki object.
118 function Text_Wiki_Parse(&$obj)
120 // set the reference to the calling Text_Wiki object;
121 // this allows us access to the shared source text, token
125 // set the name of this rule; generally used when adding
126 // to the tokens array. strip off the Text_Wiki_Parse_ portion.
129 $tmp = substr(get_class($this), 16);
130 $this->rule
= ucwords(strtolower($tmp));
132 // override config options for the rule if specified
133 if (isset($this->wiki
->parseConf
[$this->rule
]) &&
134 is_array($this->wiki
->parseConf
[$this->rule
])) {
136 $this->conf
= array_merge(
138 $this->wiki
->parseConf
[$this->rule
]
147 * Abstrct method to parse source text for matches.
149 * Applies the rule's regular expression to the source text, passes
150 * every match to the process() method, and replaces the matched text
151 * with the results of the processing.
155 * @see Text_Wiki_Parse::process()
161 $this->wiki
->source
= preg_replace_callback(
163 array(&$this, 'process'),
171 * Abstract method to generate replacements for matched text.
175 * @param array $matches An array of matches from the parse() method
176 * as generated by preg_replace_callback. $matches[0] is the full
177 * matched string, $matches[1] is the first matched pattern,
178 * $matches[2] is the second matched pattern, and so on.
180 * @return string The processed text replacement; defaults to the
181 * full matched string (i.e., no changes to the text).
183 * @see Text_Wiki_Parse::parse()
187 function process(&$matches)
195 * Simple method to safely get configuration key values.
199 * @param string $key The configuration key.
201 * @param mixed $default If the key does not exist, return this value
204 * @return mixed The configuration key value (if it exists) or the
205 * default value (if not).
209 function getConf($key, $default = null
)
211 if (isset($this->conf
[$key])) {
212 return $this->conf
[$key];
221 * Extract 'attribute="value"' portions of wiki markup.
223 * This kind of markup is typically used only in macros, but is useful
226 * The syntax is pretty strict; there can be no spaces between the
227 * option name, the equals, and the first double-quote; the value
228 * must be surrounded by double-quotes. You can escape characters in
229 * the value with a backslash, and the backslash will be stripped for
234 * @param string $text The "attributes" portion of markup.
236 * @return array An associative array of key-value pairs where the
237 * key is the option name and the value is the option value.
241 function getAttrs($text)
243 // find the =" sections;
244 $tmp = explode('="', trim($text));
247 $k = count($tmp) - 1;
251 // loop through the sections
252 foreach ($tmp as $i => $val) {
254 // first element is always the first key
260 // find the last double-quote in the value.
261 // the part to the left is the value for the last key,
262 // the part to the right is the next key name
263 $pos = strrpos($val, '"');
264 $attrs[$key] = stripslashes(substr($val, 0, $pos));
265 $key = trim(substr($val, $pos+
1));