root/trunk/includes/formatters/wikka.php

Revision 1, 6.9 KB (checked in by anonymous, 7 years ago)

initial import

Line 
1<?php
2
3// This may look a bit strange, but all possible formatting tags have to be in a single regular expression for this to work correctly. Yup!
4if (!function_exists("wakka2callback"))
5{
6    function wakka2callback($things)
7    {
8        $thing = $things[1];
9        $result='';
10        global $Wiki;
11
12        static $oldIndentLevel = 0;
13        static $oldIndentLength= 0;
14        static $indentClosers = array();
15        static $newIndentSpace= array();
16        static $br = 1;
17        global $token;
18        //        static $token = array();
19        // convert HTML thingies
20
21        // escaped text
22        if (preg_match("/^\"\"(.*)\"\"$/s", $thing, $matches))
23        {
24            //                      return $matches[1];
25            return $matches[1];
26        }
27
28        else if ($thing == "<")
29            return "&lt;";
30        else if ($thing == ">")
31            return "&gt;";
32        // bold
33        else if ($thing == "**")
34        {
35            return (++$token['bold'] % 2 ? "<strong>" : "</strong>");
36        }
37        // italic
38        else if ($thing == "//")
39        {
40            return (++$token['italic'] % 2 ? "<em>" : "</em>");
41        }
42        // underline
43        else if ($thing == "__")
44        {
45            return (++$token['underline'] % 2 ? "<em class='underline'>" : "</em>");
46        }
47        // monospace
48        else if ($thing == "##")
49        {
50            return (++$token['monospace'] % 2 ? "<tt>" : "</tt>");
51        }
52
53        // warning
54        else if ($thing == "||")
55        {
56            return (++$token['warning'] % 2 ? "<span class='warning'>" : "</span>");
57        }
58
59
60        // Deleted
61        else if ($thing == "@@")
62        {
63            return (++$token['deleted'] % 2 ? "<span class=\"del\">" : "</span>");
64        }
65        // Inserted
66        else if ($thing == "££")
67        {
68            return (++$token['inserted'] % 2 ? "<span class=\"add\">" : "</span>");
69        }
70        // urls
71        else if (preg_match("/^([a-z]+:\/\/\S+?)([^[:alnum:]^\/])?$/", $thing, $matches)) {
72            $url = $matches[1];
73
74            return "<a href=\"$url\">$url</a>".$matches[2];
75        }
76        // header level 5
77        else if ($thing == "==")
78        {
79            $br = 0;
80            return (++$token['l5'] % 2 ? "<h5>" : "</h5>");
81        }
82        // header level 4
83        else if ($thing == "===")
84        {
85            $br = 0;
86            return (++$token['l4'] % 2 ? "<h4>" : "</h4>");
87        }
88        // header level 3
89        else if ($thing == "====")
90        {
91            $br = 0;
92            return (++$token['l3'] % 2 ? "<h3>" : "</h3>");
93        }
94        // header level 2
95        else if ($thing == "=====")
96        {
97            $br = 0;
98            return (++$token['l2'] % 2 ? "<h2>" : "</h2>");
99        }
100        // header level 1
101        else if ($thing == "======")
102        {
103            $br = 0;
104            return (++$token['l1'] % 2 ? "<h1>" : "</h1>");
105        }
106        // separators
107        else if (preg_match("/^-{4,}$/", $thing, $matches))
108        {
109            // TODO: This could probably be improved for situations where someone puts text on the same line as a separator.
110            //       Which is a stupid thing to do anyway! HAW HAW! Ahem.
111            $br = 0;
112            return '<hr noshade="noshade" size="1" />';
113        }
114        // forced line breaks
115        else if ($thing == "---")
116        {
117            return "<br />";
118        }
119        // code text
120        else if (preg_match("/^\%\%(.*)\%\%$/s", $thing, $matches))
121        {
122            // check if a language has been specified
123            $code = $matches[1];
124            if (preg_match("/^\((.+?)\)(.*)$/s", $code, $matches))
125            {
126                list(, $language, $code) = $matches;
127            }
128            switch ($language)
129            {
130                case "php":
131                    case "table":
132                    case "wakka":
133                    $formatter = $language;
134                break;
135                default:
136                $formatter = "code";
137            }
138
139            $output .= Wikise(trim($code), $formatter);
140
141            return $output;
142        }
143        // forced links
144        else if (preg_match("/^\[\[(\S*)(\s+(.+))?\]\]$/", $thing, $matches))
145        {
146            list (, $url, , $text) = $matches;
147            if ($url)
148            {
149                if ($url!=($url=(preg_replace("/@@|££||\[\[/","",$url))))$result="</span>";
150                if (!$text) $text = $url;
151                $text=preg_replace("/@@|££|\[\[/","",$text);
152                return $result.$Wiki->WikiLink($url, "", $text);
153            }
154            else
155            {
156                return "";
157            }
158        }
159        // indented text
160        else if (preg_match("/\n(\t+|([ ]{1})+)(-|([0-9,a-z,A-Z]+)\))?/s", $thing, $matches))
161        {
162            // new line
163            $result .= ($br ? "<br />\n" : "");
164
165            // we definitely want no line break in this one.
166            $br = 0;
167
168            // find out which indent type we want
169            $newIndentType = $matches[3];
170            if (!$newIndentType) { $opener = "<div class=\"indent\">"; $closer = "</div>"; $br = 1; }
171            else if ($newIndentType == "-") { $opener = "\n<ul>\n"; $closer = "</li>\n</ul>"; $li = 1; }
172            else { $opener = "<ol type=\"".$matches[4]."\">\n"; $closer = "</li>\n</ol>"; $li = 1; }
173
174            // get new indent level
175
176            if (strpos($matches[1],"\t")) $newIndentLevel = strlen($matches[1]);
177            else
178            {
179                $newIndentLevel=$oldIndentLevel;
180                $newIndentLength = strlen($matches[1]);
181                if ($newIndentLength>$oldIndentLength)
182                {
183                    $newIndentLevel++;
184                    $newIndentSpace[$newIndentLength]=$newIndentLevel;
185                }
186                else if ($newIndentLength<$oldIndentLength)
187                    $newIndentLevel=$newIndentSpace[$newIndentLength];
188            }
189            $op=0;
190            if ($newIndentLevel > $oldIndentLevel)
191            {
192                for ($i = 0; $i < $newIndentLevel - $oldIndentLevel; $i++)
193                {
194                    $result .= $opener;
195                    $op=1;
196                    array_push($indentClosers, $closer);
197                }
198            }
199            else if ($newIndentLevel < $oldIndentLevel)
200            {
201                for ($i = 0; $i < $oldIndentLevel - $newIndentLevel; $i++)
202                {
203                    $op=1;
204                    $result .= array_pop($indentClosers);
205                    if ($oldIndentLevel && $li) $result .= "</li>";
206                }
207            }
208
209            if (isset($li) && $op) $result .= "<li>";
210            else if (isset($li))
211                $result .= "</li>\n<li>";
212
213            $oldIndentLevel = $newIndentLevel;
214            $oldIndentLength= $newIndentLength;
215
216            return $result;
217        }
218        // new lines
219        else if ($thing == "\n")
220        {
221            // if we got here, there was no tab in the next line; this means that we can close all open indents.
222            $c = count($indentClosers);
223            for ($i = 0; $i < $c; $i++)
224            {
225                $result .= array_pop($indentClosers);
226                $br = 0;
227            }
228            $oldIndentLevel = 0;
229            $oldIndentLength= 0;
230            $newIndentSpace=array();
231
232            $result .= ($br ? "<br />\n" : "\n");
233            $br = 1;
234            return $result;
235        }
236        // events
237        else if (preg_match("/^\{\{(.*?)\}\}$/s", $thing, $matches))
238        {
239            if ($matches[1])
240                return $Wiki->Action($matches[1]);
241            else
242                return "{{}}";
243        }
244        // interwiki links!
245        else if (preg_match("/^[A-Z][A-Z,a-z]+[:]([A-Z,a-z,0-9]*)$/s", $thing))
246
247        {
248            return $Wiki->WikiLink($thing);
249        }
250        // wiki links!
251        else if (preg_match("/^[A-Z][a-z]+[A-Z,0-9][A-Z,a-z,0-9]*$/s", $thing))
252        {
253            return $Wiki->WikiLink($thing);
254        }
255        // if we reach this point, it must have been an accident.
256        return $thing;
257    }
258}
259
260
261$text = str_replace("\r", "", $text);
262$text = trim($text)."\n";
263unset($token);
264$token = array();
265$text = preg_replace_callback(
266        "/(\%\%.*?\%\%|".
267        "\"\".*?\"\"|".
268        "\[\[.*?\]\]|".
269        "\b[a-z]+:\/\/\S+|".
270        "\*\*|\#\#|@@|££|__|<|>|\/\/|\|\||".
271        "======|=====|====|===|==|".
272        "-{4,}|---|".
273        "\n(\t+|([ ]{1})+)(-|[0-9,a-z,A-Z]+\))?|".
274        "\{\{.*?\}\}|".
275        "\b[A-Z][A-Z,a-z]+[:]([A-Z,a-z,0-9]*)\b|".
276        "\b([A-Z][a-z]+[A-Z,0-9][A-Z,a-z,0-9]*)\b|".
277        "\n)/ms", "wakka2callback", $text);
278
279// find orphan tag
280/*
281foreach($token as $key=>$value)
282{
283    error_log($_site." | $key=$value");
284    if(!($value % 2))
285    {
286        error_log($_site." | $key=$value");
287        switch($key)
288        {
289            case "italic":    ++$token[$key]; $text.="</i>"; break;
290        }
291    }
292}
293*/
294// we're cutting the last <br />
295$text = preg_replace("/<br \/>$/","", trim($text));
296print($text);
297?>
Note: See TracBrowser for help on using the browser.