1 <!-- $Id: transform.php,v 1.7 2000-11-08 15:40:00 ahollosi Exp $ -->
3 // expects $pagehash and $html to be set
6 function tokenize($str, $pattern, &$orig, &$ntokens) {
7 global $FieldSeparator;
8 // Find any strings in $str that match $pattern and
9 // store them in $tokens[], replacing them with a token
10 // replaced strings are stored in $orig
12 while (preg_match("/^(.*?)($pattern)/", $str, $matches)) {
13 $linktoken = $FieldSeparator . $FieldSeparator . ($ntokens++) . $FieldSeparator;
14 $new .= $matches[1] . $linktoken;
15 $orig[] = $matches[2];
16 $str = substr($str, strlen($matches[0]));
23 // Prepare replacements for references [\d+]
24 for ($i = 1; $i < (NUM_LINKS + 1); $i++) {
25 if (! empty($pagehash['refs'][$i])) {
26 if (preg_match("/($InlineImages)$/i", $pagehash['refs'][$i])) {
28 $embedded[$i] = LinkImage($pagehash['refs'][$i]);
31 $embedded[$i] = "<a href=\"" . $pagehash['refs'][$i] . "\">[$i]</a>";
36 $numlines = count($pagehash["content"]);
38 // only call these once, for efficiency
39 $quick_search_box = RenderQuickSearch();
40 $full_search_box = RenderFullSearch();
41 $most_popular_list = RenderMostPopular();
44 // Loop over all lines of the page and apply transformation rules
45 for ($index = 0; $index < $numlines; $index++) {
49 $replacements = array();
51 $tmpline = $pagehash["content"][$index];
53 if (!strlen($tmpline) || $tmpline == "\r") {
54 // this is a blank line, send <p>
55 $html .= SetHTMLOutputMode("p", ZERO_DEPTH, 0);
59 /* If your web server is not accessble to the general public, you may
60 allow this code below, which allows embedded HTML. If just anyone can reach
61 your web server it is highly advised that you do not allow this.
63 elseif (preg_match("/(^\|)(.*)/", $tmpline, $matches)) {
65 $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);
72 //////////////////////////////////////////////////////////
73 // New linking scheme: links are in brackets. This will
74 // emulate typical HTML linking as well as Wiki linking.
76 // First need to protect [[.
78 $tmpline = tokenize($tmpline, "\[\[", $replacements, $ntokens);
79 while ($oldn < $ntokens)
80 $replacements[$oldn++] = "[";
82 // Now process the [\d+] links which are numeric references
84 $tmpline = tokenize($tmpline, "\[\s*\d+\s*\]", $replacements ,$ntokens);
85 while ($oldn < $ntokens) {
86 $num = (int)substr($replacements[$oldn], 1);
87 if (! empty($embedded[$num]))
88 $replacements[$oldn] = $embedded[$num];
92 // match anything else between brackets
94 $tmpline = tokenize($tmpline, "\[.+?\]", $replacements, $ntokens);
95 while ($oldn < $ntokens) {
96 $link = ParseAndLink($replacements[$oldn]);
97 $replacements[$oldn] = $link['link'];
101 //////////////////////////////////////////////////////////
102 // replace all URL's with tokens, so we don't confuse them
103 // with Wiki words later. Wiki words in URL's break things.
105 $tmpline = tokenize($tmpline, "!?\b($AllowedProtocols):[^\s<>\[\]\"'()]*[^\s<>\[\]\"'(),.?]", $replacements, $ntokens);
106 while ($oldn < $ntokens) {
107 if($replacements[$oldn][0] == '!')
108 $replacements[$oldn] = substr($replacements[$oldn], 1);
110 $replacements[$oldn] = LinkURL($replacements[$oldn]);
114 //////////////////////////////////////////////////////////
116 // Wikiwords preceeded by a '!' are not linked
119 $tmpline = tokenize($tmpline, "!?$WikiNameRegexp", $replacements, $ntokens);
120 while ($oldn < $ntokens) {
121 $old = $replacements[$oldn];
122 if ($old[0] == '!') {
123 $replacements[$oldn] = substr($old,1);
124 } elseif (IsWikiPage($dbi, $old)) {
125 $replacements[$oldn] = LinkExistingWikiWord($old);
127 $replacements[$oldn] = LinkUnknownWikiWord($old);
132 // escape HTML metachars
133 $tmpline = str_replace("&", "&", $tmpline);
134 $tmpline = str_replace(">", ">", $tmpline);
135 $tmpline = str_replace("<", "<", $tmpline);
137 // four or more dashes to <hr>
138 $tmpline = ereg_replace("^-{4,}", "<hr>", $tmpline);
140 // %%% are linebreaks
141 $tmpline = str_replace("%%%", "<br>", $tmpline);
144 $tmpline = preg_replace("|(''''')(.*?)(''''')|",
145 "<strong><em>\\2</em></strong>", $tmpline);
148 $tmpline = preg_replace("|(''')(.*?)(''')|",
149 "<strong>\\2</strong>", $tmpline);
152 $tmpline = preg_replace("|(__)(.*?)(__)|",
153 "<strong>\\2</strong>", $tmpline);
156 $tmpline = preg_replace("|('')(.*?)('')|",
157 "<em>\\2</em>", $tmpline);
160 // HTML modes: pre, unordered/ordered lists, term/def (using TAB)
161 if (preg_match("/(^\t+)(.*?)(:\t)(.*$)/", $tmpline, $matches)) {
162 // this is a dictionary list item
163 $numtabs = strlen($matches[1]);
164 $html .= SetHTMLOutputMode("dl", SINGLE_DEPTH, $numtabs);
166 if(trim($matches[2]))
167 $tmpline = "<dt>" . $matches[2];
168 $tmpline .= "<dd>" . $matches[4];
170 } elseif (preg_match("/(^\t+)(\*|\d+|#)/", $tmpline, $matches)) {
171 // this is part of a list
172 $numtabs = strlen($matches[1]);
173 if ($matches[2] == "*") {
176 $listtag = "ol"; // a rather tacit assumption. oh well.
178 $tmpline = preg_replace("/^(\t+)(\*|\d+|#)/", "", $tmpline);
179 $html .= SetHTMLOutputMode($listtag, SINGLE_DEPTH, $numtabs);
182 // tabless markup for unordered and ordered lists
183 // list types can be mixed, so we only look at the last
184 // character. Changes e.g. from "**#*" to "###*" go unnoticed.
185 // and wouldn't make a difference to the HTML layout anyway.
187 // unordered lists <UL>: "*"
188 } elseif (preg_match("/^([#*]*\*)[^#]/", $tmpline, $matches)) {
189 // this is part of an unordered list
190 $numtabs = strlen($matches[1]);
193 $tmpline = preg_replace("/^([#*]*\*)/", "", $tmpline);
194 $html .= SetHTMLOutputMode($listtag, SINGLE_DEPTH, $numtabs);
197 // ordered lists <OL>: "#"
198 } elseif (preg_match("/^([#*]*\#)/", $tmpline, $matches)) {
199 // this is part of an ordered list
200 $numtabs = strlen($matches[1]);
203 $tmpline = preg_replace("/^([#*]*\#)/", "", $tmpline);
204 $html .= SetHTMLOutputMode($listtag, SINGLE_DEPTH, $numtabs);
207 // definition lists <DL>: ";text:text"
208 } elseif (preg_match("/(^;+)(.*?):(.*$)/", $tmpline, $matches)) {
209 // this is a dictionary list item
210 $numtabs = strlen($matches[1]);
211 $html .= SetHTMLOutputMode("dl", SINGLE_DEPTH, $numtabs);
213 if(trim($matches[2]))
214 $tmpline = "<dt>" . $matches[2];
215 $tmpline .= "<dd>" . $matches[3];
218 } elseif (preg_match("/^\s+/", $tmpline)) {
219 // this is preformatted text, i.e. <pre>
220 $html .= SetHTMLOutputMode("pre", ZERO_DEPTH, 0);
222 } elseif (preg_match("/^(!{1,3})[^!]/", $tmpline, $whichheading)) {
223 // lines starting with !,!!,!!! are headings
224 if($whichheading[1] == '!') $heading = "h3";
225 elseif($whichheading[1] == '!!') $heading = "h2";
226 elseif($whichheading[1] == '!!!') $heading = "h1";
227 $tmpline = preg_replace("/^!+/", "", $tmpline);
228 $html .= SetHTMLOutputMode($heading, ZERO_DEPTH, 0);
231 // it's ordinary output if nothing else
232 $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);
235 $tmpline = str_replace("%%Search%%", $quick_search_box, $tmpline);
236 $tmpline = str_replace("%%Fullsearch%%", $full_search_box, $tmpline);
237 $tmpline = str_replace("%%Mostpopular%%", $most_popular_list, $tmpline);
238 if(defined('WIKI_ADMIN') && strstr($tmpline, "%%ADMIN-"))
239 $tmpline = ParseAdminTokens($tmpline);
241 ///////////////////////////////////////////////////////
244 for ($i = 0; $i < $ntokens; $i++)
245 $tmpline = str_replace($FieldSeparator.$FieldSeparator.$i.$FieldSeparator, $replacements[$i], $tmpline);
248 $html .= "$tmpline\n";
251 $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);