xref: /plugin/siteexport/syntax/toctools.php (revision 7462771189df6f2c2af9010eb8746ad2e13be7d1)
1<?php
2/**
3 * Siteexport Plugin
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
10if (!defined('DOKU_INC')) define('DOKU_INC', /** @scrutinizer ignore-type */ realpath(dirname(__FILE__) . '/../../') . '/');
11if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
12require_once(DOKU_PLUGIN . 'syntax.php');
13
14/**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
17 */
18class syntax_plugin_siteexport_toctools extends DokuWiki_Syntax_Plugin {
19
20    protected $special_pattern = '<mergehint\b[^>\r\n]*?/>';
21    protected $entry_pattern   = '<mergehint\b.*?>(?=.*?</mergehint>)';
22    protected $exit_pattern    = '</mergehint>';
23
24    function getType(){ return 'substition';}
25    function getAllowedTypes() { return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); }
26    function getPType(){ return 'stack';}
27    function getSort(){ return 999; }
28
29    /**
30     * Connect pattern to lexer
31     */
32    function connectTo($mode) {
33        $this->Lexer->addSpecialPattern($this->special_pattern,$mode,'plugin_siteexport_toctools');
34        $this->Lexer->addEntryPattern($this->entry_pattern,$mode,'plugin_siteexport_toctools');
35    }
36
37    function postConnect() {
38        $this->Lexer->addExitPattern($this->exit_pattern, 'plugin_siteexport_toctools');
39    }
40
41    /**
42     * Handle the match
43     */
44    function handle($match, $state, $pos, Doku_Handler $handler){
45        global $conf;
46        switch ($state) {
47            case DOKU_LEXER_ENTER:
48            case DOKU_LEXER_SPECIAL:
49                $data = trim(substr($match,strpos($match,' '),-1)," \t\n/");
50
51                // print "<pre>"; print_r($handler); print "</pre>";
52
53                if ($handler->status['section']) {
54                    $handler->_addCall('section_close',array(),$pos);
55                }
56
57                return array('mergehint', 'start', $data, sectionid( $data ));
58
59            case DOKU_LEXER_UNMATCHED:
60                $handler->_addCall('cdata', array($match), $pos);
61                break;
62
63            case DOKU_LEXER_EXIT:
64
65                $level = 1;
66                foreach( array_reverse( $handler->calls ) as $call ) {
67                    if ( $calls[0] == 'section_open' ) {
68                        $level = $calls[1][0];
69                        break;
70                    }
71                }
72
73                // We need to add the current plugin first and then open the section again.
74                $handler->_addCall('plugin',array('siteexport_toctools', array('mergehint', 'end', 'syntax'),DOKU_LEXER_EXIT),$pos);
75                $handler->_addCall('section_open',array($level),$pos+strlen($match));
76        }
77        return false;
78    }
79
80    /**
81     * Create output
82     */
83    public function render($mode, Doku_Renderer $renderer, $data) {
84        if ($mode == 'xhtml') {
85            list( $type, $pos, $title, $id ) = $data;
86            if ( $type == 'mergehint' ) {
87                if ( $pos == 'start' ) {
88                    $renderer->doc .= '<!-- MergeHint Start for "' . $title . '" -->';
89                    $renderer->doc .= '<div id="' . $id . '" class="siteexport mergehintwrapper"><aside class="mergehint">' . $title . '</aside><div class="mergehintcontent">';
90                } else {
91                    $renderer->doc .= '</div></div>';
92                    $renderer->doc .= '<!-- MergeHint End for "' . $title . '" -->';
93                }
94            } else {
95                $renderer->doc .= "<br style=\"page-break-after:always;\" />";
96            }
97            return true;
98        }
99        return false;
100    }
101}
102
103//Setup VIM: ex: et ts=4 enc=utf-8 :