1<?php
2
3/**
4 * DokuWiki Plugin latexit (Syntax Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Adam Kučera <adam.kucera@wrent.cz>
8 */
9// must be run within Dokuwiki
10if (!defined('DOKU_INC'))
11    die();
12
13/**
14 * Syntax component handels all substitutions and new DW commands in original text.
15 */
16class syntax_plugin_latexit_base extends DokuWiki_Syntax_Plugin {
17
18    /**
19     * Order in which this Syntax plugin will be called.
20     * @var int
21     */
22    protected $sort;
23
24    /**
25     * @return string Syntax mode type
26     */
27    public function getType() {
28        return 'substition';
29    }
30
31    /**
32     * @return int Sort order - Low numbers go before high numbers
33     */
34    public function getSort() {
35        if (!isset($this->sort)) {
36            return 245;
37        } else {
38            return $this->sort;
39        }
40    }
41
42    /**
43     * Connect lookup pattern to lexer.
44     *
45     * @param string $mode Parser mode
46     */
47    public function connectTo($mode) {
48        $this->Lexer->addSpecialPattern('~~~*RECURSIVE~*~~', $mode, 'plugin_latexit_base');
49        $this->Lexer->addSpecialPattern('\\\cite.*?\}', $mode, 'plugin_latexit_base');
50    }
51
52    /**
53     * This syntax plugin should be used as a singleton.
54     * (so it can change its sort, when latex will be rendered)
55     * @return boolean
56     */
57    public function isSingleton() {
58        return true;
59    }
60
61    /**
62     * Handle matches of the latexit syntax
63     *
64     * @param string $match The match of the syntax
65     * @param int    $state The state of the handler
66     * @param int    $pos The position in the document
67     * @param Doku_Handler    $handler The handler
68     * @return array Data for the renderer
69     */
70    public function handle($match, $state, $pos, Doku_Handler $handler) {
71        //parse citations from the text (this will be done by this plugin only for latex export)
72        //FIXME cite in paper regex is from zotero plugin, it has to match exactly
73        if (preg_match('/\\\cite(\[([a-zA-Z0-9 \.,\-:]*)\])?\{([a-zA-Z0-9\-:]*?)\}/', $match, $matches)) {
74            //$pageRef = $matches[2];
75            $citeKey = $matches[3];
76            return $citeKey;
77        } //parse RECURSIVE command
78        elseif (preg_match('#~~RECURSIVE~~#', $match)) {
79            $tildas = explode('RECURSIVE', $match);
80            if ($tildas[0] == $tildas[1]) {
81                return array($state, $tildas);
82            }
83        }
84        return array();
85    }
86
87    /**
88     * Render xhtml output or metadata
89     *
90     * @param string         $mode      Renderer mode (supported modes: xhtml)
91     * @param Doku_Renderer  $renderer  The renderer
92     * @param array          $data      The data from the handler() function
93     * @return bool If rendering was successful.
94     */
95    public function render($mode, Doku_Renderer $renderer, $data) {
96        //this will count the level of an following header according to number of ~ used
97        if(is_array($data)) {
98
99            switch(strlen($data[1][0])) {
100                case 6:
101                    $level = 1;
102                    break;
103                case 5:
104                    $level = 2;
105                    break;
106                case 4:
107                    $level = 3;
108                    break;
109                case 3:
110                    $level = 4;
111                    break;
112                case 2:
113                    $level = 5;
114                    break;
115                default:
116                    $level = 5;
117                    break;
118            }
119        }
120        //inserts the information about set header level even to XHMTL
121        if($mode == 'xhtml') {
122            if(is_array($data)) {
123                $renderer->doc .= '<h'.$level.'>'.hsc($this->getConf('link_insertion_message')).'</h'.$level.'>';
124            }
125            return true;
126        } elseif($mode == 'latex') {
127            //set the next link to be added recursively
128            if(is_array($data)) {
129                //there might be more plugins rendering latex and calling this functions could cause an error
130                if(method_exists($renderer, '_setRecursive')) {
131                    $renderer->_setRecursive(true);
132                    $renderer->_increaseLevel($level - 1);
133                }
134            } //insert citation
135            else {
136                $renderer->doc .= '\\cite{'.$data.'}';
137                $renderer->_bibEntry($data);
138            }
139            return true;
140        }
141
142        return false;
143    }
144
145    /**
146     * Set sort order of the syntax component
147     * @param int $sort Sort order.
148     */
149    public function _setSort($sort) {
150        $this->sort = $sort;
151    }
152
153}
154