-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.php
172 lines (153 loc) · 4.72 KB
/
syntax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* DokuWiki Plugin imagecarousel (Syntax Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author lisps
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
class syntax_plugin_imagecarousel extends DokuWiki_Syntax_Plugin {
/**
* @return string Syntax mode type
*/
public function getType() {
return 'container';
}
/**
* @return string Paragraph type
*/
public function getPType() {
return 'block';
}
/**
* @return int Sort order - Low numbers go before high numbers
*/
public function getSort() {
return 104;
}
public function getAllowedTypes() {
return array('substition');
}
/**
* Connect lookup pattern to lexer.
*
* @param string $mode Parser mode
*/
public function connectTo($mode) {
// $this->Lexer->addSpecialPattern('<carousel>.*?</carousel>',$mode,'plugin_imagecarousel');
$this->Lexer->addEntryPattern('<carousel.*?>',$mode,'plugin_imagecarousel');
// $this->Lexer->addSpecialPattern("\{\{[^\}]+\}\}",$mode,'media');
}
public function postConnect() {
$this->Lexer->addPattern('\n {2,}[\*]','plugin_imagecarousel');
$this->Lexer->addExitPattern('</carousel>','plugin_imagecarousel');
}
protected $first_item = [];
/**
* Handle matches of the imagecarousel syntax
*
* @param string $match The match of the syntax
* @param int $state The state of the handler
* @param int $pos The position in the document
* @param Doku_Handler $handler The handler
* @return array Data for the renderer
*/
public function handle($match, $state, $pos, Doku_Handler $handler){
global $ID;
$data = array();
$data['state'] = $state;
switch($state) {
case DOKU_LEXER_ENTER:
$match = substr($match, 9,-1); // strip markup
$flags = $this->parseFlags($match);
$flags['slick'] = json_encode(array_merge(
$this->getDefaultFlags(),
$flags['slick']
));
$data['flags'] = $flags;
break;
case DOKU_LEXER_UNMATCHED:
if (trim($match) !== '') {
$handler->_addCall('cdata', array($match), $pos);
}
break;
case DOKU_LEXER_EXIT:
$this->first_item[$ID] = false;
break;
case DOKU_LEXER_MATCHED:
if($match === "\n *") {
if(!isset($this->first_item[$ID]) || !$this->first_item[$ID]) {
$this->first_item[$ID] = true;
$data['first_item'] = true;
}
}
break;
}
return $data;
}
/**
* Render xhtml output or metadata
*
* @param string $mode Renderer mode (supported modes: xhtml)
* @param Doku_Renderer $renderer The renderer
* @param array $data The data from the handler() function
* @return bool If rendering was successful.
*/
public function render($mode, Doku_Renderer $renderer, $data) {
if($mode != 'xhtml') return false;
if($data['state'] === DOKU_LEXER_ENTER) {
$width = ' style="width:'.hsc($data['flags']['self']['width']).'" ';
$renderer->doc .= '<div class="slick '.$data['flags']['self']['position'].'" data-slick=\''.$data['flags']['slick'].'\' '.$width.'>';
$renderer->doc .= '<div>';
} else if($data['state'] === DOKU_LEXER_EXIT) {
$renderer->doc .= '</div></div>';
} else if($data['state'] === DOKU_LEXER_MATCHED) {
if(!isset($data['first_item'])) {
$renderer->doc .= '</div>';
$renderer->doc .= '<div>';
}
}
return true;
}
protected function getDefaultFlags() {
$conf = $this->getConf('default');
return $this->parseFlags($conf);
}
protected function parseFlags($confString) {
$confString = explode('&',$confString);
$flags = array(
'slick' => array(),
'self' => array(
'position' => 'center',
'width' => '100%'
),
);
foreach($confString as $flag) {
switch($flag) {
case 'center':
$flags['self']['position'] = 'center';
break;
case 'left':
$flags['self']['position'] = 'left';
break;
}
$tmp = explode('=',$flag,2);
if(count($tmp) === 2) {
switch($tmp[0]) {
case 'width':
$flags['self']['width'] = $tmp[1];
break;
default: //slick parameter
if($tmp[1] === "true") $tmp[1] = true;
else if($tmp[1] === "false") $tmp[1] = false;
else if(is_numeric($tmp[1])) $tmp[1] = intval($tmp[1]);
$flags['slick'][$tmp[0]] = $tmp[1];
break;
}
}
}
return $flags;
}
}
// vim:ts=4:sw=4:et: