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