Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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