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_span extends DokuWiki_Syntax_Plugin {
16
17    /** @var helper_plugin_folded */
18    protected $helper = null;
19
20    function getType(){ return 'formatting'; }
21    function getAllowedTypes() { return array('substition','protected','disabled','formatting'); }
22    function getSort(){ return 405; }
23    function connectTo($mode) { $this->Lexer->addEntryPattern('\+\+.*?\|(?=.*\+\+)',$mode,'plugin_folded_span'); }
24    function postConnect() { $this->Lexer->addExitPattern('\+\+','plugin_folded_span'); }
25
26   /**
27    * Handle the match
28    */
29    function handle($match, $state, $pos, Doku_Handler $handler){
30        if ($state == DOKU_LEXER_ENTER){
31            $match = trim(substr($match,2,-1)); // strip markup
32        } else if ($state == DOKU_LEXER_UNMATCHED) {
33            $handler->addCall('cdata',array($match), $pos);
34            return false;
35        }
36        return array($state, $match);
37    }
38
39   /**
40    * Create output
41    */
42    function render($mode, Doku_Renderer $renderer, $data) {
43        if (empty($data)) return false;
44        list($state, $cdata) = $data;
45
46        if($mode == 'xhtml') {
47            switch ($state){
48               case DOKU_LEXER_ENTER:
49                if ($this->helper === null) {
50                    $this->helper = plugin_load('helper', 'folded');
51                }
52                $folded_id = $this->helper->getNextID();
53
54                if ($this->getConf('unfold_default')) {
55                    $renderer->doc .= '<a class="folder open" href="#'.$folded_id.'">';
56                } else {
57                    $renderer->doc .= '<a class="folder" href="#'.$folded_id.'">';
58                }
59
60                if ($cdata)
61                    $renderer->doc .= ' '.$renderer->cdata($cdata);
62
63                if ($this->getConf('unfold_default')) {
64                    $renderer->doc .= '</a><span class="folded" id="'.$folded_id.'">';
65                } else {
66                    $renderer->doc .= '</a><span class="folded hidden" id="'.$folded_id.'">';
67                }
68                break;
69
70              case DOKU_LEXER_UNMATCHED:
71                $renderer->cdata($cdata);
72                break;
73
74              case DOKU_LEXER_EXIT:
75                $renderer->doc .= '</span>';
76                break;
77            }
78            return true;
79        } else {
80            if ($cdata) $renderer->cdata($cdata);
81        }
82        return false;
83    }
84}
85