xref: /plugin/struct/action/output.php (revision 28b818ce3a66c4ba844635bd790b3cf768a357d8)
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    /**
35     * Appends the instruction to render our syntax output component to each page
36     * after the first found headline or the very begining if no headline was found
37     *
38     * @param Doku_Event $event
39     * @param $param
40     */
41    public function handle_output(Doku_Event &$event, $param) {
42        global $ACT;
43        global $ID;
44        // blank $ACT happens when instructions are rendered in indexer
45        if(!blank($ACT) && $ACT != 'show') return; //FIXME what about export_*?
46        if(!page_exists($ID)) return;
47
48        $ins = -1;
49        $pos = 0;
50        foreach($event->data->calls as $num => $call) {
51            // try to find the first header
52            if($call[0] == 'header') {
53                $pos = $call[2];
54                $ins = $num;
55                break;
56            }
57
58            // abort when after we looked at the first 150 bytes
59            if($call[3] > 150) {
60                break;
61            }
62        }
63
64        // insert our own call after the found position
65        array_splice(
66            $event->data->calls,
67            $ins+1,
68            0,
69            array(
70                array(
71                    'plugin',
72                    array(
73                        'struct_output', array('pos' => $pos), DOKU_LEXER_SPECIAL, ''
74                    ),
75                    $pos
76                )
77            )
78        );
79    }
80
81}
82
83// vim:ts=4:sw=4:et:
84