xref: /plugin/sortablejs/syntax.php (revision 5e7b4f663ac127b2ac1b8519a1abcec12ef26ee9)
1<?php
2/**
3 based on code from http://www.kryogenix.org/code/browser/sorttable/ by Stuart Langridge
4 (distributed under the conditions of MIT licence from http://www.kryogenix.org/code/browser/licence.html)
5 maintaned by:
6 2007-2016 by oiv (Otto Vainio at otto@valjakko.net)
7 2016-? by vaxquis AKA FyiurAmron (spamove@gmail.com)
8 */
9// must be run within Dokuwiki
10if ( !defined( 'DOKU_INC' ) )
11    die();
12if ( !defined( 'DOKU_PLUGIN' ) )
13    define( 'DOKU_PLUGIN', DOKU_INC.'lib/plugins/' );
14require_once(DOKU_PLUGIN.'syntax.php');
15//
16class syntax_plugin_sortablejs extends DokuWiki_Syntax_Plugin {
17
18    function getType() {
19        return 'container';
20    }
21
22    function getPType() {
23        return 'block';
24    }
25
26    function getSort() {
27        return 371;
28    }
29
30    function getAllowedTypes() {
31        return array( 'container', 'formatting', 'substition' );
32    }
33
34    function connectTo( $mode ) {
35        $this->Lexer->addEntryPattern( '<sortable[^>]*>(?=.*?</sortable>)', $mode, 'plugin_sortablejs' );
36    }
37
38    function postConnect() {
39        $this->Lexer->addExitPattern( '</sortable>', 'plugin_sortablejs' );
40    }
41
42    function handle( $match, $state, $pos, Doku_Handler $handler ) {
43
44        switch ( $state ) {
45            case DOKU_LEXER_ENTER :
46                $match = substr( $match, 9, -1 );
47                $match = trim( $match );
48                $scl = "";
49                if ( strlen( $match ) > 0 ) {
50                    $scl = $this->__validateOptions( $match );
51                }
52                return array( $state, $scl );
53            case DOKU_LEXER_UNMATCHED :
54                return array( $state, $match );
55            case DOKU_LEXER_EXIT :
56                return array( $state, "" );
57        }
58        return array();
59    }
60
61    function render( $mode, Doku_Renderer $renderer, $data ) {
62        list($state, $match) = $data;
63        if ( $mode == 'xhtml' ) {
64            switch ( $state ) {
65                case DOKU_LEXER_ENTER :
66                    $renderer->doc .= "<div class=\"sortable$match\">";
67                    break;
68                case DOKU_LEXER_UNMATCHED :
69                    $instructions = p_get_instructions( $match );
70                    foreach( $instructions as $instruction ) {
71                        call_user_func_array( array( &$renderer, $instruction[0] ), $instruction[1] );
72                    }
73
74                    break;
75                case DOKU_LEXER_EXIT :
76                    $renderer->doc .= "</div>";
77                    break;
78            }
79            return true;
80        } else if ( $mode == 'odt' ) {
81            switch ( $state ) {
82                case DOKU_LEXER_ENTER :
83                    // In ODT, tables must not be inside a paragraph. Make sure we
84                    // closed any opened paragraph
85                    $renderer->p_close();
86                    break;
87                case DOKU_LEXER_UNMATCHED :
88                    $instructions = array_slice( p_get_instructions( $match ), 1, -1 );
89                    foreach( $instructions as $instruction ) {
90                        call_user_func_array( array( &$renderer, $instruction[0] ), $instruction[1] );
91                    }
92                    break;
93                case DOKU_LEXER_EXIT :
94                    //$renderer->p_open();
95                    // DO NOT re-open the paragraph, it would cause an error if the table is the last content on a page
96                    break;
97            }
98            return true;
99        }
100        return false;
101    }
102
103    function __validateOptions( $opts ) {
104        $oa = explode( " ", $opts );
105        $ret = "";
106        foreach( $oa as $opt ) {
107            list($c, $v) = explode( "=", $opt );
108            if ( $c == "sumrow" ) {
109                $c = $v;
110                $v = "sumrow";
111                if ( $c == "" ) {
112                    $c = "1";
113                }
114            } else if ( $c == "3phase" ) {
115                $v = $c;
116                $c = "";
117            }
118            if ( $v != null ) {
119                $cmpr = $v;
120            } else {
121                if ( preg_match( '/r?\d*/', $c, $matches ) ) {
122                    $cmpr = 'sort';
123                }
124            }
125            switch ( $cmpr ) {
126                case '3phase':
127                    $ret .= " threephase";
128                    break;
129                case 'nosort':
130                    $ret .= " col_".$c."_nosort";
131                    break;
132                case 'numeric':
133                    $ret .= " col_".$c."_numeric";
134                    break;
135                case 'date':
136                    $ret .= " col_".$c."_date";
137                    break;
138                case 'alpha':
139                case 'text':
140                    $ret .= " col_".$c."_alpha";
141                    break;
142                case 'sort':
143                    $ret .= ' sort'.$opt;
144                    break;
145                case 'sumrow':
146                    $ret .= ' sortbottom_'.$c;
147                    //$ret = ' sortbottom' . $ret;
148                    break;
149            }
150        }
151        return $ret;
152    }
153}
154