1<?php
2/**
3 * DokuWiki Action Plugin Medialist
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author  Satoshi Sahara <sahara.satoshi@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class action_plugin_medialist extends DokuWiki_Action_Plugin {
13
14    /**
15     * Register event handlers
16     */
17    public function register(Doku_Event_Handler $controller) {
18        $controller->register_hook(
19            'RENDERER_CONTENT_POSTPROCESS', 'AFTER', $this, 'handle_postprocess', array()
20        );
21    }
22
23
24    /**
25     * handler of renderer output : Post process immediately after renderer completions
26     *
27     * replace medialst placeholders in xhtml of the page
28     */
29    public function handle_postprocess(Doku_Event $event, $param) {
30        global $ACT;
31
32        if ($ACT != 'show') return;
33
34        if ($event->data[0] == 'xhtml') {
35            $pattern = '#<!-- MEDIALIST ([^\r\n]+?) -->#';
36
37            // regular expression search and replace using anonymous function callback
38            $event->data[1] = preg_replace_callback( $pattern,
39                function ($matches) {
40                    $medialist = $this->loadHelper('medialist');
41                    $data = '{{medialist>'.$matches[1].'}}';
42                    $params = $medialist->parse($data);
43                    return $medialist->render_xhtml($params);
44                },
45                $event->data[1]
46            );
47            return true;
48        }
49    }
50
51}
52
53