xref: /plugin/siteexport/syntax/toctools.php (revision 774ba46f66b138042e53c2a092493ed36c3e9d72)
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    public function getType(){ return 'substition';}
25    public function getAllowedTypes() { return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); }
26    public function getPType(){ return 'stack';}
27    public function getSort(){ return 999; }
28
29    /**
30     * Connect pattern to lexer
31     */
32    public 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    public function postConnect() {
38        $this->Lexer->addExitPattern($this->exit_pattern, 'plugin_siteexport_toctools');
39    }
40
41    private function findPreviousSectionOpen( Doku_Handler $handler ) {
42        foreach( array_reverse( $handler->calls ) as $call ) {
43            if ( $calls[0] == 'section_open' ) {
44                return $calls[1][0];
45            }
46        }
47        return 1;
48    }
49
50    private function addInstructionstoHandler( $match, $state, $pos, Doku_Handler $handler, $instructions ) {
51
52        if ($handler->status['section']) {
53            $handler->_addCall('section_close', array(), $pos);
54        }
55
56        // We need to add the current plugin first and then open the section again.
57        $level = $this->findPreviousSectionOpen( $handler );
58        $handler->_addCall('plugin', array('siteexport_toctools', $instructions, $state), $pos);
59        $handler->_addCall('section_open', array($level), $pos+strlen($match) );
60    }
61
62    /**
63     * Handle the match
64     */
65    public function handle($match, $state, $pos, Doku_Handler $handler){
66        global $conf;
67        switch ($state) {
68            case DOKU_LEXER_ENTER:
69            case DOKU_LEXER_SPECIAL:
70                $data = trim(substr($match,strpos($match,' '),-1)," \t\n/");
71                $this->addInstructionstoHandler( $match, $state, $pos, $handler, array('mergehint', 'start', $data, sectionid( $data ) ) );
72                break;
73            case DOKU_LEXER_UNMATCHED:
74                $handler->_addCall('cdata', array($match), $pos);
75                break;
76            case DOKU_LEXER_EXIT:
77                $this->addInstructionstoHandler( $match, $state, $pos, $handler, array('mergehint', 'end', 'syntax' ) );
78                break;
79        }
80        return false;
81    }
82
83    /**
84     * Create output
85     */
86    public function render($mode, Doku_Renderer $renderer, $data) {
87        if ($mode == 'xhtml') {
88            list( $type, $pos, $title, $id ) = $data;
89            if ( $type == 'mergehint' ) {
90                if ( $pos == 'start' ) {
91                    $renderer->doc .= '<!-- MergeHint Start for "' . $title . '" -->';
92                    $renderer->doc .= '<div id="' . $id . '" class="siteexport mergehintwrapper"><aside class="mergehint">' . $title . '</aside><div class="mergehintcontent">';
93                } else {
94                    $renderer->doc .= '</div></div>';
95                    $renderer->doc .= '<!-- MergeHint End for "' . $title . '" -->';
96                }
97            } else {
98                $renderer->doc .= "<br style=\"page-break-after:always;\" />";
99            }
100            return true;
101        }
102        return false;
103    }
104}
105
106//Setup VIM: ex: et ts=4 enc=utf-8 :