1<?php
2/**
3 * DokuWiki Syntax Plugin Medialist
4 *
5 * Show a list of media files (images/archives ...) referred in a given page
6 * or stored in a given namespace.
7 *
8 * Syntax:  {{medialist>[id]}}
9 *          {{medialist>[ns]:}} or {{medialist>[ns]:*}}
10 *
11 *   [id] - a valid page id (use @ID@ for the current page)
12 *   [ns] - a namespace (use @NS@: for the current namespace)
13 *
14 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
15 * @author  Michael Klier <chi@chimeric.de>
16 * @author  Satoshi Sahara <sahara.satoshi@gmail.com>
17 */
18// must be run within DokuWiki
19if(!defined('DOKU_INC')) die();
20
21/**
22 * All DokuWiki plugins to extend the parser/rendering mechanism
23 * need to inherit from this class
24 */
25class syntax_plugin_medialist extends DokuWiki_Syntax_Plugin {
26
27    protected $pattern = '{{medialist>[^\r\n]+?}}';
28
29    function getType()  { return 'substition'; }
30    function getPType() { return 'block'; }
31    function getSort()  { return 299; }
32
33    /**
34     * Connect pattern to lexer
35     */
36    function connectTo($mode) {
37        $this->Lexer->addSpecialPattern($this->pattern, $mode, 'plugin_medialist');
38    }
39
40    /**
41     * Handle the match
42     */
43    function handle($match, $state, $pos, Doku_Handler $handler) {
44        return array($state, $match);
45    }
46
47    /**
48     * Create output
49     */
50    function render($format, Doku_Renderer $renderer, $data) {
51        global $ACT;
52
53        list($state, $match) = $data;
54
55        $medialist = $this->loadHelper('medialist');
56        $params = $medialist->parse($match);
57
58        switch ($format) {
59            case 'xhtml':
60                if (in_array($ACT, array('preview'))) {
61                    $renderer->doc .= $medialist->render_xhtml($params);
62                } else {
63                    // output placeholder which will be replaced in action component
64                    $renderer->doc .= '<!-- MEDIALIST '. substr($match, 12, -2) .' -->'.DOKU_LF;
65
66                    // another implementation: reqires disabling xhtml cache of whole page...
67                    //$renderer->info['cache'] = false; // rendered result may not cached
68                    //$renderer->doc .= $medialist->render_xhtml($params);
69                }
70                return true;
71            case 'metadata':
72                return false;
73        }
74        return false;
75    }
76
77}
78