xref: /plugin/siteexport/syntax/toc.php (revision 7462771189df6f2c2af9010eb8746ad2e13be7d1)
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                        } else
213                        if ($renderer->meta['sitetoc']['pagebreak']) {
214                            $sitepagebreak = array(array(
215                                'plugin',
216                                array(
217                                    'siteexport_toctools',
218                                    array(
219                                        'pagebreak',
220                                        null,
221                                        null
222                                    )
223                                )
224                            ));
225                            $instr = array_merge($instr, $instructions, $sitepagebreak);
226                        } else {
227                            // Concat
228                            $instr = array_merge($instr, $instructions);
229                        }
230                    }
231
232                    if (!empty($instr)) {
233                        $this->_cleanAllInstructions($instr);
234
235                        // if the last element is a pagebreak, remove it.
236                        if ($instr[count($instr)-1][1][0] == 'siteexport_toctools' && $instr[count($instr)-1][1][1][0] == 'pagebreak' ) {
237                            $instr = array_slice($instr, 0, -1);
238                        }
239
240
241                        // print "<pre>"; print_r($instr); print "</pre>";
242                        $this->_render_output($renderer, $mode, $instr);
243                    }
244
245                    $renderer->section_close();
246                }
247                return true;
248            }
249
250            // Save the current ID
251            $LNID = $SID;
252
253            // Add ID to flags['mergeDoc']
254            if ($renderer->meta['sitetoc']['mergeDoc'] === true) { // || (count($renderer->meta['sitetoc']['siteexportTOC']) > 0 && $renderer->meta['sitetoc']['siteexportMergeDoc'] === true) ) {
255                $this->mergedPages[] = array($SID, $DEPTH);
256                resolve_pageid(getNS($ID), $SID, $exists);
257            } else {
258                // // print normal internal link (XHTML odt)
259                $renderer->internallink($LNID, $NAME, null);
260
261                // Display Description underneath
262                if ($renderer->meta['sitetoc']['showDescription'] === true) {
263                    $renderer->cdata(p_get_metadata($SID, 'description abstract', true));
264                }
265            }
266
267            // Render Metadata
268        } else if ($mode == 'metadata') {
269            if (!is_array($data) && $data == 'save__meta') {
270                $renderer->meta['sitetoc']['siteexportTOC'] = $this->savedToc;
271
272                foreach ($this->savedToc as $page) {
273                    $renderer->meta['relation']['references'][$page['id']] = $page['exists'];
274                }
275
276                $this->savedToc = array();
277            } else if (!isset($data['start']) && !isset($data['pos'])) {
278                $this->savedToc[] = $this->__addTocItem($SID, $NAME, $DEPTH, $renderer);
279            }
280        } else {
281            return false;
282        }
283
284        return true;
285    }
286
287    /*
288     * pull apart the ID and create an Entry for the TOC
289     */
290    private function __addTocItem($id, $name, $depth, $renderer) {
291        global $conf;
292        global $ID;
293
294        // Render Title
295        $default = $renderer->_simpleTitle($id);
296        $exists = false; $isImage = false; $linktype = null;
297        resolve_pageid(getNS($ID), $id, $exists);
298        $name = $renderer->_getLinkTitle($name, $default, $isImage, $id, $linktype);
299
300        //keep hash anchor
301        list($id, $hash) = explode('#', $id, 2);
302        if (!empty($hash)) $hash = $renderer->_headerToLink($hash);
303
304        // Build Sitetoc Item
305        $item = array();
306        $item['id'] = $id;
307        $item['name'] = $name;
308        $item['anchor'] = $hash;
309        $item['depth'] = $depth;
310        $item['exists'] = $exists;
311        if (!$conf['skipacl'] && auth_quickaclcheck($item['id']) < AUTH_READ) {
312            return false;
313        }
314
315        return $item;
316    }
317
318    /*
319     * Render the output of one page
320     */
321    private function _render_output($renderer, $mode, $instr) {
322        global $ID;
323
324        // Section IDs
325        // $addID = sectionID($addID, $check);    //not possible to use a:b:c for id
326
327        if ($mode == 'xhtml') {
328
329            //--------RENDER
330            //renderer information(TOC build / Cache used)
331            $info = array();
332            $content = p_render($mode, $instr, $info);
333
334            //Remove TOC`s, section edit buttons and tags
335            $content = $this->_cleanXHTML($content);
336
337            // embed the included page
338            // $renderer->doc .= '<div class="include">';
339            //add an anchor to find start of a inserted page
340            // $renderer->doc .= "<a name='$addID' id='$addID'>";
341            $renderer->doc .= $content;
342            // $renderer->doc .= '</div>';
343        } else if ($mode == 'odt') {
344
345            // Loop through the instructions
346            foreach ($instr as $instruction) {
347                // Execute the callback against the Renderer
348                call_user_func_array(array($renderer, $instruction[0]), $instruction[1]);
349            }
350        }
351    }
352
353    /*
354     * Corrects relative internal links and media and
355     * converts headers of included pages to subheaders of the current page
356     */
357    private function _convertInstructions($instr, $id, &$renderer, $depth = 1) {
358        global $ID;
359        global $conf;
360
361        $n = count($instr);
362
363        for ($i = 0; $i < $n; $i++) {
364            //internal links(links inside this wiki) an relative links
365            if ((substr($instr[$i][0], 0, 12) == 'internallink')) {
366                $this->_convert_link($renderer, $instr[$i], $id);
367            }
368            else if ((substr($instr[$i][0], 0, 13) == 'internalmedia')) {
369                $this->_convert_media($renderer, $instr[$i], $id);
370            }
371            else if ((substr($instr[$i][0], 0, 6) == 'header')) {
372                $this->_convert_header($renderer, $instr[$i], $depth-1); // -1 because the depth starts at 1
373            }
374            else if ((substr($instr[$i][0], 0, 12) == 'section_open')) {
375                $this->_convert_section($renderer, $instr[$i], $depth-1); // -1 because the depth starts at 1
376            }
377        }
378
379        //if its the document start, cut off the first element(document information)
380        if ($instr[0][0] == 'document_start')
381        return array_slice($instr, 1, -1);
382        else
383        return $instr;
384    }
385
386    /*
387     * Convert link of given instruction
388     */
389    private function _convert_link(&$renderer, &$instr, $id) {
390        global $ID;
391
392        $exists = false;
393
394        resolve_pageid(getNS($id), $instr[1][0], $exists);
395        list($pageID, $pageReference) = explode("#", $instr[1][0], 2);
396
397        if (in_array($pageID, $this->includedPages)) {
398            // Crate new internal Links
399            $check = null;
400
401            // Either get existing reference or create from first heading. If still not there take the alternate ID
402            $pageNameLink = empty($pageReference) ? sectionID($pageID, $check) : $pageReference;
403
404            $instr[1][0] = $ID . "#" . $pageNameLink;
405
406        } else {
407            // Convert external Links to plain Text
408
409            $instr = array(
410                        "cdata",
411            array($instr[1][1]),
412            $instr[2]
413            );
414        }
415    }
416
417    /*
418     * Convert internalmedia of given instruction
419     */
420    private function _convert_media(&$renderer, &$instr, $id) {
421        global $ID;
422
423        // Resolvemedia returns the absolute path to media by reference
424        $exists = false;
425        resolve_mediaid(getNS($id), $instr[1][0], $exists);
426    }
427
428    /**
429     * @param integer $depth
430     */
431    private function _convert_header(&$renderer, &$instr, $depth) {
432        // More Depth!
433        $instr[1][1] += $depth;
434    }
435
436    /**
437     * @param integer $depth
438     */
439    private function _convert_section(&$renderer, &$instr, $depth) {
440        // More Depth!
441        $instr[1][0] += $depth;
442    }
443
444    private function _mergeWithHeaders($existing, $newInstructions, $level = 1, $mergeHint = array()) {
445
446        $returnInstructions = array();
447        $preparedInstructions = array();
448        $existingStart = $existingEnd = 0;
449        $firstRun = true;
450
451        while ($this->_findNextHeaderSection($existing, $level, $existingStart, $existingEnd)) {
452
453            if ($firstRun) {
454                $returnInstructions = array_merge($returnInstructions, array_slice($existing, 0, $existingStart));
455                $firstRun = false;
456            }
457
458            $currentSlice = array_slice($existing, $existingStart, $existingEnd-$existingStart);
459
460            // Find matching part with headername
461            $newStart = $newEnd = 0;
462            if ($this->_findNextHeaderSection($newInstructions, $level, $newStart, $newEnd, $currentSlice[0][1][0])) {
463
464                $newSlice = array_slice($newInstructions, $newStart, $newEnd-$newStart);
465                if ($newSlice[0][0] == 'header')
466                    array_shift($newSlice); // Remove Heading
467
468                // merge found parts on next level.
469                $returnedInstructions = $this->_mergeWithHeaders($currentSlice, $newSlice, $level+1, $mergeHint);
470
471                // Put them at the end!
472                $preparedInstructions = array_merge($preparedInstructions, $returnedInstructions);
473
474                // Remove from input
475                array_splice($newInstructions, $newStart, $newEnd-$newStart);
476            } else {
477                // Nothing else found
478                $preparedInstructions = array_merge($preparedInstructions, $currentSlice);
479            }
480
481            $existingStart = $existingEnd;
482        }
483
484        // Append the rest
485        $returnInstructions = array_merge($returnInstructions, array_slice($existing, $existingStart));
486
487        // Check for section close inconsistencies and put one at the very end ...
488        $section_postpend = array();
489        if (
490            (
491                ($tmp1 = array_slice($newInstructions, -1))
492                && ($tmp1[0][0] == 'section_close')
493            )
494            &&
495            (
496                ($tmp2 = array_slice($newInstructions, -2))
497                && ($tmp2[0][0] == 'section_close')
498            )
499        ) {
500            $section_postpend = array_splice($newInstructions, -1);
501        }
502        if (
503            (
504                ($tmp3 = array_slice($returnInstructions, -1))
505                && ($tmp3[0][0] == 'section_close')
506            )
507            &&
508            (
509                ($tmp4 = array_slice($returnInstructions, -2))
510                && ($tmp4[0][0] == 'section_close')
511            )
512        ) {
513            $section_postpend = array_merge($section_postpend, array_splice($returnInstructions, -1));
514        }
515
516        // What if there are headings left inside the $newInstructions?????
517        // Find matching part with headername
518        $newStart = $newEnd = 0;
519        $section_prepend = array();
520        if ($this->_findNextHeaderSection($newInstructions, $level, $newStart, $newEnd)) {
521            // If there are header in here, build a prepend and have the rest at the end
522            $section_prepend = array_splice($newInstructions, 0, $newStart);
523        } else {
524            // If not, prepend all of it.
525            $section_prepend = $newInstructions;
526            $newInstructions = array();
527        }
528
529        $this->_insertMergeHint($section_prepend, $mergeHint);
530
531        $returnInstructions = array_merge($returnInstructions, $section_prepend, $preparedInstructions, $newInstructions, $section_postpend);
532
533        return $returnInstructions;
534    }
535
536    /**
537     * @param integer $level
538     */
539    private function _findNextHeaderSection($section, $level, &$start, &$end, $headerName = null) {
540
541        $inCount = count($section);
542        $currentSlice = -1;
543
544        // Find Level 1 Header that matches.
545        for ($i = $start; $i < $inCount; $i++) {
546
547            $instruction = $section[$i];
548            $end = $i; // Or it will be lost and a section close will be missing.
549
550            // First Level Header
551            if ($instruction[0] == 'header' && $instruction[1][1] == $level) {
552
553                if ($currentSlice > 0) {
554                    return true;
555                }
556
557                if ($headerName == null || ($headerName == $instruction[1][0])) {
558                    // Begin of new slice ...
559                    $start = $currentSlice = $i;
560                }
561            }
562        }
563
564        // Nothing found
565        $end = $i; // Or it will be lost and a section close will be missing.
566        return $currentSlice > 0;
567    }
568
569    private function _cleanAllInstructions(&$instr) {
570        $this->_cleanInstructions($instr, '/section_(close|open)/');
571        $this->_cleanInstructions($instr, '/listu_(close|open)/');
572        $this->_cleanInstructions($instr, '/listo_(close|open)/');
573    }
574
575    /**
576     * @param string $tag
577     */
578    private function _cleanInstructions(&$instructions, $tag) {
579
580        $inCount = count($instructions);
581        for ($i = 0; $i < $inCount; $i++) {
582
583            // Last instruction
584            if ($i == $inCount-1) {
585                break;
586            }
587
588            if (preg_match($tag, $instructions[$i][0]) && preg_match($tag, $instructions[$i+1][0]) && $instructions[$i][0] != $instructions[$i+1][0]) {
589
590                // found different tags, but both match the expression and follow each other - so they can be elliminated
591                array_splice($instructions, $i, 2);
592                $inCount -= 2;
593                $i--;
594            }
595
596        }
597    }
598
599    /**
600     * Strip everything except for the headers
601     */
602    private function _initialHeaderStructure($instructions) {
603        $inCount = count($instructions);
604        for ($i = 0; $i < $inCount; $i++) {
605
606            // Last instruction
607            if ($i == $inCount-1) {
608                break;
609            }
610
611            if (!in_array($instructions[$i][0], array('header', 'section_open', 'section_close', 'p_open', 'p_close'))) {
612                // found non-matching
613                array_splice($instructions, $i, 1);
614                $inCount--;
615                $i--;
616            }
617        }
618        return $instructions;
619    }
620
621    private function _insertMergeHint(&$instructions, $mergeHint) {
622
623        // Surround new slice with a mergehint
624        if (empty($mergeHint)) { return; }
625
626        // No emtpy insruction sets.
627        $this->_cleanAllInstructions($instructions);
628
629        if (empty($instructions)) { return; }
630
631        $mergeHintPrepend = array(array(
632            'plugin',
633            array(
634                'siteexport_toctools',
635                array(
636                    'mergehint',
637                    'start',
638                    $mergeHint,
639                    $mergeHintId
640                )
641            )
642        ));
643
644        $mergeHintPostpend = array(array(
645            'plugin',
646            array(
647                'siteexport_toctools',
648                array(
649                    'mergehint',
650                    'end',
651                    $mergeHint
652                )
653            )
654        ));
655
656        if ($instructions[0][0] == 'plugin' && $instructions[0][1][0] == 'siteexport_toctools' && $instructions[0][1][1][1] == 'start' ) {
657/*
658        print "<pre>"; print_r($instructions); print "</pre>";
659//*/
660            // This is already section merge hint ... but it will have a section at its end ... hopefully
661            $mergeHintPrependPrepend = array();
662            do {
663                array_push( $mergeHintPrependPrepend, array_shift( $instructions ) );
664            } while( $instructions[0][0] != 'section_open' );
665            array_splice($mergeHintPrepend, 0, 0, $mergeHintPrependPrepend);
666
667        }
668
669        // only section content should be surrounded.
670        if ($instructions[0][0] != 'section_open') { return; }
671
672        // save for later use
673        $mergeHints = array();
674        $mergeHintId = sectionid($mergeHint, $mergeHints);
675        $this->merghintIds[$mergeHintId] = $mergeHint;
676
677/*
678        print "<pre>"; print_r($instructions); print "</pre>";
679//*/
680
681        $instructions = array_merge($mergeHintPrepend, $instructions, $mergeHintPostpend);
682    }
683
684    /**
685     * Remove TOC, section edit buttons and tags
686     */
687    private function _cleanXHTML($xhtml) {
688        $replace = array(
689            '!<div class="toc">.*?(</div>\n</div>)!s' => '', // remove TOCs
690            '#<!-- SECTION \[(\d*-\d*)\] -->#s'       => '', // remove section edit buttons
691            '!<div id="tags">.*?(</div>)!s'           => ''  // remove category tags
692        );
693        $xhtml = preg_replace(array_keys($replace), array_values($replace), $xhtml);
694        return $xhtml;
695    }
696
697    /**
698     * Allow the plugin to prevent DokuWiki creating a second instance of itself
699     *
700     * @return bool   true if the plugin can not be instantiated more than once
701     */
702    public function isSingleton() {
703        return true;
704    }
705}
706// vim:ts=4:sw=4:et:enc=utf-8:
707