Subversion Repositories Applications.papyrus

Rev

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