1 <!-- $Id: wiki_transform.php3,v 1.13 2000-07-12 18:47:53 dairiki Exp $ -->
3 // expects $pagehash and $html to be set
5 // Set up inline links and images
6 for ($i = 1; $i < (NUM_LINKS + 1); $i++) {
7 if (! empty($pagehash['refs'][$i])) {
8 if (preg_match("/png$/i", $pagehash['refs'][$i])) {
10 $embedded[$i] = "<img src='" . $pagehash['refs'][$i] . "'>";
12 // ordinary embedded link
13 $embedded[$i] = "<a href='" . $pagehash['refs'][$i] . "'>[$i]</a>";
18 $numlines = count($pagehash["content"]);
20 // Loop over all lines of the page and apply transformation rules
21 for ($index = 0; $index < $numlines; $index++) {
26 $tmpline = $pagehash["content"][$index];
28 if (!strlen($tmpline) || $tmpline == "\r") {
29 // this is a blank line, send <p>
30 $html .= SetHTMLOutputMode("p", ZERO_DEPTH, 0);
34 /* If your web server is not accessble to the general public, you may
35 allow this code below, which allows embedded HTML. If just anyone can reach
36 your web server it is highly advised that you do not allow this.
38 elseif (preg_match("/(^\|)(.*)/", $tmpline, $matches)) {
40 $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);
47 //////////////////////////////////////////////////////////
48 // New linking scheme: links are in brackets. This will
49 // emulate typical HTML linking as well as Wiki linking.
51 // match anything between brackets except only numbers
53 $numBracketLinks = preg_match_all("/\[.+?\]/", $tmpline, $brktlinks);
54 /* On 12 Jul,2000 Jeff <dairiki@dairiki.org> adds:
56 * Simple sorting doesnt work, since (in ASCII) '[' comes between
57 * the upper- and lower-case characters.
59 * Using sort "[[Link] [Link]" will come out wrong, using
60 * rsort "[[link] [link]" will come out wrong.
61 * (An appropriate usort would work.)
63 * I've added a look-behind assertion to the preg_replace which,
64 * I think, fixes the problem. I only hope that all PHP versions
65 * support look-behind assertions....
66 // sort instead of rsort or "[[link] [link]" will be rendered wrong.
71 for ($i = 0; $i < $numBracketLinks; $i++) {
72 $brktlink = preg_quote($brktlinks[0][$i]);
73 $linktoken = $FieldSeparator . $FieldSeparator . ++$ntokens . $FieldSeparator;
75 * If you're wondering about the double $FieldSeparator,
76 * consider what happens to (the admittedly sick):
77 * "[Link1] [Link2]1[Link3]"
79 * Answer: without the double field separator, it gets
80 * tokenized to "%1% %2%1%3%" (using % to represent $FieldSeparator),
81 * which will get munged as soon as '%1%' is substituted with it's
84 $tmpline = preg_replace("|(?<!\[)$brktlink|",
88 $tokens[] = $linktoken;
89 $replacements[] = ParseAndLink($brktlinks[0][$i]);
92 //////////////////////////////////////////////////////////
93 // replace all URL's with tokens, so we don't confuse them
94 // with Wiki words later. Wiki words in URL's break things.
96 $hasURLs = preg_match_all("/\b($AllowedProtocols):[^\s\<\>\[\]\"'\(\)]*[^\s\<\>\[\]\"'\(\)\,\.\?]/", $tmpline, $urls);
98 // have to sort, otherwise errors creep in when the domain appears
99 // in two consecutive URL's on the same line, but the second is
100 // longer e.g. http://c2.com followed by http://c2.com/wiki
104 for ($i = 0; $i < $hasURLs; $i++) {
105 $inplaceURL = preg_quote($urls[0][$i]);
106 $URLtoken = $FieldSeparator . $FieldSeparator . ++$ntokens . $FieldSeparator;
107 $tmpline = preg_replace("|$inplaceURL|",
111 $tokens[] = $URLtoken;
112 $replacements[] = LinkURL($urls[0][$i]);
115 // escape HTML metachars
116 $tmpline = ereg_replace("[&]", "&", $tmpline);
117 $tmpline = ereg_replace("[>]", ">", $tmpline);
118 $tmpline = ereg_replace("[<]", "<", $tmpline);
120 // four or more dashes to <hr>
121 $tmpline = ereg_replace("^-{4,}", "<hr>", $tmpline);
124 // %%% are linebreaks
125 $tmpline = str_replace("%%%", "<br>", $tmpline);
128 $tmpline = preg_replace("|(''''')(.*?)(''''')|",
129 "<strong><em>\\2</em></strong>",
133 $tmpline = preg_replace("|(''')(.*?)(''')|",
134 "<strong>\\2</strong>",
138 $tmpline = preg_replace("|(__)(.*?)(__)|",
139 "<strong>\\2</strong>",
143 $tmpline = preg_replace("|('')(.*?)('')|",
148 // Wikiwords preceeded by a '!' are not linked
149 if (preg_match_all("#!?\b(([A-Z][a-z]+){2,})\b#",
151 // uniq the list of matches
153 for ($i = 0; $link[0][$i]; $i++) {
154 // $realfile = $link[0][$i];
155 $hash[$link[0][$i]]++;
158 // all '!WikiName' entries are sorted first
160 while (list($realfile, $val) = each($hash)) {
161 $token = $FieldSeparator . $FieldSeparator . ++$ntokens . $FieldSeparator;
162 $tmpline = str_replace($realfile, $token, $tmpline);
164 if (strstr($realfile, '!')) {
165 $replacements[] = substr($realfile, 1);
167 elseif (IsWikiPage($dbi, $realfile)) {
168 $replacements[] = LinkExistingWikiWord($realfile);
170 $replacements[] = LinkUnknownWikiWord($realfile);
175 ///////////////////////////////////////////////////////
177 for ($i = 0; $i < $ntokens; $i++)
178 $tmpline = str_replace($tokens[$i], $replacements[$i], $tmpline);
181 // match and replace all user-defined links ([1], [2], [3]...)
182 preg_match_all("|\[(\d+)\]|", $tmpline, $match);
183 if (count($match[0])) {
184 for ($k = 0; $k < count($match[0]); $k++) {
185 if (! empty($embedded[$match[1][$k]])) {
186 $linkpattern = preg_quote($match[0][$k]);
187 $tmpline = preg_replace("|$linkpattern|",
188 $embedded[$match[1][$k]],
194 // HTML modes: pre, unordered/ordered lists, term/def
195 if (preg_match("/(^\t)(.*?)(:\t)(.*$)/", $tmpline, $matches)) {
196 // this is a dictionary list item
197 $html .= SetHTMLOutputMode("dl", SINGLE_DEPTH, 1);
198 $tmpline = "<dt>" . $matches[2] . "<dd>" . $matches[4];
200 // oops, the \d needed to be \d+, thanks alister@minotaur.nu
201 } elseif (preg_match("/(^\t+)(\*|\d+|#)/", $tmpline, $matches)) {
202 // this is part of a list
203 $numtabs = strlen($matches[1]);
204 if ($matches[2] == "*") {
207 $listtag = "ol"; // a rather tacit assumption. oh well.
209 $tmpline = preg_replace("/^(\t+)(\*|\d+|#)/", "", $tmpline);
210 $html .= SetHTMLOutputMode($listtag, SINGLE_DEPTH, $numtabs);
213 // tabless markup for unordered and ordered lists
215 // first, unordered lists: one or more astericks at the
216 // start of a line indicate a <UL> block
218 } elseif (preg_match("/^([*]+)/", $tmpline, $matches)) {
219 // this is part of an unordered list
220 $numtabs = strlen($matches[1]);
223 $tmpline = preg_replace("/^([*]+)/", "", $tmpline);
224 $html .= SetHTMLOutputMode($listtag, SINGLE_DEPTH, $numtabs);
227 // second, ordered lists <OL>
228 } elseif (preg_match("/^([#]+)/", $tmpline, $matches)) {
229 // this is part of an ordered list
230 $numtabs = strlen($matches[1]);
233 $tmpline = preg_replace("/^([#]+)/", "", $tmpline);
234 $html .= SetHTMLOutputMode($listtag, SINGLE_DEPTH, $numtabs);
238 } elseif (preg_match("/^\s+/", $tmpline)) {
239 // this is preformatted text, i.e. <pre>
240 $html .= SetHTMLOutputMode("pre", ZERO_DEPTH, 0);
242 } elseif (preg_match("/^(!{1,3})[^!]/", $tmpline, $whichheading)) {
243 // lines starting with !,!!,!!! are headings
244 if($whichheading[1] == '!') $heading = "h3";
245 elseif($whichheading[1] == '!!') $heading = "h2";
246 elseif($whichheading[1] == '!!!') $heading = "h1";
247 $tmpline = preg_replace("/^!+/", "", $tmpline);
248 $html .= SetHTMLOutputMode($heading, ZERO_DEPTH, 0);
251 // it's ordinary output if nothing else
252 $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);
255 $tmpline = str_replace("%%Search%%", RenderQuickSearch(), $tmpline);
256 $tmpline = str_replace("%%Fullsearch%%", RenderFullSearch(), $tmpline);
257 $tmpline = str_replace("%%Mostpopular%%", RenderMostPopular(), $tmpline);
263 $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);