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    private $checkArray = array();
25
26    public function getType(){ return 'substition';}
27    public function getAllowedTypes() { return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); }
28    public function getPType(){ return 'stack';}
29    public function getSort(){ return 999; }
30
31    /**
32     * Connect pattern to lexer
33     */
34    public function connectTo($mode) {
35        $this->Lexer->addSpecialPattern($this->special_pattern,$mode,'plugin_siteexport_toctools');
36        $this->Lexer->addEntryPattern($this->entry_pattern,$mode,'plugin_siteexport_toctools');
37    }
38
39    public function postConnect() {
40        $this->Lexer->addExitPattern($this->exit_pattern, 'plugin_siteexport_toctools');
41    }
42
43    private function findPreviousSectionOpen( Doku_Handler $handler ) {
44        foreach( array_reverse( $handler->calls ) as $calls ) {
45            if ( $calls[0] == 'section_open' ) {
46                return $calls[1][0];
47            }
48        }
49        return 1;
50    }
51
52    private function addInstructionstoHandler( $match, $state, $pos, Doku_Handler $handler, $instructions ) {
53
54        // Switch for DW Hogfather+
55        if ( ( method_exists( $handler, 'getStatus') && $handler->getStatus('section') ) || $handler->status['section'] ) {
56            $handler->_addCall('section_close', array(), $pos);
57        }
58
59        // We need to add the current plugin first and then open the section again.
60        $level = $this->findPreviousSectionOpen( $handler );
61        $handler->_addCall('plugin', array('siteexport_toctools', $instructions, $state), $pos);
62        $handler->_addCall('section_open', array($level), $pos+strlen($match) );
63    }
64
65    /**
66     * Handle the match
67     */
68    public function handle($match, $state, $pos, Doku_Handler $handler){
69        global $conf;
70        switch ($state) {
71            case DOKU_LEXER_ENTER:
72            case DOKU_LEXER_SPECIAL:
73                $data = trim(substr($match,strpos($match,' '),-1)," \t\n/");
74                $this->addInstructionstoHandler( $match, $state, $pos, $handler, array('mergehint', 'start', $data, sectionID( $data, $this->checkArray ) ) );
75                break;
76            case DOKU_LEXER_UNMATCHED:
77                $handler->_addCall('cdata', array($match), $pos);
78                break;
79            case DOKU_LEXER_EXIT:
80                $this->addInstructionstoHandler( $match, $state, $pos, $handler, array('mergehint', 'end', 'syntax' ) );
81                break;
82        }
83        return false;
84    }
85
86    /**
87     * Create output
88     */
89    public function render($mode, Doku_Renderer $renderer, $data) {
90
91        list( $type, $pos, $title, $id ) = $data;
92        if ($mode == 'xhtml') {
93            if ( $type == 'mergehint' ) {
94
95                $startHint = '<!-- MergeHint Start for "';
96                $lastDiv = '<div class="mergehintcontent">';
97
98                if ( $pos == 'start' ) {
99                    $renderer->doc .= $startHint . $title . '" -->';
100                    $renderer->doc .= '<div id="' . $id . '" class="siteexport mergehintwrapper"><aside class="mergehint">' . $title . '</aside>' . $lastDiv;
101                } else {
102
103                    // check if anything was inserted. We have to strip all tags,
104                    // we're just looking for real content here
105                    $lastPos = strrpos($renderer->doc, $lastDiv);
106                    if ( $lastPos !== false ) {
107
108                        $remaining = substr( $renderer->doc, $lastPos + strlen($lastDiv) );
109                        $remaining = strip_tags( $remaining );
110                        $remaining = trim($remaining);
111                        if ( strlen( $remaining ) == 0 ) {
112                            // empty
113                            $lastPos = strrpos($renderer->doc, $startHint);
114                            $renderer->doc = substr($renderer->doc, 0, $lastPos);
115                            return true;
116                        }
117                    }
118
119                    $renderer->doc .= '</div></div>';
120                    $renderer->doc .= '<!-- MergeHint End for "' . $title . '" -->';
121                }
122            } else {
123                $renderer->doc .= "<br style=\"page-break-after:always;\" />";
124            }
125            return true;
126        } else if ( $mode = 'markdown' && $type == 'mergehint' && $pos == 'start' ) {
127            $renderer->doc .= DOKU_LF . '##### ' . $title . DOKU_LF;
128            return true;
129        }
130        return false;
131    }
132}
133
134//Setup VIM: ex: et ts=4 enc=utf-8 :