1<?php
2/**
3 * DokuWiki Plugin rndtxt (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9class syntax_plugin_rndtxt extends DokuWiki_Syntax_Plugin
10{
11    /** @inheritDoc */
12    public function getType()
13    {
14        return 'substition';
15    }
16
17    /** @inheritDoc */
18    public function getPType()
19    {
20        return 'normal';
21    }
22
23    /** @inheritDoc */
24    public function getSort()
25    {
26        return 155;
27    }
28
29    /** @inheritDoc */
30    public function connectTo($mode)
31    {
32        $this->Lexer->addSpecialPattern('{\?.*?\?}', $mode, 'plugin_rndtxt');
33    }
34
35    /** @inheritDoc */
36    public function handle($match, $state, $pos, Doku_Handler $handler)
37    {
38        $match = substr($match, 2, -2);
39        $data = explode('|', $match);
40        $data = array_map('trim', $data);
41        $data = array_unique($data);
42        return $data;
43    }
44
45    /** @inheritDoc */
46    public function render($mode, Doku_Renderer $renderer, $data)
47    {
48        if ($mode !== 'xhtml') {
49            return false;
50        }
51
52        $renderer->nocache();
53
54        $idx = array_rand($data);
55        $renderer->cdata($data[$idx]);
56
57        return true;
58    }
59}
60
61