1<?php
2/**
3 * DokuWiki Plugin randomquote (Syntax Component)
4 *
5 * @license MIT License
6 * @author  Ingo Kleiber <ingo@kleiber.me>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class syntax_plugin_randomquote extends DokuWiki_Syntax_Plugin {
13    /**
14     * @return string Syntax mode type
15     */
16    public function getType() {
17        return 'protected';
18    }
19    /**
20     * @return string Paragraph type
21     */
22    public function getPType() {
23        return 'stack';
24    }
25    /**
26     * @return int Sort order - Low numbers go before high numbers
27     */
28    public function getSort() {
29        return 322;
30    }
31
32    /**
33     * Connect lookup pattern to lexer.
34     *
35     * @param string $mode Parser mode
36     */
37    public function connectTo($mode) {
38        $this->Lexer->addSpecialPattern('<randomquote>',$mode,'plugin_randomquote');
39    }
40
41    /**
42     * Handle matches of the randomquote syntax
43     *
44     * @param string $match The match of the syntax
45     * @param int    $state The state of the handler
46     * @param int    $pos The position in the document
47     * @param Doku_Handler    $handler The handler
48     * @return array Data for the renderer
49     */
50    public function handle($match, $state, $pos, Doku_Handler $handler){
51        return $match;
52    }
53
54    /**
55     * Render xhtml output or metadata
56     *
57     * @param string         $mode      Renderer mode (supported modes: xhtml)
58     * @param Doku_Renderer  $renderer  The renderer
59     * @param array          $data      The data from the handler() function
60     * @return bool If rendering was successful.
61     */
62    public function render($mode, Doku_Renderer $renderer, $data) {
63        if($mode != 'xhtml') return false;
64        $quotes = file('./lib/plugins/randomquote/quotes.txt', FILE_IGNORE_NEW_LINES);
65
66        $renderer->doc .= '<div class="randomquote">"'.$quotes[rand(0,sizeof($quotes)-1)].'"</div>';
67        return true;
68    }
69}
70