1 <!-- $Id: wiki_transform.php3,v 1.8 2000-06-19 20:21:31 ahollosi 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++) {
8 if (! empty($pagehash[$thiskey])) {
9 if (preg_match("/png$/i", $pagehash[$thiskey])) {
11 $embedded[$i] = "<img src='" . $pagehash[$thiskey] . "'>";
13 // ordinary embedded link
14 $embedded[$i] = "<a href='" . $pagehash[$thiskey] . "'>[$i]</a>";
19 $numlines = count($pagehash["content"]);
21 // Loop over all lines of the page and apply transformation rules
22 for ($index = 0; $index < $numlines; $index++) {
23 $tmpline = $pagehash["content"][$index];
25 if (!strlen($tmpline) || $tmpline == "\r") {
26 // this is a blank line, send <p>
27 $html .= SetHTMLOutputMode("p", ZERO_DEPTH, 0);
31 /* If your web server is not accessble to the general public, you may
32 allow this code below, which allows embedded HTML. If just anyone can reach
33 your web server it is highly advised that you do not allow this.
35 elseif (preg_match("/(^\|)(.*)/", $tmpline, $matches)) {
37 $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);
44 //////////////////////////////////////////////////////////
45 // New linking scheme: links are in brackets. This will
46 // emulate typical HTML linking as well as Wiki linking.
48 // match anything between brackets except only numbers
50 $numBracketLinks = preg_match_all("/\[.+?\]/", $tmpline, $brktlinks);
51 // sort instead of rsort or "[[link] [link]" will be rendered wrong.
55 for ($i = 0; $i < $numBracketLinks; $i++) {
56 $brktlink = preg_quote($brktlinks[0][$i]);
57 $linktoken = "${FieldSeparator}brkt${i}brkt${FieldSeparator}";
58 $tmpline = preg_replace("|$brktlink|",
63 //////////////////////////////////////////////////////////
64 // replace all URL's with tokens, so we don't confuse them
65 // with Wiki words later. Wiki words in URL's break things.
67 $hasURLs = preg_match_all("/\b($AllowedProtocols):[^\s\<\>\[\]\"'\(\)]*[^\s\<\>\[\]\"'\(\)\,\.\?]/", $tmpline, $urls);
69 // have to sort, otherwise errors creep in when the domain appears
70 // in two consecutive URL's on the same line, but the second is
71 // longer e.g. http://c2.com followed by http://c2.com/wiki
75 for ($i = 0; $i < $hasURLs; $i++) {
76 $inplaceURL = preg_quote($urls[0][$i]);
77 $URLtoken = "${FieldSeparator}${i}${FieldSeparator}";
78 $tmpline = preg_replace("|$inplaceURL|",
83 // escape HTML metachars
84 $tmpline = ereg_replace("[&]", "&", $tmpline);
85 $tmpline = ereg_replace("[>]", ">", $tmpline);
86 $tmpline = ereg_replace("[<]", "<", $tmpline);
88 // four or more dashes to <hr>
89 $tmpline = ereg_replace("^-{4,}", "<hr>", $tmpline);
93 $tmpline = str_replace("%%%", "<br>", $tmpline);
96 $tmpline = preg_replace("|(''''')(.*?)(''''')|",
97 "<strong><em>\\2</em></strong>",
101 $tmpline = preg_replace("|(''')(.*?)(''')|",
102 "<strong>\\2</strong>",
106 $tmpline = preg_replace("|('')(.*?)('')|",
111 // Wikiwords preceeded by a '!' are not linked
112 if (preg_match_all("#!?\b(([A-Z][a-z]+){2,})\b#",
114 // uniq the list of matches
116 for ($i = 0; $link[0][$i]; $i++) {
117 // $realfile = $link[0][$i];
118 $hash[$link[0][$i]]++;
121 // all '!WikiName' entries are sorted first
123 while (list($realfile, $val) = each($hash)) {
124 if (strstr($realfile, '!')) {
125 $tmpline = str_replace($realfile,
126 "${FieldSeparator}nlnk${FieldSeparator}" .
127 substr($realfile, 1),
130 elseif (IsWikiPage($dbi, $realfile)) {
131 $tmpline = preg_replace(
132 "#([^$FieldSeparator]|^)\b$realfile\b#",
133 "\\1" . LinkExistingWikiWord($realfile),
136 $tmpline = preg_replace(
137 "#([^$FieldSeparator]|^)\b$realfile\b#",
138 "\\1" . LinkUnknownWikiWord($realfile),
142 // get rid of placeholders
143 $tmpline = str_replace("${FieldSeparator}nlnk${FieldSeparator}",
147 ///////////////////////////////////////////////////////
148 // put bracketed links back, linked
149 for ($i = 0; $i < $numBracketLinks; $i++) {
150 // forms: [free style text link]
151 // [Named link to site|http://c2.com/]
152 // [mailto:anystylelink@somewhere.com]
153 $brktlink = ParseAndLink($brktlinks[0][$i]);
154 $linktoken = "${FieldSeparator}brkt${i}brkt${FieldSeparator}";
155 $tmpline = preg_replace("|$linktoken|",
160 ///////////////////////////////////////////////////////
161 // put URLs back, linked
162 for ($i = 0; $i < $hasURLs; $i++) {
163 $inplaceURL = LinkURL($urls[0][$i]);
164 $URLtoken = "${FieldSeparator}${i}${FieldSeparator}";
165 $tmpline = preg_replace("|$URLtoken|",
171 // match and replace all user-defined links ([1], [2], [3]...)
172 preg_match_all("|\[(\d+)\]|", $tmpline, $match);
173 if (count($match[0])) {
174 for ($k = 0; $k < count($match[0]); $k++) {
175 if (! empty($embedded[$match[1][$k]])) {
176 $linkpattern = preg_quote($match[0][$k]);
177 $tmpline = preg_replace("|$linkpattern|",
178 $embedded[$match[1][$k]],
184 // HTML modes: pre, unordered/ordered lists, term/def
185 if (preg_match("/(^\t)(.*?)(:\t)(.*$)/", $tmpline, $matches)) {
186 // this is a dictionary list item
187 $html .= SetHTMLOutputMode("dl", SINGLE_DEPTH, 1);
188 $tmpline = "<dt>" . $matches[2] . "<dd>" . $matches[4];
190 // oops, the \d needed to be \d+, thanks alister@minotaur.nu
191 } elseif (preg_match("/(^\t+)(\*|\d+|#)/", $tmpline, $matches)) {
192 // this is part of a list
193 $numtabs = strlen($matches[1]);
194 if ($matches[2] == "*") {
197 $listtag = "ol"; // a rather tacit assumption. oh well.
199 $tmpline = preg_replace("/^(\t+)(\*|\d+|#)/", "", $tmpline);
200 $html .= SetHTMLOutputMode($listtag, SINGLE_DEPTH, $numtabs);
203 } elseif (preg_match("/^\s+/", $tmpline)) {
204 // this is preformatted text, i.e. <pre>
205 $html .= SetHTMLOutputMode("pre", ZERO_DEPTH, 0);
207 } elseif (preg_match("/^(!{1,3})[^!]/", $tmpline, $whichheading)) {
208 // lines starting with !,!!,!!! are headings
209 if($whichheading[1] == '!') $heading = "h3";
210 elseif($whichheading[1] == '!!') $heading = "h2";
211 elseif($whichheading[1] == '!!!') $heading = "h1";
212 $tmpline = preg_replace("/^!+/", "", $tmpline);
213 $html .= SetHTMLOutputMode($heading, ZERO_DEPTH, 0);
216 // it's ordinary output if nothing else
217 $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);
220 $html .= "$tmpline"; // at last, emit the code
224 $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);