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