Index: trunk/extensions/wikihiero/wikihiero.php |
— | — | @@ -25,7 +25,7 @@ |
26 | 26 | ////////////////////////////////////////////////////////////////////////// |
27 | 27 | |
28 | 28 | // Register MediaWiki extension |
29 | | -$wgHooks['ParserFirstCallInit'][] = 'WH_Register'; |
| 29 | +$wgHooks['ParserFirstCallInit'][] = 'wfRegisterWikiHiero'; |
30 | 30 | $wgExtensionCredits['parserhook'][] = array( |
31 | 31 | 'path' => __FILE__, |
32 | 32 | 'name' => 'WikiHiero', |
— | — | @@ -35,32 +35,10 @@ |
36 | 36 | ); |
37 | 37 | $wgExtensionMessagesFiles['Wikihiero'] = dirname(__FILE__) . '/wikihiero.i18n.php'; |
38 | 38 | |
39 | | -function WH_Register( &$parser ) { |
40 | | - $parser->setHook( 'hiero', 'WikiHieroLoader' ); |
| 39 | +$wgAutoloadClasses['WikiHiero'] = dirname( __FILE__ ) . '/wh_main.php'; |
| 40 | + |
| 41 | +function wfRegisterWikiHiero( &$parser ) { |
| 42 | + $parser->setHook( 'hiero', 'WikiHiero::parserHook' ); |
41 | 43 | return true; |
42 | 44 | } |
43 | 45 | |
44 | | -function WikiHieroLoad() { |
45 | | - static $loaded = false; |
46 | | - if ( !$loaded ) { |
47 | | - require( dirname( __FILE__ ) . '/wh_main.php' ); |
48 | | - $loaded = true; |
49 | | - } |
50 | | -} |
51 | | - |
52 | | -// MediaWiki entry point |
53 | | -function WikiHieroLoader( $text, $attribs, $parser ) { |
54 | | - WikiHieroLoad(); |
55 | | - $parser->setHook( 'hiero', 'WikiHieroHook' ); |
56 | | - return WikiHieroHook( $text, $attribs, $parser ); |
57 | | -} |
58 | | - |
59 | | -// Generic embedded entry point |
60 | | -function WikiHiero($hiero, $mode=WH_MODE_DEFAULT, $scale=WH_SCALE_DEFAULT, $line=false) { |
61 | | - WikiHieroLoad(); |
62 | | - return _WikiHiero( $hiero, $mode, $scale, $line ); |
63 | | -} |
64 | | - |
65 | | -// If anyone needs WikiHieroHTML() etc., loader functions should be put here. |
66 | | -// Hopefully everyone's using the general-purpose entry point above. |
67 | | - |
Index: trunk/extensions/wikihiero/wh_main.php |
— | — | @@ -24,6 +24,10 @@ |
25 | 25 | // |
26 | 26 | ////////////////////////////////////////////////////////////////////////// |
27 | 27 | |
| 28 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 29 | + die( 'Not an entry point' ); |
| 30 | +} |
| 31 | + |
28 | 32 | //======================================================================== |
29 | 33 | // I N C L U D E S |
30 | 34 | include( dirname(__FILE__) . '/wh_list.php' ); |
— | — | @@ -38,8 +42,8 @@ |
39 | 43 | define("WH_MODE_DEFAULT", -1); // use default mode |
40 | 44 | define("WH_MODE_TEXT", 0); // text only |
41 | 45 | define("WH_MODE_HTML", 1); // HTML without CSS |
42 | | -define("WH_MODE_STYLE", 2); // HTML and CSS // not supporter |
43 | | -define("WH_MODE_IMAGE", 3); // picture (PNG) // not supporter |
| 46 | +define("WH_MODE_STYLE", 2); // HTML and CSS // not supported |
| 47 | +define("WH_MODE_IMAGE", 3); // picture (PNG) // not supported |
44 | 48 | define("WH_MODE_RAW", 4); // MdC test as it |
45 | 49 | |
46 | 50 | define("WH_TYPE_NONE", 0); |
— | — | @@ -57,20 +61,414 @@ |
58 | 62 | define("WH_VER_MED", 2); |
59 | 63 | define("WH_VER_MIN", 14); |
60 | 64 | |
61 | | -global $wgScriptPath; |
62 | | -define("WH_IMG_DIR", $wgScriptPath . '/extensions/wikihiero/img/' ); //"img/"); // |
| 65 | +global $wgExtensionAssetsPath; |
| 66 | +define("WH_IMG_DIR", $wgExtensionAssetsPath . '/wikihiero/img/' ); |
63 | 67 | define("WH_IMG_PRE", "hiero_"); |
64 | 68 | define("WH_IMG_EXT", "png"); |
65 | 69 | |
66 | 70 | define("WH_DEBUG_MODE", false); |
67 | 71 | |
| 72 | +class WikiHiero { |
| 73 | + private $scale = 100; |
| 74 | + |
| 75 | + public function __construct( $scale = WH_SCALE_DEFAULT ) { |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Render hieroglyph text |
| 80 | + * |
| 81 | + * @param $text string: text to convert |
| 82 | + * @param $mode string: convertion mode [DEFAULT|TEXT|HTML|STYLE|IMAGE] (def=HTML) |
| 83 | + * @param $scale string: global scale in percentage (def=100%) |
| 84 | + * @param $line string: use line [true|false] (def=false) |
| 85 | + * @return string: converted code |
| 86 | + */ |
| 87 | + public static function render( $text, $mode=WH_MODE_DEFAULT, $scale=WH_SCALE_DEFAULT, $line=false ) { |
| 88 | + if ( $mode == WH_MODE_DEFAULT ) { |
| 89 | + $mode = WH_MODE_HTML; |
| 90 | + } |
| 91 | + |
| 92 | + $hiero = new WikiHiero( $scale ); |
| 93 | + |
| 94 | + switch($mode) { |
| 95 | + case WH_MODE_TEXT: return $hiero->renderText( $text, $line); |
| 96 | + case WH_MODE_HTML: return $hiero->renderHtml( $text, $scale, $line); |
| 97 | + case WH_MODE_STYLE: die("ERROR: CSS version not yet implemented"); |
| 98 | + case WH_MODE_IMAGE: die("ERROR: Image version not yet implemented"); |
| 99 | + } |
| 100 | + die("ERROR: Unknown mode!"); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * |
| 105 | + */ |
| 106 | + public static function parserHook( $input ) { |
| 107 | + // Strip newlines to avoid breakage in the wiki parser block pass |
| 108 | + return str_replace( "\n", " ", self::render( $input, WH_MODE_HTML ) ); |
| 109 | + } |
| 110 | + |
| 111 | + public function getScale() { |
| 112 | + return $this->scale; |
| 113 | + } |
| 114 | + |
| 115 | + public function setScale( $scale ) { |
| 116 | + $this->scale = $scale; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Renders a glyph |
| 121 | + * |
| 122 | + * @param $glyph string: glyph's code to render |
| 123 | + * @param $option string: option to add into <img> tag (use for height) |
| 124 | + * @return string: a string to add to the stream |
| 125 | + */ |
| 126 | + private function renderGlyph( $glyph, $option = '' ) { |
| 127 | + global $wh_phonemes; |
| 128 | + global $wh_files; |
| 129 | + |
| 130 | + if($glyph == "..") { // Render void block |
| 131 | + $width = WH_HEIGHT; |
| 132 | + return "<table width='{$width}px' border='0' cellspacing='0' cellpadding='0'><tr><td> </td></tr></table>"; |
| 133 | + } |
| 134 | + elseif($glyph == ".") // Render half-width void block |
| 135 | + { |
| 136 | + $width = WH_HEIGHT/2; |
| 137 | + return "<table width='{$width}px' border='0' cellspacing='0' cellpadding='0'><tr><td> </td></tr></table>"; |
| 138 | + } |
| 139 | + elseif($glyph == '<') // Render open cartouche |
| 140 | + { |
| 141 | + $height = intval(WH_HEIGHT * $this->scale / 100); |
| 142 | + $code = $wh_phonemes[$glyph]; |
| 143 | + return "<img src='".htmlspecialchars(WH_IMG_DIR.WH_IMG_PRE."{$code}.".WH_IMG_EXT)."' height='{$height}px' title='".htmlspecialchars($glyph)."' alt='".htmlspecialchars($glyph)."' />"; |
| 144 | + } |
| 145 | + elseif($glyph == '>') // Render close cartouche |
| 146 | + { |
| 147 | + $height = intval(WH_HEIGHT * $this->scale / 100); |
| 148 | + $code = $wh_phonemes[$glyph]; |
| 149 | + return "<img src='".htmlspecialchars(WH_IMG_DIR.WH_IMG_PRE."{$code}.".WH_IMG_EXT)."' height='{$height}px' title='".htmlspecialchars($glyph)."' alt='".htmlspecialchars($glyph)."' />"; |
| 150 | + } |
| 151 | + |
| 152 | + if(array_key_exists($glyph, $wh_phonemes)) |
| 153 | + { |
| 154 | + $code = $wh_phonemes[$glyph]; |
| 155 | + if(array_key_exists($code, $wh_files)) |
| 156 | + return "<img style='margin:".WH_IMG_MARGIN."px;' $option src='".htmlspecialchars(WH_IMG_DIR.WH_IMG_PRE."{$code}.".WH_IMG_EXT)."' title='".htmlspecialchars("{$code} [{$glyph}]")."' alt='".htmlspecialchars($glyph)."' />"; |
| 157 | + else |
| 158 | + return "<font title='".htmlspecialchars($code)."'>".htmlspecialchars($glyph)."</font>"; |
| 159 | + } |
| 160 | + elseif(array_key_exists($glyph, $wh_files)) |
| 161 | + return "<img style='margin:".WH_IMG_MARGIN."px;' $option src='".htmlspecialchars(WH_IMG_DIR.WH_IMG_PRE."{$glyph}.".WH_IMG_EXT)."' title='".htmlspecialchars($glyph)."' alt='".htmlspecialchars($glyph)."' />"; |
| 162 | + else |
| 163 | + return htmlspecialchars($glyph); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Resize a glyph |
| 168 | + * |
| 169 | + * @param $item string: glyph's code |
| 170 | + * @param $is_cartouche bool: true if glyph inside a cartouche |
| 171 | + * @param $total int: total size of a group for multi-glyph block |
| 172 | + * @return size |
| 173 | + */ |
| 174 | + private function resizeGlyph( $item, $is_cartouche=false, $total=0 ) { |
| 175 | + global $wh_phonemes; |
| 176 | + global $wh_files; |
| 177 | + |
| 178 | + if(array_key_exists($item, $wh_phonemes)) { |
| 179 | + $glyph = $wh_phonemes[$item]; |
| 180 | + } else { |
| 181 | + $glyph = $item; |
| 182 | + } |
| 183 | + |
| 184 | + $margin = 2 * WH_IMG_MARGIN; |
| 185 | + if($is_cartouche) { |
| 186 | + $margin += 2 * intval(WH_CARTOUCHE_WIDTH * $this->scale / 100); |
| 187 | + } |
| 188 | + |
| 189 | + if(array_key_exists($glyph, $wh_files)) { |
| 190 | + $height = $margin + $wh_files[$glyph][1]; |
| 191 | + if($total) { |
| 192 | + if($total > WH_HEIGHT) { |
| 193 | + return (intval( $height * WH_HEIGHT / $total ) - $margin) * $this->scale / 100; |
| 194 | + } else { |
| 195 | + return ($height - $margin) * $this->scale / 100; |
| 196 | + } |
| 197 | + } else { |
| 198 | + if($height > WH_HEIGHT) { |
| 199 | + return (intval( WH_HEIGHT * WH_HEIGHT / $height ) - $margin) * $this->scale / 100; |
| 200 | + } else { |
| 201 | + return ($height - $margin) * $this->scale / 100; |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + return (WH_HEIGHT - $margin) * $this->scale / 100; |
| 207 | + } |
| 208 | + |
| 209 | + //------------------------------------------------------------------------ |
| 210 | + // WikiHieroText - Render hieroglyph text in text mode |
| 211 | + //------------------------------------------------------------------------ |
| 212 | + // hiero << text to convert |
| 213 | + // line << use line [true|false] (def=false) |
| 214 | + // return >> string with converted code |
| 215 | + //------------------------------------------------------------------------ |
| 216 | + public function renderText($hiero, $line=false) { |
| 217 | + global $wh_text_conv; |
| 218 | + |
| 219 | + $html = ""; |
| 220 | + |
| 221 | + if($line) |
| 222 | + $html .= "<hr />\n"; |
| 223 | + |
| 224 | + for($char=0; $char<strlen($hiero); $char++) |
| 225 | + { |
| 226 | + if(array_key_exists($hiero[$char], $wh_text_conv)) |
| 227 | + { |
| 228 | + $html .= $wh_text_conv[$hiero[$char]]; |
| 229 | + if($hiero[$char] == '!') |
| 230 | + if($line) |
| 231 | + $html .= "<hr />\n"; |
| 232 | + } |
| 233 | + else |
| 234 | + $html .= $hiero[$char]; |
| 235 | + } |
| 236 | + |
| 237 | + return $html; |
| 238 | + } |
| 239 | + |
| 240 | + //------------------------------------------------------------------------ |
| 241 | + // WikiHiero - Render hieroglyph text |
| 242 | + //------------------------------------------------------------------------ |
| 243 | + // hiero << text to convert |
| 244 | + // scale << global scale in percentage (def=100%) |
| 245 | + // line << use line [true|false] (def=false) |
| 246 | + // return >> string with converted code |
| 247 | + //------------------------------------------------------------------------ |
| 248 | + public function renderHtml($hiero, $scale = WH_SCALE_DEFAULT, $line=false) { |
| 249 | + global $wh_prefabs; |
| 250 | + global $wh_files; |
| 251 | + global $wh_phonemes; |
| 252 | + |
| 253 | + if($scale != WH_SCALE_DEFAULT) |
| 254 | + $this->setScale( $scale ); |
| 255 | + |
| 256 | + $html = ""; |
| 257 | + |
| 258 | + if($line) { |
| 259 | + $html .= "<hr />\n"; |
| 260 | + } |
| 261 | + |
| 262 | + //------------------------------------------------------------------------ |
| 263 | + // Split text into block, then split block into item |
| 264 | + $block = array(); |
| 265 | + $block[0] = array(); |
| 266 | + $block[0][0] = ""; |
| 267 | + $block_id = 0; |
| 268 | + $item_id = 0; |
| 269 | + $parenthesis = 0; |
| 270 | + $type = WH_TYPE_NONE; |
| 271 | + $is_cartouche = false; |
| 272 | + |
| 273 | + for($char=0; $char<strlen($hiero); $char++) { |
| 274 | + |
| 275 | + if( $hiero[$char] == '(' ) { |
| 276 | + $parenthesis++; |
| 277 | + } elseif( $hiero[$char] == ')' ) { |
| 278 | + $parenthesis--; |
| 279 | + } |
| 280 | + |
| 281 | + if( $parenthesis == 0 ) { |
| 282 | + if($hiero[$char] == '-' || $hiero[$char] == ' ') { |
| 283 | + if($type != WH_TYPE_NONE) { |
| 284 | + $block_id++; |
| 285 | + $block[$block_id] = array(); |
| 286 | + $item_id = 0; |
| 287 | + $block[$block_id][$item_id] = ""; |
| 288 | + $type = WH_TYPE_NONE; |
| 289 | + } |
| 290 | + } |
| 291 | + } else {// don't slit block if inside parenthesis |
| 292 | + if($hiero[$char] == '-') { |
| 293 | + $item_id++; |
| 294 | + $block[$block_id][$item_id] = '-'; |
| 295 | + $type = WH_TYPE_CODE; |
| 296 | + } |
| 297 | + } |
| 298 | + |
| 299 | + if($hiero[$char] == '!' ) { |
| 300 | + if($item_id > 0) { |
| 301 | + $block_id++; |
| 302 | + $block[$block_id] = array(); |
| 303 | + $item_id = 0; |
| 304 | + } |
| 305 | + $block[$block_id][$item_id] = $hiero[$char]; |
| 306 | + $type = WH_TYPE_END; |
| 307 | + |
| 308 | + } elseif(preg_match("/[*:()]/", $hiero[$char])) { |
| 309 | + |
| 310 | + if($type == WH_TYPE_GLYPH || $type == WH_TYPE_CODE) { |
| 311 | + $item_id++; |
| 312 | + $block[$block_id][$item_id] = ""; |
| 313 | + } |
| 314 | + $block[$block_id][$item_id] = $hiero[$char]; |
| 315 | + $type = WH_TYPE_CODE; |
| 316 | + |
| 317 | + } elseif(ctype_alnum($hiero[$char]) || $hiero[$char] == '.' || $hiero[$char] == '<' || $hiero[$char] == '>') { |
| 318 | + if($type == WH_TYPE_END) { |
| 319 | + $block_id++; |
| 320 | + $block[$block_id] = array(); |
| 321 | + $item_id = 0; |
| 322 | + $block[$block_id][$item_id] = ""; |
| 323 | + } elseif($type == WH_TYPE_CODE) { |
| 324 | + $item_id++; |
| 325 | + $block[$block_id][$item_id] = ""; |
| 326 | + } |
| 327 | + $block[$block_id][$item_id] .= $hiero[$char]; |
| 328 | + $type = WH_TYPE_GLYPH; |
| 329 | + } |
| 330 | + } |
| 331 | + |
| 332 | + // DEBUG: See the block split table |
| 333 | + if(WH_DEBUG_MODE) { |
| 334 | + |
| 335 | + foreach($block as $code) { |
| 336 | + echo "| "; |
| 337 | + foreach($code as $item) { |
| 338 | + echo "$item | "; |
| 339 | + } |
| 340 | + echo "<br />\n"; |
| 341 | + } |
| 342 | + } |
| 343 | + |
| 344 | + $contentHtml = $tableHtml = $tableContentHtml = ""; |
| 345 | + //$html .= WH_TABLE_S."<tr>\n"; |
| 346 | + |
| 347 | + //------------------------------------------------------------------------ |
| 348 | + // Loop into all blocks |
| 349 | + foreach($block as $code) { |
| 350 | + |
| 351 | + // simplest case, the block contain only 1 code -> render |
| 352 | + if(count($code) == 1) |
| 353 | + { |
| 354 | + if($code[0] == "!") { // end of line |
| 355 | + $tableHtml = "</tr>".WH_TABLE_E.WH_TABLE_S."<tr>\n"; |
| 356 | + if($line) { |
| 357 | + $contentHtml .= "<hr />\n"; |
| 358 | + } |
| 359 | + |
| 360 | + } elseif(strchr($code[0], '<')) { // start cartouche |
| 361 | + $contentHtml .= WH_TD_S . self::renderGlyph($code[0]).WH_TD_E; |
| 362 | + $is_cartouche = true; |
| 363 | + $contentHtml .= "<td>".WH_TABLE_S."<tr><td height='".intval(WH_CARTOUCHE_WIDTH * $this->scale / 100)."px' bgcolor='black'></td></tr><tr><td>".WH_TABLE_S."<tr>"; |
| 364 | + |
| 365 | + } elseif(strchr($code[0], '>')) { // end cartouche |
| 366 | + $contentHtml .= "</tr>".WH_TABLE_E."</td></tr><tr><td height='".intval(WH_CARTOUCHE_WIDTH * $this->scale / 100)."px' bgcolor='black'></td></tr>".WH_TABLE_E."</td>"; |
| 367 | + $is_cartouche = false; |
| 368 | + $contentHtml .= WH_TD_S . self::renderGlyph($code[0]).WH_TD_E; |
| 369 | + |
| 370 | + } elseif($code[0] != "") { // assum is glyph or '..' or '.' |
| 371 | + $option = "height='".$this->resizeGlyph($code[0], $is_cartouche)."px'"; |
| 372 | + |
| 373 | + $contentHtml .= WH_TD_S . self::renderGlyph($code[0], $option).WH_TD_E; |
| 374 | + } |
| 375 | + |
| 376 | + // block contain more than 1 glyph |
| 377 | + } else { |
| 378 | + |
| 379 | + // convert all code into '&' to test prefabs glyph |
| 380 | + $temp = ""; |
| 381 | + foreach($code as $t) { |
| 382 | + if(preg_match("/[*:!()]/", $t[0])) { |
| 383 | + $temp .= "&"; |
| 384 | + } else { |
| 385 | + $temp .= $t; |
| 386 | + } |
| 387 | + } |
| 388 | + |
| 389 | + // test is block is into tje prefabs list |
| 390 | + if(in_array($temp, $wh_prefabs)) { |
| 391 | + $option = "height='".$this->resizeGlyph($temp, $is_cartouche)."px'"; |
| 392 | + |
| 393 | + $contentHtml .= WH_TD_S . self::renderGlyph($temp, $option).WH_TD_E; |
| 394 | + |
| 395 | + // block must be manualy computed |
| 396 | + } else { |
| 397 | + // get block total height |
| 398 | + $line_max = 0; |
| 399 | + $total = 0; |
| 400 | + $height = 0; |
| 401 | + |
| 402 | + foreach($code as $t) { |
| 403 | + if($t == ":") { |
| 404 | + if($height > $line_max) { |
| 405 | + $line_max = $height; |
| 406 | + } |
| 407 | + $total += $line_max; |
| 408 | + $line_max = 0; |
| 409 | + |
| 410 | + } elseif($t == "*") { |
| 411 | + if($height > $line_max) { |
| 412 | + $line_max = $height; |
| 413 | + } |
| 414 | + } else { |
| 415 | + if(array_key_exists($t, $wh_phonemes)) { |
| 416 | + $glyph = $wh_phonemes[$t]; |
| 417 | + } else { |
| 418 | + $glyph = $t; |
| 419 | + } |
| 420 | + if(array_key_exists($glyph, $wh_files)) { |
| 421 | + $height = 2 + $wh_files[$glyph][1]; |
| 422 | + } |
| 423 | + } |
| 424 | + } // end foreach |
| 425 | + |
| 426 | + if($height > $line_max) { |
| 427 | + $line_max = $height; |
| 428 | + } |
| 429 | + |
| 430 | + $total += $line_max; |
| 431 | + |
| 432 | + // render all glyph into the block |
| 433 | + $temp = ""; |
| 434 | + foreach($code as $t) { |
| 435 | + |
| 436 | + if($t == ":") { |
| 437 | + $temp .= "<br />"; |
| 438 | + |
| 439 | + } elseif($t == "*") { |
| 440 | + $temp .= " "; |
| 441 | + |
| 442 | + } else { |
| 443 | + // resize the glyph according to the block total height |
| 444 | + $option = "height='".$this->resizeGlyph($t, $is_cartouche, $total)."px'"; |
| 445 | + $temp .= self::renderGlyph($t, $option); |
| 446 | + } |
| 447 | + } // end foreach |
| 448 | + |
| 449 | + $contentHtml .= WH_TD_S.$temp.WH_TD_E; |
| 450 | + } |
| 451 | + $contentHtml .= "\n"; |
| 452 | + } |
| 453 | + |
| 454 | + if(strlen($contentHtml) > 0) { |
| 455 | + $tableContentHtml .= $tableHtml.$contentHtml; |
| 456 | + $contentHtml = $tableHtml = ""; |
| 457 | + } |
| 458 | + } |
| 459 | + |
| 460 | + if(strlen($tableContentHtml) > 0) { |
| 461 | + $html .= WH_TABLE_S."<tr>\n".$tableContentHtml."</tr>".WH_TABLE_E; |
| 462 | + } |
| 463 | + |
| 464 | + return "<table border='0' cellspacing='0' cellpadding='0' style='display:inline;' class='mw-hierotable' dir='ltr'><tr><td>\n$html\n</td></tr></table>"; |
| 465 | + } |
| 466 | + |
| 467 | +} |
| 468 | + |
68 | 469 | //======================================================================== |
69 | 470 | // G L O B A L S |
70 | | -global $wh_mode, $wh_scale, $wh_phonemes, $wh_text_conv; |
| 471 | +global $wh_phonemes, $wh_text_conv; |
71 | 472 | |
72 | | -$wh_mode = WH_MODE_HTML; // default value |
73 | | -$wh_scale = 100; // default value |
74 | | - |
75 | 473 | $wh_phonemes = array( // convertion table phoneme -> gardiner code |
76 | 474 | "mSa" => "A12", |
77 | 475 | "xr" => "A15", |
— | — | @@ -574,402 +972,16 @@ |
575 | 973 | ">" => ")|", |
576 | 974 | ); |
577 | 975 | |
578 | | -//------------------------------------------------------------------------ |
579 | | -// WikiHieroHook - Parser callback |
580 | | -//------------------------------------------------------------------------ |
581 | | -// hiero << text to convert |
582 | | -// return >> string with converted code |
583 | | -//------------------------------------------------------------------------ |
584 | | -function WikiHieroHook($hiero) { |
585 | | - $ret = _WikiHiero($hiero, WH_MODE_HTML); |
586 | | - |
587 | | - // Strip newlines to avoid breakage in the wiki parser block pass |
588 | | - return str_replace( "\n", " ", $ret ); |
589 | | -} |
590 | | - |
591 | 976 | //======================================================================== |
592 | 977 | // F U N C T I O N S |
593 | 978 | |
594 | | -//------------------------------------------------------------------------ |
595 | | -// WH_RenderGlyph - Render a glyph |
596 | | -//------------------------------------------------------------------------ |
597 | | -// glyph << glyph's code to render |
598 | | -// option << option to add into <img> tag (use for height) |
599 | | -// return >> a string to add to the stream |
600 | | -//------------------------------------------------------------------------ |
601 | | -function WH_RenderGlyph($glyph, $option='') { |
602 | | - global $wh_phonemes; |
603 | | - global $wh_files; |
604 | | - global $wh_scale; |
605 | 979 | |
606 | | - if($glyph == "..") { // Render void block |
607 | | - $width = WH_HEIGHT; |
608 | | - return "<table width='{$width}px' border='0' cellspacing='0' cellpadding='0'><tr><td> </td></tr></table>"; |
609 | | - } |
610 | | - elseif($glyph == ".") // Render half-width void block |
611 | | - { |
612 | | - $width = WH_HEIGHT/2; |
613 | | - return "<table width='{$width}px' border='0' cellspacing='0' cellpadding='0'><tr><td> </td></tr></table>"; |
614 | | - } |
615 | | - elseif($glyph == '<') // Render open cartouche |
616 | | - { |
617 | | - $height = intval(WH_HEIGHT * $wh_scale / 100); |
618 | | - $code = $wh_phonemes[$glyph]; |
619 | | - return "<img src='".htmlspecialchars(WH_IMG_DIR.WH_IMG_PRE."{$code}.".WH_IMG_EXT)."' height='{$height}px' title='".htmlspecialchars($glyph)."' alt='".htmlspecialchars($glyph)."' />"; |
620 | | - } |
621 | | - elseif($glyph == '>') // Render close cartouche |
622 | | - { |
623 | | - $height = intval(WH_HEIGHT * $wh_scale / 100); |
624 | | - $code = $wh_phonemes[$glyph]; |
625 | | - return "<img src='".htmlspecialchars(WH_IMG_DIR.WH_IMG_PRE."{$code}.".WH_IMG_EXT)."' height='{$height}px' title='".htmlspecialchars($glyph)."' alt='".htmlspecialchars($glyph)."' />"; |
626 | | - } |
627 | | - |
628 | | - if(array_key_exists($glyph, $wh_phonemes)) |
629 | | - { |
630 | | - $code = $wh_phonemes[$glyph]; |
631 | | - if(array_key_exists($code, $wh_files)) |
632 | | - return "<img style='margin:".WH_IMG_MARGIN."px;' $option src='".htmlspecialchars(WH_IMG_DIR.WH_IMG_PRE."{$code}.".WH_IMG_EXT)."' title='".htmlspecialchars("{$code} [{$glyph}]")."' alt='".htmlspecialchars($glyph)."' />"; |
633 | | - else |
634 | | - return "<font title='".htmlspecialchars($code)."'>".htmlspecialchars($glyph)."</font>"; |
635 | | - } |
636 | | - elseif(array_key_exists($glyph, $wh_files)) |
637 | | - return "<img style='margin:".WH_IMG_MARGIN."px;' $option src='".htmlspecialchars(WH_IMG_DIR.WH_IMG_PRE."{$glyph}.".WH_IMG_EXT)."' title='".htmlspecialchars($glyph)."' alt='".htmlspecialchars($glyph)."' />"; |
638 | | - else |
639 | | - return htmlspecialchars($glyph); |
640 | | -} |
641 | | - |
642 | | -//------------------------------------------------------------------------ |
643 | | -// WH_Resize - Resize a glyph |
644 | | -//------------------------------------------------------------------------ |
645 | | -// item << glyph's code |
646 | | -// is_cartouche << true if glyph inside a cartouche |
647 | | -// total << total size of a group for multi-glyph block |
648 | | -// return >> size |
649 | | -//------------------------------------------------------------------------ |
650 | | -function WH_Resize($item, $is_cartouche=false, $total=0) { |
651 | | - global $wh_phonemes; |
652 | | - global $wh_files; |
653 | | - global $wh_scale; |
654 | | - |
655 | | - if(array_key_exists($item, $wh_phonemes)) { |
656 | | - $glyph = $wh_phonemes[$item]; |
657 | | - } else { |
658 | | - $glyph = $item; |
659 | | - } |
660 | | - |
661 | | - $margin = 2 * WH_IMG_MARGIN; |
662 | | - if($is_cartouche) { |
663 | | - $margin += 2 * intval(WH_CARTOUCHE_WIDTH * $wh_scale / 100); |
664 | | - } |
665 | | - |
666 | | - if(array_key_exists($glyph, $wh_files)) { |
667 | | - $height = $margin + $wh_files[$glyph][1]; |
668 | | - if($total) { |
669 | | - if($total > WH_HEIGHT) { |
670 | | - return (intval( $height * WH_HEIGHT / $total ) - $margin) * $wh_scale / 100; |
671 | | - } else { |
672 | | - return ($height - $margin) * $wh_scale / 100; |
673 | | - } |
674 | | - } else { |
675 | | - if($height > WH_HEIGHT) { |
676 | | - return (intval( WH_HEIGHT * WH_HEIGHT / $height ) - $margin) * $wh_scale / 100; |
677 | | - } else { |
678 | | - return ($height - $margin) * $wh_scale / 100; |
679 | | - } |
680 | | - } |
681 | | - } |
682 | | - |
683 | | - return (WH_HEIGHT - $margin) * $wh_scale / 100; |
684 | | - } |
685 | | - |
686 | 980 | //======================================================================== |
687 | 981 | // |
688 | 982 | // W i k i H i e r o |
689 | 983 | // |
690 | 984 | |
691 | 985 | //------------------------------------------------------------------------ |
692 | | -// WikiHiero - Render hieroglyph text |
693 | | -//------------------------------------------------------------------------ |
694 | | -// hiero << text to convert |
695 | | -// mode << convertion mode [DEFAULT|TEXT|HTML|STYLE|IMAGE] (def=HTML) |
696 | | -// scale << global scale in percentage (def=100%) |
697 | | -// line << use line [true|false] (def=false) |
698 | | -// return >> string with converted code |
699 | | -//------------------------------------------------------------------------ |
700 | | -function _WikiHiero($hiero, $mode=WH_MODE_DEFAULT, $scale=WH_SCALE_DEFAULT, $line=false) { |
701 | | - if($mode != WH_MODE_DEFAULT) { |
702 | | - $wh_mode = $mode; |
703 | | - } |
704 | | - |
705 | | - switch($wh_mode) { |
706 | | - case WH_MODE_TEXT: return WikiHieroText($hiero, $line); |
707 | | - case WH_MODE_HTML: return WikiHieroHTML($hiero, $scale, $line); |
708 | | - case WH_MODE_STYLE: die("ERROR: CSS version not yet implemented"); |
709 | | - case WH_MODE_IMAGE: die("ERROR: Image version not yet implemented"); |
710 | | - } |
711 | | - die("ERROR: Unknown mode!"); |
712 | | -} |
713 | | - |
714 | | -//------------------------------------------------------------------------ |
715 | | -// WikiHieroText - Render hieroglyph text in text mode |
716 | | -//------------------------------------------------------------------------ |
717 | | -// hiero << text to convert |
718 | | -// line << use line [true|false] (def=false) |
719 | | -// return >> string with converted code |
720 | | -//------------------------------------------------------------------------ |
721 | | -function WikiHieroText($hiero, $line=false) { |
722 | | - global $wh_text_conv; |
723 | | - |
724 | | - $html = ""; |
725 | | - |
726 | | - if($line) |
727 | | - $html .= "<hr />\n"; |
728 | | - |
729 | | - for($char=0; $char<strlen($hiero); $char++) |
730 | | - { |
731 | | - if(array_key_exists($hiero[$char], $wh_text_conv)) |
732 | | - { |
733 | | - $html .= $wh_text_conv[$hiero[$char]]; |
734 | | - if($hiero[$char] == '!') |
735 | | - if($line) |
736 | | - $html .= "<hr />\n"; |
737 | | - } |
738 | | - else |
739 | | - $html .= $hiero[$char]; |
740 | | - } |
741 | | - |
742 | | - return $html; |
743 | | - } |
744 | | - |
745 | | -//------------------------------------------------------------------------ |
746 | | -// WikiHiero - Render hieroglyph text |
747 | | -//------------------------------------------------------------------------ |
748 | | -// hiero << text to convert |
749 | | -// scale << global scale in percentage (def=100%) |
750 | | -// line << use line [true|false] (def=false) |
751 | | -// return >> string with converted code |
752 | | -//------------------------------------------------------------------------ |
753 | | -function WikiHieroHTML($hiero, $scale=WH_SCALE_DEFAULT, $line=false) { |
754 | | - global $wh_prefabs; |
755 | | - global $wh_files; |
756 | | - global $wh_phonemes; |
757 | | - global $wh_scale; |
758 | | - |
759 | | - if($scale != WH_SCALE_DEFAULT) |
760 | | - $wh_scale = $scale; |
761 | | - |
762 | | - $html = ""; |
763 | | - |
764 | | - if($line) { |
765 | | - $html .= "<hr />\n"; |
766 | | - } |
767 | | - |
768 | | - //------------------------------------------------------------------------ |
769 | | - // Split text into block, then split block into item |
770 | | - $block = array(); |
771 | | - $block[0] = array(); |
772 | | - $block[0][0] = ""; |
773 | | - $block_id = 0; |
774 | | - $item_id = 0; |
775 | | - $parenthesis = 0; |
776 | | - $type = WH_TYPE_NONE; |
777 | | - $is_cartouche = false; |
778 | | - |
779 | | - for($char=0; $char<strlen($hiero); $char++) { |
780 | | - |
781 | | - if( $hiero[$char] == '(' ) { |
782 | | - $parenthesis++; |
783 | | - } elseif( $hiero[$char] == ')' ) { |
784 | | - $parenthesis--; |
785 | | - } |
786 | | - |
787 | | - if( $parenthesis == 0 ) { |
788 | | - if($hiero[$char] == '-' || $hiero[$char] == ' ') { |
789 | | - if($type != WH_TYPE_NONE) { |
790 | | - $block_id++; |
791 | | - $block[$block_id] = array(); |
792 | | - $item_id = 0; |
793 | | - $block[$block_id][$item_id] = ""; |
794 | | - $type = WH_TYPE_NONE; |
795 | | - } |
796 | | - } |
797 | | - } else {// don't slit block if inside parenthesis |
798 | | - if($hiero[$char] == '-') { |
799 | | - $item_id++; |
800 | | - $block[$block_id][$item_id] = '-'; |
801 | | - $type = WH_TYPE_CODE; |
802 | | - } |
803 | | - } |
804 | | - |
805 | | - if($hiero[$char] == '!' ) { |
806 | | - if($item_id > 0) { |
807 | | - $block_id++; |
808 | | - $block[$block_id] = array(); |
809 | | - $item_id = 0; |
810 | | - } |
811 | | - $block[$block_id][$item_id] = $hiero[$char]; |
812 | | - $type = WH_TYPE_END; |
813 | | - |
814 | | - } elseif(preg_match("/[*:()]/", $hiero[$char])) { |
815 | | - |
816 | | - if($type == WH_TYPE_GLYPH || $type == WH_TYPE_CODE) { |
817 | | - $item_id++; |
818 | | - $block[$block_id][$item_id] = ""; |
819 | | - } |
820 | | - $block[$block_id][$item_id] = $hiero[$char]; |
821 | | - $type = WH_TYPE_CODE; |
822 | | - |
823 | | - } elseif(ctype_alnum($hiero[$char]) || $hiero[$char] == '.' || $hiero[$char] == '<' || $hiero[$char] == '>') { |
824 | | - if($type == WH_TYPE_END) { |
825 | | - $block_id++; |
826 | | - $block[$block_id] = array(); |
827 | | - $item_id = 0; |
828 | | - $block[$block_id][$item_id] = ""; |
829 | | - } elseif($type == WH_TYPE_CODE) { |
830 | | - $item_id++; |
831 | | - $block[$block_id][$item_id] = ""; |
832 | | - } |
833 | | - $block[$block_id][$item_id] .= $hiero[$char]; |
834 | | - $type = WH_TYPE_GLYPH; |
835 | | - } |
836 | | - } |
837 | | - |
838 | | - // DEBUG: See the block split table |
839 | | - if(WH_DEBUG_MODE) { |
840 | | - |
841 | | - foreach($block as $code) { |
842 | | - echo "| "; |
843 | | - foreach($code as $item) { |
844 | | - echo "$item | "; |
845 | | - } |
846 | | - echo "<br />\n"; |
847 | | - } |
848 | | - } |
849 | | - |
850 | | - $contentHtml = $tableHtml = $tableContentHtml = ""; |
851 | | - //$html .= WH_TABLE_S."<tr>\n"; |
852 | | - |
853 | | - //------------------------------------------------------------------------ |
854 | | - // Loop into all blocks |
855 | | - foreach($block as $code) { |
856 | | - |
857 | | - // simplest case, the block contain only 1 code -> render |
858 | | - if(count($code) == 1) |
859 | | - { |
860 | | - if($code[0] == "!") { // end of line |
861 | | - $tableHtml = "</tr>".WH_TABLE_E.WH_TABLE_S."<tr>\n"; |
862 | | - if($line) { |
863 | | - $contentHtml .= "<hr />\n"; |
864 | | - } |
865 | | - |
866 | | - } elseif(strchr($code[0], '<')) { // start cartouche |
867 | | - $contentHtml .= WH_TD_S.WH_RenderGlyph($code[0]).WH_TD_E; |
868 | | - $is_cartouche = true; |
869 | | - $contentHtml .= "<td>".WH_TABLE_S."<tr><td height='".intval(WH_CARTOUCHE_WIDTH * $wh_scale / 100)."px' bgcolor='black'></td></tr><tr><td>".WH_TABLE_S."<tr>"; |
870 | | - |
871 | | - } elseif(strchr($code[0], '>')) { // end cartouche |
872 | | - $contentHtml .= "</tr>".WH_TABLE_E."</td></tr><tr><td height='".intval(WH_CARTOUCHE_WIDTH * $wh_scale / 100)."px' bgcolor='black'></td></tr>".WH_TABLE_E."</td>"; |
873 | | - $is_cartouche = false; |
874 | | - $contentHtml .= WH_TD_S.WH_RenderGlyph($code[0]).WH_TD_E; |
875 | | - |
876 | | - } elseif($code[0] != "") { // assum is glyph or '..' or '.' |
877 | | - $option = "height='".WH_Resize($code[0], $is_cartouche)."px'"; |
878 | | - |
879 | | - $contentHtml .= WH_TD_S.WH_RenderGlyph($code[0], $option).WH_TD_E; |
880 | | - } |
881 | | - |
882 | | - // block contain more than 1 glyph |
883 | | - } else { |
884 | | - |
885 | | - // convert all code into '&' to test prefabs glyph |
886 | | - $temp = ""; |
887 | | - foreach($code as $t) { |
888 | | - if(preg_match("/[*:!()]/", $t[0])) { |
889 | | - $temp .= "&"; |
890 | | - } else { |
891 | | - $temp .= $t; |
892 | | - } |
893 | | - } |
894 | | - |
895 | | - // test is block is into tje prefabs list |
896 | | - if(in_array($temp, $wh_prefabs)) { |
897 | | - $option = "height='".WH_Resize($temp, $is_cartouche)."px'"; |
898 | | - |
899 | | - $contentHtml .= WH_TD_S.WH_RenderGlyph($temp, $option).WH_TD_E; |
900 | | - |
901 | | - // block must be manualy computed |
902 | | - } else { |
903 | | - // get block total height |
904 | | - $line_max = 0; |
905 | | - $total = 0; |
906 | | - $height = 0; |
907 | | - |
908 | | - foreach($code as $t) { |
909 | | - if($t == ":") { |
910 | | - if($height > $line_max) { |
911 | | - $line_max = $height; |
912 | | - } |
913 | | - $total += $line_max; |
914 | | - $line_max = 0; |
915 | | - |
916 | | - } elseif($t == "*") { |
917 | | - if($height > $line_max) { |
918 | | - $line_max = $height; |
919 | | - } |
920 | | - } else { |
921 | | - if(array_key_exists($t, $wh_phonemes)) { |
922 | | - $glyph = $wh_phonemes[$t]; |
923 | | - } else { |
924 | | - $glyph = $t; |
925 | | - } |
926 | | - if(array_key_exists($glyph, $wh_files)) { |
927 | | - $height = 2 + $wh_files[$glyph][1]; |
928 | | - } |
929 | | - } |
930 | | - } // end foreach |
931 | | - |
932 | | - if($height > $line_max) { |
933 | | - $line_max = $height; |
934 | | - } |
935 | | - |
936 | | - $total += $line_max; |
937 | | - |
938 | | - // render all glyph into the block |
939 | | - $temp = ""; |
940 | | - foreach($code as $t) { |
941 | | - |
942 | | - if($t == ":") { |
943 | | - $temp .= "<br />"; |
944 | | - |
945 | | - } elseif($t == "*") { |
946 | | - $temp .= " "; |
947 | | - |
948 | | - } else { |
949 | | - // resize the glyph according to the block total height |
950 | | - $option = "height='".WH_Resize($t, $is_cartouche, $total)."px'"; |
951 | | - $temp .= WH_RenderGlyph($t, $option); |
952 | | - } |
953 | | - } // end foreach |
954 | | - |
955 | | - $contentHtml .= WH_TD_S.$temp.WH_TD_E; |
956 | | - } |
957 | | - $contentHtml .= "\n"; |
958 | | - } |
959 | | - |
960 | | - if(strlen($contentHtml) > 0) { |
961 | | - $tableContentHtml .= $tableHtml.$contentHtml; |
962 | | - $contentHtml = $tableHtml = ""; |
963 | | - } |
964 | | - } |
965 | | - |
966 | | - if(strlen($tableContentHtml) > 0) { |
967 | | - $html .= WH_TABLE_S."<tr>\n".$tableContentHtml."</tr>".WH_TABLE_E; |
968 | | - } |
969 | | - |
970 | | - return "<table border='0' cellspacing='0' cellpadding='0' style='display:inline;' class='mw-hierotable' dir='ltr'><tr><td>\n$html\n</td></tr></table>"; |
971 | | -} |
972 | | - |
973 | | -//------------------------------------------------------------------------ |
974 | 986 | // WH_GetCode - Get glyph code from file name |
975 | 987 | //------------------------------------------------------------------------ |
976 | 988 | // file << file name |
— | — | @@ -980,7 +992,7 @@ |
981 | 993 | } |
982 | 994 | |
983 | 995 | //------------------------------------------------------------------------ |
984 | | -// WH_GetCode - Get glyph code from file name |
| 996 | +// WH_Credit - Get glyph code from file name |
985 | 997 | //------------------------------------------------------------------------ |
986 | 998 | // return >> credit string |
987 | 999 | //------------------------------------------------------------------------ |