Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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