summaryrefslogtreecommitdiff
path: root/includes/pear/Text/Wiki/Parse/Creole/Url.php
blob: 4422a3170c03fe4a12ceabeb336f3a71d1af3095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

/**
 *
 * Parse for URLS in the source text.
 *
 * raw       -- http://example.com
 * no descr. -- [[http://example.com]]
 * described -- [[http://example.com|Example Description]]
 *
 * When rendering a URL token, this will convert URLs pointing to a .gif,
 * .jpg, or .png image into an inline <img /> tag (for the 'xhtml'
 * format).
 *
 * @category Text
 *
 * @package Text_Wiki
 *
 * @author Michele Tomaiuolo <tomamic@yahoo.it>
 *
 * @license LGPL
 *
 * @version $Id: Url.php 293784 2010-01-20 18:48:09Z justinpatrin $
 *
 */

class Text_Wiki_Parse_Url extends Text_Wiki_Parse {

    /**
     *
     * Constructor.  Overrides the Text_Wiki_Parse constructor so that we
     * can set the $regex property dynamically (we need to include the
     * Text_Wiki $delim character).
     *
     * @param object &$obj The calling "parent" Text_Wiki object.
     *
     * @param string $name The token name to use for this rule.
     *
     */

    function Text_Wiki_Parse_Url(&$obj)
    {
        parent::Text_Wiki_Parse($obj);
        $this->regex = '/((?:\[\[ *((?:\w+:\/\/|mailto:|\/)[^\|\]\n ]*)( *\| *([^\]\n]*))? *\]\])|((?<=[^\~\w])(https?:\/\/|ftps?:\/\/|mailto:)[^\'\"\n ' . $this->wiki->delim . ']*[A-Za-z0-9\/\?\=\&\~\_#]))/';
    }


    /**
     *
     * Generates a replacement for the matched text.
     *
     * Token options are:
     *
     * 'href' => the URL link href portion
     *
     * 'text' => the displayed text of the URL link
     *
     * @access public
     *
     * @param array &$matches The array of matches from parse().
     *
     * @return string A token to be used as a placeholder
     * in the source text for the preformatted text.
     *
     */

    function process(&$matches)
    {
        if (isset($matches[2])) $href = trim($matches[2]);
        if (isset($matches[4])) $text = trim($matches[4]);
        if (isset($matches[5])) $rawurl = $matches[5];
        if (empty($href)) $href = $rawurl;

        if (empty($text)) {
            $text = $href;
            if (strpos($text, '/') === FALSE) {
				$text = str_replace('http://', '', $text);
				$text = str_replace('mailto:', '', $text);
			}
            return $this->wiki->addToken(
                $this->rule,
                array(
					'type' => 'inline',
                    'href' => $href,
                    'text' => $text
                )
            );
        } else {
            return $this->wiki->addToken(
                $this->rule,
                array(
                    'type' => 'start',
                    'href' => $href,
                    'text' => $text
                )
            ) . $text .
            $this->wiki->addToken(
                $this->rule,
                array(
                    'type' => 'end',
                    'href' => $href,
                    'text' => $text
                )
            );
        }
    }

}
?>