1<?php
2/**
3 * magnifier Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13
14require_once(DOKU_PLUGIN.'syntax.php');
15
16class syntax_plugin_magnifier extends DokuWiki_Syntax_Plugin {
17
18    private $headers = array();
19
20    function getInfo(){
21        return array_merge(confToHash(dirname(__FILE__).'/plugin.info.txt'), array());
22    }
23
24    function getType() { return 'substition'; }
25    function getPType() { return 'normal'; }
26    function getSort() { return 98; }
27
28    function connectTo($mode) {
29
30        $this->Lexer->addSpecialPattern('{{magnifier>[^}]+}}', $mode, 'plugin_magnifier');
31    }
32
33    function handle($match, $state, $pos, Doku_Handler $handler) {
34
35        // {{magnifier>:image:test.png?widthxheight}}
36
37        $orig = substr($match, 12, -2);
38
39        list($id, $param) = explode('?', $orig, 2); // find ID + Params
40        list($w, $h) = explode('x', $param, 2); // find Size
41
42        $p1 = Doku_Handler_Parse_Media($orig);
43        return array(trim($id), $w, $h, $orig, $p1);
44    }
45
46    function render($mode, Doku_Renderer $renderer, $data) {
47        global $ID, $conf, $JSINFO;
48
49        list($id, $w, $h, $orig, $p1) = $data;
50        if ( empty($id) ) { $exists = false; } else
51        {
52            $id = cleanID($id);
53            $page   = resolve_id(getNS($ID),$id);
54            $file   = mediaFN($page);
55            $exists = @file_exists($file) && @is_file($file);
56        }
57
58        if ($mode == 'xhtml') {
59
60            $params = ''; $params2 = '';
61
62            if ( $exists ) {
63                // is Media
64
65                $scID = sectionID(noNs($id), $this->headers);
66
67                $p = array();
68                $p['alt'] = $id;
69                $p['class'] = 'magnifierImage';
70                $p['id'] = 'magnifierimage_' . $scID;
71                $p['magnifierImage'] = ml($id);
72                if ($p1['width']) $p['width'] = $p1['width'];
73                if ($p1['height']) $p['height'] = $p1['height'];
74                if ($p1['title'] && !$p['title']) { $p['title'] = $p1['title']; $p['alt'] = $p1['title']; }
75                if ($p1['align']) $p['class'] .= ' media' . $p1['align'];
76
77                $p2 = buildAttributes($p);
78                $renderer->doc .= '<img src="' . ml($id, array( 'w' => $p['width'], 'h' => $p['height'] ) ) . '" '.$p2.'/>';
79
80                return true;
81            }
82        }
83        return false;
84    }
85
86    function _renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script, $class='') {
87
88        $more .= ' class="wikilink' . ($exists?1:2) . (!empty($class) ? ' ' . $class : '' ). '"';
89        $name = trim(preg_replace("%^(\s|\r|\n)*?<a.+?>(.*)?</a>(\s|\r|\n)*?$%is", "$2", preg_replace("%^(\s|\r|\n)*?<p.*?>(.*)?</p>(\s|\r|\n)*?$%is", "$2", $name)));
90
91        if ( !is_array($isImageMap) ) {
92            return '<a href="'.$id.'" ' . $more . ' >' . $name . '</a>' . $script;
93        } else {
94            $return = '<area href="'.$id.'" ' . $more . '';
95            $return .= ' title="'.$name.'" alt="'.$name.'"';
96            $return .= ' shape="'.$isImageMap['shape'].'" coords="'.$isImageMap['coords'].'" />' . $script;
97
98            return $return;
99        }
100    }
101}
102// vim:ts=4:sw=4:et:enc=utf-8:
103