xref: /plugin/struct/action/output.php (revision 1edf6e46bf5ccbff2cab3b71d69ef4cf5f20a379)
1<?php
2/**
3 * DokuWiki Plugin struct (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12/**
13 * Class action_plugin_struct_output
14 *
15 * This action component handles the automatic output of all schema data that has been assigned
16 * to the current page by appending the appropriate instruction to the handler calls.
17 *
18 * The real output creation is done within the syntax component
19 * @see syntax_plugin_struct_output
20 */
21class action_plugin_struct_output extends DokuWiki_Action_Plugin {
22
23    /**
24     * Registers a callback function for a given event
25     *
26     * @param Doku_Event_Handler $controller DokuWiki's event controller object
27     * @return void
28     */
29    public function register(Doku_Event_Handler $controller) {
30        $controller->register_hook('PARSER_HANDLER_DONE', 'AFTER', $this, 'handle_output');
31    }
32
33    /**
34     * Appends the instruction to render our syntax output component to each page
35     * after the first found headline or the very begining if no headline was found
36     *
37     * @param Doku_Event $event
38     * @param $param
39     */
40    public function handle_output(Doku_Event $event, $param) {
41        global $ID;
42        if(!page_exists($ID)) return;
43        $ins = -1;
44        $pos = 0;
45        foreach($event->data->calls as $num => $call) {
46            // try to find the first header
47            if($call[0] == 'header') {
48                $pos = $call[2];
49                $ins = $num;
50                break;
51            }
52
53            // abort when after we looked at the first 150 bytes
54            if(isset($call[3]) && $call[3] > 150) {
55                break;
56            }
57        }
58
59        // insert our own call after the found position
60        array_splice(
61            $event->data->calls,
62            $ins+1,
63            0,
64            array(
65                array(
66                    'plugin',
67                    array(
68                        'struct_output', array('pos' => $pos), DOKU_LEXER_SPECIAL, ''
69                    ),
70                    $pos
71                )
72            )
73        );
74    }
75
76}
77
78// vim:ts=4:sw=4:et:
79