1<?php
2/**
3 * DokuWiki Plugin latexcaption (Renderer Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Ben van Magill <ben.vanmagill16@gmail.com>
7 * @author  Till Biskup <till@till-biskup>
8 */
9
10
11class syntax_plugin_latexcaption_reference extends \dokuwiki\Extension\SyntaxPlugin {
12
13    /** @var $helper helper_plugin_latexcaption */
14    var $helper = null;
15
16    function getInfo(){
17        return confToHash(dirname(__FILE__).'/../plugin.info.txt');
18    }
19
20    public function getType() {
21        return 'substition';
22    }
23
24    public function getAllowedTypes() {
25        return array('formatting', 'substition', 'disabled', 'container', 'protected');
26    }
27
28    public function getPType() {
29        return 'normal';
30    }
31
32    public function getSort() {
33        return 319;
34    }
35
36
37    public function connectTo($mode) {
38        $this->Lexer->addSpecialPattern('{{ref>.+?}}',$mode,'plugin_latexcaption_reference');
39    }
40
41    public function handle($match, $state, $pos, Doku_Handler $handler){
42        if (substr($match,0,6) != '{{ref>') {
43            return array();
44        }
45        return array($state, substr($match,6,-2));
46    }
47
48    public function render($mode, Doku_Renderer $renderer, $data) {
49        if (empty($data)) {
50            return false;
51        }
52
53        list($state,$match) = $data;
54        global $caption_count;
55
56        $label = $match;
57        $langset = ($this->getConf('abbrev') ? 'abbrev' : 'long');
58
59        // Only special state allowed
60        if ($state !== DOKU_LEXER_SPECIAL) {
61                return true;
62            }
63
64        /** @var Doku_Renderer_xhtml $renderer */
65        if ($mode == 'xhtml') {
66            global $INFO;
67
68            if (!$this->helper)
69                $this->helper = plugin_load('helper', 'latexcaption');
70
71            $markup = '<a href="#'.$label.'">';
72
73            // Retrieve the figure label from the global array or metadata
74            $caption = $caption_count[$label] ? $caption_count[$label] : $INFO['meta']['plugin']['latexcaption']['references'][$label];
75
76            if ($caption) {
77                list($type, $num, $parnum) = $caption;
78                if (substr($type, 0, 3) == 'sub') {
79                    $type = substr($type, 3);
80                    $markup .= $this->getLang($type.$langset).' '.$parnum.'('.$this->helper->number_to_alphabet($num).')';
81                }
82                else{
83                    $markup .= $this->getLang($type.$langset).' '.$num;
84                }
85            } else {
86                $markup .= '??REF:'.$label.'??';
87            }
88            $markup .= '</a>';
89            $renderer->doc .= $markup;
90
91            return true;
92        }
93
94        if ($mode == 'latex') {
95            $renderer->doc .= '\ref{'.$label.'}';
96            return true;
97        }
98
99        if ($mode == 'odt') {
100            $renderer->doc .= '<text:sequence-ref text:reference-format="value" text:ref-name="'.$label.'">';
101            $renderer->doc .= $caption_count[$label];
102            $renderer->doc .= '</text:sequence-ref>';
103            return true;
104        }
105
106        // unsupported $mode
107        return true;
108    }
109}
110
111// vim:ts=4:sw=4:et:
112