1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4
5/**
6 * Display Fortune cookies
7 *
8 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author     Andreas Gohr <andi@splitbrain.org>
10 */
11
12
13class syntax_plugin_xfortune extends SyntaxPlugin
14{
15    /**
16     * What kind of syntax are we?
17     */
18    public function getType()
19    {
20        return 'substition';
21    }
22
23    /**
24     * What about paragraphs?
25     */
26    public function getPType()
27    {
28        return 'block';
29    }
30
31    /**
32     * Where to sort in?
33     */
34    public function getSort()
35    {
36        return 302;
37    }
38
39    /**
40     * Connect pattern to lexer
41     */
42    public function connectTo($mode)
43    {
44        $this->Lexer->addSpecialPattern('\{\{xfortune>[^}]*\}\}', $mode, 'plugin_xfortune');
45    }
46
47    /**
48     * Handle the match
49     */
50    public function handle($match, $state, $pos, Doku_Handler $handler)
51    {
52        $match = substr($match, 11, -2); //strip markup from start and end
53
54        $data = [];
55
56        //handle params
57        [$cookie, $params] = explode('?', $match, 2);
58
59        //xfortune cookie file
60        $data['cookie'] = cleanID($cookie);
61
62        //time interval for changing cookies
63        $data['time'] = 30;
64        if (preg_match('/\b(\d+)\b/i', $params, $match)) {
65            $data['time'] = (int) $match[1];
66        }
67
68        //no hammering please!
69        if ($data['time'] < 5) $data['time'] = 5;
70
71        return $data;
72    }
73
74    /**
75     * Create output
76     */
77    public function render($mode, Doku_Renderer $renderer, $data)
78    {
79        if ($mode != 'xhtml') return false;
80
81        $attr = ['class' => 'plugin_xfortune', 'data-time' => $data['time'], 'data-cookie' => $data['cookie']];
82
83        $renderer->doc .= '<div ' . buildAttributes($attr) . '>';
84        $renderer->doc .= helper_plugin_xfortune::getCookieHTML($data['cookie']);
85        $renderer->doc .= '</div>';
86
87        return true;
88    }
89}
90
91//Setup VIM: ex: et ts=4 enc=utf-8 :
92