1<?php
2/**
3 * Plugin imagereference
4 *
5 * Syntax: <imgref linkname> - creates a figure link to an image
6 *         <tabref linkname> - creates a table link to a table
7 *
8 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author     Martin Heinemann <info@martinheinemann.net>
10 * @author     Gerrit Uitslag <klapinklapin@gmail.com>
11 */
12
13
14/**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
17 */
18class syntax_plugin_imagereference_imgref extends DokuWiki_Syntax_Plugin {
19
20    /**
21     * @return string Syntax type
22     */
23    function getType() {
24        return 'substition';
25    }
26    /**
27     * @return string Paragraph type
28     */
29    function getPType() {
30        return 'normal';
31    }
32    /**
33     * @return int Sort order
34     */
35    function getSort() {
36        return 197;
37    }
38
39    /**
40     * Connect lookup pattern to lexer.
41     *
42     * @param string $mode Parser mode
43     */
44    function connectTo($mode) {
45        $this->Lexer->addSpecialPattern('<imgref.*?>', $mode, 'plugin_imagereference_imgref');
46        $this->Lexer->addSpecialPattern('<tabref.*?>', $mode, 'plugin_imagereference_imgref');
47    }
48    /**
49     * Handle matches of the imgref syntax
50     *
51     * @param string $match The match of the syntax
52     * @param int    $state The state of the handler
53     * @param int    $pos The position in the document
54     * @param Doku_Handler    $handler The handler
55     * @return false|array Data for the renderer
56     */
57    function handle($match, $state, $pos, Doku_Handler $handler) {
58        $reftype = substr($match, 1, 3);
59        $ref = substr($match, 7, -1);
60
61        $parts = explode('#', $ref, 2);
62        if(count($parts) == 1) {
63            $page = '';
64            $ref = $parts[0];
65        } else {
66            $page = $parts[0];
67            $ref = $parts[1];
68        }
69
70        if($ref != '') {
71            return array(
72                'page' => trim($page),
73                'caprefname' => trim($ref),
74                'type'    => $reftype
75            );
76        }
77        return false;
78    }
79    /**
80     * Render xhtml output or metadata
81     *
82     * @param string         $mode      Renderer mode (supported modes: xhtml and metadata)
83     * @param Doku_Renderer  $renderer  The renderer
84     * @param array          $data      The data from the handler function
85     * @return bool If rendering was successful.
86     */
87    function render($mode, Doku_Renderer $renderer, $data) {
88        global $ID, $ACT;
89        if($data === false) return false;
90
91        switch($mode) {
92            case 'xhtml' :
93                /** @var Doku_Renderer_xhtml $renderer */
94
95                if($data['page'] == '') {
96                    $data['page'] = $ID;
97                }
98                resolve_pageid(getNS($ID), $data['page'], $exists);
99
100                //determine referencenumber
101                if($ACT == 'preview' && $data['page'] == $ID) {
102                    $caprefs = syntax_plugin_imagereference_imgcaption::getCaptionreferences($ID, $data['type']);
103                } else {
104                    $caprefs = p_get_metadata($data['page'], 'captionreferences '.$data['type']);
105                }
106                if(is_array($caprefs)) {
107                    $refNumber = array_search($data['caprefname'], $caprefs);
108                } else {
109                    $refNumber = false;
110                }
111
112                if(!$refNumber) {
113                    $refNumber = "##";
114                }
115
116                $renderer->doc .= '<a href="'.wl($data['page']).'#'.$data['type'].'_'.cleanID($data['caprefname']).'">'.$this->getLang($data['type'].'full').'&nbsp;'.$refNumber.'</a>';
117                return true;
118
119            case 'latex' :
120                $renderer->doc .= $this->getLang($data['type'].'full')." \\ref{".$data['caprefname']."}";
121                return true;
122        }
123        return false;
124    }
125}
126//Setup VIM: ex: et ts=4 enc=utf-8 :
127