1<?php
2// must be run within Dokuwiki
3if (!defined('DOKU_INC')) die();
4if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
5require_once(DOKU_PLUGIN.'syntax.php');
6
7class syntax_plugin_tablefilterjs extends DokuWiki_Syntax_Plugin {
8
9  function getType() { return 'container';}
10  function getPType(){ return 'block';}
11  function getSort() { return 500; }
12  function getAllowedTypes() {return array('container','formatting','substition');}
13	function connectTo($mode) {
14    $this->Lexer->addEntryPattern('<filter[^>]*>(?=.*?</filter>)',$mode,'plugin_tablefilterjs');
15  }
16  function postConnect() {
17    $this->Lexer->addExitPattern('</filter>','plugin_tablefilterjs');
18  }
19  function __filter_data($str)
20  {
21	$filter_data = array();
22
23	$key = '';
24
25	//0 no 1
26	$in_str = 0;
27	//0 header 1 regex
28	$state = 0;
29	for( $i = 0; $i < strlen( $str ); $i++ )
30	{
31	    if($state == 0)
32	    {
33		if( $str[$i] == '=' )
34		{
35		    $state = 1;
36
37		    while( $str[$i] != '/'  )
38		    {
39			$i++;
40			if( $i == strlen( $str ) )
41			{
42			    return $filter_data;
43			}
44		    }
45
46		    //$filter_data[ trim($key) ] .= $str[ $i ];
47
48		} else
49		{
50
51		    if( $in_str == 1 && $str[$i] == '\\' )
52		    {
53			$i++;
54			$key .= $str[ $i ];
55			continue;
56		    }
57
58		    if( $str[$i] == '"' || $str[$i] == "'")
59		    {
60			if( $in_str == 1 )
61			    $in_str = 0;
62			else
63			    $in_str = 1;
64			continue;
65		    }
66
67		    $key .= $str[ $i ];
68		}
69
70	    } else
71	    {
72		if( $str[$i] == '/' )
73		{
74		    $i++;
75		    $state = 0;
76		    while( $str[$i] != ' ' && $i < strlen( $str ) )
77		    {
78			$filter_data[ trim($key) ][1] .= $str[ $i ];
79			$i++;
80			if( $i == strlen( $str ) )
81			{
82			    return $filter_data;
83			}
84		    }
85		    $key = '';
86		    continue;
87		}
88		$filter_data[ trim($key) ][0] .= $str[ $i ];
89	    }
90	}
91	return $filter_data;
92  }
93  function __encodeHTML($str)
94  {
95      return str_replace(array('"', '\'', '&', '<'), array('&quot;', '&#39;', '&amp;', '&lt;'), $str);
96  }
97  function handle($match, $state, $pos, Doku_Handler $handler){
98
99    switch ($state) {
100      case DOKU_LEXER_ENTER :
101        $match = substr($match,8,-1);
102
103        return array($state, $this->__filter_data($match));
104        break;
105      case DOKU_LEXER_UNMATCHED :
106        return array($state, $match);
107        break;
108      case DOKU_LEXER_EXIT :
109        return array($state, "");
110        break;
111    }
112    return array();
113  }
114
115  function render($mode, Doku_Renderer $renderer, $data) {
116    list($state,$match) = $data;
117    if ($mode == 'xhtml'){
118      switch ($state) {
119        case DOKU_LEXER_ENTER :
120	  $json = new JSON();
121          $renderer->doc .= '<div class="tablefilterjs" data-filters="'.$this->__encodeHTML($json->encode($match)).'">';
122          break;
123	case DOKU_LEXER_UNMATCHED :
124          $instructions = p_get_instructions($match);
125          foreach ($instructions as $instruction) {
126            call_user_func_array(array(&$renderer, $instruction[0]),$instruction[1]);
127          }
128	break;
129        case DOKU_LEXER_EXIT :
130          $renderer->doc .=  "</div>";
131          break;
132      }
133      return true;
134    }
135  }
136}
137