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