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        $this->Lexer->addSpecialPattern('{{autoref>.+?}}',$mode,'plugin_latexcaption_reference');
40    }
41
42    public function handle($match, $state, $pos, Doku_Handler $handler){
43        // Strip the {{}}
44        $match = substr($match,2,-2);
45        list($type, $label) = explode('>',$match);
46        // Set the params
47        $params['label'] = $label;
48        $params['type'] = $type;
49
50        return array($state, $match, $pos, $params);
51    }
52
53    public function render($mode, Doku_Renderer $renderer, $data) {
54        if (empty($data)) {
55            return false;
56        }
57
58        list($state, $match, $pos, $params) = $data;
59        // Only special state allowed
60        if ($state !== DOKU_LEXER_SPECIAL) {
61                return true;
62            }
63
64        global $caption_count;
65        global $INFO;
66
67        $type = $params['type'];
68        $label = $params['label'];
69
70        $langset = ($this->getConf('abbrev') ? 'abbrev' : 'long');
71        // Should we display the reference type
72        $disptype = (substr($type, 0, 4) == 'auto') || $this->getConf('alwaysautoref');
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 we cant find a matching label we assume its for a mathjax equation
77        $mathjaxref = false;
78        if (!$caption && $this->getConf('mathjaxref')) {
79            // Only allow true if mathjax plugin is available
80            $mathjaxref = !plugin_isdisabled('mathjax');
81        }
82
83        /** @var Doku_Renderer_xhtml $renderer */
84        if ($mode == 'xhtml') {
85            if (!$this->helper)
86                $this->helper = plugin_load('helper', 'latexcaption');
87
88            if ($mathjaxref) {
89                // Only passing reference through for mathjax rendering in js
90                $markup = '<a href="#mjx-eqn'.rawurlencode(':').$label.'">';
91                $markup .= ($disptype) ? $this->getLang('equation'.$langset).' ' : '';
92                $markup .= '\eqref{'.$label.'}</a>';
93                $renderer->doc .= $markup;
94                return true;
95            }
96
97            $markup = '<a href="#'.$label.'">';
98
99            if ($caption) {
100                list($type, $num, $parnum) = $caption;
101                if (substr($type, 0, 3) == 'sub') {
102                    $type = substr($type, 3);
103                    $markup .= $disptype ? $this->getLang($type.$langset).' ' : '';
104                    $markup .= $parnum.'('.$this->helper->number_to_alphabet($num).')';
105                }
106                else{
107                    $markup .= $disptype ? $this->getLang($type.$langset).' ' : '';
108                    $markup .= $num;
109                }
110            } else {
111                $markup .= '??REF:'.$label.'??';
112            }
113            $markup .= '</a>';
114            $renderer->doc .= $markup;
115
116            return true;
117        }
118
119        if ($mode == 'latex') {
120            if ($disptype) {
121                $renderer->doc .= '\autoref'.'{'.$label.'}';
122            } else {
123                $renderer->doc .= '\ref'.'{'.$label.'}';
124            }
125
126            return true;
127        }
128
129        if ($mode == 'odt') {
130            $renderer->doc .= '<text:sequence-ref text:reference-format="value" text:ref-name="'.$label.'">';
131            $renderer->doc .= $caption_count[$label];
132            $renderer->doc .= '</text:sequence-ref>';
133            return true;
134        }
135
136        // unsupported $mode
137        return true;
138    }
139}
140
141// vim:ts=4:sw=4:et:
142