Subversion Repositories Applications.papyrus

Rev

Rev 1087 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
439 ddelon 1
<?php
2
/*
3
wakka.php
4
Copyright (c) 2002, Hendrik Mans <hendrik@mans.de>
5
Copyright 2002, 2003 David DELON
6
Copyright 2002, 2003 Charles NEPOTE
7
Copyright 2002, 2003 Patrick PAUL
8
Copyright  2003  Eric DELORD
9
Copyright  2003  Eric FELDSTEIN
10
All rights reserved.
11
Redistribution and use in source and binary forms, with or without
12
modification, are permitted provided that the following conditions
13
are met:
14
1. Redistributions of source code must retain the above copyright
15
notice, this list of conditions and the following disclaimer.
16
2. Redistributions in binary form must reproduce the above copyright
17
notice, this list of conditions and the following disclaimer in the
18
documentation and/or other materials provided with the distribution.
19
3. The name of the author may not be used to endorse or promote products
20
derived from this software without specific prior written permission.
21
 
22
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
*/
33
// 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!
34
 
35
if (!function_exists("wakka2callback"))
36
{
37
	function wakka2callback($things)
38
	{
39
		$thing = $things[1];
40
        $result='';
41
 
42
		static $oldIndentLevel = 0;
43
		static $oldIndentLength= 0;
44
		static $indentClosers = array();
45
		static $newIndentSpace= array();
46
		static $br = 1;
47
 
48
		$brf=0;
49
		global $wiki;
50
 
51
		// convert HTML thingies
52
		if ($thing == "<")
53
			return "&lt;";
54
		else if ($thing == ">")
55
			return "&gt;";
56
		// bold
57
		else if ($thing == "**")
58
		{
59
			static $bold = 0;
60
			return (++$bold % 2 ? "<b>" : "</b>");
61
		}
62
		// italic
63
		else if ($thing == "//")
64
		{
65
			static $italic = 0;
66
			return (++$italic % 2 ? "<i>" : "</i>");
67
		}
68
		// underlinue
69
		else if ($thing == "__")
70
		{
71
			static $underline = 0;
72
			return (++$underline % 2 ? "<u>" : "</u>");
73
		}
74
		// monospace
75
		else if ($thing == "##")
76
		{
77
			static $monospace = 0;
78
			return (++$monospace % 2 ? "<tt>" : "</tt>");
79
		}
80
		// Deleted
81
                else if ($thing == "@@")
82
                {
83
                        static $deleted = 0;
84
                        return (++$deleted % 2 ? "<span class=\"del\">" : "</span>");
85
                }
86
                // Inserted
87
                else if ($thing == "££")
88
                {
89
                        static $inserted = 0;
90
                        return (++$inserted % 2 ? "<span class=\"add\">" : "</span>");
91
                }
92
		// urls
93
		else if (preg_match("/^([a-z]+:\/\/\S+?)([^[:alnum:]^\/])?$/", $thing, $matches)) {
94
			$url = $matches[1];
95
			if (!isset($matches[2])) $matches[2] = '';
96
			return "<a href=\"$url\">$url</a>".$matches[2];
97
		}
98
		// header level 5
99
                else if ($thing == "==")
100
                {
101
                        static $l5 = 0;
102
			$br = 0;
103
                        return (++$l5 % 2 ? "<h5>" : "</h5>");
104
                }
105
		// header level 4
106
                else if ($thing == "===")
107
                {
108
                        static $l4 = 0;
109
			$br = 0;
110
                        return (++$l4 % 2 ? "<h4>" : "</h4>");
111
                }
112
		// header level 3
113
                else if ($thing == "====")
114
                {
115
                        static $l3 = 0;
116
			$br = 0;
117
                        return (++$l3 % 2 ? "<h3>" : "</h3>");
118
                }
119
		// header level 2
120
                else if ($thing == "=====")
121
                {
122
                        static $l2 = 0;
123
			$br = 0;
124
                        return (++$l2 % 2 ? "<h2>" : "</h2>");
125
                }
126
		// header level 1
127
                else if ($thing == "======")
128
                {
129
                        static $l1 = 0;
130
			$br = 0;
131
                        return (++$l1 % 2 ? "<h1>" : "</h1>");
132
                }
133
		// forced line breaks
134
		else if ($thing == "---")
135
		{
136
			return "<br />";
137
		}
138
		// escaped text
139
		else if (preg_match("/^\"\"(.*)\"\"$/s", $thing, $matches))
140
		{
141
			return $matches[1];
142
		}
143
		// code text
144
		else if (preg_match("/^\%\%(.*)\%\%$/s", $thing, $matches))
145
		{
146
			// check if a language has been specified
147
			$code = $matches[1];
148
			$language='';
149
			if (preg_match("/^\((.+?)\)(.*)$/s", $code, $matches))
150
			{
151
				list(, $language, $code) = $matches;
152
			}
153
			//Select formatter for syntaxe hightlighting
154
			if (file_exists("formatters/coloration_".$language.".php")){
155
				$formatter = "coloration_".$language;
156
			}else{
157
				$formatter = "code";
158
			}
159
 
160
			$output = "<div class=\"code\">";
161
			$output .= $wiki->Format(trim($code), $formatter);
162
			$output .= "</div>";
163
 
164
			return $output;
165
		}
166
		// raw inclusion from another wiki
167
		// (regexp documentation : see "forced link" below)
168
		else if (preg_match("/^\[\[\|(\S*)(\s+(.+))?\]\]$/", $thing, $matches))
169
		{
170
			list (,$url,,$text) = $matches;
171
			if (!$text) $text = "404";
172
			if ($url)
173
			{
174
    				$url.="/wakka.php?wiki=".$text."/raw";
175
				return $wiki->Format($wiki->Format($url, "raw"),"wakka");
176
			}
177
			else
178
			{
179
				return "";
180
			}
181
		}
182
		// forced links
183
		// \S : any character that is not a whitespace character
184
		// \s : any whitespace character
954 florian 185
		else if (preg_match("/^\[\[(\S*)(\s+(.+))?\]\]$/", $thing, $matches))
439 ddelon 186
		{
187
			list (, $url, $text) = $matches;
188
			if ($url)
189
			{
190
				if ($url!=($url=(preg_replace("/@@|££|\[\[/","",$url))))$result="</span>";
191
				if (!$text) $text = $url;
192
				$text=preg_replace("/@@|££|\[\[/","",$text);
193
				return $result.$wiki->Link($url, "", $text);
194
			}
195
			else
196
			{
197
				return "";
198
			}
199
		}
200
		// indented text
201
		else if ((preg_match("/\n(\t+|([ ]{1})+)(-|([0-9,a-z,A-Z]+)\))?/s", $thing, $matches))
202
		 ||  (preg_match("/^(\t+|([ ]{1})+)(-|([0-9,a-z,A-Z]+)\))?/s", $thing, $matches) && $brf=1))
203
		{
204
			// new line
205
			if ($brf) $br=0;
206
			$result .= ($br ? "<br />\n" : "");
207
 
208
			// we definitely want no line break in this one.
209
			$br = 0;
210
 
211
			// find out which indent type we want
212
			if (!isset($matches[3])) $matches[3] = '';
213
			$newIndentType = $matches[3];
214
			if (!$newIndentType) { $opener = "<div class=\"indent\">"; $closer = "</div>"; $br = 1; }
215
			else if ($newIndentType == "-") { $opener = "<ul>\n"; $closer = "</li>\n</ul>"; $li = 1; }
216
			else { $opener = "<ol type=\"".$matches[4]."\">\n"; $closer = "</li>\n</ol>"; $li = 1; }
217
 
218
			// get new indent level
219
 
220
			if (strpos($matches[1],"\t")) $newIndentLevel = strlen($matches[1]);
221
			else
222
			{
223
				$newIndentLevel=$oldIndentLevel;
224
				$newIndentLength = strlen($matches[1]);
225
				if ($newIndentLength>$oldIndentLength)
226
				{
227
					$newIndentLevel++;
228
					$newIndentSpace[$newIndentLength]=$newIndentLevel;
229
				}
230
				else if ($newIndentLength<$oldIndentLength)
231
						$newIndentLevel=$newIndentSpace[$newIndentLength];
232
			}
233
  			$op=0;
234
			if ($newIndentLevel > $oldIndentLevel)
235
			{
236
				for ($i = 0; $i < $newIndentLevel - $oldIndentLevel; $i++)
237
				{
238
					$result .= $opener;
239
					$op=1;
240
					array_push($indentClosers, $closer);
241
				}
242
			}
243
			else if ($newIndentLevel < $oldIndentLevel)
244
			{
245
				for ($i = 0; $i < $oldIndentLevel - $newIndentLevel; $i++)
246
				{
247
					$op=1;
248
					$result .= array_pop($indentClosers);
249
			                if ($oldIndentLevel && $li) $result .= "</li>";
250
				}
251
			}
252
 
253
			if (isset($li) && $op) $result .= "<li>";
254
			else if (isset($li))
255
				$result .= "</li>\n<li>";
256
 
257
			$oldIndentLevel = $newIndentLevel;
258
			$oldIndentLength= $newIndentLength;
259
 
260
			return $result;
261
		}
262
		// new lines
263
		else if ($thing == "\n")
264
		{
265
			// if we got here, there was no tab in the next line; this means that we can close all open indents.
266
			$c = count($indentClosers);
267
			for ($i = 0; $i < $c; $i++)
268
			{
269
				$result .= array_pop($indentClosers);
270
				$br = 0;
271
			}
272
			$oldIndentLevel = 0;
273
			$oldIndentLength= 0;
274
			$newIndentSpace=array();
275
 
276
			$result .= ($br ? "<br />\n" : "\n");
277
			$br = 1;
278
			return $result;
279
		}
280
		// events
281
		else if (preg_match("/^\{\{(.*?)\}\}$/s", $thing, $matches))
282
		{
283
			if ($matches[1])
284
				return $wiki->Action($matches[1]);
285
			else
286
				return "{{}}";
287
		}
288
		// interwiki links!
289
                else if (preg_match("/^[A-Z][A-Z,a-z]+[:]([A-Z,a-z,0-9]*)$/s", $thing))
290
 
291
		{
292
			return $wiki->Link($thing);
293
		}
294
		// wiki links!
295
		else if (preg_match("/^[A-Z][a-z]+[A-Z,0-9][A-Z,a-z,0-9]*$/s", $thing))
296
		{
297
			return $wiki->Link($thing);
298
		}
299
		// separators
300
		else if (preg_match("/-{4,}/", $thing, $matches))
301
		{
302
			// TODO: This could probably be improved for situations where someone puts text on the same line as a separator.
303
			//       Which is a stupid thing to do anyway! HAW HAW! Ahem.
304
			$br = 0;
305
			return "<hr />";
306
		}
307
		// if we reach this point, it must have been an accident.
308
		return $thing;
309
	}
310
}
311
 
312
 
313
$text = str_replace("\r", "", $text);
314
$text = chop($text)."\n";
315
$text = preg_replace_callback(
316
	"/(\%\%.*?\%\%|".
317
	"\"\".*?\"\"|".
318
	"\[\[.*?\]\]|".
319
	"\b[a-z]+:\/\/\S+|".
320
	"\*\*|\#\#|@@|££|__|<|>|\/\/|".
321
	"======|=====|====|===|==|".
322
	"-{4,}|---|".
323
	"\n(\t+|([ ]{1})+)(-|[0-9,a-z,A-Z]+\))?|".
324
	"^(\t+|([ ]{1})+)(-|[0-9,a-z,A-Z]+\))?|".
325
	"\{\{.*?\}\}|".
326
        "\b[A-Z][A-Z,a-z]+[:]([A-Z,a-z,0-9]*)\b|".
327
	"\b([A-Z][a-z]+[A-Z,0-9][A-Z,a-z,0-9]*)\b|".
328
	"\n)/ms", "wakka2callback", $text);
329
 
330
// we're cutting the last <br />
331
$text = preg_replace("/<br \/>$/","", trim($text));
332
echo $text ;
333
?>