xref: /plugin/siteexport/syntax/toctools.php (revision 3d79496e103a3a9ad38075d648b24912e4fc9292)
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 $call ) {
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        if ($handler->getStatus('section')) {
55            $handler->_addCall('section_close', array(), $pos);
56        }
57
58        // We need to add the current plugin first and then open the section again.
59        $level = $this->findPreviousSectionOpen( $handler );
60        $handler->_addCall('plugin', array('siteexport_toctools', $instructions, $state), $pos);
61        $handler->_addCall('section_open', array($level), $pos+strlen($match) );
62    }
63
64    /**
65     * Handle the match
66     */
67    public function handle($match, $state, $pos, Doku_Handler $handler){
68        global $conf;
69        switch ($state) {
70            case DOKU_LEXER_ENTER:
71            case DOKU_LEXER_SPECIAL:
72                $data = trim(substr($match,strpos($match,' '),-1)," \t\n/");
73                $this->addInstructionstoHandler( $match, $state, $pos, $handler, array('mergehint', 'start', $data, sectionID( $data, $checkArray ) ) );
74                break;
75            case DOKU_LEXER_UNMATCHED:
76                $handler->_addCall('cdata', array($match), $pos);
77                break;
78            case DOKU_LEXER_EXIT:
79                $this->addInstructionstoHandler( $match, $state, $pos, $handler, array('mergehint', 'end', 'syntax' ) );
80                break;
81        }
82        return false;
83    }
84
85    /**
86     * Create output
87     */
88    public function render($mode, Doku_Renderer $renderer, $data) {
89        if ($mode == 'xhtml') {
90            list( $type, $pos, $title, $id ) = $data;
91            if ( $type == 'mergehint' ) {
92                if ( $pos == 'start' ) {
93                    $renderer->doc .= '<!-- MergeHint Start for "' . $title . '" -->';
94                    $renderer->doc .= '<div id="' . $id . '" class="siteexport mergehintwrapper"><aside class="mergehint">' . $title . '</aside><div class="mergehintcontent">';
95                } else {
96                    $renderer->doc .= '</div></div>';
97                    $renderer->doc .= '<!-- MergeHint End for "' . $title . '" -->';
98                }
99            } else {
100                $renderer->doc .= "<br style=\"page-break-after:always;\" />";
101            }
102            return true;
103        }
104        return false;
105    }
106}
107
108//Setup VIM: ex: et ts=4 enc=utf-8 :