1<?php
2/**
3 * Folded text Plugin: enables folded text font size with syntax ++ text ++
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Fabian van-de-l_Isle <webmaster [at] lajzar [dot] co [dot] uk>
7 * @author     Christopher Smith <chris@jalakai.co.uk>
8 * @author     Esther Brunner <esther@kaffeehaus.ch>
9 */
10
11/**
12 * All DokuWiki plugins to extend the parser/rendering mechanism
13 * need to inherit from this class
14 */
15class syntax_plugin_folded_div extends DokuWiki_Syntax_Plugin {
16    protected $helper = null;
17
18    function getType(){ return 'container'; }
19    function getPType() { return 'stack'; }
20    function getAllowedTypes() { return array('container','substition','protected','disabled','paragraphs','formatting'); }
21    function getSort(){ return 404; }
22    function connectTo($mode) { $this->Lexer->addEntryPattern('\+\+\+\+.*?\|(?=.*\+\+\+\+)',$mode,'plugin_folded_div'); }
23    function postConnect() { $this->Lexer->addExitPattern('\+\+\+\+','plugin_folded_div'); }
24
25   /**
26    * Handle the match
27    */
28    function handle($match, $state, $pos, Doku_Handler $handler){
29        if ($state == DOKU_LEXER_ENTER){
30            $match = trim(substr($match,4,-1)); // strip markup
31        } else if ($state == DOKU_LEXER_UNMATCHED) {
32            $handler->addCall('cdata',array($match), $pos);
33            return false;
34        }
35        return array($state, $match);
36    }
37
38   /**
39    * Create output
40    */
41    function render($mode, Doku_Renderer $renderer, $data) {
42        if (empty($data)) return false;
43        list($state, $cdata) = $data;
44
45        if($mode == 'xhtml') {
46            switch ($state){
47              case DOKU_LEXER_ENTER:
48                if ($this->helper === null) {
49                    $this->helper = plugin_load('helper', 'folded');
50                }
51                $folded_id = $this->helper->getNextID();
52
53                if ($this->getConf('unfold_default')) {
54                    $renderer->doc .= '<p><a class="folder open" href="#'.$folded_id.'">';
55                } else {
56                    $renderer->doc .= '<p><a class="folder" href="#'.$folded_id.'">';
57                }
58
59                if ($cdata)
60                    $renderer->doc .= ' '.$renderer->cdata($cdata);
61
62                if ($this->getConf('unfold_default')) {
63                    $renderer->doc .= '</a></p><div class="folded" id="'.$folded_id.'">';
64                } else {
65                    $renderer->doc .= '</a></p><div class="folded hidden" id="'.$folded_id.'">';
66                }
67                break;
68
69              case DOKU_LEXER_UNMATCHED:                            // defensive, shouldn't occur
70                $renderer->cdata($cdata);
71                break;
72
73              case DOKU_LEXER_EXIT:
74                $renderer->doc .= '</div>';
75                break;
76            }
77            return true;
78        } else {
79            //  handle unknown formats generically - by calling standard render methods
80            switch ( $state ) {
81               case DOKU_LEXER_ENTER:
82                $renderer->p_open();
83                $renderer->cdata($cdata);
84                $renderer->p_close();
85                break;
86              case DOKU_LEXER_UNMATCHED:                            // defensive, shouldn't occur
87                $renderer->cdata($cdata);
88                break;
89              case DOKU_LEXER_EXIT:
90                break;
91            }
92        }
93        return false;
94    }
95}
96