1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5/**
6 * DokuWiki Plugin bibtex4dw (Syntax Component)
7 *
8 * Parse citation commands (i.e., actual references to the literature)
9 *
10 * For parsing the <bibtex>...</bibtex> structures of the markup,
11 * see the file "bibtex.php".
12 *
13 * PHP versions 5, 7 and 8
14 *
15 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
16 * @author  Till Biskup <till@till-biskup.de>
17 * @version 0.3
18 * @date    2023-05-28
19 */
20
21// must be run within Dokuwiki
22if (!defined('DOKU_INC')) die();
23
24if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
25if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
26if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
27
28require_once DOKU_PLUGIN.'syntax.php';
29
30class syntax_plugin_bibtex4dw_cite extends DokuWiki_Syntax_Plugin {
31    public function getType() {
32        return 'substition';
33    }
34
35    public function getPType() {
36        return 'normal';
37    }
38
39    public function getSort() {
40        return 32;
41    }
42
43    public function connectTo($mode) {
44        $this->Lexer->addSpecialPattern('\{\[.+?\]\}', $mode, 'plugin_bibtex4dw_cite');
45    }
46
47    public function handle($match, $state, $pos, Doku_Handler $handler){
48        // Strip syntax and return only bibtex key(s)
49        preg_match('/\{\[(.+?)\]\}/', $match, $matches);
50        return array($matches[1], $state, $pos);
51    }
52
53    public function render($mode, Doku_Renderer $renderer, $data) {
54        @list($match, $state, $pos) = $data;
55        global $ID;
56        require_once(DOKU_PLUGIN.'bibtex4dw/lib/bibtexrender.php');
57        $bibtexrenderer = bibtexrender_plugin_bibtex4dw::getResource($ID);
58
59        // Check whether the reference exists, otherwise silently ignore
60        // The problem still exists when all keys in one block do not exist
61        $bibkeys = explode(',', $match);
62        if ((count($bibkeys) > 1) || $bibtexrenderer->printCitekey($match)) {
63
64            if($mode == 'xhtml') {
65                $renderer->doc .= '[' ;
66                foreach ($bibkeys as $bibkey) {
67                    if ($bibtexrenderer->keyExists($bibkey)) {
68                        $renderer->doc .= '<span class="bibtex_citekey"><a href="#ref__'
69                            . $bibkey . '" name="reft__' . $bibkey . '" id="reft__'
70                            . $bibkey . '" class="bibtex_citekey">';
71                        $renderer->doc .= $bibtexrenderer->printCitekey($bibkey);
72                        $renderer->doc .= '</a><span>';
73                        $renderer->doc .= $bibtexrenderer->printReference($bibkey);
74                        $renderer->doc .= '</span></span>';
75                        // Suppress comma after last bibkey (alternative: implode)
76                        if ($bibkey != $bibkeys[sizeof($bibkeys)-1]) {
77                            $renderer->doc .= ', ';
78                        }
79                    } else {
80                        $renderer->doc .= $bibkey;
81                    }
82                }
83                $renderer->doc .= "]";
84            }
85
86            if($mode == 'latex') {
87                $renderer->doc .= '\cite{';
88                foreach ($bibkeys as $bibkey) {
89                    $renderer->doc .= $bibkey;
90                    // Suppress comma after last bibkey (alternative: implode)
91                    if ($bibkey != $bibkeys[sizeof($bibkeys)-1]) {
92                        $renderer->doc .= ",";
93                    }
94                }
95                $renderer->doc .= "}";
96            }
97
98            if($mode == 'odt') {
99                $renderer->doc .= "[" ;
100                foreach ($bibkeys as $bibkey) {
101                    $renderer->doc .= $bibtexrenderer->printCitekey($bibkey);
102                    // Suppress comma after last bibkey (alternative: implode)
103                    if ($bibkey != $bibkeys[sizeof($bibkeys)-1]) {
104                        $renderer->doc .= ', ';
105                    }
106                }
107                $renderer->doc .= "]";
108            }
109            return true;
110        }
111        return false;
112    }
113}
114?>
115