summaryrefslogtreecommitdiff
path: root/includes/pear/Text/Wiki/Render/Xhtml/Url.php
blob: f4f2e801155424c72f20a482e18b32471ab0a3b8 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
// vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
/**
 * Url rule end renderer for Xhtml
 *
 * PHP versions 4 and 5
 *
 * @category   Text
 * @package    Text_Wiki
 * @author     Paul M. Jones <pmjones@php.net>
 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
 * @version    CVS: $Id$
 * @link       http://pear.php.net/package/Text_Wiki
 */

/**
 * This class renders URL links in XHTML.
 *
 * @category   Text
 * @package    Text_Wiki
 * @author     Paul M. Jones <pmjones@php.net>
 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
 * @version    Release: @package_version@
 * @link       http://pear.php.net/package/Text_Wiki
 */
class Text_Wiki_Render_Xhtml_Url extends Text_Wiki_Render {


    public $conf = [
		"target"       => "_blank",
		"images"       => true,
		"img_ext"      => [ "jpg", "jpeg", "gif", "png" ],
		"css_inline"   => null,
		"css_footnote" => null,
		"css_descr"    => null,
		"css_img"      => null,
	];

    /**
    *
    * Renders a token into text matching the requested format.
    *
    * @access public
    *
    * @param array $options The "options" portion of the token (second
    * element).
    *
    * @return string The text rendered from the token options.
    *
    */

    function token($options)
    {
        // create local variables from the options array (text,
        // href, type)
        extract($options);

        // find the rightmost dot and determine the filename
        // extension.
        $pos = strrpos($href, ".");
        $ext = strtolower(substr($href, $pos + 1));
        $href = $this->textEncode($href);

        // does the filename extension indicate an image file?
        if ($this->getConf("images") &&
            in_array($ext, $this->getConf("img_ext", array()))) {

            // create alt text for the image
            if (! isset($text) || $text == "") {
                $text = basename($href);
                $text = $this->textEncode($text);
            }

            // generate an image tag
            $css = $this->formatConf(' class="%s"', "css_img");
            $start = "<img$css src=\"$href\" alt=\"$text\" title=\"$text\" /><!-- ";
            $end = " -->";

        } else {

            // should we build a target clause?
			$target = ( $href[0] == "#" ||
				strtolower( substr( $href, 0, 7 ) ) == "mailto:" ) ? "" : $this->getConf( "target" );

			// generate a regular link (not an image)
			$text = $this->textEncode( $text );
			$css = $this->formatConf( ' class="%s"', "css_$type" );
			$start = "<a$css href=\"$href\"";

			if ($target && $target != "_self") {
				// use a "popup" window.  this is XHTML compliant, suggested by
				// Aaron Kalin.  uses the $target as the new window name.
				$target = $this->textEncode( $target );
				$start .= " onclick=\"window.open(this.href, '$target');";
				$start .= " return false;\"";
			}

			if (isset( $name )) {
				$start .= " id=\"$name\"";
			}

			// finish up output
			$start .= ">";
			$end = "</a>";

			// make numbered references look like footnotes when no
			// CSS class specified, make them superscript by default
			if ($type == "footnote" && !$css) {
				$start = "<sup>" . $start;
				$end = $end . "</sup>";
			}
		}

		if ($options["type"] == "start") {
			$output = $start;
		}
		else { $end .= "</sup>"; }
			$output = ( $options["type"] == "end" ) ? $end : $start . $text . $end;

		return $output;
	}

}