1 <!-- $Id: wiki_transform.php3,v 1.9 2000-06-21 22:57:48 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++) {
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++) {
22 $tmpline = $pagehash["content"][$index];
24 if (!strlen($tmpline) || $tmpline == "\r") {
25 // this is a blank line, send <p>
26 $html .= SetHTMLOutputMode("p", ZERO_DEPTH, 0);
30 /* If your web server is not accessble to the general public, you may
31 allow this code below, which allows embedded HTML. If just anyone can reach
32 your web server it is highly advised that you do not allow this.
34 elseif (preg_match("/(^\|)(.*)/", $tmpline, $matches)) {
36 $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);
43 //////////////////////////////////////////////////////////
44 // New linking scheme: links are in brackets. This will
45 // emulate typical HTML linking as well as Wiki linking.
47 // match anything between brackets except only numbers
49 $numBracketLinks = preg_match_all("/\[.+?\]/", $tmpline, $brktlinks);
50 // sort instead of rsort or "[[link] [link]" will be rendered wrong.
54 for ($i = 0; $i < $numBracketLinks; $i++) {
55 $brktlink = preg_quote($brktlinks[0][$i]);
56 $linktoken = "${FieldSeparator}brkt${i}brkt${FieldSeparator}";
57 $tmpline = preg_replace("|$brktlink|",
62 //////////////////////////////////////////////////////////
63 // replace all URL's with tokens, so we don't confuse them
64 // with Wiki words later. Wiki words in URL's break things.
66 $hasURLs = preg_match_all("/\b($AllowedProtocols):[^\s\<\>\[\]\"'\(\)]*[^\s\<\>\[\]\"'\(\)\,\.\?]/", $tmpline, $urls);
68 // have to sort, otherwise errors creep in when the domain appears
69 // in two consecutive URL's on the same line, but the second is
70 // longer e.g. http://c2.com followed by http://c2.com/wiki
74 for ($i = 0; $i < $hasURLs; $i++) {
75 $inplaceURL = preg_quote($urls[0][$i]);
76 $URLtoken = "${FieldSeparator}${i}${FieldSeparator}";
77 $tmpline = preg_replace("|$inplaceURL|",
82 // escape HTML metachars
83 $tmpline = ereg_replace("[&]", "&", $tmpline);
84 $tmpline = ereg_replace("[>]", ">", $tmpline);
85 $tmpline = ereg_replace("[<]", "<", $tmpline);
87 // four or more dashes to <hr>
88 $tmpline = ereg_replace("^-{4,}", "<hr>", $tmpline);
92 $tmpline = str_replace("%%%", "<br>", $tmpline);
95 $tmpline = preg_replace("|(''''')(.*?)(''''')|",
96 "<strong><em>\\2</em></strong>",
100 $tmpline = preg_replace("|(''')(.*?)(''')|",
101 "<strong>\\2</strong>",
105 $tmpline = preg_replace("|(__)(.*?)(__)|",
106 "<strong>\\2</strong>",
110 $tmpline = preg_replace("|('')(.*?)('')|",
115 // Wikiwords preceeded by a '!' are not linked
116 if (preg_match_all("#!?\b(([A-Z][a-z]+){2,})\b#",
118 // uniq the list of matches
120 for ($i = 0; $link[0][$i]; $i++) {
121 // $realfile = $link[0][$i];
122 $hash[$link[0][$i]]++;
125 // all '!WikiName' entries are sorted first
127 while (list($realfile, $val) = each($hash)) {
128 if (strstr($realfile, '!')) {
129 $tmpline = str_replace($realfile,
130 "${FieldSeparator}nlnk${FieldSeparator}" .
131 substr($realfile, 1),
134 elseif (IsWikiPage($dbi, $realfile)) {
135 $tmpline = preg_replace(
136 "#([^$FieldSeparator]|^)\b$realfile\b#",
137 "\\1" . LinkExistingWikiWord($realfile),
140 $tmpline = preg_replace(
141 "#([^$FieldSeparator]|^)\b$realfile\b#",
142 "\\1" . LinkUnknownWikiWord($realfile),
146 // get rid of placeholders
147 $tmpline = str_replace("${FieldSeparator}nlnk${FieldSeparator}",
151 ///////////////////////////////////////////////////////
152 // put bracketed links back, linked
153 for ($i = 0; $i < $numBracketLinks; $i++) {
154 // forms: [free style text link]
155 // [Named link to site|http://c2.com/]
156 // [mailto:anystylelink@somewhere.com]
157 $brktlink = ParseAndLink($brktlinks[0][$i]);
158 $linktoken = "${FieldSeparator}brkt${i}brkt${FieldSeparator}";
159 $tmpline = preg_replace("|$linktoken|",
164 ///////////////////////////////////////////////////////
165 // put URLs back, linked
166 for ($i = 0; $i < $hasURLs; $i++) {
167 $inplaceURL = LinkURL($urls[0][$i]);
168 $URLtoken = "${FieldSeparator}${i}${FieldSeparator}";
169 $tmpline = preg_replace("|$URLtoken|",
175 // match and replace all user-defined links ([1], [2], [3]...)
176 preg_match_all("|\[(\d+)\]|", $tmpline, $match);
177 if (count($match[0])) {
178 for ($k = 0; $k < count($match[0]); $k++) {
179 if (! empty($embedded[$match[1][$k]])) {
180 $linkpattern = preg_quote($match[0][$k]);
181 $tmpline = preg_replace("|$linkpattern|",
182 $embedded[$match[1][$k]],
188 // HTML modes: pre, unordered/ordered lists, term/def
189 if (preg_match("/(^\t)(.*?)(:\t)(.*$)/", $tmpline, $matches)) {
190 // this is a dictionary list item
191 $html .= SetHTMLOutputMode("dl", SINGLE_DEPTH, 1);
192 $tmpline = "<dt>" . $matches[2] . "<dd>" . $matches[4];
194 // oops, the \d needed to be \d+, thanks alister@minotaur.nu
195 } elseif (preg_match("/(^\t+)(\*|\d+|#)/", $tmpline, $matches)) {
196 // this is part of a list
197 $numtabs = strlen($matches[1]);
198 if ($matches[2] == "*") {
201 $listtag = "ol"; // a rather tacit assumption. oh well.
203 $tmpline = preg_replace("/^(\t+)(\*|\d+|#)/", "", $tmpline);
204 $html .= SetHTMLOutputMode($listtag, SINGLE_DEPTH, $numtabs);
207 } elseif (preg_match("/^\s+/", $tmpline)) {
208 // this is preformatted text, i.e. <pre>
209 $html .= SetHTMLOutputMode("pre", ZERO_DEPTH, 0);
211 } elseif (preg_match("/^(!{1,3})[^!]/", $tmpline, $whichheading)) {
212 // lines starting with !,!!,!!! are headings
213 if($whichheading[1] == '!') $heading = "h3";
214 elseif($whichheading[1] == '!!') $heading = "h2";
215 elseif($whichheading[1] == '!!!') $heading = "h1";
216 $tmpline = preg_replace("/^!+/", "", $tmpline);
217 $html .= SetHTMLOutputMode($heading, ZERO_DEPTH, 0);
220 // it's ordinary output if nothing else
221 $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);
224 $html .= "$tmpline"; // at last, emit the code
228 $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);