1<?php
2
3
4use ComboStrap\Call;
5use ComboStrap\CallStack;
6
7class action_plugin_combo_blockpostprocessing extends DokuWiki_Action_Plugin
8{
9
10
11    public function register(\Doku_Event_Handler $controller)
12    {
13        /**
14         * Found in {@link Doku_Handler::finalize()}
15         *
16         * Doc: https://www.dokuwiki.org/devel:event:parser_handler_done
17         */
18        $controller->register_hook(
19            'PARSER_HANDLER_DONE',
20            'AFTER',
21            $this,
22            '_post_process_block',
23            array()
24        );
25
26    }
27
28
29    /**
30     * Transform the special heading atx call
31     * in an enter and exit heading atx calls
32     *
33     * Code extracted and adapted from the end of {@link Doku_Handler::header()}
34     *
35     * @param   $event Doku_Event
36     */
37    function _post_process_block(&$event, $param)
38    {
39        /**
40         * @var Doku_Handler $handler
41         */
42        $handler = $event->data;
43        $callStack = CallStack::createFromHandler($handler);
44        $callStack->moveToStart();
45
46
47        while ($actualCall = $callStack->next()) {
48            if ($actualCall->isDisplaySet()) {
49                if ($actualCall->getDisplay() === Call::BlOCK_DISPLAY) {
50
51
52                    /**
53                     * Previous call control
54                     */
55                    $previous = $callStack->previous();
56                    if ($previous->isUnMatchedEmptyCall()) {
57                        /**
58                         * An empty unmatched call will create a block
59                         * Delete
60                         */
61                        $callStack->deleteActualCallAndPrevious();
62                        $previous = $callStack->getActualCall();
63                    }
64                    if ($previous->getTagName() === "p" && $previous->getState() === DOKU_LEXER_ENTER) {
65                        $callStack->deleteActualCallAndPrevious();
66                    }
67
68                    /**
69                     * Go back on the actual call
70                     */
71                    $callStack->next();
72
73                    /**
74                     * Next call Control
75                     */
76                    $next = $callStack->next();
77                    if ($next->getTagName() === "p" && $next->getState() === DOKU_LEXER_EXIT) {
78                        $callStack->deleteActualCallAndPrevious();
79                    }
80                }
81            }
82        }
83
84
85    }
86
87
88}
89