xref: /plugin/siteexport/syntax/toctools.php (revision d3a16081c96b59fc21f8b56bca6668d62ac6d998)
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        // 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        if ($mode == 'xhtml') {
91            list( $type, $pos, $title, $id ) = $data;
92            if ( $type == 'mergehint' ) {
93                if ( $pos == 'start' ) {
94                    $renderer->doc .= '<!-- MergeHint Start for "' . $title . '" -->';
95                    $renderer->doc .= '<div id="' . $id . '" class="siteexport mergehintwrapper"><aside class="mergehint">' . $title . '</aside><div class="mergehintcontent">';
96                } else {
97                    $renderer->doc .= '</div></div>';
98                    $renderer->doc .= '<!-- MergeHint End for "' . $title . '" -->';
99                }
100            } else {
101                $renderer->doc .= "<br style=\"page-break-after:always;\" />";
102            }
103            return true;
104        }
105        return false;
106    }
107}
108
109//Setup VIM: ex: et ts=4 enc=utf-8 :