xref: /plugin/magnifier/syntax.php (revision fe6a52107ea68656cc31eb0719180dea03f67e34)
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    function getInfo(){
19        return array_merge(confToHash(dirname(__FILE__).'/plugin.info.txt'), array());
20    }
21
22    function getType() { return 'substition'; }
23    function getPType() { return 'normal'; }
24    function getSort() { return 98; }
25
26    function connectTo($mode) {
27
28        $this->Lexer->addSpecialPattern('{{magnifier>[^}]+}}', $mode, 'plugin_magnifier');
29    }
30
31    function handle($match, $state, $pos, &$handler) {
32
33	    // {{magnifier>:image:test.png?widthxheight}}
34
35        $orig = substr($match, 12, -2);
36
37        list($id, $param) = explode('?', $orig, 2); // find ID + Params
38        list($w, $h) = explode('x', $param, 2); // find Size
39        /*
40        if ( preg_match("/{{[^}]+}}/", $name)) {
41
42        $displayImage = substr($name, 2, -2); // strip markup
43        $name = array();
44        list($name['id'], $name['name']) = explode('|', $displayImage, 2); // find ID/Params + Name Extension
45        list($name['id'], $name['param']) = explode('?', $name['id'], 2); // find ID + Params
46        list($name['w'], $name['h']) = explode('x', $name['param'], 2); // find Size
47        }
48        */
49        return array(trim($id), $w, $h, $orig);
50    }
51
52    function render($mode, &$renderer, $data) {
53        global $ID, $conf, $JSINFO;
54
55        list($id, $w, $h, $orig) = $data;
56        if ( empty($id) ) { $exists = false; } else
57        {
58        	$id = cleanID($id);
59            $page   = resolve_id(getNS($ID),$id);
60            $file   = mediaFN($page);
61            $exists = @file_exists($file) && @is_file($file);
62        }
63
64        if ($mode == 'xhtml') {
65
66            $params = ''; $params2 = '';
67
68            if ( $exists ) {
69                // is Media
70
71                $p1 = Doku_Handler_Parse_Media($orig);
72                $scID = sectionID(noNs($id), $renderer->headers);
73
74                $p = array();
75                $p['alt'] = $id;
76                $p['class'] = 'magnifierImage';
77                $p['id'] = 'magnifierimage_' . $scID;
78                $p['magnifierImage'] = ml($id);
79                if ($p1['width']) $p['width'] = $p1['width'];
80                if ($p1['height']) $p['height'] = $p1['height'];
81                if ($p1['title'] && !$p['title']) { $p['title'] = $p1['title']; $p['alt'] = $p1['title']; }
82                if ($p1['align']) $p['class'] .= ' media' . $p1['align'];
83
84                $p2 = buildAttributes($p);
85                $renderer->doc .= '<img src="' . ml($id, array( 'w' => $p['width'], 'h' => $p['height'] ) ) . '" '.$p2.'/>';
86
87                return true;
88            }
89        }
90        return false;
91    }
92
93    function _renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script, $class='') {
94
95        $more .= ' class="wikilink' . ($exists?1:2) . (!empty($class) ? ' ' . $class : '' ). '"';
96        $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)));
97
98        if ( !is_array($isImageMap) ) {
99            return '<a href="'.$id.'" ' . $more . ' >' . $name . '</a>' . $script;
100        } else {
101            $return = '<area href="'.$id.'" ' . $more . '';
102            $return .= ' title="'.$name.'" alt="'.$name.'"';
103            $return .= ' shape="'.$isImageMap['shape'].'" coords="'.$isImageMap['coords'].'" />' . $script;
104
105            return $return;
106        }
107    }
108}
109// vim:ts=4:sw=4:et:enc=utf-8:
110