1<?php
2/**
3 * DokuWiki Plugin top (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <gohr@cosmocode.de>
7 */
8
9class syntax_plugin_top extends DokuWiki_Syntax_Plugin {
10    /**
11     * @return string Syntax mode type
12     */
13    public function getType()
14    {
15        return 'protected';
16    }
17
18    /**
19     * @return int Sort order - Low numbers go before high numbers
20     */
21    public function getSort()
22    {
23        return 200;
24    }
25
26    /**
27     * Connect lookup pattern to lexer.
28     *
29     * @param string $mode Parser mode
30     */
31    public function connectTo($mode)
32    {
33        $this->Lexer->addSpecialPattern('\\{\\{top(?:\|.+?)?\\}\\}', $mode, 'plugin_top');
34    }
35
36    /**
37     * Handle matches of the top syntax
38     *
39     * @param string $match The match of the syntax
40     * @param int $state The state of the handler
41     * @param int $pos The position in the document
42     * @param Doku_Handler $handler The handler
43     * @return array Data for the renderer
44     */
45    public function handle($match, $state, $pos, Doku_Handler $handler)
46    {
47        if ($state==DOKU_LEXER_SPECIAL) {
48            $options = array('lang' => null, 'month' => null, 'tag' => 'ul', 'score' => 'false' );
49            $match = rtrim($match,'\}');
50            $match = substr($match,5);
51            if ($match != '') {
52                $match = ltrim($match,'\|');
53                $match = explode(",", $match);
54                foreach ($match as $option) {
55                    list($key, $val) = explode('=', $option);
56                    $options[$key] = $val;
57                }
58            }
59            return array($state, $options);
60        } else {
61            return array($state, '');
62        }
63    }
64
65    /**
66     * Render xhtml output or metadata
67     *
68     * @param string $mode Renderer mode (supported modes: xhtml)
69     * @param Doku_Renderer $renderer The renderer
70     * @param array $data The data from the handler() function
71     * @return bool If rendering was successful.
72     */
73    public function render($mode, Doku_Renderer $renderer, $data)
74    {
75        if ($mode == 'metadata') return false;
76        if ($data[0] != DOKU_LEXER_SPECIAL) return false;
77
78        /** @var helper_plugin_top $hlp */
79        $hlp  = plugin_load('helper', 'top');
80        $list = $hlp->best($data[1]['lang'], $data[1]['month'], 20);
81
82        if ($data[1]['tag'] == 'ol') {
83            $renderer->listo_open();
84        } else {
85            $renderer->listu_open();
86        }
87
88        $num_items=0;
89        foreach ($list as $item) {
90            if (!$hlp->isReadable($item['page'])) continue;
91            $num_items = $num_items +1;
92            $renderer->listitem_open(1);
93            if (strpos($item['page'], ':') === false) {
94                $item['page'] = ':' . $item['page'];
95            }
96            $renderer->internallink($item['page']);
97            if ($data[1]['score'] === 'true') $renderer->cdata(' (' . $item['value'] . ')');
98            $renderer->listitem_close();
99            if ($num_items >= 10) break;
100        }
101
102        if ($data[1]['tag'] == 'ol') {
103            $renderer->listo_close();
104        } else {
105            $renderer->listu_close();
106        }
107        return true;
108    }
109}
110// vim:ts=4:sw=4:et:
111