[dokuwiki] My first syntax plugin

Here is my first attempt at a syntax plugin, it is to add
folding/collapsing section support. It is almost done, but I need
ideas on how I can add a final </div> tag when there are no more
sections to fold.

-Chris

<?php
/**
 * FoldSec Plugin: Gives the ability to create folding sections
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Chris Stoll <stollcri [at] gmail [dot] com>
 */

if(!defined('DOKU_INC'))
define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'inc/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');

class syntax_plugin_foldsec extends DokuWiki_Syntax_Plugin {

    function getType(){
        return 'baseonly';
    }
   
    function getSort(){
        return 45;
    }

    function connectTo($mode) {
        $this->Lexer->addSpecialPattern('[ \t]*={8,}[^\n]+={2,}[
\t]*(?=\n)',$mode,'plugin_foldsec');
    }

    function handle($match, $state, $pos, &$handler){
        $match = substr($match,8,-8);
        return array($pos,$match);
    }

    function render($mode, &$renderer, $data) {
        if($mode == 'xhtml'){
            $pos=cleanID($data[0]);
            $title=cleanID($data[1]);
            
            if ($renderer->info['foldsec']) {
                $renderer->doc .= '</div>';
            }
            
            // ----- Move to external .js
            $renderer->doc .= '<script type="text/javascript">'.DOKU_LF;
            $renderer->doc .= 'function fold( divid ) {'.DOKU_LF;
            $renderer->doc .= '  var divstyle =
document.getElementById(divid).style;'.DOKU_LF;
            $renderer->doc .= '  var showhide = (divstyle.display ==
\'none\')?\'block\':\'none\';'.DOKU_LF;
            $renderer->doc .= '  divstyle.display = showhide;'.DOKU_LF;
            $renderer->doc .= '}'.DOKU_LF;
            $renderer->doc .= '</script>'.DOKU_LF;
            // ----- End move
            
            $renderer->doc .= '<a name="'.$title.'"></a>'.DOKU_LF;
            $renderer->doc .= '<h6 onclick="fold(\''.$title.'_'.$pos.'\');">';
            $renderer->doc .= $title;
            $renderer->doc .= '</h6>'.DOKU_LF;
            $renderer->doc .= '<div class="foldsec"
id="'.$title.'_'.$pos.'" style="display:none";>'.DOKU_LF;
            $renderer->info['foldsec'] = true;
            return true;
        }
        return false;
    }
}

//Setup VIM: ex: et ts=4 enc=utf-8 :
--
DokuWiki mailing list - more info at
http://wiki.splitbrain.org/wiki:mailinglist

Other related posts: