1<?php
2
3if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
4if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
5require_once(DOKU_PLUGIN.'syntax.php');
6
7/**
8 * All DokuWiki plugins to extend the parser/rendering mechanism
9 * need to inherit from this class
10 */
11class syntax_plugin_revealjs_fragmentblock extends DokuWiki_Syntax_Plugin {
12
13    public function getType() { return 'substition'; }
14    public function getSort() { return 32; }
15    public function getPType() { return 'block'; }
16
17
18    /**
19     * Connect lookup pattern to lexer.
20     *
21     * @param $aMode String The desired rendermode.
22     * @return none
23     * @public
24     * @see render()
25     */
26    public function connectTo($mode) {
27        $this->Lexer->addSpecialPattern('<\/?(?:no-)?fragment-(?:block|list)\b.*?>', $mode, 'plugin_revealjs_fragmentblock');
28    }
29
30
31    /**
32     * Handler to prepare matched data for the rendering process.
33     *
34     * @param $aMatch String The text matched by the patterns.
35     * @param $aState Integer The lexer state for the match.
36     * @param $aPos Integer The character position of the matched text.
37     * @param $aHandler Object Reference to the Doku_Handler object.
38     * @return Integer The current lexer state for the match.
39     * @public
40     * @see render()
41     * @static
42     */
43    public function handle($match, $state, $pos, Doku_Handler $handler) {
44        list($type, $param1, $param2) = preg_split("/\s+/", substr($match, 1, -1), 3);
45        if ($param1) {
46            if ($this->_is_valid_style($param1)) $style = $param1;
47            elseif (intval($param1) > 0) $index = $param1;
48        }
49        if ($param2) {
50            if (intval($param2) > 0) $index = $param2;
51            elseif ($this->_is_valid_style($param2)) $style = $param2;
52        }
53        return array($type, $style, $index);
54    }
55
56
57    /**
58     * Handle the actual output creation.
59     *
60     * @param $aFormat String The output format to generate.
61     * @param $aRenderer Object A reference to the renderer object.
62     * @param $aData Array The data created by the <tt>handle()</tt>
63     * method.
64     * @return Boolean <tt>TRUE</tt> if rendered successfully, or
65     * <tt>FALSE</tt> otherwise.
66     * @public
67     * @see handle()
68     */
69    public function render($mode, Doku_Renderer $renderer, $data) {
70        if($mode == 'xhtml' && is_a($renderer, 'renderer_plugin_revealjs')) {
71            list($type, $style, $index) = $data;
72            switch ($type) {
73                case 'fragment-block' :
74                    $renderer->doc .= DOKU_LF.'<div class="fragment' . ($style ? ' '.$style : '') . '"' .
75                        ($index ? ' data-fragment-index="'.$index.'"' : '') . '>';
76                    break;
77                case '/fragment-block' :
78                    $renderer->doc .= '</div>'.DOKU_LF;
79                    break;
80                case 'fragment-list' :
81                    $renderer->fragment_list_open = true;
82                    $renderer->fragment_style = $style;
83                    break;
84                case '/fragment-list' :
85                    $renderer->fragment_list_open = false;
86                    $renderer->fragment_style = '';
87                    break;
88                case 'no-fragment-list' :
89                    $renderer->no_fragment_list_open = true;
90                    break;
91                case '/no-fragment-list' :
92                    $renderer->no_fragment_list_open = false;
93                    break;
94            }
95            return true;
96        }
97        return false;
98    }
99
100
101    /**
102     * Validate fragment style: $style
103     */
104    private function _is_valid_style($style) {
105        $pattern = '/^(?:grow|shrink|fade-(?:in|out|up|down|left|right)|current-visible|highlight(?:-current)?-(?:red|green|blue))$/';
106        if (preg_match($pattern, $style)) return $style;
107        return '';
108    }
109
110}
111