1<?php
2
3require_once(__DIR__ . '/../vendor/autoload.php');
4
5use ComboStrap\CallStack;
6use ComboStrap\ExceptionNotFound;
7use ComboStrap\ExecutionContext;
8use ComboStrap\HeadingTag;
9use ComboStrap\MarkupPath;
10use ComboStrap\Outline;
11use ComboStrap\WikiPath;
12
13class action_plugin_combo_instructionspostprocessing extends DokuWiki_Action_Plugin
14{
15
16
17    /**
18     * This section are not HTML
19     * section, they are edit section
20     * that delimits the edit area
21     */
22    const EDIT_SECTION_OPEN = 'section_open';
23    const EDIT_SECTION_CLOSE = 'section_close';
24    const HEADING_TAGS = [
25        HeadingTag::HEADING_TAG,
26        syntax_plugin_combo_headingatx::TAG,
27        syntax_plugin_combo_headingwiki::TAG
28    ];
29
30    const CANONICAL = Outline::CANONICAL;
31
32
33    public
34    function register(\Doku_Event_Handler $controller)
35    {
36        /**
37         * Found in {@link Doku_Handler::finalize()}
38         *
39         * Doc: https://www.dokuwiki.org/devel:event:parser_handler_done
40         */
41        $controller->register_hook(
42            'PARSER_HANDLER_DONE',
43            'AFTER',
44            $this,
45            '_post_processing',
46            array()
47        );
48
49    }
50
51
52    /**
53     * Transform the special heading atx call
54     * in an enter and exit heading atx calls
55     *
56     * Add the section close / open
57     *
58     * Build the toc
59     * And create the main are
60     *
61     * Code extracted and adapted from the end of {@link Doku_Handler::header()}
62     *
63     * @param   $event Doku_Event
64     */
65    function _post_processing(&$event, $param)
66    {
67
68        /**
69         * @var Doku_Handler $handler
70         */
71        $handler = $event->data;
72
73        $executionContext = ExecutionContext::getActualOrCreateFromEnv();
74
75        try {
76            $fetcherMarkup = $executionContext->getExecutingMarkupHandler();
77            $isFragment = $fetcherMarkup->isFragment() === true;
78            try {
79                $executingPath = $fetcherMarkup->getRequestedExecutingPath();
80            } catch (ExceptionNotFound $e) {
81                $executingPath = null;
82            }
83        } catch (ExceptionNotFound $e) {
84
85            /**
86             * Not on admin pages
87             */
88            $action = $executionContext->getExecutingAction();
89            if($action===ExecutionContext::ADMIN_ACTION){
90                return;
91            }
92
93            /**
94             * What fucked up is fucked up !
95             * {@link pageinfo()} in common may starts before the {@link action_plugin_combo_docustom handler } is called
96             * {@link action_plugin_combo_docustom}
97             */
98            $requestedPath = $executionContext->getRequestedPath();
99            $executingPath = null;
100            $isFragment = true;
101            try {
102                $executingId = $executionContext->getExecutingWikiId();
103
104                /**
105                 * In preview mode, this is always a `fragment run`
106                 * * otherwise we get warning on the outline because the heading should start with heading 1 or 2, not 3
107                 * * and this is used in {@link \ComboStrap\Parser::parseMarkupToHandler()}
108                 */
109                if ($executionContext->getExecutingAction() !== ExecutionContext::PREVIEW_ACTION) {
110
111                    $isSlot = MarkupPath::createPageFromPathObject($requestedPath)->isSlot();
112                    if ($isSlot === false) {
113                        if ($executingId === $requestedPath->getWikiId()) {
114                            $isFragment = false;
115                        }
116                    }
117
118                }
119                $executingPath = WikiPath::createMarkupPathFromId($executingId);
120            } catch (ExceptionNotFound $e) {
121                //
122            }
123        }
124
125        /**
126         * Fragment execution
127         */
128        if ($isFragment) {
129            $callStack = CallStack::createFromHandler($handler);
130            // no outline or edit button for dynamic rendering
131            // but closing of atx heading
132            $handler->calls = Outline::createFromCallStack($callStack, null, true)
133                ->toFragmentInstructionCalls();
134            return;
135        }
136
137        /**
138         * Document execution
139         * (add outline section, ...)
140         */
141        $callStack = CallStack::createFromHandler($handler);
142        if ($executingPath !== null) {
143            $executingMarkupPath = MarkupPath::createPageFromPathObject($executingPath);
144        } else {
145            $executingMarkupPath = null;
146        }
147        $outline = Outline::createFromCallStack($callStack, $executingMarkupPath, $isFragment);
148        $handler->calls = $outline->toHtmlSectionOutlineCalls();
149        /**
150         * No more supported
151         * $handler->calls = $outline->toDokuWikiTemplateInstructionCalls();
152         */
153
154    }
155
156}
157