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