| 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 | // convert HTML thingies |
|---|
| 19 | if ($thing == "<") |
|---|
| 20 | return "<"; |
|---|
| 21 | else if ($thing == ">") |
|---|
| 22 | return ">"; |
|---|
| 23 | // bold |
|---|
| 24 | else if ($thing == "**") |
|---|
| 25 | { |
|---|
| 26 | static $bold = 0; |
|---|
| 27 | return (++$bold % 2 ? "<b>" : "</b>"); |
|---|
| 28 | } |
|---|
| 29 | // italic |
|---|
| 30 | else if ($thing == "//") |
|---|
| 31 | { |
|---|
| 32 | static $italic = 0; |
|---|
| 33 | return (++$italic % 2 ? "<i>" : "</i>"); |
|---|
| 34 | } |
|---|
| 35 | // underlinue |
|---|
| 36 | else if ($thing == "__") |
|---|
| 37 | { |
|---|
| 38 | static $underline = 0; |
|---|
| 39 | return (++$underline % 2 ? "<u>" : "</u>"); |
|---|
| 40 | } |
|---|
| 41 | // monospace |
|---|
| 42 | else if ($thing == "##") |
|---|
| 43 | { |
|---|
| 44 | static $monospace = 0; |
|---|
| 45 | return (++$monospace % 2 ? "<tt>" : "</tt>"); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | // Deleted |
|---|
| 49 | else if ($thing == "@@") |
|---|
| 50 | { |
|---|
| 51 | static $deleted = 0; |
|---|
| 52 | return (++$deleted % 2 ? "<span class=\"del\">" : "</span>"); |
|---|
| 53 | } |
|---|
| 54 | // Inserted |
|---|
| 55 | else if ($thing == "££") |
|---|
| 56 | { |
|---|
| 57 | static $inserted = 0; |
|---|
| 58 | return (++$inserted % 2 ? "<span class=\"add\">" : "</span>"); |
|---|
| 59 | } |
|---|
| 60 | // urls |
|---|
| 61 | else if (preg_match("/^([a-z]+:\/\/\S+?)([^[:alnum:]^\/])?$/", $thing, $matches)) { |
|---|
| 62 | $url = $matches[1]; |
|---|
| 63 | |
|---|
| 64 | return "<a href=\"$url\">$url</a>".$matches[2]; |
|---|
| 65 | } |
|---|
| 66 | // header level 5 |
|---|
| 67 | else if ($thing == "==") |
|---|
| 68 | { |
|---|
| 69 | static $l5 = 0; |
|---|
| 70 | $br = 0; |
|---|
| 71 | return (++$l5 % 2 ? "<h5>" : "</h5>"); |
|---|
| 72 | } |
|---|
| 73 | // header level 4 |
|---|
| 74 | else if ($thing == "===") |
|---|
| 75 | { |
|---|
| 76 | static $l4 = 0; |
|---|
| 77 | $br = 0; |
|---|
| 78 | return (++$l4 % 2 ? "<h4>" : "</h4>"); |
|---|
| 79 | } |
|---|
| 80 | // header level 3 |
|---|
| 81 | else if ($thing == "====") |
|---|
| 82 | { |
|---|
| 83 | static $l3 = 0; |
|---|
| 84 | $br = 0; |
|---|
| 85 | return (++$l3 % 2 ? "<h3>" : "</h3>"); |
|---|
| 86 | } |
|---|
| 87 | // header level 2 |
|---|
| 88 | else if ($thing == "=====") |
|---|
| 89 | { |
|---|
| 90 | static $l2 = 0; |
|---|
| 91 | $br = 0; |
|---|
| 92 | return (++$l2 % 2 ? "<h2>" : "</h2>"); |
|---|
| 93 | } |
|---|
| 94 | // header level 1 |
|---|
| 95 | else if ($thing == "======") |
|---|
| 96 | { |
|---|
| 97 | static $l1 = 0; |
|---|
| 98 | $br = 0; |
|---|
| 99 | return (++$l1 % 2 ? "<h1>" : "</h1>"); |
|---|
| 100 | } |
|---|
| 101 | // separators |
|---|
| 102 | else if (preg_match("/-{4,}/", $thing, $matches)) |
|---|
| 103 | { |
|---|
| 104 | // TODO: This could probably be improved for situations where someone puts text on the same line as a separator. |
|---|
| 105 | // Which is a stupid thing to do anyway! HAW HAW! Ahem. |
|---|
| 106 | $br = 0; |
|---|
| 107 | return "<hr noshade=\"noshade\" size=\"1\" />"; |
|---|
| 108 | } |
|---|
| 109 | // forced line breaks |
|---|
| 110 | else if ($thing == "---") |
|---|
| 111 | { |
|---|
| 112 | return "<br />"; |
|---|
| 113 | } |
|---|
| 114 | // escaped text |
|---|
| 115 | else if (preg_match("/^\"\"(.*)\"\"$/s", $thing, $matches)) |
|---|
| 116 | { |
|---|
| 117 | return $matches[1]; |
|---|
| 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 | $formatter = "php"; |
|---|
| 132 | break; |
|---|
| 133 | case "table": |
|---|
| 134 | $formatter = "table"; |
|---|
| 135 | break; |
|---|
| 136 | default: |
|---|
| 137 | $formatter = "code"; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | $output .= $Format(trim($code), $formatter); |
|---|
| 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.="/static/".$text."/raw"; |
|---|
| 152 | return Format(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.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 | $newIndentType = $matches[3]; |
|---|
| 186 | if (!$newIndentType) { $opener = "<div class=\"indent\">"; $closer = "</div>"; $br = 1; } |
|---|
| 187 | else if ($newIndentType == "-") { $opener = "\n<ul>\n"; $closer = "</li>\n</ul>"; $li = 1; } |
|---|
| 188 | else { $opener = "<ol type=\"".$matches[4]."\">\n"; $closer = "</li>\n</ol>"; $li = 1; } |
|---|
| 189 | |
|---|
| 190 | // get new indent level |
|---|
| 191 | |
|---|
| 192 | if (strpos($matches[1],"\t")) $newIndentLevel = strlen($matches[1]); |
|---|
| 193 | else |
|---|
| 194 | { |
|---|
| 195 | $newIndentLevel=$oldIndentLevel; |
|---|
| 196 | $newIndentLength = strlen($matches[1]); |
|---|
| 197 | if ($newIndentLength>$oldIndentLength) |
|---|
| 198 | { |
|---|
| 199 | $newIndentLevel++; |
|---|
| 200 | $newIndentSpace[$newIndentLength]=$newIndentLevel; |
|---|
| 201 | } |
|---|
| 202 | else if ($newIndentLength<$oldIndentLength) |
|---|
| 203 | $newIndentLevel=$newIndentSpace[$newIndentLength]; |
|---|
| 204 | } |
|---|
| 205 | $op=0; |
|---|
| 206 | if ($newIndentLevel > $oldIndentLevel) |
|---|
| 207 | { |
|---|
| 208 | for ($i = 0; $i < $newIndentLevel - $oldIndentLevel; $i++) |
|---|
| 209 | { |
|---|
| 210 | $result .= $opener; |
|---|
| 211 | $op=1; |
|---|
| 212 | array_push($indentClosers, $closer); |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | else if ($newIndentLevel < $oldIndentLevel) |
|---|
| 216 | { |
|---|
| 217 | for ($i = 0; $i < $oldIndentLevel - $newIndentLevel; $i++) |
|---|
| 218 | { |
|---|
| 219 | $op=1; |
|---|
| 220 | $result .= array_pop($indentClosers); |
|---|
| 221 | if ($oldIndentLevel && $li) $result .= "</li>"; |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | if (isset($li) && $op) $result .= "<li>"; |
|---|
| 226 | else if (isset($li)) |
|---|
| 227 | $result .= "</li>\n<li>"; |
|---|
| 228 | |
|---|
| 229 | $oldIndentLevel = $newIndentLevel; |
|---|
| 230 | $oldIndentLength= $newIndentLength; |
|---|
| 231 | |
|---|
| 232 | return $result; |
|---|
| 233 | } |
|---|
| 234 | // new lines |
|---|
| 235 | else if ($thing == "\n") |
|---|
| 236 | { |
|---|
| 237 | // if we got here, there was no tab in the next line; this means that we can close all open indents. |
|---|
| 238 | $c = count($indentClosers); |
|---|
| 239 | for ($i = 0; $i < $c; $i++) |
|---|
| 240 | { |
|---|
| 241 | $result .= array_pop($indentClosers); |
|---|
| 242 | $br = 0; |
|---|
| 243 | } |
|---|
| 244 | $oldIndentLevel = 0; |
|---|
| 245 | $oldIndentLength= 0; |
|---|
| 246 | $newIndentSpace=array(); |
|---|
| 247 | |
|---|
| 248 | $result .= ($br ? "<br />\n" : "\n"); |
|---|
| 249 | $br = 1; |
|---|
| 250 | return $result; |
|---|
| 251 | } |
|---|
| 252 | // events |
|---|
| 253 | else if (preg_match("/^\{\{(.*?)\}\}$/s", $thing, $matches)) |
|---|
| 254 | { |
|---|
| 255 | if ($matches[1]) |
|---|
| 256 | return Action($matches[1]); |
|---|
| 257 | else |
|---|
| 258 | return "{{}}"; |
|---|
| 259 | } |
|---|
| 260 | // interwiki links! |
|---|
| 261 | else if (preg_match("/^[A-Z][A-Z,a-z]+[:]([A-Z,a-z,0-9]*)$/s", $thing)) |
|---|
| 262 | |
|---|
| 263 | { |
|---|
| 264 | return Link($thing); |
|---|
| 265 | } |
|---|
| 266 | // wiki links! |
|---|
| 267 | else if (preg_match("/^[A-Z][a-z]+[A-Z,0-9][A-Z,a-z,0-9]*$/s", $thing)) |
|---|
| 268 | { |
|---|
| 269 | return Link($thing); |
|---|
| 270 | } |
|---|
| 271 | // if we reach this point, it must have been an accident. |
|---|
| 272 | return $thing; |
|---|
| 273 | } |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | $text = str_replace("\r", "", $text); |
|---|
| 278 | $text = trim($text)."\n"; |
|---|
| 279 | $text = preg_replace_callback( |
|---|
| 280 | "/(\%\%.*?\%\%|". |
|---|
| 281 | "\"\".*?\"\"|". |
|---|
| 282 | "\[\[.*?\]\]|". |
|---|
| 283 | "\b[a-z]+:\/\/\S+|". |
|---|
| 284 | "\*\*|\#\#|@@|££|__|<|>|\/\/|". |
|---|
| 285 | "======|=====|====|===|==|". |
|---|
| 286 | "-{4,}|---|". |
|---|
| 287 | "\n(\t+|([ ]{1})+)(-|[0-9,a-z,A-Z]+\))?|". |
|---|
| 288 | "\{\{.*?\}\}|". |
|---|
| 289 | "\b[A-Z][A-Z,a-z]+[:]([A-Z,a-z,0-9]*)\b|". |
|---|
| 290 | "\b([A-Z][a-z]+[A-Z,0-9][A-Z,a-z,0-9]*)\b|". |
|---|
| 291 | "\n)/ms", "wakka2callback", $text); |
|---|
| 292 | |
|---|
| 293 | // we're cutting the last <br /> |
|---|
| 294 | $text = preg_replace("/<br \/>$/","", trim($text)); |
|---|
| 295 | print($text); |
|---|
| 296 | ?> |
|---|