revise.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Paul M. Jones <pmjones@ciaweb.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: revise.php,v 1.2 2004/01/31 15:52:08 pmjones Exp $
  20. /**
  21. *
  22. * This class implements a Text_Wiki_Rule to find source text marked for
  23. * revision.
  24. *
  25. * @author Paul M. Jones <pmjones@ciaweb.net>
  26. *
  27. * @package Text_Wiki
  28. *
  29. */
  30. class Text_Wiki_Rule_revise extends Text_Wiki_Rule {
  31. /**
  32. *
  33. * The regular expression used to parse the source text and find
  34. * matches conforming to this rule. Used by the parse() method.
  35. *
  36. * @access public
  37. *
  38. * @var string
  39. *
  40. * @see parse()
  41. *
  42. */
  43. var $regex = "/\@\@({*?.*}*?)\@\@/U";
  44. /**
  45. *
  46. * The characters to use as marking text to be stricken.
  47. *
  48. * @access public
  49. *
  50. * @var string
  51. *
  52. * @see parse()
  53. *
  54. */
  55. var $delmark = '---';
  56. /**
  57. *
  58. * The characters to use as marking text to be added.
  59. *
  60. * @access public
  61. *
  62. * @var string
  63. *
  64. * @see parse()
  65. *
  66. */
  67. var $insmark = '+++';
  68. /**
  69. *
  70. * Generates a replacement for the matched text. Token options are:
  71. *
  72. * 'type' => ['start'|'end'] The starting or ending point of the
  73. * inserted text. The text itself is left in the source.
  74. *
  75. * @access public
  76. *
  77. * @param array &$matches The array of matches from parse().
  78. *
  79. * @return string A pair of delimited tokens to be used as a
  80. * placeholder in the source text surrounding the teletype text.
  81. *
  82. */
  83. function process(&$matches)
  84. {
  85. $output = '';
  86. $src = $matches[1];
  87. $delmark = $this->delmark;
  88. $insmark = $this->insmark;
  89. // '---' must be before '+++' (if they both appear)
  90. $del = strpos($src, $delmark);
  91. $ins = strpos($src, $insmark);
  92. // if neither is found, return right away
  93. if ($del === false && $ins === false) {
  94. return $matches[0];
  95. }
  96. // handle text to be deleted
  97. if ($del !== false) {
  98. // move forward to the end of the deletion mark
  99. $del += strlen($delmark);
  100. if ($ins === false) {
  101. // there is no insertion text following
  102. $text = substr($src, $del);
  103. } else {
  104. // there is insertion text following,
  105. // mitigate the length
  106. $text = substr($src, $del, $ins - $del);
  107. }
  108. $output .= $this->addToken(array('type' => 'del_start'));
  109. $output .= $text;
  110. $output .= $this->addToken(array('type' => 'del_end'));
  111. }
  112. // handle text to be inserted
  113. if ($ins !== false) {
  114. // move forward to the end of the insert mark
  115. $ins += strlen($insmark);
  116. $text = substr($src, $ins);
  117. $output .= $this->addToken(array('type' => 'ins_start'));
  118. $output .= $text;
  119. $output .= $this->addToken(array('type' => 'ins_end'));
  120. }
  121. return $output;
  122. }
  123. /**
  124. *
  125. * Renders a token into text matching the requested format.
  126. *
  127. * @access public
  128. *
  129. * @param array $options The "options" portion of the token (second
  130. * element).
  131. *
  132. * @return string The text rendered from the token options.
  133. *
  134. */
  135. function renderXhtml($options)
  136. {
  137. if ($options['type'] == 'del_start') {
  138. return '<del>';
  139. }
  140. if ($options['type'] == 'del_end') {
  141. return '</del>';
  142. }
  143. if ($options['type'] == 'ins_start') {
  144. return '<ins>';
  145. }
  146. if ($options['type'] == 'ins_end') {
  147. return '</ins>';
  148. }
  149. }
  150. }
  151. ?>