Commit | Line | Data |
---|---|---|
6855525e JL |
1 | <?php |
2 | ||
3 | /** | |
4 | * | |
5 | * Parse for links to wiki pages. | |
6 | * | |
7 | * @category Text | |
8 | * | |
9 | * @package Text_Wiki | |
10 | * | |
11 | * @author Paul M. Jones <pmjones@php.net> | |
12 | * | |
13 | * @license LGPL | |
14 | * | |
15 | * @version $Id: Wikilink.php,v 1.4 2005/02/23 17:38:29 pmjones Exp $ | |
16 | * | |
17 | */ | |
18 | ||
19 | /** | |
20 | * | |
21 | * Parse for links to wiki pages. | |
22 | * | |
23 | * Wiki page names are typically in StudlyCapsStyle made of | |
24 | * WordsSmashedTogether. | |
25 | * | |
26 | * You can also create described links to pages in this style: | |
27 | * [WikiPageName nice text link to use for display] | |
28 | * | |
29 | * The token options for this rule are: | |
30 | * | |
31 | * 'page' => the wiki page name. | |
32 | * | |
33 | * 'text' => the displayed link text. | |
34 | * | |
35 | * 'anchor' => a named anchor on the target wiki page. | |
36 | * | |
37 | * @category Text | |
38 | * | |
39 | * @package Text_Wiki | |
40 | * | |
41 | * @author Paul M. Jones <pmjones@php.net> | |
42 | * | |
43 | */ | |
44 | ||
45 | class Text_Wiki_Parse_Wikilink extends Text_Wiki_Parse { | |
46 | ||
47 | var $conf = array ( | |
48 | 'ext_chars' => false | |
49 | ); | |
50 | ||
51 | /** | |
52 | * | |
53 | * Constructor. | |
54 | * | |
55 | * We override the Text_Wiki_Parse constructor so we can | |
56 | * explicitly comment each part of the $regex property. | |
57 | * | |
58 | * @access public | |
59 | * | |
60 | * @param object &$obj The calling "parent" Text_Wiki object. | |
61 | * | |
62 | */ | |
63 | ||
64 | function Text_Wiki_Parse_Wikilink(&$obj) | |
65 | { | |
66 | parent::Text_Wiki_Parse($obj); | |
67 | ||
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"; | |
75 | } else { | |
76 | // the default character set, should be fine | |
77 | // for most purposes. | |
78 | $upper = "A-Z"; | |
79 | $lower = "a-z0-9"; | |
80 | $either = "A-Za-z0-9"; | |
81 | } | |
82 | ||
83 | // build the regular expression for finding WikiPage names. | |
84 | $this->regex = | |
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) | |
98 | } | |
99 | ||
100 | ||
101 | /** | |
102 | * | |
103 | * First parses for described links, then for standalone links. | |
104 | * | |
105 | * @access public | |
106 | * | |
107 | * @return void | |
108 | * | |
109 | */ | |
110 | ||
111 | function parse() | |
112 | { | |
113 | // described wiki links | |
114 | $tmp_regex = '/\[' . $this->regex . ' (.+?)\]/'; | |
115 | $this->wiki->source = preg_replace_callback( | |
116 | $tmp_regex, | |
117 | array(&$this, 'processDescr'), | |
118 | $this->wiki->source | |
119 | ); | |
120 | ||
121 | // standalone wiki links | |
122 | if ($this->getConf('ext_chars')) { | |
123 | $either = "A-Za-z0-9\xc0-\xfe"; | |
124 | } else { | |
125 | $either = "A-Za-z0-9"; | |
126 | } | |
127 | ||
128 | $tmp_regex = '/(^|[^$either\-_])' . $this->regex . '/'; | |
129 | $this->wiki->source = preg_replace_callback( | |
130 | $tmp_regex, | |
131 | array(&$this, 'process'), | |
132 | $this->wiki->source | |
133 | ); | |
134 | } | |
135 | ||
136 | ||
137 | /** | |
138 | * | |
139 | * Generate a replacement for described links. | |
140 | * | |
141 | * @access public | |
142 | * | |
143 | * @param array &$matches The array of matches from parse(). | |
144 | * | |
145 | * @return A delimited token to be used as a placeholder in | |
146 | * the source text, plus any text priot to the match. | |
147 | * | |
148 | */ | |
149 | ||
150 | function processDescr(&$matches) | |
151 | { | |
152 | // set the options | |
153 | $options = array( | |
154 | 'page' => $matches[1], | |
155 | 'text' => $matches[5], | |
156 | 'anchor' => $matches[3] | |
157 | ); | |
158 | ||
159 | // create and return the replacement token and preceding text | |
160 | return $this->wiki->addToken($this->rule, $options); // . $matches[7]; | |
161 | } | |
162 | ||
163 | ||
164 | /** | |
165 | * | |
166 | * Generate a replacement for standalone links. | |
167 | * | |
168 | * | |
169 | * @access public | |
170 | * | |
171 | * @param array &$matches The array of matches from parse(). | |
172 | * | |
173 | * @return A delimited token to be used as a placeholder in | |
174 | * the source text, plus any text prior to the match. | |
175 | * | |
176 | */ | |
177 | ||
178 | function process(&$matches) | |
179 | { | |
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]; | |
184 | } | |
185 | ||
186 | // set the options | |
187 | $options = array( | |
188 | 'page' => $matches[2], | |
189 | 'text' => $matches[2] . $matches[3], | |
190 | 'anchor' => $matches[3] | |
191 | ); | |
192 | ||
193 | // create and return the replacement token and preceding text | |
194 | return $matches[1] . $this->wiki->addToken($this->rule, $options); | |
195 | } | |
196 | } | |
197 | ?> |