1<?php
2/**
3 * DokuWiki Plugin filelisting (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Szymon Olewniczak <dokuwiki@cosmocode.de>
7 */
8
9use dokuwiki\File\PageResolver;
10
11class syntax_plugin_filelisting extends DokuWiki_Syntax_Plugin {
12    /**
13     * @return string Syntax mode type
14     */
15    public function getType() {
16        return 'substition';
17    }
18
19    /**
20     * @return string Paragraph type
21     */
22    public function getPType() {
23        return 'block';
24    }
25
26    /**
27     * @return int Sort order - Low numbers go before high numbers
28     */
29    public function getSort() {
30        return 13;
31    }
32
33    /**
34     * Connect lookup pattern to lexer.
35     *
36     * @param string $mode Parser mode
37     */
38    public function connectTo($mode) {
39        $this->Lexer->addSpecialPattern('{{filelisting>?.*?}}',$mode,'plugin_filelisting');
40    }
41
42    /**
43     * Handle matches of the filelisting syntax
44     *
45     * @param string          $match   The match of the syntax
46     * @param int             $state   The state of the handler
47     * @param int             $pos     The position in the document
48     * @param Doku_Handler    $handler The handler
49     * @return array Data for the renderer
50     */
51    public function handle($match, $state, $pos, Doku_Handler $handler){
52        $param = substr($match, strlen('{{filelisting'), -strlen('}}'));
53        //remove '>' from the path
54        $ns = strlen($param) !== 0 ? $ns = substr($param, 1) : '';
55
56        return array($ns);
57    }
58
59    /**
60     * Render xhtml output or metadata
61     *
62     * @param string         $mode      Renderer mode (supported modes: xhtml)
63     * @param Doku_Renderer  $renderer  The renderer
64     * @param array          $data      The data from the handler() function
65     * @return bool If rendering was successful.
66     */
67    public function render($mode, Doku_Renderer $renderer, $data) {
68        global $INFO;
69
70        if ($mode == 'metadata') {
71            //inform the cache that the plugin is used
72            $renderer->meta['filelisting'] = true;
73
74            return true;
75        } elseif ($mode == 'xhtml') {
76
77            $cur_ns = getNS($INFO['id']);
78
79            list($ns) = $data;
80            if (empty($ns)) {
81                $ns = $cur_ns;
82            } else {
83                $resolver = new PageResolver($INFO['id']);
84                $ns = $resolver->resolveId($ns);
85            }
86
87            /** @var helper_plugin_filelisting $hlp */
88            $hlp = plugin_load('helper', 'filelisting');
89
90            $renderer->doc .= $hlp->tpl_filelisting(false, $ns);
91
92            return true;
93        }
94        return false;
95    }
96}
97
98// vim:ts=4:sw=4:et:
99