root/trunk/includes/formatters/wakka-old.php

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

initial import

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