e26df501230adcbba92071c20986bc3a19d71294
5 * Parse for links to wiki pages.
11 * @author Paul M. Jones <pmjones@php.net>
15 * @version $Id: Wikilink.php,v 1.4 2005/02/23 17:38:29 pmjones Exp $
21 * Parse for links to wiki pages.
23 * Wiki page names are typically in StudlyCapsStyle made of
24 * WordsSmashedTogether.
26 * You can also create described links to pages in this style:
27 * [WikiPageName nice text link to use for display]
29 * The token options for this rule are:
31 * 'page' => the wiki page name.
33 * 'text' => the displayed link text.
35 * 'anchor' => a named anchor on the target wiki page.
41 * @author Paul M. Jones <pmjones@php.net>
45 class Text_Wiki_Parse_Wikilink
extends Text_Wiki_Parse
{
55 * We override the Text_Wiki_Parse constructor so we can
56 * explicitly comment each part of the $regex property.
60 * @param object &$obj The calling "parent" Text_Wiki object.
64 function Text_Wiki_Parse_Wikilink(&$obj)
66 parent
::Text_Wiki_Parse($obj);
68 if ($this->getConf('ext_chars')) {
69 // use an extended character set; this should
70 // allow for umlauts and so on. taken from the
71 // Tavi project defaults.php file.
72 $upper = "A-Z\xc0-\xde";
73 $lower = "a-z0-9\xdf-\xfe";
74 $either = "A-Za-z0-9\xc0-\xfe";
76 // the default character set, should be fine
80 $either = "A-Za-z0-9";
83 // build the regular expression for finding WikiPage names.
85 "(!?" . // START WikiPage pattern (1)
86 "[$upper]" . // 1 upper
87 "[$either]*" . // 0+ alpha or digit
88 "[$lower]+" . // 1+ lower or digit
89 "[$upper]" . // 1 upper
90 "[$either]*" . // 0+ or more alpha or digit
91 ")" . // END WikiPage pattern (/1)
92 "((\#" . // START Anchor pattern (2)(3)
93 "[$either]" . // 1 alpha
94 "(" . // start sub pattern (4)
95 "[-_$either:.]*" . // 0+ dash, alpha, digit, underscore, colon, dot
96 "[-_$either]" . // 1 dash, alpha, digit, or underscore
97 ")?)?)"; // end subpatterns (/4)(/3)(/2)
103 * First parses for described links, then for standalone links.
113 // described wiki links
114 $tmp_regex = '/\[' . $this->regex
. ' (.+?)\]/';
115 $this->wiki
->source
= preg_replace_callback(
117 array(&$this, 'processDescr'),
121 // standalone wiki links
122 if ($this->getConf('ext_chars')) {
123 $either = "A-Za-z0-9\xc0-\xfe";
125 $either = "A-Za-z0-9";
128 $tmp_regex = '/(^|[^$either\-_])' . $this->regex
. '/';
129 $this->wiki
->source
= preg_replace_callback(
131 array(&$this, 'process'),
139 * Generate a replacement for described links.
143 * @param array &$matches The array of matches from parse().
145 * @return A delimited token to be used as a placeholder in
146 * the source text, plus any text priot to the match.
150 function processDescr(&$matches)
154 'page' => $matches[1],
155 'text' => $matches[5],
156 'anchor' => $matches[3]
159 // create and return the replacement token and preceding text
160 return $this->wiki
->addToken($this->rule
, $options); // . $matches[7];
166 * Generate a replacement for standalone links.
171 * @param array &$matches The array of matches from parse().
173 * @return A delimited token to be used as a placeholder in
174 * the source text, plus any text prior to the match.
178 function process(&$matches)
180 // when prefixed with !, it's explicitly not a wiki link.
181 // return everything as it was.
182 if ($matches[2]{0} == '!') {
183 return $matches[1] . substr($matches[2], 1) . $matches[3];
188 'page' => $matches[2],
189 'text' => $matches[2] . $matches[3],
190 'anchor' => $matches[3]
193 // create and return the replacement token and preceding text
194 return $matches[1] . $this->wiki
->addToken($this->rule
, $options);