Folding Sections Plugin

Here is my folding section plugin. I am still working on this, so any feedback is appreciated.

When you do a search and go to a page that has folded sections, not only do the sections not unfold nor highlight inside the section, but they won't unfold. I know where the problem is, but I am trying to find a way to fix it using the plugin facilities.

I want to find a way to automatically close out the last section at the end of a page, like other heading levels do. Right now you have to close out the last section or your page layout and XHTML compliance is broken.

To use this plugin created the directory foldsec inside the plugins directory. Next create a file named syntax.php and copy the code below into it. So, you have a file lib/plugins/foldsec/syntax.php containing the text below.

<?php
/**
 * Name: lib/plugins/foldsec/syntax.php (version 0.2)
 *
 * FoldSec Plugin: Gives the ability to create folding sections
 *
 * Usage: ======== Title 01========
 *        line 01
 *        line 02
 *        ======== Title 02 ========
 *        line 01
 *        line 02
 *        ~~FOLD:END~~
 *
 * Note: The ~~FOLD:END~~ only has to appear at the end of the last folding section
 * 
 * @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.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
class syntax_plugin_foldsec extends DokuWiki_Syntax_Plugin {
 
    function getInfo(){
        return array(
            'author' => 'Chris Stoll',
            'email'  => 'stollcri [at] gmail [dot] com',
            'date'   => '2005-06-09',
            'name'   => 'Folding Sections',
            'desc'   => 'Allows section to fold and unfold. (Sometimes also called collpsible headings.)',
            'url'    => 'http://www.dokuwiki.org/plugin:folding_sections',
        );
    }
 
    function getType(){
        return 'baseonly';
    }
 
    function getPType(){
        return 'block';
    }
 
    function getSort(){
        // parse this before the other headers
        return 45;
    }
 
    function connectTo($mode) {
        // match between eight consecutive equal signs
        $this->Lexer->addSpecialPattern('[ \t]*={8,}[^\n]+={2,}[ \t]*(?=\n)',$mode,'plugin_foldsec');
        // match between two consecutive tildes
        $this->Lexer->addSpecialPattern('[ \t]*~{2,}[^\n]+~{2,}[ \t]*(?=\n)',$mode,'plugin_foldsec');
    }
 
    function handle($match, $state, $pos, &$handler){
        // strip off the tags
        if (strpos($match,'========')===false) {
            $match = trim(substr(trim($match),2,-2));
            $process = false;
        } else {
            $match = trim(substr(trim($match),8,-8));
            $process = true;
        }
        return array($pos,$match,$process);
    }
 
    function render($mode, &$renderer, $data) {
        if($mode == 'xhtml'){
            $pos=cleanID($data[0]);
            $title=cleanID($data[1]);
            $process=$data[2];
 
            if (!$process) {
                if ($data[1]=='FOLD:END' && !$renderer->info['foldsecend']) {
                    $renderer->info['foldsecend']=true;
                    $renderer->doc .= '</p>'.DOKU_LF; // for XML/XHTML compliance
                    $renderer->doc .= '</div>'.DOKU_LF;
                    $renderer->doc .= '<p>'.DOKU_LF; // for XML/XHTML compliance
                }
                return true;
            }
 
            if ($renderer->info['foldsec']) {
                // close out the previos section, unless it was already closed
                if (!$renderer->info['foldsecend']) {
                    $renderer->doc .= '</p>'.DOKU_LF; // for XML/XHTML compliance
                    $renderer->doc .= '</div>'.DOKU_LF;
                }
            } else {
                // add only if this is the first folding section
                //
                // I left these in here so that I can simply distribute one file
                // and it would cause less problems for inexperienced users.
                // Just drop in this file and it should work. - stollcri
                //
                $renderer->doc .= '</p>'.DOKU_LF; // for XML/XHTML compliance
                $renderer->doc .= DOKU_LF;
                $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;
                $renderer->doc .= DOKU_LF;
                $renderer->doc .= '<style type="text/css">'.DOKU_LF;
                $renderer->doc .= '  h6          {cursor:pointer; color:#436976; font-size:120%; font-weight:bold; margin-left:40px; margin-top:0px; margin-bottom:5px; padding-top:0.5em; border-bottom:1px solid #8cacbb;}'.DOKU_LF;
                $renderer->doc .= '  h6:hover    {color:#FF9933; text-decoration:none;}'.DOKU_LF;
                $renderer->doc .= '  div.foldsec {color:black; background-color:#DEE7EC; margin-left:45px; margin-top:5px; margin-bottom:0px; padding-left:12px;}'.DOKU_LF;
                $renderer->doc .= '</style>'.DOKU_LF;
                $renderer->doc .= DOKU_LF;
            }
            $renderer->doc .= '<a name="'.$title.'"></a>'.DOKU_LF;
            $renderer->doc .= '<h6 onclick="fold(\''.$title.'_'.$pos.'\');">';
            $renderer->doc .= $data[1];
            $renderer->doc .= '</h6>'.DOKU_LF;
            $renderer->doc .= '<div class="foldsec" id="'.$title.'_'.$pos.'" style="display:none";>'.DOKU_LF;
            $renderer->doc .= '<p>'.DOKU_LF; // for XML/XHTML compliance
 
            $renderer->info['foldsecend'] = false;
            $renderer->info['foldsec'] = true;
            return true;
        }
        return false;
    }
}
 
//Setup VIM: ex: et ts=4 enc=utf-8 :

Comment

Hi, I get a issue with this plugin. So that other plugins which have a syntax with ~~ like ~~LASTMOD~~ doesn't work after the folding selection plugin was installed.

The reason of it was following line:
>$this->Lexer->addSpecialPattern('[ \t]*~{2,}[^\n]+~{2,}[ \t]*(?=\n)',$mode,'plugin_foldsec');
>
So I changed it to this and now all plugins works.
>$this->Lexer->addSpecialPattern('~~FOLD:END~~',$mode,'plugin_foldsec');
>


I hope this hint helps other with this problem. Anja Vag 2005-10-21 15:48

 
plugin/folding_sections.txt · Last modified: 2005/10/21 15:50 by asoahc
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Imprint Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsGitXRefTranslate