xref: /plugin/siteexport/syntax/toc.php (revision cfed89d7d665a80dcab67a661678dd1103345eed)
1<?php
2/**
3 * Search with Scopes
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
13
14require_once(DOKU_PLUGIN . 'syntax.php');
15
16class syntax_plugin_siteexport_toc extends DokuWiki_Syntax_Plugin {
17
18    private $insideToc = false;
19    private $savedToc = array();
20    private $options = array();
21
22    private $mergedPages = array();
23    private $includedPages = array();
24    private $merghintIds = array();
25
26    public function getType() { return 'protected'; }
27    public function getPType() { return 'block'; }
28    public function getAllowedTypes() { return array('container'); }
29    public function getSort() { return 100; }
30
31    /**
32     * Connect pattern to lexer
33     */
34    public function connectTo($mode) {
35        $this->Lexer->addEntryPattern('<toc>(?=.*?</toc>)', $mode, 'plugin_siteexport_toc');
36        $this->Lexer->addEntryPattern('<toc .+?>(?=.*?</toc>)', $mode, 'plugin_siteexport_toc');
37        $this->Lexer->addSpecialPattern("\[\[.+?\]\]", $mode, 'plugin_siteexport_toc');
38    }
39
40    public function postConnect() {
41        $this->Lexer->addExitPattern('</toc.*?>', 'plugin_siteexport_toc');
42    }
43
44    public function handle($match, $state, $pos, Doku_Handler $handler) {
45        global $ID, $INFO;
46
47        switch ($state) {
48            case DOKU_LEXER_ENTER:
49
50                $this->insideToc = true;
51                $this->options = explode(' ', substr($match, 5, -1)?:"");
52                return array('start' => true, 'pos' => $pos, 'options' => $this->options);
53
54            case DOKU_LEXER_SPECIAL:
55
56                if ($this->insideToc) {
57
58                    $link = preg_replace(array('/^\[\[/', '/\]\]$/u'), '', $match);
59                    // Split title from URL
60                    $link = explode('|', $link, 2);
61                    if (!isset($link[1])) {
62                        $link[1] = NULL;
63                    } else if (preg_match('/^\{\{[^\}]+\}\}$/', $link[1])) {
64                        // If the title is an image, convert it to an array containing the image details
65                        $link[1] = Doku_Handler_Parse_Media($link[1]);
66                    }
67                    $link[0] = trim($link[0]);
68
69                    if (!(preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u', $link[0]) ||
70                    preg_match('/^\\\\\\\\[\w.:?\-;,]+?\\\\/u', $link[0]) ||
71                    preg_match('#^([a-z0-9\-\.+]+?)://#i', $link[0]) ||
72                    preg_match('<' . PREG_PATTERN_VALID_EMAIL . '>', $link[0]) ||
73                    preg_match('!^#.+!', $link[0]))
74                    ) {
75
76                        // Get current depth from call stack
77                        $depth = 1;
78                        if ($handler->CallWriter instanceof Doku_Handler_List) {
79
80                            $calls = array_reverse($handler->CallWriter->calls);
81                            $call = $calls[0];
82                            foreach ($calls as $item) {
83                                if (in_array($item[0], array('list_item', 'list_open'))) { $call = $item; break; }
84                            }
85
86                            $listType = null;
87                            $depth = $handler->CallWriter->interpretSyntax($call[1][0], $listType)-1; // Minus one because of plus one inside the interpret function
88                        }
89
90                        if (empty($link[0])) { break; } // No empty elements. This would lead to problems
91                        return array($link[0], $link[1], $depth);
92                    } else {
93                        // use parser! - but with another p
94                        $handler->internallink($match, $state, $pos);
95                    }
96                } else {
97                    // use parser!
98                    $handler->internallink($match, $state, $pos);
99                }
100
101                return false;
102            case DOKU_LEXER_UNMATCHED:
103
104                $handler->_addCall('cdata', array($match), $pos);
105
106                return false;
107            case DOKU_LEXER_EXIT:
108
109                $this->insideToc = false;
110                return 'save__meta';
111        }
112        return false;
113    }
114
115    public function render($mode, Doku_Renderer $renderer, $data) {
116        global $ID, $lang, $INFO;
117
118        list($SID, $NAME, $DEPTH) = $data;
119
120        resolve_pageid(getNS($ID), $SID, $exists = null);
121//        $SID = cleanID($SID); // hier kein cleanID, da sonst moeglicherweise der anker verloren geht
122
123        //    Render XHTML and ODT
124        if ($mode == 'xhtml' || $mode == 'odt') {
125
126            // TOC Title
127            if (is_array($data) && $data['start'] == true) {
128
129                if (is_Array($data['options'])) {
130                    foreach ($data['options'] as $opt) {
131                        switch ($opt) {
132                            case 'description' : $renderer->meta['sitetoc']['showDescription'] = true; break;
133                            case 'notoc' : $renderer->meta['sitetoc']['noTOC'] = true; break;
134                            case 'merge' : $renderer->meta['sitetoc']['mergeDoc'] = true; break;
135                            case 'nohead' : $renderer->meta['sitetoc']['noTocHeader'] = true; break;
136                            case 'mergeheader' : $renderer->meta['sitetoc']['mergeHeader'] = true; break;
137                            case 'pagebreak' : $renderer->meta['sitetoc']['pagebreak'] = true; break;
138                            case 'mergehint' : $renderer->meta['sitetoc']['mergehint'] = true; break;
139                        }
140                    }
141                }
142
143                $renderer->section_open("1 sitetoc");
144                if ($renderer->meta['sitetoc']['noTocHeader'] === false) {
145                    $renderer->header($lang['toc'], 1, $data['pos']);
146                }
147
148                return true;
149            } else
150
151            // All Output has been done
152            if (!is_array($data) && $data == 'save__meta') {
153
154                // Close TOC
155                $renderer->section_close();
156
157                if ($renderer->meta['sitetoc']['noTOC'] === true) {
158                    $renderer->doc = preg_replace("/<div.*?sitetoc.*?$/si", "", $renderer->doc);
159                }
160
161                // If this is not set, we may have it as Metadata
162                if (empty($this->mergedPages) && $renderer->meta['sitetoc']['mergeDoc']) {
163                    $toc = $renderer->meta['sitetoc']['siteexportTOC'];
164
165                    if (is_array($toc)) {
166                        foreach ($toc as $tocItem) {
167                            $this->mergedPages[] = array($tocItem['id'], $tocItem['depth']);
168                        }
169                    }
170
171                }
172
173                // If there is some data to be merged
174                if (count($this->mergedPages) > 0) {
175
176                    $renderer->doc = ''; // Start fresh!
177
178                    $renderer->section_open("1 mergedsite" . ($renderer->meta['sitetoc']['mergehint'] && count($this->mergedPages) > 1 ? ' mergehint' : ''));
179
180                    // Prepare lookup Array
181                    foreach ($this->mergedPages as $tocItem) {
182                        list($this->includedPages[]) = explode('#', $tocItem[0]);
183                    }
184
185                    // Load the instructions
186                    $instr = array();
187                    foreach ($this->mergedPages as $tocElement) {
188
189                        list($tocItem, $depth) = $tocElement;
190                        $file = wikiFN($tocItem);
191
192                        if (@file_exists($file)) {
193                            $instructions = p_cached_instructions($file, false, $tocItem);
194                        } else {
195                            $instructions = p_get_instructions(io_readWikiPage($file, $tocItem));
196                        }
197
198                        // Convert Link and header instructions
199                        $instructions = $this->_convertInstructions($instructions, $addID = null, $renderer, $depth);
200
201                        if ($renderer->meta['sitetoc']['mergeHeader'] && count($this->mergedPages) > 1) {
202                            // get a hint for merged pages
203                            if ($renderer->meta['sitetoc']['mergehint']) {
204                                // only if the first section is already there
205                                $mergeHint = p_get_metadata($tocItem, 'mergehint', METADATA_RENDER_USING_SIMPLE_CACHE);
206                                if (empty($mergeHint)) { $mergeHint = p_get_metadata($tocItem, 'thema', METADATA_RENDER_USING_SIMPLE_CACHE); }
207                                if (empty($mergeHint)) { $mergeHint = tpl_pagetitle($tocItem, true); }
208                                $instructions = $this->_mergeWithHeaders($this->_initialHeaderStructure($instructions), $instructions, 1, $mergeHint);
209                            }
210                            // Merge
211                            $instr = $this->_mergeWithHeaders($instr, $instructions, 1);
212
213                        } else
214                        if ($renderer->meta['sitetoc']['pagebreak']) {
215                            $sitepagebreak = array(array(
216                                'plugin',
217                                array(
218                                    'siteexport_toctools',
219                                    array(
220                                        'pagebreak',
221                                        null,
222                                        null
223                                    )
224                                )
225                            ));
226                            $instr = array_merge($instr, $instructions, $sitepagebreak);
227                        } else {
228                            // Concat
229                            $instr = array_merge($instr, $instructions);
230                        }
231                    }
232
233                    if (!empty($instr)) {
234                        if ( $this->_cleanAllInstructions($instr, true) ) {
235                            // There are no toc elements, remove the mergesite mergehint
236                            $renderer->doc = preg_replace( '/(class=".*?\s)mergedsite/', '\1', $renderer->doc );
237                            $renderer->doc = preg_replace( '/(class=".*?\s)mergehint/', '\1', $renderer->doc );
238                        }
239
240                        // print "<pre>"; print_r($instr); print "</pre>";
241                        $this->_render_output($renderer, $mode, $instr);
242                    }
243
244                    $renderer->section_close();
245                }
246                return true;
247            }
248
249            // Save the current ID
250            $LNID = $SID;
251
252            // Add ID to flags['mergeDoc']
253            if ($renderer->meta['sitetoc']['mergeDoc'] === true) { // || (count($renderer->meta['sitetoc']['siteexportTOC']) > 0 && $renderer->meta['sitetoc']['siteexportMergeDoc'] === true) ) {
254                $this->mergedPages[] = array($SID, $DEPTH);
255                resolve_pageid(getNS($ID), $SID, $exists);
256            } else {
257                // // print normal internal link (XHTML odt)
258                $renderer->internallink($LNID, $NAME, null);
259
260                // Display Description underneath
261                if ($renderer->meta['sitetoc']['showDescription'] === true) {
262                    $renderer->cdata(p_get_metadata($SID, 'description abstract', true));
263                }
264            }
265
266            // Render Metadata
267        } else if ($mode == 'metadata') {
268            if (!is_array($data) && $data == 'save__meta') {
269                $renderer->meta['sitetoc']['siteexportTOC'] = $this->savedToc;
270
271                foreach ($this->savedToc as $page) {
272                    $renderer->meta['relation']['references'][$page['id']] = $page['exists'];
273                }
274
275                $this->savedToc = array();
276            } else if (!isset($data['start']) && !isset($data['pos'])) {
277                $this->savedToc[] = $this->__addTocItem($SID, $NAME, $DEPTH, $renderer);
278            }
279        } else {
280            return false;
281        }
282
283        return true;
284    }
285
286    /*
287     * pull apart the ID and create an Entry for the TOC
288     */
289    private function __addTocItem($id, $name, $depth, $renderer) {
290        global $conf;
291        global $ID;
292
293        // Render Title
294        $default = $renderer->_simpleTitle($id);
295        $exists = false; $isImage = false; $linktype = null;
296        resolve_pageid(getNS($ID), $id, $exists);
297        $name = $renderer->_getLinkTitle($name, $default, $isImage, $id, $linktype);
298
299        //keep hash anchor
300        list($id, $hash) = explode('#', $id, 2);
301        if (!empty($hash)) $hash = $renderer->_headerToLink($hash);
302
303        // Build Sitetoc Item
304        $item = array();
305        $item['id'] = $id;
306        $item['name'] = $name;
307        $item['anchor'] = $hash;
308        $item['depth'] = $depth;
309        $item['exists'] = $exists;
310        if (!$conf['skipacl'] && auth_quickaclcheck($item['id']) < AUTH_READ) {
311            return false;
312        }
313
314        return $item;
315    }
316
317    /*
318     * Render the output of one page
319     */
320    private function _render_output($renderer, $mode, $instr) {
321        global $ID;
322
323        // Section IDs
324        // $addID = sectionID($addID, $check);    //not possible to use a:b:c for id
325
326        if ($mode == 'xhtml') {
327
328            //--------RENDER
329            //renderer information(TOC build / Cache used)
330            $info = array();
331            $content = p_render($mode, $instr, $info);
332
333            //Remove TOC`s, section edit buttons and tags
334            $content = $this->_cleanXHTML($content);
335
336            // embed the included page
337            // $renderer->doc .= '<div class="include">';
338            //add an anchor to find start of a inserted page
339            // $renderer->doc .= "<a name='$addID' id='$addID'>";
340            $renderer->doc .= $content;
341            // $renderer->doc .= '</div>';
342        } else if ($mode == 'odt') {
343
344            // Loop through the instructions
345            foreach ($instr as $instruction) {
346                // Execute the callback against the Renderer
347                call_user_func_array(array($renderer, $instruction[0]), $instruction[1]);
348            }
349        }
350    }
351
352    /*
353     * Corrects relative internal links and media and
354     * converts headers of included pages to subheaders of the current page
355     */
356    private function _convertInstructions($instr, $id, &$renderer, $depth = 1) {
357        global $ID;
358        global $conf;
359
360        $n = count($instr);
361
362        for ($i = 0; $i < $n; $i++) {
363            //internal links(links inside this wiki) an relative links
364            if ((substr($instr[$i][0], 0, 12) == 'internallink')) {
365                $this->_convert_link($renderer, $instr[$i], $id);
366            }
367            else if ((substr($instr[$i][0], 0, 13) == 'internalmedia')) {
368                $this->_convert_media($renderer, $instr[$i], $id);
369            }
370            else if ((substr($instr[$i][0], 0, 6) == 'header')) {
371                $this->_convert_header($renderer, $instr[$i], $depth-1); // -1 because the depth starts at 1
372            }
373            else if ((substr($instr[$i][0], 0, 12) == 'section_open')) {
374                $this->_convert_section($renderer, $instr[$i], $depth-1); // -1 because the depth starts at 1
375            }
376        }
377
378        //if its the document start, cut off the first element(document information)
379        if ($instr[0][0] == 'document_start')
380        return array_slice($instr, 1, -1);
381        else
382        return $instr;
383    }
384
385    /*
386     * Convert link of given instruction
387     */
388    private function _convert_link(&$renderer, &$instr, $id) {
389        global $ID;
390
391        $exists = false;
392
393        resolve_pageid(getNS($id), $instr[1][0], $exists);
394        list($pageID, $pageReference) = explode("#", $instr[1][0], 2);
395
396        if (in_array($pageID, $this->includedPages)) {
397            // Crate new internal Links
398            $check = null;
399
400            // Either get existing reference or create from first heading. If still not there take the alternate ID
401            $pageNameLink = empty($pageReference) ? sectionID($pageID, $check) : $pageReference;
402
403            $instr[1][0] = $ID . "#" . $pageNameLink;
404
405        } else {
406            // Convert external Links to plain Text
407
408            $instr = array(
409                        "cdata",
410            array($instr[1][1]),
411            $instr[2]
412            );
413        }
414    }
415
416    /*
417     * Convert internalmedia of given instruction
418     */
419    private function _convert_media(&$renderer, &$instr, $id) {
420        global $ID;
421
422        // Resolvemedia returns the absolute path to media by reference
423        $exists = false;
424        resolve_mediaid(getNS($id), $instr[1][0], $exists);
425    }
426
427    /**
428     * @param integer $depth
429     */
430    private function _convert_header(&$renderer, &$instr, $depth) {
431        // More Depth!
432        $instr[1][1] += $depth;
433    }
434
435    /**
436     * @param integer $depth
437     */
438    private function _convert_section(&$renderer, &$instr, $depth) {
439        // More Depth!
440        $instr[1][0] += $depth;
441    }
442
443    private function _mergeWithHeaders($existing, $newInstructions, $level = 1, $mergeHint = array()) {
444
445        $returnInstructions = array();
446        $preparedInstructions = array();
447        $existingStart = $existingEnd = 0;
448        $firstRun = true;
449
450        while ($this->_findNextHeaderSection($existing, $level, $existingStart, $existingEnd)) {
451
452            if ($firstRun) {
453                $returnInstructions = array_merge($returnInstructions, array_slice($existing, 0, $existingStart));
454                $firstRun = false;
455            }
456
457            $currentSlice = array_slice($existing, $existingStart, $existingEnd-$existingStart);
458
459            // Find matching part with headername
460            $newStart = $newEnd = 0;
461            if ($this->_findNextHeaderSection($newInstructions, $level, $newStart, $newEnd, $currentSlice[0][1][0])) {
462
463                $newSlice = array_slice($newInstructions, $newStart, $newEnd-$newStart);
464                if ($newSlice[0][0] == 'header')
465                    array_shift($newSlice); // Remove Heading
466
467                // merge found parts on next level.
468                $returnedInstructions = $this->_mergeWithHeaders($currentSlice, $newSlice, $level+1, $mergeHint);
469
470                // Put them at the end!
471                $preparedInstructions = array_merge($preparedInstructions, $returnedInstructions);
472
473                // Remove from input
474                array_splice($newInstructions, $newStart, $newEnd-$newStart);
475            } else {
476                // Nothing else found
477                $preparedInstructions = array_merge($preparedInstructions, $currentSlice);
478            }
479
480            $existingStart = $existingEnd;
481        }
482
483        // Append the rest
484        $returnInstructions = array_merge($returnInstructions, array_slice($existing, $existingStart));
485
486        // Check for section close inconsistencies and put one at the very end ...
487        $section_postpend = array();
488        if (
489            (
490                ($tmp1 = array_slice($newInstructions, -1))
491                && ($tmp1[0][0] == 'section_close')
492            )
493            &&
494            (
495                ($tmp2 = array_slice($newInstructions, -2))
496                && ($tmp2[0][0] == 'section_close')
497            )
498        ) {
499            $section_postpend = array_splice($newInstructions, -1);
500        }
501        if (
502            (
503                ($tmp3 = array_slice($returnInstructions, -1))
504                && ($tmp3[0][0] == 'section_close')
505            )
506            &&
507            (
508                ($tmp4 = array_slice($returnInstructions, -2))
509                && ($tmp4[0][0] == 'section_close')
510            )
511        ) {
512            $section_postpend = array_merge($section_postpend, array_splice($returnInstructions, -1));
513        }
514
515        // What if there are headings left inside the $newInstructions?????
516        // Find matching part with headername
517        $newStart = $newEnd = 0;
518        $section_prepend = array();
519        if ($this->_findNextHeaderSection($newInstructions, $level, $newStart, $newEnd)) {
520            // If there are header in here, build a prepend and have the rest at the end
521            $section_prepend = array_splice($newInstructions, 0, $newStart);
522        } else {
523            // If not, prepend all of it.
524            $section_prepend = $newInstructions;
525            $newInstructions = array();
526        }
527
528        $this->_insertMergeHint($section_prepend, $mergeHint);
529
530        $returnInstructions = array_merge($returnInstructions, $section_prepend, $preparedInstructions, $newInstructions, $section_postpend);
531
532        return $returnInstructions;
533    }
534
535    /**
536     * @param integer $level
537     */
538    private function _findNextHeaderSection($section, $level, &$start, &$end, $headerName = null) {
539
540        $inCount = count($section);
541        $currentSlice = -1;
542
543        // Find Level 1 Header that matches.
544        for ($i = $start; $i < $inCount; $i++) {
545
546            $instruction = $section[$i];
547            $end = $i; // Or it will be lost and a section close will be missing.
548
549            // First Level Header
550            if ($instruction[0] == 'header' && $instruction[1][1] == $level) {
551
552                if ($currentSlice > 0) {
553                    return true;
554                }
555
556                if ($headerName == null || ($headerName == $instruction[1][0])) {
557                    // Begin of new slice ...
558                    $start = $currentSlice = $i;
559                }
560            }
561        }
562
563        // Nothing found
564        $end = $i; // Or it will be lost and a section close will be missing.
565        return $currentSlice > 0;
566    }
567
568    private function _cleanAllInstructions(&$instr, $advanced=false) {
569        $this->_cleanInstructions($instr, '/p_(close|open)/');
570        $this->_cleanInstructions($instr, '/section_(close|open)/');
571        $this->_cleanInstructions($instr, '/listu_(close|open)/');
572        $this->_cleanInstructions($instr, '/listo_(close|open)/');
573
574        if ( !$advanced ) {
575            return false;
576        }
577
578        $currentMergeHint = null;
579        $listOfMergeHintNames= [];
580        for( $i=0; $i<count($instr); $i++ ) {
581
582            $hasMoreEntries = count($instr)-1 > $i;
583
584            if ( $instr[$i][0] == 'header' ) {
585                // reset after header
586                $currentMergeHint = null;
587            }
588
589            if ( $instr[$i][1][0] == 'siteexport_toctools' && $instr[$i][1][0][0] != 'pagebreak' ) {
590                if ( $currentMergeHint != null && $instr[$i][1][1][2] == $currentMergeHint[1][1][2] ) {
591                    // print "<p>Removing Mergehint in between  </p>";
592                    array_splice($instr, $i--, 1);
593                } else {
594                    // print "<p>Resetting Mergehint '" . $instr[$i][1][1][2] . "' == '" . $currentMergeHint[1][1][2] . "'</p>";
595                    $currentMergeHint = $instr[$i];
596                    $listOfMergeHintNames[] = $instr[$i][1][1][2];
597                }
598            }
599        }
600
601        // There is only ONE distinct mergehint -> remove all
602        $listOfMergeHintNames = array_unique($listOfMergeHintNames);
603        if ( count($listOfMergeHintNames) == 1 ) {
604            for( $i=0; $i<count($instr); $i++ ) {
605                if ( $instr[$i][1][0] == 'siteexport_toctools' && $instr[$i][1][0][0] != 'pagebreak' ) {
606                    array_splice($instr, $i--, 1);
607                }
608            }
609        }
610
611        return count($listOfMergeHintNames) == 1;
612    }
613
614    /**
615     * @param string $tag
616     */
617    private function _cleanInstructions(&$instructions, $tag) {
618
619
620/*
621        print "<pre>";
622        print "$tag ->\n";
623        print_r($instructions);
624        print "</pre>";
625//*/
626        $inCount = count($instructions);
627        for ($i = 0; $i < $inCount; $i++) {
628
629            // Last instruction
630            if ($i == $inCount-1) {
631                break;
632            }
633
634            if (preg_match($tag, $instructions[$i][0]) && preg_match($tag, $instructions[$i+1][0]) && $instructions[$i][0] != $instructions[$i+1][0]) {
635/*
636        print "<pre>";
637        print "Removed ->\n";
638        print_r($instructions[$i-1]);
639        print "---\n";
640        print_r($instructions[$i]);
641        print_r($instructions[$i+1]);
642        print "---\n";
643        print_r($instructions[$i+2]);
644        print "</pre>";
645//*/
646
647                // found different tags, but both match the expression and follow each other - so they can be elliminated
648                array_splice($instructions, $i, 2);
649                $inCount -= 2;
650                $i--;
651            }
652        }
653/*
654        print "<pre>";
655        print "$tag ->\n";
656        print_r($instructions);
657        print "</pre>";
658//*/
659    }
660
661    /**
662     * Strip everything except for the headers
663     */
664    private function _initialHeaderStructure($instructions) {
665        $inCount = count($instructions);
666        for ($i = 0; $i < $inCount; $i++) {
667
668            // Last instruction
669            if ($i == $inCount-1) {
670                break;
671            }
672
673            if (!in_array($instructions[$i][0], array('header', 'section_open', 'section_close', 'p_open', 'p_close'))) {
674                // found non-matching
675                array_splice($instructions, $i, 1);
676                $inCount--;
677                $i--;
678            }
679        }
680        return $instructions;
681    }
682
683    private function _insertMergeHint(&$instructions, $mergeHint) {
684
685        // Surround new slice with a mergehint
686        if (empty($mergeHint)) { return; }
687
688        // No emtpy insruction sets.
689        $this->_cleanAllInstructions($instructions);
690
691        if (empty($instructions)) { return; }
692
693        $mergeHintPrepend = $this->_toctoolPrepends( $instructions );
694
695        // only section content should be surrounded.
696        if ($instructions[0][0] != 'section_open') { return; }
697
698        // save for later use
699        $mergeHints = array();
700        $mergeHintId = sectionid($mergeHint, $mergeHints);
701        $this->merghintIds[$mergeHintId] = $mergeHint;
702
703        // Insert section information
704        array_push( $mergeHintPrepend, array(
705            'plugin',
706            array(
707                'siteexport_toctools',
708                array(
709                    'mergehint',
710                    'start',
711                    $mergeHint,
712                    $mergeHintId
713                )
714            )
715        ) );
716
717        $mergeHintPostpend = array(array(
718            'plugin',
719            array(
720                'siteexport_toctools',
721                array(
722                    'mergehint',
723                    'end',
724                    $mergeHint
725                )
726            )
727        ));
728
729/*
730        print "<pre>"; print_r($instructions); print "</pre>";
731//*/
732        $instructions = array_merge($mergeHintPrepend, $instructions, $mergeHintPostpend);
733    }
734
735    private function _toctoolPrepends( &$instructions ) {
736
737        $mergeHintPrependPrepend = array();
738
739        // 2021-01-14 This did no good - if a merged page had two mergehints, the first was stripped.
740/*
741        if ( $instructions[0][0] == 'plugin' && $instructions[0][1][0] == 'siteexport_toctools' && $instructions[0][1][1][1] == 'start' ) {
742
743            // This is already section merge hint ... but it will have a section at its end ... hopefully
744            do {
745                $_instructions = array_shift( $instructions );
746                array_push( $mergeHintPrependPrepend, $_instructions);
747            } while( !($_instructions[0] == 'plugin' && $_instructions[1][0] == 'siteexport_toctools' && $_instructions[1][1][1] == 'end' ) ) ;
748            array_splice($mergeHintPrepend, 0, 0, $mergeHintPrependPrepend);
749        }
750//*/
751/*
752        print "<pre>"; print_r($instructions); print "</pre>";
753//*/
754        return $mergeHintPrependPrepend;
755    }
756
757    /**
758     * Remove TOC, section edit buttons and tags
759     */
760    private function _cleanXHTML($xhtml) {
761        $replace = array(
762            '!<div class="toc">.*?(</div>\n</div>)!s' => '', // remove TOCs
763            '#<!-- SECTION \[(\d*-\d*)\] -->#s'       => '', // remove section edit buttons
764            '!<div id="tags">.*?(</div>)!s'           => ''  // remove category tags
765        );
766        $xhtml = preg_replace(array_keys($replace), array_values($replace), $xhtml);
767        return $xhtml;
768    }
769
770    /**
771     * Allow the plugin to prevent DokuWiki creating a second instance of itself
772     *
773     * @return bool   true if the plugin can not be instantiated more than once
774     */
775    public function isSingleton() {
776        return true;
777    }
778}
779// vim:ts=4:sw=4:et:enc=utf-8:
780