]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_transform.php3
Minor updates to the text.
[SourceForge/phpwiki.git] / wiki_transform.php3
1 <!-- $Id: wiki_transform.php3,v 1.10 2000-06-26 21:26:45 ahollosi Exp $ -->
2 <?
3    // expects $pagehash and $html to be set
4
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])) {
9             // embed PNG images
10             $embedded[$i] = "<img src='" . $pagehash['refs'][$i] . "'>";
11          } else {
12             // ordinary embedded link
13             $embedded[$i] = "<a href='" . $pagehash['refs'][$i] . "'>[$i]</a>";
14          }
15       }
16    }
17
18    $numlines = count($pagehash["content"]);
19
20    // Loop over all lines of the page and apply transformation rules
21    for ($index = 0; $index < $numlines; $index++) {
22       $tmpline = $pagehash["content"][$index];
23
24       if (!strlen($tmpline) || $tmpline == "\r") {
25          // this is a blank line, send <p>
26          $html .= SetHTMLOutputMode("p", ZERO_DEPTH, 0);
27          continue;
28       }
29
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.
33
34       elseif (preg_match("/(^\|)(.*)/", $tmpline, $matches)) {
35          // HTML mode
36          $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);
37          $html .= $matches[2];
38          continue;
39       }
40 */
41
42
43       //////////////////////////////////////////////////////////
44       // New linking scheme: links are in brackets. This will
45       // emulate typical HTML linking as well as Wiki linking.
46
47       // match anything between brackets except only numbers
48       // trying: 
49       $numBracketLinks = preg_match_all("/\[.+?\]/", $tmpline, $brktlinks);
50       // sort instead of rsort or "[[link] [link]" will be rendered wrong.
51       sort($brktlinks[0]);
52       reset($brktlinks[0]);
53
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|",
58                                  $linktoken,
59                                  $tmpline);
60       }
61
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.
65
66       $hasURLs = preg_match_all("/\b($AllowedProtocols):[^\s\<\>\[\]\"'\(\)]*[^\s\<\>\[\]\"'\(\)\,\.\?]/", $tmpline, $urls);
67
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 
71       rsort($urls[0]);
72       reset($urls[0]);
73
74       for ($i = 0; $i < $hasURLs; $i++) {
75          $inplaceURL = preg_quote($urls[0][$i]);
76          $URLtoken = "${FieldSeparator}${i}${FieldSeparator}";
77          $tmpline = preg_replace("|$inplaceURL|",
78                                  $URLtoken,
79                                  $tmpline);
80       }
81
82       // escape HTML metachars
83       $tmpline = ereg_replace("[&]", "&amp;", $tmpline);
84       $tmpline = ereg_replace("[>]", "&gt;", $tmpline);
85       $tmpline = ereg_replace("[<]", "&lt;", $tmpline);
86
87       // four or more dashes to <hr>
88       $tmpline = ereg_replace("^-{4,}", "<hr>", $tmpline);
89
90
91       // %%% are linebreaks
92       $tmpline = str_replace("%%%", "<br>", $tmpline);
93
94       // bold italics
95       $tmpline = preg_replace("|(''''')(.*?)(''''')|",
96                               "<strong><em>\\2</em></strong>",
97                               $tmpline);
98
99       // bold
100       $tmpline = preg_replace("|(''')(.*?)(''')|",
101                               "<strong>\\2</strong>",
102                               $tmpline);
103
104       // bold
105       $tmpline = preg_replace("|(__)(.*?)(__)|",
106                               "<strong>\\2</strong>",
107                               $tmpline);
108
109       // italics
110       $tmpline = preg_replace("|('')(.*?)('')|",
111                               "<em>\\2</em>",
112                               $tmpline);
113
114       // Link Wiki words
115       // Wikiwords preceeded by a '!' are not linked
116       if (preg_match_all("#!?\b(([A-Z][a-z]+){2,})\b#",
117                          $tmpline, $link)) {
118          // uniq the list of matches
119          unset($hash);
120          for ($i = 0; $link[0][$i]; $i++) {
121             // $realfile = $link[0][$i];
122             $hash[$link[0][$i]]++;
123          }
124
125          // all '!WikiName' entries are sorted first
126          ksort($hash);
127          while (list($realfile, $val) = each($hash)) {
128             if (strstr($realfile, '!')) {
129               $tmpline = str_replace($realfile,
130                            "${FieldSeparator}nlnk${FieldSeparator}" .
131                              substr($realfile, 1),
132                            $tmpline);
133             }          
134             elseif (IsWikiPage($dbi, $realfile)) {
135                $tmpline = preg_replace(
136                            "#([^$FieldSeparator]|^)\b$realfile\b#",
137                            "\\1" . LinkExistingWikiWord($realfile),
138                            $tmpline);
139             } else {
140                $tmpline = preg_replace(
141                            "#([^$FieldSeparator]|^)\b$realfile\b#",
142                            "\\1" . LinkUnknownWikiWord($realfile),
143                            $tmpline);
144             }
145          }
146          // get rid of placeholders
147          $tmpline = str_replace("${FieldSeparator}nlnk${FieldSeparator}",
148                            "", $tmpline);
149       }
150
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|", 
160                                  $brktlink,
161                                  $tmpline);
162       }
163
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|", 
170                                  $inplaceURL,
171                                  $tmpline);
172       }
173
174
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]],
183                                        $tmpline);
184             }
185          }
186       }
187
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];
193
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] == "*") {
199             $listtag = "ul";
200          } else {
201             $listtag = "ol"; // a rather tacit assumption. oh well.
202          }
203          $tmpline = preg_replace("/^(\t+)(\*|\d+|#)/", "", $tmpline);
204          $html .= SetHTMLOutputMode($listtag, SINGLE_DEPTH, $numtabs);
205          $html .= "<li>";
206
207       } elseif (preg_match("/^\s+/", $tmpline)) {
208          // this is preformatted text, i.e. <pre>
209          $html .= SetHTMLOutputMode("pre", ZERO_DEPTH, 0);
210
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);
218
219       } else {
220          // it's ordinary output if nothing else
221          $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);
222       }
223
224       $tmpline = str_replace("%%Search%%", RenderQuickSearch(), $tmpline);
225       $tmpline = str_replace("%%Fullsearch%%", RenderFullSearch(), $tmpline);
226       $tmpline = str_replace("%%Mostpopular%%", RenderMostPopular(), $tmpline);
227
228       $html .= "$tmpline"; // at last, emit the code
229    }
230
231
232    $html .= SetHTMLOutputMode("", ZERO_DEPTH, 0);
233 ?>