xref: /plugin/struct/action/output.php (revision 0e4a3e7c4c66fd58fc1bbd12af349ac28c468460)
182c064c1SAndreas Gohr<?php
282c064c1SAndreas Gohr/**
382c064c1SAndreas Gohr * DokuWiki Plugin struct (Action Component)
482c064c1SAndreas Gohr *
582c064c1SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
682c064c1SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
782c064c1SAndreas Gohr */
882c064c1SAndreas Gohr
982c064c1SAndreas Gohr// must be run within Dokuwiki
1082c064c1SAndreas Gohrif(!defined('DOKU_INC')) die();
1182c064c1SAndreas Gohr
1282c064c1SAndreas Gohr/**
1382c064c1SAndreas Gohr * Class action_plugin_struct_output
1482c064c1SAndreas Gohr *
1582c064c1SAndreas Gohr * This action component handles the automatic output of all schema data that has been assigned
1682c064c1SAndreas Gohr * to the current page by appending the appropriate instruction to the handler calls.
1782c064c1SAndreas Gohr *
1882c064c1SAndreas Gohr * The real output creation is done within the syntax component
1982c064c1SAndreas Gohr * @see syntax_plugin_struct_output
2082c064c1SAndreas Gohr */
2182c064c1SAndreas Gohrclass action_plugin_struct_output extends DokuWiki_Action_Plugin {
2282c064c1SAndreas Gohr
2382c064c1SAndreas Gohr    /**
2482c064c1SAndreas Gohr     * Registers a callback function for a given event
2582c064c1SAndreas Gohr     *
2682c064c1SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
2782c064c1SAndreas Gohr     * @return void
2882c064c1SAndreas Gohr     */
2982c064c1SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
3082c064c1SAndreas Gohr        $controller->register_hook('PARSER_HANDLER_DONE', 'AFTER', $this, 'handle_output');
3182c064c1SAndreas Gohr    }
3282c064c1SAndreas Gohr
3382c064c1SAndreas Gohr    /**
3482c064c1SAndreas Gohr     * Appends the instruction to render our syntax output component to each page
3528b818ceSAndreas Gohr     * after the first found headline or the very begining if no headline was found
3682c064c1SAndreas Gohr     *
3782c064c1SAndreas Gohr     * @param Doku_Event $event
3882c064c1SAndreas Gohr     * @param $param
3982c064c1SAndreas Gohr     */
40881e940cSAndreas Gohr    public function handle_output(Doku_Event $event, $param) {
41*0e4a3e7cSMichael Große        global $ID;
4202481400SAndreas Gohr        if(!page_exists($ID)) return;
4328b818ceSAndreas Gohr        $ins = -1;
4428b818ceSAndreas Gohr        $pos = 0;
4528b818ceSAndreas Gohr        foreach($event->data->calls as $num => $call) {
4628b818ceSAndreas Gohr            // try to find the first header
4728b818ceSAndreas Gohr            if($call[0] == 'header') {
4828b818ceSAndreas Gohr                $pos = $call[2];
4928b818ceSAndreas Gohr                $ins = $num;
5028b818ceSAndreas Gohr                break;
5128b818ceSAndreas Gohr            }
5282c064c1SAndreas Gohr
5328b818ceSAndreas Gohr            // abort when after we looked at the first 150 bytes
5428b818ceSAndreas Gohr            if($call[3] > 150) {
5528b818ceSAndreas Gohr                break;
5628b818ceSAndreas Gohr            }
5728b818ceSAndreas Gohr        }
5828b818ceSAndreas Gohr
5928b818ceSAndreas Gohr        // insert our own call after the found position
6028b818ceSAndreas Gohr        array_splice(
6128b818ceSAndreas Gohr            $event->data->calls,
6228b818ceSAndreas Gohr            $ins+1,
6328b818ceSAndreas Gohr            0,
6428b818ceSAndreas Gohr            array(
6528b818ceSAndreas Gohr                array(
6682c064c1SAndreas Gohr                    'plugin',
6782c064c1SAndreas Gohr                    array(
685a1eab78SAndreas Gohr                        'struct_output', array('pos' => $pos), DOKU_LEXER_SPECIAL, ''
6982c064c1SAndreas Gohr                    ),
7082c064c1SAndreas Gohr                    $pos
7128b818ceSAndreas Gohr                )
7228b818ceSAndreas Gohr            )
7382c064c1SAndreas Gohr        );
7482c064c1SAndreas Gohr    }
7582c064c1SAndreas Gohr
7682c064c1SAndreas Gohr}
7782c064c1SAndreas Gohr
7882c064c1SAndreas Gohr// vim:ts=4:sw=4:et:
79