xref: /plugin/siteexport/syntax/toctools.php (revision 5ce337f21e72a51359dd610ca4ea571cff0d07aa)
1300ce4a2SGerry Weißbach<?php
2300ce4a2SGerry Weißbach/**
3300ce4a2SGerry Weißbach * Siteexport Plugin
4300ce4a2SGerry Weißbach *
5300ce4a2SGerry Weißbach * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6300ce4a2SGerry Weißbach * @author     i-net software <tools@inetsoftware.de>
7300ce4a2SGerry Weißbach * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8300ce4a2SGerry Weißbach */
9300ce4a2SGerry Weißbach
10a8c17ab5Si-net /// softwareif (!defined('DOKU_INC')) define('DOKU_INC', /** @scrutinizer ignore-type */ realpath(dirname(__FILE__) . '/../../') . '/');
11300ce4a2SGerry Weißbachif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
12300ce4a2SGerry Weißbach
13300ce4a2SGerry Weißbach/**
14300ce4a2SGerry Weißbach * All DokuWiki plugins to extend the parser/rendering mechanism
15300ce4a2SGerry Weißbach * need to inherit from this class
16300ce4a2SGerry Weißbach */
17300ce4a2SGerry Weißbachclass syntax_plugin_siteexport_toctools extends DokuWiki_Syntax_Plugin {
18300ce4a2SGerry Weißbach
1974627711SGerry Weißbach    protected $special_pattern = '<mergehint\b[^>\r\n]*?/>';
2074627711SGerry Weißbach    protected $entry_pattern   = '<mergehint\b.*?>(?=.*?</mergehint>)';
2174627711SGerry Weißbach    protected $exit_pattern    = '</mergehint>';
22300ce4a2SGerry Weißbach
233d79496eSGerry Weißbach    private $checkArray = array();
243d79496eSGerry Weißbach
25774ba46fSGerry Weißbach    public function getType(){ return 'substition';}
26774ba46fSGerry Weißbach    public function getAllowedTypes() { return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); }
27774ba46fSGerry Weißbach    public function getPType(){ return 'stack';}
28774ba46fSGerry Weißbach    public function getSort(){ return 999; }
29300ce4a2SGerry Weißbach
30300ce4a2SGerry Weißbach    /**
31300ce4a2SGerry Weißbach     * Connect pattern to lexer
32300ce4a2SGerry Weißbach     */
33774ba46fSGerry Weißbach    public function connectTo($mode) {
3474627711SGerry Weißbach        $this->Lexer->addSpecialPattern($this->special_pattern,$mode,'plugin_siteexport_toctools');
3574627711SGerry Weißbach        $this->Lexer->addEntryPattern($this->entry_pattern,$mode,'plugin_siteexport_toctools');
3674627711SGerry Weißbach    }
3774627711SGerry Weißbach
38774ba46fSGerry Weißbach    public function postConnect() {
3974627711SGerry Weißbach        $this->Lexer->addExitPattern($this->exit_pattern, 'plugin_siteexport_toctools');
40300ce4a2SGerry Weißbach    }
41300ce4a2SGerry Weißbach
42268cf88bSGerry Weißbach    private function findPreviousSectionOpen( Doku_Handler $handler ) {
43846652f5SGerry Weißbach        foreach( array_reverse( $handler->calls ) as $calls ) {
44c1198967SGerry Weißbach            if ( $calls[0] == 'section_open' ) {
45c1198967SGerry Weißbach                return $calls[1][0];
46268cf88bSGerry Weißbach            }
47268cf88bSGerry Weißbach        }
48268cf88bSGerry Weißbach        return 1;
49268cf88bSGerry Weißbach    }
50268cf88bSGerry Weißbach
51268cf88bSGerry Weißbach    private function addInstructionstoHandler( $match, $state, $pos, Doku_Handler $handler, $instructions ) {
52268cf88bSGerry Weißbach
53538f2ac6SGerry Weißbach        // Switch for DW Hogfather+
54538f2ac6SGerry Weißbach        if ( ( method_exists( $handler, 'getStatus') && $handler->getStatus('section') ) || $handler->status['section'] ) {
55268cf88bSGerry Weißbach            $handler->_addCall('section_close', array(), $pos);
56268cf88bSGerry Weißbach        }
57268cf88bSGerry Weißbach
58268cf88bSGerry Weißbach        // We need to add the current plugin first and then open the section again.
59268cf88bSGerry Weißbach        $level = $this->findPreviousSectionOpen( $handler );
60268cf88bSGerry Weißbach        $handler->_addCall('plugin', array('siteexport_toctools', $instructions, $state), $pos);
61268cf88bSGerry Weißbach        $handler->_addCall('section_open', array($level), $pos+strlen($match) );
62268cf88bSGerry Weißbach    }
63268cf88bSGerry Weißbach
64300ce4a2SGerry Weißbach    /**
65300ce4a2SGerry Weißbach     * Handle the match
66300ce4a2SGerry Weißbach     */
67774ba46fSGerry Weißbach    public function handle($match, $state, $pos, Doku_Handler $handler){
6874627711SGerry Weißbach        global $conf;
6974627711SGerry Weißbach        switch ($state) {
7074627711SGerry Weißbach            case DOKU_LEXER_ENTER:
7174627711SGerry Weißbach            case DOKU_LEXER_SPECIAL:
7274627711SGerry Weißbach                $data = trim(substr($match,strpos($match,' '),-1)," \t\n/");
73d3a16081SGerry Weißbach                $this->addInstructionstoHandler( $match, $state, $pos, $handler, array('mergehint', 'start', $data, sectionID( $data, $this->checkArray ) ) );
74268cf88bSGerry Weißbach                break;
7574627711SGerry Weißbach            case DOKU_LEXER_UNMATCHED:
7674627711SGerry Weißbach                $handler->_addCall('cdata', array($match), $pos);
7774627711SGerry Weißbach                break;
7874627711SGerry Weißbach            case DOKU_LEXER_EXIT:
79268cf88bSGerry Weißbach                $this->addInstructionstoHandler( $match, $state, $pos, $handler, array('mergehint', 'end', 'syntax' ) );
8074627711SGerry Weißbach                break;
8174627711SGerry Weißbach        }
8274627711SGerry Weißbach        return false;
83300ce4a2SGerry Weißbach    }
84300ce4a2SGerry Weißbach
85300ce4a2SGerry Weißbach    /**
86300ce4a2SGerry Weißbach     * Create output
87300ce4a2SGerry Weißbach     */
88a8c17ab5Si-net /// software    public function render($mode, Doku_Renderer $renderer, $data) {
89644c76feSGerry Weißbach
90300ce4a2SGerry Weißbach        list( $type, $pos, $title, $id ) = $data;
91644c76feSGerry Weißbach        if ($mode == 'xhtml') {
92300ce4a2SGerry Weißbach            if ( $type == 'mergehint' ) {
93c1198967SGerry Weißbach
94*3a388851SGerry Weißbach                $startHint = '<!-- MergeHint Start for "';
95c1198967SGerry Weißbach                $lastDiv = '<div class="mergehintcontent">';
96c1198967SGerry Weißbach
97300ce4a2SGerry Weißbach                if ( $pos == 'start' ) {
98*3a388851SGerry Weißbach                    $renderer->doc .= $startHint . $title . '" -->';
99c1198967SGerry Weißbach                    $renderer->doc .= '<div id="' . $id . '" class="siteexport mergehintwrapper"><aside class="mergehint">' . $title . '</aside>' . $lastDiv;
100c1198967SGerry Weißbach                } else {
101c1198967SGerry Weißbach
102c1198967SGerry Weißbach                    // check if anything was inserted. We have to strip all tags,
103c1198967SGerry Weißbach                    // we're just looking for real content here
104c1198967SGerry Weißbach                    $lastPos = strrpos($renderer->doc, $lastDiv);
105c1198967SGerry Weißbach                    if ( $lastPos !== false ) {
106c1198967SGerry Weißbach
107c1198967SGerry Weißbach                        $remaining = substr( $renderer->doc, $lastPos + strlen($lastDiv) );
108c1198967SGerry Weißbach                        $remaining = strip_tags( $remaining );
109c1198967SGerry Weißbach                        $remaining = trim($remaining);
110c1198967SGerry Weißbach                        if ( strlen( $remaining ) == 0 ) {
111c1198967SGerry Weißbach                            // empty
112c1198967SGerry Weißbach                            $lastPos = strrpos($renderer->doc, $startHint);
113c1198967SGerry Weißbach                            $renderer->doc = substr($renderer->doc, 0, $lastPos);
114*3a388851SGerry Weißbach                            return true;
115*3a388851SGerry Weißbach                        }
116*3a388851SGerry Weißbach                    }
117*3a388851SGerry Weißbach
11806a81c5cSGerry Weißbach                    $renderer->doc .= '</div></div>';
119300ce4a2SGerry Weißbach                    $renderer->doc .= '<!-- MergeHint End for "' . $title . '" -->';
120300ce4a2SGerry Weißbach                }
121300ce4a2SGerry Weißbach            } else {
122300ce4a2SGerry Weißbach                $renderer->doc .= "<br style=\"page-break-after:always;\" />";
123300ce4a2SGerry Weißbach            }
124300ce4a2SGerry Weißbach            return true;
125644c76feSGerry Weißbach        } else if ( $mode = 'markdown' && $type == 'mergehint' && $pos == 'start' ) {
126644c76feSGerry Weißbach            $renderer->doc .= DOKU_LF . '##### ' . $title . DOKU_LF;
127644c76feSGerry Weißbach            return true;
128300ce4a2SGerry Weißbach        }
129300ce4a2SGerry Weißbach        return false;
130300ce4a2SGerry Weißbach    }
131300ce4a2SGerry Weißbach}
132300ce4a2SGerry Weißbach
133300ce4a2SGerry Weißbach//Setup VIM: ex: et ts=4 enc=utf-8 :