Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
248 jpm 1
<?php
2
 
3
/**
4
*
5
* Parse for URLS in the source text.
6
*
7
* Various URL markings are supported: inline (the URL by itself),
8
* numbered or footnote reference (where the URL is enclosed in square brackets), and
9
* named reference (where the URL is enclosed in square brackets and has a
10
* name included inside the brackets).  E.g.:
11
*
12
* inline    -- http://example.com
13
* numbered  -- [http://example.com]
14
* described -- [http://example.com Example Description]
15
*
16
* When rendering a URL token, this will convert URLs pointing to a .gif,
17
* .jpg, or .png image into an inline <img /> tag (for the 'xhtml'
18
* format).
19
*
20
* Token options are:
21
*
22
* 'type' => ['inline'|'footnote'|'descr'] the type of URL
23
*
24
* 'href' => the URL link href portion
25
*
26
* 'text' => the displayed text of the URL link
27
*
28
* $Id: Url.php,v 1.1 2005-01-20 19:43:20 jpm Exp $
29
*
30
* @author Paul M. Jones <pmjones@ciaweb.net>
31
*
32
* @package Text_Wiki
33
*
34
*/
35
 
36
class Text_Wiki_Parse_Url extends Text_Wiki_Parse {
37
 
38
 
39
    /**
40
    *
41
    * Keeps a running count of numbered-reference URLs.
42
    *
43
    * @access public
44
    *
45
    * @var int
46
    *
47
    */
48
 
49
    var $footnoteCount = 0;
50
 
51
 
52
    /**
53
    *
54
    * URL schemes recognized by this rule.
55
    *
56
    * @access public
57
    *
58
    * @var array
59
    *
60
    */
61
 
62
    var $conf = array(
63
        'schemes' => array(
64
            'http://',
65
            'https://',
66
            'ftp://',
67
            'gopher://',
68
            'news://',
69
            'mailto:'
70
        )
71
    );
72
 
73
 
74
    /**
75
    *
76
    * Constructor.
77
    *
78
    * We override the constructor so we can comment the regex nicely.
79
    *
80
    * @access public
81
    *
82
    */
83
 
84
    function Text_Wiki_Parse_Url(&$obj)
85
    {
86
        parent::Text_Wiki_Parse($obj);
87
 
88
        // convert the list of recognized schemes to a regex-safe string,
89
        // where the pattern delim is a slash
90
        $tmp = array();
91
        $list = $this->getConf('schemes', array());
92
        foreach ($list as $val) {
93
            $tmp[] = preg_quote($val, '/');
94
        }
95
        $schemes = implode('|', $tmp);
96
 
97
        // build the regex
98
        $this->regex =
99
            "($schemes)" . // allowed schemes
100
            "(" . // start pattern
101
            "[^ \\/\"\'{$this->wiki->delim}]*\\/" . // no spaces, backslashes, slashes, double-quotes, single quotes, or delimiters;
102
            ")*" . // end pattern
103
            "[^ \\t\\n\\/\"\'{$this->wiki->delim}]*" .
104
            "[A-Za-z0-9\\/?=&~_]";
105
    }
106
 
107
 
108
    /**
109
    *
110
    * Find three different kinds of URLs in the source text.
111
    *
112
    * @access public
113
    *
114
    */
115
 
116
    function parse()
117
    {
118
        // -------------------------------------------------------------
119
        //
120
        // Described-reference (named) URLs.
121
        //
122
 
123
        // the regular expression for this kind of URL
124
        $tmp_regex = '/\[(' . $this->regex . ') ([^\]]+)\]/';
125
 
126
        // use a custom callback processing method to generate
127
        // the replacement text for matches.
128
        $this->wiki->source = preg_replace_callback(
129
            $tmp_regex,
130
            array(&$this, 'processDescr'),
131
            $this->wiki->source
132
        );
133
 
134
 
135
        // -------------------------------------------------------------
136
        //
137
        // Numbered-reference (footnote-style) URLs.
138
        //
139
 
140
        // the regular expression for this kind of URL
141
        $tmp_regex = '/\[(' . $this->regex . ')\]/U';
142
 
143
        // use a custom callback processing method to generate
144
        // the replacement text for matches.
145
        $this->wiki->source = preg_replace_callback(
146
            $tmp_regex,
147
            array(&$this, 'processFootnote'),
148
            $this->wiki->source
149
        );
150
 
151
 
152
        // -------------------------------------------------------------
153
        //
154
        // Normal inline URLs.
155
        //
156
 
157
        // the regular expression for this kind of URL
158
 
159
        $tmp_regex = '/(^|[^A-Za-z])(' . $this->regex . ')(.*?)/';
160
 
161
        // use the standard callback for inline URLs
162
        $this->wiki->source = preg_replace_callback(
163
            $tmp_regex,
164
            array(&$this, 'process'),
165
            $this->wiki->source
166
        );
167
    }
168
 
169
 
170
    /**
171
    *
172
    * Process inline URLs.
173
    *
174
    * @param array &$matches
175
    *
176
    * @param array $matches An array of matches from the parse() method
177
    * as generated by preg_replace_callback.  $matches[0] is the full
178
    * matched string, $matches[1] is the first matched pattern,
179
    * $matches[2] is the second matched pattern, and so on.
180
    *
181
    * @return string The processed text replacement.
182
    *
183
    */
184
 
185
    function process(&$matches)
186
    {
187
        // set options
188
        $options = array(
189
            'type' => 'inline',
190
            'href' => $matches[2],
191
            'text' => $matches[2]
192
        );
193
 
194
        // tokenize
195
        return $matches[1] . $this->wiki->addToken($this->rule, $options) . $matches[5];
196
    }
197
 
198
 
199
    /**
200
    *
201
    * Process numbered (footnote) URLs.
202
    *
203
    * Token options are:
204
    * @param array &$matches
205
    *
206
    * @param array $matches An array of matches from the parse() method
207
    * as generated by preg_replace_callback.  $matches[0] is the full
208
    * matched string, $matches[1] is the first matched pattern,
209
    * $matches[2] is the second matched pattern, and so on.
210
    *
211
    * @return string The processed text replacement.
212
    *
213
    */
214
 
215
    function processFootnote(&$matches)
216
    {
217
        // keep a running count for footnotes
218
        $this->footnoteCount++;
219
 
220
        // set options
221
        $options = array(
222
            'type' => 'footnote',
223
            'href' => $matches[1],
224
            'text' => $this->footnoteCount
225
        );
226
 
227
        // tokenize
228
        return $this->wiki->addToken($this->rule, $options);
229
    }
230
 
231
 
232
    /**
233
    *
234
    * Process described-reference (named-reference) URLs.
235
    *
236
    * Token options are:
237
    *     'type' => ['inline'|'footnote'|'descr'] the type of URL
238
    *     'href' => the URL link href portion
239
    *     'text' => the displayed text of the URL link
240
    *
241
    * @param array &$matches
242
    *
243
    * @param array $matches An array of matches from the parse() method
244
    * as generated by preg_replace_callback.  $matches[0] is the full
245
    * matched string, $matches[1] is the first matched pattern,
246
    * $matches[2] is the second matched pattern, and so on.
247
    *
248
    * @return string The processed text replacement.
249
    *
250
    */
251
 
252
    function processDescr(&$matches)
253
    {
254
        // set options
255
        $options = array(
256
            'type' => 'descr',
257
            'href' => $matches[1],
258
            'text' => $matches[4]
259
        );
260
 
261
        // tokenize
262
        return $this->wiki->addToken($this->rule, $options);
263
    }
264
}
265
?>