includes/wikiengine/Parse/Default/Revise.php
changeset 1 fe660c52c48f
equal deleted inserted replaced
0:902822492a68 1:fe660c52c48f
       
     1 <?php
       
     2 
       
     3 /**
       
     4 * 
       
     5 * Parses for text marked as revised (insert/delete).
       
     6 * 
       
     7 * @category Text
       
     8 * 
       
     9 * @package Text_Wiki
       
    10 * 
       
    11 * @author Paul M. Jones <pmjones@php.net>
       
    12 * 
       
    13 * @license LGPL
       
    14 * 
       
    15 * @version $Id: Revise.php,v 1.3 2005/02/23 17:38:29 pmjones Exp $
       
    16 * 
       
    17 */
       
    18 
       
    19 /**
       
    20 * 
       
    21 * Parses for text marked as revised (insert/delete).
       
    22 *
       
    23 * @category Text
       
    24 * 
       
    25 * @package Text_Wiki
       
    26 * 
       
    27 * @author Paul M. Jones <pmjones@php.net>
       
    28 * 
       
    29 */
       
    30 
       
    31 class Text_Wiki_Parse_Revise extends Text_Wiki_Parse {
       
    32     
       
    33     
       
    34     /**
       
    35     * 
       
    36     * The regular expression used to parse the source text and find
       
    37     * matches conforming to this rule.  Used by the parse() method.
       
    38     * 
       
    39     * @access public
       
    40     * 
       
    41     * @var string
       
    42     * 
       
    43     * @see parse()
       
    44     * 
       
    45     */
       
    46     
       
    47     var $regex = "/\@\@({*?.*}*?)\@\@/U";
       
    48     
       
    49     
       
    50     /**
       
    51     * 
       
    52     * Config options.
       
    53     * 
       
    54     * @access public
       
    55     * 
       
    56     * @var array
       
    57     *
       
    58     */
       
    59     
       
    60     var $conf = array(
       
    61         'delmark' => '---',
       
    62         'insmark' => '+++'
       
    63     );
       
    64     
       
    65     
       
    66     /**
       
    67     * 
       
    68     * Generates a replacement for the matched text.  Token options are:
       
    69     * 
       
    70     * 'type' => ['start'|'end'] The starting or ending point of the
       
    71     * inserted text.  The text itself is left in the source.
       
    72     * 
       
    73     * @access public
       
    74     *
       
    75     * @param array &$matches The array of matches from parse().
       
    76     *
       
    77     * @return string A pair of delimited tokens to be used as a
       
    78     * placeholder in the source text surrounding the teletype text.
       
    79     *
       
    80     */
       
    81     
       
    82     function process(&$matches)
       
    83     {
       
    84         $output = '';
       
    85         $src = $matches[1];
       
    86         $delmark = $this->getConf('delmark'); // ---
       
    87         $insmark = $this->getConf('insmark'); // +++
       
    88         
       
    89         // '---' must be before '+++' (if they both appear)
       
    90         $del = strpos($src, $delmark);
       
    91         $ins = strpos($src, $insmark);
       
    92         
       
    93         // if neither is found, return right away
       
    94         if ($del === false && $ins === false) {
       
    95             return $matches[0];
       
    96         }
       
    97         
       
    98         // handle text to be deleted
       
    99         if ($del !== false) {
       
   100             
       
   101             // move forward to the end of the deletion mark
       
   102             $del += strlen($delmark);
       
   103             
       
   104             if ($ins === false) {
       
   105                 // there is no insertion text following
       
   106                 $text = substr($src, $del);
       
   107             } else {
       
   108                 // there is insertion text following,
       
   109                 // mitigate the length
       
   110                 $text = substr($src, $del, $ins - $del);
       
   111             }
       
   112             
       
   113             $output .= $this->wiki->addToken(
       
   114                 $this->rule, array('type' => 'del_start')
       
   115             );
       
   116             
       
   117             $output .= $text;
       
   118             
       
   119             $output .= $this->wiki->addToken(
       
   120                 $this->rule, array('type' => 'del_end')
       
   121             );
       
   122         }
       
   123         
       
   124         // handle text to be inserted
       
   125         if ($ins !== false) {
       
   126             
       
   127             // move forward to the end of the insert mark
       
   128             $ins += strlen($insmark);
       
   129             $text = substr($src, $ins);
       
   130             
       
   131             $output .= $this->wiki->addToken(
       
   132                 $this->rule, array('type' => 'ins_start')
       
   133             );
       
   134             
       
   135             $output .= $text;
       
   136             
       
   137             $output .= $this->wiki->addToken(
       
   138                 $this->rule, array('type' => 'ins_end')
       
   139             );
       
   140         }
       
   141         
       
   142         return $output;
       
   143     }
       
   144 }
       
   145 ?>