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
 
3
/**
4
*
5
* Baseline rule class for extension into a "real" parser component.
6
*
7
* Text_Wiki_Rule classes do not stand on their own; they are called by a
8
* Text_Wiki object, typcially in the transform()method. Each rule class
9
* performs three main activities: parse, process, and render.
10
*
11
* The parse() method takes a regex and applies it to the whole block of
12
* source text at one time. Each match is sent as $matches to the
13
* process() method.
14
*
15
* The process() method acts on the matched text from the source, and
16
* then processes the source text is some way.  This may mean the
17
* creation of a delimited token using addToken().  In every case, the
18
* process() method returns the text that should replace the matched text
19
* from parse().
20
*
21
* @author Paul M. Jones <pmjones@ciaweb.net>
22
*
23
* @package Text_Wiki
24
*
25
* $Id: Parse.php,v 1.1 2005-01-20 19:43:20 jpm Exp $
26
*
27
*/
28
 
29
class Text_Wiki_Parse {
30
 
31
 
32
    /**
33
    *
34
    * Configuration options for this parser rule.
35
    *
36
    * @access public
37
    *
38
    * @var string
39
    *
40
    */
41
 
42
    var $conf = array();
43
 
44
 
45
    /**
46
    *
47
    * Regular expression to find matching text for this rule.
48
    *
49
    * @access public
50
    *
51
    * @var string
52
    *
53
    * @see parse()
54
    *
55
    */
56
 
57
    var $regex = null;
58
 
59
 
60
    /**
61
    *
62
    * The name of this rule for new token array elements.
63
    *
64
    * @access public
65
    *
66
    * @var string
67
    *
68
    */
69
 
70
    var $rule = null;
71
 
72
 
73
    /**
74
    *
75
    * A reference to the calling Text_Wiki object.
76
    *
77
    * This is needed so that each rule has access to the same source
78
    * text, token set, URLs, interwiki maps, page names, etc.
79
    *
80
    * @access public
81
    *
82
    * @var object
83
    */
84
 
85
    var $wiki = null;
86
 
87
 
88
    /**
89
    *
90
    * Constructor for this parser rule.
91
    *
92
    * @access public
93
    *
94
    * @param object &$obj The calling "parent" Text_Wiki object.
95
    *
96
    */
97
 
98
    function Text_Wiki_Parse(&$obj)
99
    {
100
        // set the reference to the calling Text_Wiki object;
101
        // this allows us access to the shared source text, token
102
        // array, etc.
103
        $this->wiki =& $obj;
104
 
105
        // set the name of this rule; generally used when adding
106
        // to the tokens array. strip off the Text_Wiki_Parse_ portion.
107
        // text_wiki_parse_
108
        // 0123456789012345
109
        $tmp = substr(get_class($this), 16);
110
        $this->rule = ucwords(strtolower($tmp));
111
 
112
        // override config options for the rule if specified
113
        if (isset($this->wiki->parseConf[$this->rule]) &&
114
            is_array($this->wiki->parseConf[$this->rule])) {
115
 
116
            $this->conf = array_merge(
117
                $this->conf,
118
                $this->wiki->parseConf[$this->rule]
119
            );
120
 
121
        }
122
    }
123
 
124
 
125
    /**
126
    *
127
    * Abstrct method to parse source text for matches.
128
    *
129
    * Applies the rule's regular expression to the source text, passes
130
    * every match to the process() method, and replaces the matched text
131
    * with the results of the processing.
132
    *
133
    * @access public
134
    *
135
    * @see Text_Wiki_Parse::process()
136
    *
137
    */
138
 
139
    function parse()
140
    {
141
        $this->wiki->source = preg_replace_callback(
142
            $this->regex,
143
            array(&$this, 'process'),
144
            $this->wiki->source
145
        );
146
    }
147
 
148
 
149
    /**
150
    *
151
    * Abstract method to generate replacements for matched text.
152
    *
153
    * @access public
154
    *
155
    * @param array $matches An array of matches from the parse() method
156
    * as generated by preg_replace_callback.  $matches[0] is the full
157
    * matched string, $matches[1] is the first matched pattern,
158
    * $matches[2] is the second matched pattern, and so on.
159
    *
160
    * @return string The processed text replacement; defaults to the
161
    * full matched string (i.e., no changes to the text).
162
    *
163
    * @see Text_Wiki_Parse::parse()
164
    *
165
    */
166
 
167
    function process(&$matches)
168
    {
169
        return $matches[0];
170
    }
171
 
172
 
173
    /**
174
    *
175
    * Simple method to safely get configuration key values.
176
    *
177
    * @access public
178
    *
179
    * @param string $key The configuration key.
180
    *
181
    * @param mixed $default If the key does not exist, return this value
182
    * instead.
183
    *
184
    * @return mixed The configuration key value (if it exists) or the
185
    * default value (if not).
186
    *
187
    */
188
 
189
    function getConf($key, $default = null)
190
    {
191
        if (isset($this->conf[$key])) {
192
            return $this->conf[$key];
193
        } else {
194
            return $default;
195
        }
196
    }
197
 
198
 
199
    /**
200
    *
201
    * Extract 'attribute="value"' portions of wiki markup.
202
    *
203
    * This kind of markup is typically used only in macros, but is useful
204
    * anywhere.
205
    *
206
    * The syntax is pretty strict; there can be no spaces between the
207
    * option name, the equals, and the first double-quote; the value
208
    * must be surrounded by double-quotes.  You can escape characters in
209
    * the value with a backslash, and the backslash will be stripped for
210
    * you.
211
    *
212
    * @access public
213
    *
214
    * @param string $text The "attributes" portion of markup.
215
    *
216
    * @return array An associative array of key-value pairs where the
217
    * key is the option name and the value is the option value.
218
    *
219
    */
220
 
221
    function getAttrs($text)
222
    {
223
        // find the =" sections;
224
        $tmp = explode('="', trim($text));
225
 
226
        // basic setup
227
        $k = count($tmp) - 1;
228
        $attrs = array();
229
        $key = null;
230
 
231
        // loop through the sections
232
        foreach ($tmp as $i => $val) {
233
 
234
            // first element is always the first key
235
            if ($i == 0) {
236
                $key = trim($val);
237
                continue;
238
            }
239
 
240
            // find the last double-quote in the value.
241
            // the part to the left is the value for the last key,
242
            // the part to the right is the next key name
243
            $pos = strrpos($val, '"');
244
            $attrs[$key] = stripslashes(substr($val, 0, $pos));
245
            $key = trim(substr($val, $pos+1));
246
 
247
        }
248
 
249
        return $attrs;
250
 
251
    }
252
}
253
?>