/usr/share/php/Text/Wiki.php is in php-text-wiki 1.2.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 | <?php
// vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
/**
* Parse structured wiki text and render into arbitrary formats such as XHTML.
*
* PHP versions 4 and 5
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones <pmjones@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Wiki.php 248433 2007-12-17 16:03:48Z justinpatrin $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* The baseline abstract parser class.
*/
require_once 'Text/Wiki/Parse.php';
/**
* The baseline abstract render class.
*/
require_once 'Text/Wiki/Render.php';
/**
* Parse structured wiki text and render into arbitrary formats such as XHTML.
*
* This is the "master" class for handling the management and convenience
* functions to transform Wiki-formatted text.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones <pmjones@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: 1.2.1
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki {
/**
*
* The default list of rules, in order, to apply to the source text.
*
* @access public
*
* @var array
*
*/
var $rules = array(
'Prefilter',
'Delimiter',
'Code',
'Function',
'Html',
'Raw',
'Include',
'Embed',
'Anchor',
'Heading',
'Toc',
'Horiz',
'Break',
'Blockquote',
'List',
'Deflist',
'Table',
'Image',
'Phplookup',
'Center',
'Newline',
'Paragraph',
'Url',
'Freelink',
'Interwiki',
'Wikilink',
'Colortext',
'Strong',
'Bold',
'Emphasis',
'Italic',
'Underline',
'Tt',
'Superscript',
'Subscript',
'Revise',
'Tighten'
);
/**
*
* The list of rules to not-apply to the source text.
*
* @access public
*
* @var array
*
*/
var $disable = array(
'Html',
'Include',
'Embed'
);
/**
*
* Custom configuration for rules at the parsing stage.
*
* In this array, the key is the parsing rule name, and the value is
* an array of key-value configuration pairs corresponding to the $conf
* property in the target parsing rule.
*
* For example:
*
* <code>
* $parseConf = array(
* 'Include' => array(
* 'base' => '/path/to/scripts/'
* )
* );
* </code>
*
* Note that most default rules do not need any parsing configuration.
*
* @access public
*
* @var array
*
*/
var $parseConf = array();
/**
*
* Custom configuration for rules at the rendering stage.
*
* Because rendering may be different for each target format, the
* first-level element in this array is always a format name (e.g.,
* 'Xhtml').
*
* Within that first level element, the subsequent elements match the
* $parseConf format. That is, the sub-key is the rendering rule name,
* and the sub-value is an array of key-value configuration pairs
* corresponding to the $conf property in the target rendering rule.
*
* @access public
*
* @var array
*
*/
var $renderConf = array(
'Docbook' => array(),
'Latex' => array(),
'Pdf' => array(),
'Plain' => array(),
'Rtf' => array(),
'Xhtml' => array()
);
/**
*
* Custom configuration for the output format itself.
*
* Even though Text_Wiki will render the tokens from parsed text,
* the format itself may require some configuration. For example,
* RTF needs to know font names and sizes, PDF requires page layout
* information, and DocBook needs a section hierarchy. This array
* matches the $conf property of the the format-level renderer
* (e.g., Text_Wiki_Render_Xhtml).
*
* In this array, the key is the rendering format name, and the value is
* an array of key-value configuration pairs corresponding to the $conf
* property in the rendering format rule.
*
* @access public
*
* @var array
*
*/
var $formatConf = array(
'Docbook' => array(),
'Latex' => array(),
'Pdf' => array(),
'Plain' => array(),
'Rtf' => array(),
'Xhtml' => array()
);
/**
*
* The delimiter for token numbers of parsed elements in source text.
*
* @access public
*
* @var string
*
*/
var $delim = "\31";
/**
*
* The tokens generated by rules as the source text is parsed.
*
* As Text_Wiki applies rule classes to the source text, it will
* replace portions of the text with a delimited token number. This
* is the array of those tokens, representing the replaced text and
* any options set by the parser for that replaced text.
*
* The tokens array is sequential; each element is itself a sequential
* array where element 0 is the name of the rule that generated the
* token, and element 1 is an associative array where the key is an
* option name and the value is an option value.
*
* @access private
*
* @var array
*
*/
var $tokens = array();
/**
* How many tokens generated pro rules.
*
* Intended to load only necessary render objects
*
* @access private
* @var array
*/
var $_countRulesTokens = array();
/**
*
* The source text to which rules will be applied.
*
* This text will be transformed in-place, which means that it will
* change as the rules are applied.
*
* @access private
*
* @var string
*
*/
var $source = '';
/**
* The output text
*
* @var string
*/
var $output = '';
/**
*
* Array of rule parsers.
*
* Text_Wiki creates one instance of every rule that is applied to
* the source text; this array holds those instances. The array key
* is the rule name, and the array value is an instance of the rule
* class.
*
* @access private
*
* @var array
*
*/
var $parseObj = array();
/**
*
* Array of rule renderers.
*
* Text_Wiki creates one instance of every rule that is applied to
* the source text; this array holds those instances. The array key
* is the rule name, and the array value is an instance of the rule
* class.
*
* @access private
*
* @var array
*
*/
var $renderObj = array();
/**
*
* Array of format renderers.
*
* @access private
*
* @var array
*
*/
var $formatObj = array();
/**
*
* Array of paths to search, in order, for parsing and rendering rules.
*
* @access private
*
* @var array
*
*/
var $path = array(
'parse' => array(),
'render' => array()
);
/**
*
* The directory separator character.
*
* @access private
*
* @var string
*
*/
var $_dirSep = DIRECTORY_SEPARATOR;
/**
* Temporary configuration variable
*
* @var string
*/
var $renderingType = 'normal';
/**
* Stack of rendering callbacks
*
* @var Array
*/
var $_renderCallbacks = array();
/**
* Current output block
*
* @var string
*/
var $_block;
/**
* A stack of blocks
*
* @param Array
*/
var $_blocks;
/**
*
* Constructor.
*
* **DEPRECATED**
* Please use the singleton() or factory() methods.
*
* @access public
*
* @param array $rules The set of rules to load for this object. Defaults
* to null, which will load the default ruleset for this parser.
*/
function Text_Wiki($rules = null)
{
if (is_array($rules)) {
$this->rules = array();
foreach ($rules as $rule) {
$this->rules[] = ucfirst($rule);
}
}
$this->addPath(
'parse',
$this->fixPath(dirname(__FILE__)) . 'Wiki/Parse/Default/'
);
$this->addPath(
'render',
$this->fixPath(dirname(__FILE__)) . 'Wiki/Render/'
);
}
/**
* Singleton.
*
* This avoids instantiating multiple Text_Wiki instances where a number
* of objects are required in one call, e.g. to save memory in a
* CMS invironment where several parsers are required in a single page.
*
* $single = & singleton();
*
* or
*
* $single = & singleton('Parser', array('Prefilter', 'Delimiter', 'Code', 'Function',
* 'Html', 'Raw', 'Include', 'Embed', 'Anchor', 'Heading', 'Toc', 'Horiz',
* 'Break', 'Blockquote', 'List', 'Deflist', 'Table', 'Image', 'Phplookup',
* 'Center', 'Newline', 'Paragraph', 'Url', 'Freelink', 'Interwiki', 'Wikilink',
* 'Colortext', 'Strong', 'Bold', 'Emphasis', 'Italic', 'Underline', 'Tt',
* 'Superscript', 'Subscript', 'Revise', 'Tighten'));
*
* Call using a subset of this list. The order of passing rulesets in the
* $rules array is important!
*
* After calling this, call $single->setParseConf(), setRenderConf() or setFormatConf()
* as usual for a constructed object of this class.
*
* The internal static array of singleton objects has no index on the parser
* rules, the only index is on the parser name. So if you call this multiple
* times with different rules but the same parser name, you will get the same
* static parser object each time.
*
* @access public
* @static
* @since Method available since Release 1.1.0
* @param string $parser The parser to be used (defaults to 'Default').
* @param array $rules The set of rules to instantiate the object. This
* will only be used when the first call to singleton is made, if included
* in further calls it will be effectively ignored.
* @return &object a reference to the Text_Wiki unique instantiation.
*/
function &singleton($parser = 'Default', $rules = null)
{
static $only = array();
if (!isset($only[$parser])) {
$ret = & Text_Wiki::factory($parser, $rules);
if (Text_Wiki::isError($ret)) {
return $ret;
}
$only[$parser] =& $ret;
}
return $only[$parser];
}
/**
* Returns a Text_Wiki Parser class for the specified parser.
*
* @access public
* @static
* @param string $parser The name of the parse to instantiate
* you need to have Text_Wiki_XXX installed to use $parser = 'XXX', it's E_FATAL
* @param array $rules The rules to pass into the constructor
* {@see Text_Wiki::singleton} for a list of rules
* @return Text_Wiki a Parser object extended from Text_Wiki
*/
function &factory($parser = 'Default', $rules = null)
{
$class = 'Text_Wiki_' . $parser;
$file = str_replace('_', '/', $class).'.php';
if (!class_exists($class)) {
require_once $file;
if (!class_exists($class)) {
return Text_Wiki::error(
'Class ' . $class . ' does not exist after requiring '. $file .
', install package ' . $class . "\n");
}
}
$obj = new $class($rules);
return $obj;
}
/**
*
* Set parser configuration for a specific rule and key.
*
* @access public
*
* @param string $rule The parse rule to set config for.
*
* @param array|string $arg1 The full config array to use for the
* parse rule, or a conf key in that array.
*
* @param string $arg2 The config value for the key.
*
* @return void
*
*/
function setParseConf($rule, $arg1, $arg2 = null)
{
$rule = ucwords(strtolower($rule));
if (! isset($this->parseConf[$rule])) {
$this->parseConf[$rule] = array();
}
// if first arg is an array, use it as the entire
// conf array for the rule. otherwise, treat arg1
// as a key and arg2 as a value for the rule conf.
if (is_array($arg1)) {
$this->parseConf[$rule] = $arg1;
} else {
$this->parseConf[$rule][$arg1] = $arg2;
}
}
/**
*
* Get parser configuration for a specific rule and key.
*
* @access public
*
* @param string $rule The parse rule to get config for.
*
* @param string $key A key in the conf array; if null,
* returns the entire conf array.
*
* @return mixed The whole conf array if no key is specified,
* or the specific conf key value.
*
*/
function getParseConf($rule, $key = null)
{
$rule = ucwords(strtolower($rule));
// the rule does not exist
if (! isset($this->parseConf[$rule])) {
return null;
}
// no key requested, return the whole array
if (is_null($key)) {
return $this->parseConf[$rule];
}
// does the requested key exist?
if (isset($this->parseConf[$rule][$key])) {
// yes, return that value
return $this->parseConf[$rule][$key];
} else {
// no
return null;
}
}
/**
*
* Set renderer configuration for a specific format, rule, and key.
*
* @access public
*
* @param string $format The render format to set config for.
*
* @param string $rule The render rule to set config for in the format.
*
* @param array|string $arg1 The config array, or the config key
* within the render rule.
*
* @param string $arg2 The config value for the key.
*
* @return void
*
*/
function setRenderConf($format, $rule, $arg1, $arg2 = null)
{
$format = ucwords(strtolower($format));
$rule = ucwords(strtolower($rule));
if (! isset($this->renderConf[$format])) {
$this->renderConf[$format] = array();
}
if (! isset($this->renderConf[$format][$rule])) {
$this->renderConf[$format][$rule] = array();
}
// if first arg is an array, use it as the entire
// conf array for the render rule. otherwise, treat arg1
// as a key and arg2 as a value for the render rule conf.
if (is_array($arg1)) {
$this->renderConf[$format][$rule] = $arg1;
} else {
$this->renderConf[$format][$rule][$arg1] = $arg2;
}
}
/**
*
* Get renderer configuration for a specific format, rule, and key.
*
* @access public
*
* @param string $format The render format to get config for.
*
* @param string $rule The render format rule to get config for.
*
* @param string $key A key in the conf array; if null,
* returns the entire conf array.
*
* @return mixed The whole conf array if no key is specified,
* or the specific conf key value.
*
*/
function getRenderConf($format, $rule, $key = null)
{
$format = ucwords(strtolower($format));
$rule = ucwords(strtolower($rule));
if (! isset($this->renderConf[$format]) ||
! isset($this->renderConf[$format][$rule])) {
return null;
}
// no key requested, return the whole array
if (is_null($key)) {
return $this->renderConf[$format][$rule];
}
// does the requested key exist?
if (isset($this->renderConf[$format][$rule][$key])) {
// yes, return that value
return $this->renderConf[$format][$rule][$key];
} else {
// no
return null;
}
}
/**
*
* Set format configuration for a specific rule and key.
*
* @access public
*
* @param string $format The format to set config for.
*
* @param string $key The config key within the format.
*
* @param string $val The config value for the key.
*
* @return void
*
*/
function setFormatConf($format, $arg1, $arg2 = null)
{
if (! is_array($this->formatConf[$format])) {
$this->formatConf[$format] = array();
}
// if first arg is an array, use it as the entire
// conf array for the format. otherwise, treat arg1
// as a key and arg2 as a value for the format conf.
if (is_array($arg1)) {
$this->formatConf[$format] = $arg1;
} else {
$this->formatConf[$format][$arg1] = $arg2;
}
}
/**
*
* Get configuration for a specific format and key.
*
* @access public
*
* @param string $format The format to get config for.
*
* @param mixed $key A key in the conf array; if null,
* returns the entire conf array.
*
* @return mixed The whole conf array if no key is specified,
* or the specific conf key value.
*
*/
function getFormatConf($format, $key = null)
{
// the format does not exist
if (! isset($this->formatConf[$format])) {
return null;
}
// no key requested, return the whole array
if (is_null($key)) {
return $this->formatConf[$format];
}
// does the requested key exist?
if (isset($this->formatConf[$format][$key])) {
// yes, return that value
return $this->formatConf[$format][$key];
} else {
// no
return null;
}
}
/**
*
* Inserts a rule into to the rule set.
*
* @access public
*
* @param string $name The name of the rule. Should be different from
* all other keys in the rule set.
*
* @param string $tgt The rule after which to insert this new rule. By
* default (null) the rule is inserted at the end; if set to '', inserts
* at the beginning.
*
* @return void
*
*/
function insertRule($name, $tgt = null)
{
$name = ucwords(strtolower($name));
if (! is_null($tgt)) {
$tgt = ucwords(strtolower($tgt));
}
// does the rule name to be inserted already exist?
if (in_array($name, $this->rules)) {
// yes, return
return null;
}
// the target name is not null, and not '', but does not exist
// in the list of rules. this means we're trying to insert after
// a target key, but the target key isn't there.
if (! is_null($tgt) && $tgt != '' &&
! in_array($tgt, $this->rules)) {
return false;
}
// if $tgt is null, insert at the end. We know this is at the
// end (instead of resetting an existing rule) becuase we exited
// at the top of this method if the rule was already in place.
if (is_null($tgt)) {
$this->rules[] = $name;
return true;
}
// save a copy of the current rules, then reset the rule set
// so we can insert in the proper place later.
// where to insert the rule?
if ($tgt == '') {
// insert at the beginning
array_unshift($this->rules, $name);
return true;
}
// insert after the named rule
$tmp = $this->rules;
$this->rules = array();
foreach ($tmp as $val) {
$this->rules[] = $val;
if ($val == $tgt) {
$this->rules[] = $name;
}
}
return true;
}
/**
*
* Delete (remove or unset) a rule from the $rules property.
*
* @access public
*
* @param string $rule The name of the rule to remove.
*
* @return void
*
*/
function deleteRule($name)
{
$name = ucwords(strtolower($name));
$key = array_search($name, $this->rules);
if ($key !== false) {
unset($this->rules[$key]);
}
}
/**
*
* Change from one rule to another in-place.
*
* @access public
*
* @param string $old The name of the rule to change from.
*
* @param string $new The name of the rule to change to.
*
* @return void
*
*/
function changeRule($old, $new)
{
$old = ucwords(strtolower($old));
$new = ucwords(strtolower($new));
$key = array_search($old, $this->rules);
if ($key !== false) {
// delete the new name , case it was already there
$this->deleteRule($new);
$this->rules[$key] = $new;
}
}
/**
*
* Enables a rule so that it is applied when parsing.
*
* @access public
*
* @param string $rule The name of the rule to enable.
*
* @return void
*
*/
function enableRule($name)
{
$name = ucwords(strtolower($name));
$key = array_search($name, $this->disable);
if ($key !== false) {
unset($this->disable[$key]);
}
}
/**
*
* Disables a rule so that it is not applied when parsing.
*
* @access public
*
* @param string $rule The name of the rule to disable.
*
* @return void
*
*/
function disableRule($name)
{
$name = ucwords(strtolower($name));
$key = array_search($name, $this->disable);
if ($key === false) {
$this->disable[] = $name;
}
}
/**
*
* Parses and renders the text passed to it, and returns the results.
*
* First, the method parses the source text, applying rules to the
* text as it goes. These rules will modify the source text
* in-place, replacing some text with delimited tokens (and
* populating the $this->tokens array as it goes).
*
* Next, the method renders the in-place tokens into the requested
* output format.
*
* Finally, the method returns the transformed text. Note that the
* source text is transformed in place; once it is transformed, it is
* no longer the same as the original source text.
*
* @access public
*
* @param string $text The source text to which wiki rules should be
* applied, both for parsing and for rendering.
*
* @param string $format The target output format, typically 'xhtml'.
* If a rule does not support a given format, the output from that
* rule is rule-specific.
*
* @return string The transformed wiki text.
*
*/
function transform($text, $format = 'Xhtml')
{
$this->parse($text);
return $this->render($format);
}
/**
*
* Sets the $_source text property, then parses it in place and
* retains tokens in the $_tokens array property.
*
* @access public
*
* @param string $text The source text to which wiki rules should be
* applied, both for parsing and for rendering.
*
* @return void
*
*/
function parse($text)
{
// set the object property for the source text
$this->source = $text;
// reset the tokens.
$this->tokens = array();
$this->_countRulesTokens = array();
// apply the parse() method of each requested rule to the source
// text.
foreach ($this->rules as $name) {
// do not parse the rules listed in $disable
if (! in_array($name, $this->disable)) {
// load the parsing object
$this->loadParseObj($name);
// load may have failed; only parse if
// an object is in the array now
if (is_object($this->parseObj[$name])) {
$this->parseObj[$name]->parse();
}
}
}
}
/**
*
* Renders tokens back into the source text, based on the requested format.
*
* @access public
*
* @param string $format The target output format, typically 'xhtml'.
* If a rule does not support a given format, the output from that
* rule is rule-specific.
*
* @return string The transformed wiki text.
*
*/
function render($format = 'Xhtml')
{
// the rendering method we're going to use from each rule
$format = ucwords(strtolower($format));
// the eventual output text
$this->output = '';
// when passing through the parsed source text, keep track of when
// we are in a delimited section
$in_delim = false;
// when in a delimited section, capture the token key number
$key = '';
// load the format object, or crap out if we can't find it
$result = $this->loadFormatObj($format);
if ($this->isError($result)) {
return $result;
}
// pre-rendering activity
if (is_object($this->formatObj[$format])) {
$this->output .= $this->formatObj[$format]->pre();
}
// load the render objects
foreach (array_keys($this->_countRulesTokens) as $rule) {
$this->loadRenderObj($format, $rule);
}
if ($this->renderingType == 'preg') {
$this->output = preg_replace_callback('/'.$this->delim.'(\d+)'.$this->delim.'/',
array(&$this, '_renderToken'),
$this->source);
/*
//Damn strtok()! Why does it "skip" empty parts of the string. It's useless now!
} elseif ($this->renderingType == 'strtok') {
echo '<pre>'.htmlentities($this->source).'</pre>';
$t = strtok($this->source, $this->delim);
$inToken = true;
$i = 0;
while ($t !== false) {
echo 'Token: '.$i.'<pre>"'.htmlentities($t).'"</pre><br/><br/>';
if ($inToken) {
//$this->output .= $this->renderObj[$this->tokens[$t][0]]->token($this->tokens[$t][1]);
} else {
$this->output .= $t;
}
$inToken = !$inToken;
$t = strtok($this->delim);
++$i;
}
*/
} else {
// pass through the parsed source text character by character
$this->_block = '';
$tokenStack = array();
$k = strlen($this->source);
for ($i = 0; $i < $k; $i++) {
// the current character
$char = $this->source{$i};
// are alredy in a delimited section?
if ($in_delim) {
// yes; are we ending the section?
if ($char == $this->delim) {
if (count($this->_renderCallbacks) == 0) {
$this->output .= $this->_block;
$this->_block = '';
}
if (isset($opts['type'])) {
if ($opts['type'] == 'start') {
array_push($tokenStack, $rule);
} elseif ($opts['type'] == 'end') {
if ($tokenStack[count($tokenStack) - 1] != $rule) {
return Text_Wiki::error('Unbalanced tokens, check your syntax');
} else {
array_pop($tokenStack);
}
}
}
// yes, get the replacement text for the delimited
// token number and unset the flag.
$key = (int)$key;
$rule = $this->tokens[$key][0];
$opts = $this->tokens[$key][1];
$this->_block .= $this->renderObj[$rule]->token($opts);
$in_delim = false;
} else {
// no, add to the delimited token key number
$key .= $char;
}
} else {
// not currently in a delimited section.
// are we starting into a delimited section?
if ($char == $this->delim) {
// yes, reset the previous key and
// set the flag.
$key = '';
$in_delim = true;
} else {
// no, add to the output as-is
$this->_block .= $char;
}
}
}
}
if (count($this->_renderCallbacks)) {
return $this->error('Render callbacks left over after processing finished');
}
/*
while (count($this->_renderCallbacks)) {
$this->popRenderCallback();
}
*/
if (strlen($this->_block)) {
$this->output .= $this->_block;
$this->_block = '';
}
// post-rendering activity
if (is_object($this->formatObj[$format])) {
$this->output .= $this->formatObj[$format]->post();
}
// return the rendered source text.
return $this->output;
}
/**
* Renders a token, for use only as an internal callback
*
* @param array Matches from preg_rpelace_callback, [1] is the token number
* @return string The rendered text for the token
* @access private
*/
function _renderToken($matches) {
return $this->renderObj[$this->tokens[$matches[1]][0]]->token($this->tokens[$matches[1]][1]);
}
function registerRenderCallback($callback) {
$this->_blocks[] = $this->_block;
$this->_block = '';
$this->_renderCallbacks[] = $callback;
}
function popRenderCallback() {
if (count($this->_renderCallbacks) == 0) {
return Text_Wiki::error('Render callback popped when no render callbacks in stack');
} else {
$callback = array_pop($this->_renderCallbacks);
$this->_block = call_user_func($callback, $this->_block);
if (count($this->_blocks)) {
$parentBlock = array_pop($this->_blocks);
$this->_block = $parentBlock.$this->_block;
}
if (count($this->_renderCallbacks) == 0) {
$this->output .= $this->_block;
$this->_block = '';
}
}
}
/**
*
* Returns the parsed source text with delimited token placeholders.
*
* @access public
*
* @return string The parsed source text.
*
*/
function getSource()
{
return $this->source;
}
/**
*
* Returns tokens that have been parsed out of the source text.
*
* @access public
*
* @param array $rules If an array of rule names is passed, only return
* tokens matching these rule names. If no array is passed, return all
* tokens.
*
* @return array An array of tokens.
*
*/
function getTokens($rules = null)
{
if (is_null($rules)) {
return $this->tokens;
} else {
settype($rules, 'array');
$result = array();
foreach ($this->tokens as $key => $val) {
if (in_array($val[0], $rules)) {
$result[$key] = $val;
}
}
return $result;
}
}
/**
*
* Add a token to the Text_Wiki tokens array, and return a delimited
* token number.
*
* @access public
*
* @param array $options An associative array of options for the new
* token array element. The keys and values are specific to the
* rule, and may or may not be common to other rule options. Typical
* options keys are 'text' and 'type' but may include others.
*
* @param boolean $id_only If true, return only the token number, not
* a delimited token string.
*
* @return string|int By default, return the number of the
* newly-created token array element with a delimiter prefix and
* suffix; however, if $id_only is set to true, return only the token
* number (no delimiters).
*
*/
function addToken($rule, $options = array(), $id_only = false)
{
// increment the token ID number. note that if you parse
// multiple times with the same Text_Wiki object, the ID number
// will not reset to zero.
static $id;
if (! isset($id)) {
$id = 0;
} else {
$id ++;
}
// force the options to be an array
settype($options, 'array');
// add the token
$this->tokens[$id] = array(
0 => $rule,
1 => $options
);
if (!isset($this->_countRulesTokens[$rule])) {
$this->_countRulesTokens[$rule] = 1;
} else {
++$this->_countRulesTokens[$rule];
}
// return a value
if ($id_only) {
// return the last token number
return $id;
} else {
// return the token number with delimiters
return $this->delim . $id . $this->delim;
}
}
/**
*
* Set or re-set a token with specific information, overwriting any
* previous rule name and rule options.
*
* @access public
*
* @param int $id The token number to reset.
*
* @param int $rule The rule name to use.
*
* @param array $options An associative array of options for the
* token array element. The keys and values are specific to the
* rule, and may or may not be common to other rule options. Typical
* options keys are 'text' and 'type' but may include others.
*
* @return void
*
*/
function setToken($id, $rule, $options = array())
{
$oldRule = $this->tokens[$id][0];
// reset the token
$this->tokens[$id] = array(
0 => $rule,
1 => $options
);
if ($rule != $oldRule) {
if (!($this->_countRulesTokens[$oldRule]--)) {
unset($this->_countRulesTokens[$oldRule]);
}
if (!isset($this->_countRulesTokens[$rule])) {
$this->_countRulesTokens[$rule] = 1;
} else {
++$this->_countRulesTokens[$rule];
}
}
}
/**
*
* Load a rule parser class file.
*
* @access public
*
* @return bool True if loaded, false if not.
*
*/
function loadParseObj($rule)
{
$rule = ucwords(strtolower($rule));
$file = $rule . '.php';
$class = "Text_Wiki_Parse_$rule";
if (! class_exists($class)) {
$loc = $this->findFile('parse', $file);
if ($loc) {
// found the class
include_once $loc;
} else {
// can't find the class
$this->parseObj[$rule] = null;
// can't find the class
return $this->error(
"Parse rule '$rule' not found"
);
}
}
$this->parseObj[$rule] = new $class($this);
}
/**
*
* Load a rule-render class file.
*
* @access public
*
* @return bool True if loaded, false if not.
*
*/
function loadRenderObj($format, $rule)
{
$format = ucwords(strtolower($format));
$rule = ucwords(strtolower($rule));
$file = "$format/$rule.php";
$class = "Text_Wiki_Render_$format" . "_$rule";
if (! class_exists($class)) {
// load the class
$loc = $this->findFile('render', $file);
if ($loc) {
// found the class
include_once $loc;
} else {
// can't find the class
return $this->error(
"Render rule '$rule' in format '$format' not found"
);
}
}
$this->renderObj[$rule] = new $class($this);
}
/**
*
* Load a format-render class file.
*
* @access public
*
* @return bool True if loaded, false if not.
*
*/
function loadFormatObj($format)
{
$format = ucwords(strtolower($format));
$file = $format . '.php';
$class = "Text_Wiki_Render_$format";
if (! class_exists($class)) {
$loc = $this->findFile('render', $file);
if ($loc) {
// found the class
include_once $loc;
} else {
// can't find the class
return $this->error(
"Rendering format class '$class' not found"
);
}
}
$this->formatObj[$format] = new $class($this);
}
/**
*
* Add a path to a path array.
*
* @access public
*
* @param string $type The path-type to add (parse or render).
*
* @param string $dir The directory to add to the path-type.
*
* @return void
*
*/
function addPath($type, $dir)
{
$dir = $this->fixPath($dir);
if (! isset($this->path[$type])) {
$this->path[$type] = array($dir);
} else {
array_unshift($this->path[$type], $dir);
}
}
/**
*
* Get the current path array for a path-type.
*
* @access public
*
* @param string $type The path-type to look up (plugin, filter, or
* template). If not set, returns all path types.
*
* @return array The array of paths for the requested type.
*
*/
function getPath($type = null)
{
if (is_null($type)) {
return $this->path;
} elseif (! isset($this->path[$type])) {
return array();
} else {
return $this->path[$type];
}
}
/**
*
* Searches a series of paths for a given file.
*
* @param array $type The type of paths to search (template, plugin,
* or filter).
*
* @param string $file The file name to look for.
*
* @return string|bool The full path and file name for the target file,
* or boolean false if the file is not found in any of the paths.
*
*/
function findFile($type, $file)
{
// get the set of paths
$set = $this->getPath($type);
// start looping through them
foreach ($set as $path) {
$fullname = $path . $file;
if (file_exists($fullname) && is_readable($fullname)) {
return $fullname;
}
}
// could not find the file in the set of paths
return false;
}
/**
*
* Append a trailing '/' to paths, unless the path is empty.
*
* @access private
*
* @param string $path The file path to fix
*
* @return string The fixed file path
*
*/
function fixPath($path)
{
$len = strlen($this->_dirSep);
if (! empty($path) &&
substr($path, -1 * $len, $len) != $this->_dirSep) {
return $path . $this->_dirSep;
} else {
return $path;
}
}
/**
*
* Simple error-object generator.
*
* @access public
*
* @param string $message The error message.
*
* @return object PEAR_Error
*
*/
function &error($message)
{
if (! class_exists('PEAR_Error')) {
include_once 'PEAR.php';
}
return PEAR::throwError($message);
}
/**
*
* Simple error checker.
*
* @access public
*
* @param mixed $obj Check if this is a PEAR_Error object or not.
*
* @return bool True if a PEAR_Error, false if not.
*
*/
function isError(&$obj)
{
return is_a($obj, 'PEAR_Error');
}
}
?>
|