5 * Parse for URLS in the source text.
11 * @author Paul M. Jones <pmjones@php.net>
15 * @version $Id: Url.php,v 1.3 2005/02/23 17:38:29 pmjones Exp $
21 * Parse for URLS in the source text.
23 * Various URL markings are supported: inline (the URL by itself),
24 * numbered or footnote reference (where the URL is enclosed in square
25 * brackets), and named reference (where the URL is enclosed in square
26 * brackets and has a name included inside the brackets). E.g.:
28 * inline -- http://example.com
29 * numbered -- [http://example.com]
30 * described -- [http://example.com Example Description]
32 * When rendering a URL token, this will convert URLs pointing to a .gif,
33 * .jpg, or .png image into an inline <img /> tag (for the 'xhtml'
38 * 'type' => ['inline'|'footnote'|'descr'] the type of URL
40 * 'href' => the URL link href portion
42 * 'text' => the displayed text of the URL link
48 * @author Paul M. Jones <pmjones@php.net>
52 class Text_Wiki_Parse_Url
extends Text_Wiki_Parse
{
57 * Keeps a running count of numbered-reference URLs.
65 var $footnoteCount = 0;
70 * URL schemes recognized by this rule.
94 * We override the constructor so we can comment the regex nicely.
100 function Text_Wiki_Parse_Url(&$obj)
102 parent
::Text_Wiki_Parse($obj);
104 // convert the list of recognized schemes to a regex-safe string,
105 // where the pattern delim is a slash
107 $list = $this->getConf('schemes', array());
108 foreach ($list as $val) {
109 $tmp[] = preg_quote($val, '/');
111 $schemes = implode('|', $tmp);
115 "($schemes)" . // allowed schemes
116 "(" . // start pattern
117 "[^ \\/\"\'{$this->wiki->delim}]*\\/" . // no spaces, backslashes, slashes, double-quotes, single quotes, or delimiters;
118 ")*" . // end pattern
119 "[^ \\t\\n\\/\"\'{$this->wiki->delim}]*" .
120 "[A-Za-z0-9\\/?=&~_]";
126 * Find three different kinds of URLs in the source text.
134 // -------------------------------------------------------------
136 // Described-reference (named) URLs.
139 // the regular expression for this kind of URL
140 $tmp_regex = '/\[(' . $this->regex
. ') ([^\]]+)\]/';
142 // use a custom callback processing method to generate
143 // the replacement text for matches.
144 $this->wiki
->source
= preg_replace_callback(
146 array(&$this, 'processDescr'),
151 // -------------------------------------------------------------
153 // Numbered-reference (footnote-style) URLs.
156 // the regular expression for this kind of URL
157 $tmp_regex = '/\[(' . $this->regex
. ')\]/U';
159 // use a custom callback processing method to generate
160 // the replacement text for matches.
161 $this->wiki
->source
= preg_replace_callback(
163 array(&$this, 'processFootnote'),
168 // -------------------------------------------------------------
170 // Normal inline URLs.
173 // the regular expression for this kind of URL
175 $tmp_regex = '/(^|[^A-Za-z])(' . $this->regex
. ')(.*?)/';
177 // use the standard callback for inline URLs
178 $this->wiki
->source
= preg_replace_callback(
180 array(&$this, 'process'),
188 * Process inline URLs.
190 * @param array &$matches
192 * @param array $matches An array of matches from the parse() method
193 * as generated by preg_replace_callback. $matches[0] is the full
194 * matched string, $matches[1] is the first matched pattern,
195 * $matches[2] is the second matched pattern, and so on.
197 * @return string The processed text replacement.
201 function process(&$matches)
206 'href' => $matches[2],
207 'text' => $matches[2]
211 return $matches[1] . $this->wiki
->addToken($this->rule
, $options) . $matches[5];
217 * Process numbered (footnote) URLs.
220 * @param array &$matches
222 * @param array $matches An array of matches from the parse() method
223 * as generated by preg_replace_callback. $matches[0] is the full
224 * matched string, $matches[1] is the first matched pattern,
225 * $matches[2] is the second matched pattern, and so on.
227 * @return string The processed text replacement.
231 function processFootnote(&$matches)
233 // keep a running count for footnotes
234 $this->footnoteCount++
;
238 'type' => 'footnote',
239 'href' => $matches[1],
240 'text' => $this->footnoteCount
244 return $this->wiki
->addToken($this->rule
, $options);
250 * Process described-reference (named-reference) URLs.
253 * 'type' => ['inline'|'footnote'|'descr'] the type of URL
254 * 'href' => the URL link href portion
255 * 'text' => the displayed text of the URL link
257 * @param array &$matches
259 * @param array $matches An array of matches from the parse() method
260 * as generated by preg_replace_callback. $matches[0] is the full
261 * matched string, $matches[1] is the first matched pattern,
262 * $matches[2] is the second matched pattern, and so on.
264 * @return string The processed text replacement.
268 function processDescr(&$matches)
273 'href' => $matches[1],
274 'text' => $matches[4]
278 return $this->wiki
->addToken($this->rule
, $options);