xref: /plugin/struct/action/output.php (revision e30a68df4feadb22514652023b561e4a37745e8e)
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, $ACT;
42        if (act_clean($ACT) !== 'show') {
43            return;
44        }
45        if(!page_exists($ID)) return;
46        $ins = -1;
47        $pos = 0;
48        foreach($event->data->calls as $num => $call) {
49            // try to find the first header
50            if($call[0] == 'header') {
51                $pos = $call[2];
52                $ins = $num;
53                break;
54            }
55
56            // abort when after we looked at the first 150 bytes
57            if($call[3] > 150) {
58                break;
59            }
60        }
61
62        // insert our own call after the found position
63        array_splice(
64            $event->data->calls,
65            $ins+1,
66            0,
67            array(
68                array(
69                    'plugin',
70                    array(
71                        'struct_output', array('pos' => $pos), DOKU_LEXER_SPECIAL, ''
72                    ),
73                    $pos
74                )
75            )
76        );
77    }
78
79}
80
81// vim:ts=4:sw=4:et:
82