root/trunk/includes/formatters/wakka.php

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

initial import

Line 
1<?php
2// 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!
3if (!function_exists("wakka2callback"))
4{
5    function wakka2callback($things)
6    {
7        $thing = $things[1];
8        $result='';
9        global $Wiki;
10
11        static $oldIndentLevel = 0;
12        static $oldIndentLength= 0;
13        static $indentClosers = array();
14        static $newIndentSpace= array();
15        static $br = 1;
16        global $token;
17        //        static $token = array();
18        // convert HTML thingies
19
20
21// escaped text
22                if (preg_match("/^\"\"(.*)\"\"$/s", $thing, $matches))
23                {
24//                      return $matches[1];
25                        return htmlentities($matches[1], ENT_NOQUOTES, 'UTF-8');
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)
151                {
152                    if(strlen($url)>29)
153                        $text = substr($url, 0, 32).'[...]';
154                    else
155                        $text = $url;
156                }
157
158                $text=preg_replace("/@@|££|\[\[/","",$text);
159                return $result.$Wiki->WikiLink($url, "", $text);
160            }
161            else
162            {
163                return "";
164            }
165        }
166        // indented text
167        else if (preg_match("/\n(\t+|([ ]{1})+)(-|([0-9,a-z,A-Z]+)\))?/s", $thing, $matches))
168        {
169            // new line
170            $result .= ($br ? "<br />\n" : "");
171
172            // we definitely want no line break in this one.
173            $br = 0;
174
175            // find out which indent type we want
176            $newIndentType = $matches[3];
177            if (!$newIndentType) { $opener = "<div class=\"indent\">"; $closer = "</div>"; $br = 1; }
178            else if ($newIndentType == "-") { $opener = "\n<ul>\n"; $closer = "</li>\n</ul>"; $li = 1; }
179            else { $opener = "<ol type=\"".$matches[4]."\">\n"; $closer = "</li>\n</ol>"; $li = 1; }
180
181            // get new indent level
182
183            if (strpos($matches[1],"\t")) $newIndentLevel = strlen($matches[1]);
184            else
185            {
186                $newIndentLevel=$oldIndentLevel;
187                $newIndentLength = strlen($matches[1]);
188                if ($newIndentLength>$oldIndentLength)
189                {
190                    $newIndentLevel++;
191                    $newIndentSpace[$newIndentLength]=$newIndentLevel;
192                }
193                else if ($newIndentLength<$oldIndentLength)
194                    $newIndentLevel=$newIndentSpace[$newIndentLength];
195            }
196            $op=0;
197            if ($newIndentLevel > $oldIndentLevel)
198            {
199                for ($i = 0; $i < $newIndentLevel - $oldIndentLevel; $i++)
200                {
201                    $result .= $opener;
202                    $op=1;
203                    array_push($indentClosers, $closer);
204                }
205            }
206            else if ($newIndentLevel < $oldIndentLevel)
207            {
208                for ($i = 0; $i < $oldIndentLevel - $newIndentLevel; $i++)
209                {
210                    $op=1;
211                    $result .= array_pop($indentClosers);
212                    if ($oldIndentLevel && $li) $result .= "</li>";
213                }
214            }
215
216            if (isset($li) && $op) $result .= "<li>";
217            else if (isset($li))
218                $result .= "</li>\n<li>";
219
220            $oldIndentLevel = $newIndentLevel;
221            $oldIndentLength= $newIndentLength;
222
223            return $result;
224        }
225        // new lines
226        else if ($thing == "\n")
227        {
228            // if we got here, there was no tab in the next line; this means that we can close all open indents.
229            $c = count($indentClosers);
230            for ($i = 0; $i < $c; $i++)
231            {
232                $result .= array_pop($indentClosers);
233                $br = 0;
234            }
235            $oldIndentLevel = 0;
236            $oldIndentLength= 0;
237            $newIndentSpace=array();
238
239            $result .= ($br ? "<br />\n" : "\n");
240            $br = 1;
241            return $result;
242        }
243        // events
244        else if (preg_match("/^\{\{(.*?)\}\}$/s", $thing, $matches))
245        {
246            if ($matches[1])
247                return $Wiki->Action($matches[1]);
248            else
249                return "{{}}";
250        }
251        // if we reach this point, it must have been an accident.
252        return $thing;
253    }
254}
255
256
257$text = str_replace("\r", "", $text);
258$text = trim($text)."\n";
259unset($token);
260$token = array();
261$text = preg_replace_callback(
262        "/(\%\%.*?\%\%|".
263        "\"\".*?\"\"|".
264        "\[\[.*?\]\]|".
265        "\b[a-z]+:\/\/\S+|".
266        "\*\*|\#\#|@@|££|__|<|>|\/\/|\|\||".
267        "======|=====|====|===|==|".
268        "-{4,}|---|".
269        "\n(\t+|([ ]{1})+)(-|[0-9,a-z,A-Z]+\))?|".
270        "\{\{.*?\}\}|".
271        "\b[A-Z][A-Z,a-z]+[:]([A-Z,a-z,0-9]*)\b|".
272        "\b([A-Z][a-z]+[A-Z,0-9][A-Z,a-z,0-9]*)\b|".
273        "\n)/ms", "wakka2callback", $text);
274
275// find orphan tag
276/*
277foreach($token as $key=>$value)
278{
279    error_log($_site." | $key=$value");
280    if(!($value % 2))
281    {
282        error_log($_site." | $key=$value");
283        switch($key)
284        {
285            case "italic":    ++$token[$key]; $text.="</i>"; break;
286        }
287    }
288}
289*/
290// we're cutting the last <br />
291$text = preg_replace("/<br \/>$/","", trim($text));
292print($text);
293?>
Note: See TracBrowser for help on using the browser.