-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsyntax.php
More file actions
209 lines (181 loc) · 6.52 KB
/
Copy pathsyntax.php
File metadata and controls
209 lines (181 loc) · 6.52 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* DokuWiki Plugin slider (Syntax Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <gohr@cosmocode.de>
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
class syntax_plugin_slider extends DokuWiki_Syntax_Plugin {
private $inslideopener = false;
/**
* @return string Syntax mode type
*/
public function getType() {
return 'formatting';
}
/**
* @return array Things that may be inside the syntax
*/
function getAllowedTypes() {
return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
}
/**
* @return string Paragraph type
*/
public function getPType() {
return 'stack';
}
/**
* @return int Sort order - Low numbers go before high numbers
*/
public function getSort() {
return 190;
}
/**
* Connect lookup pattern to lexer.
*
* @param string $mode Parser mode
*/
public function connectTo($mode) {
$this->Lexer->addEntryPattern('<slider.*?>(?=.*?</slider>)',$mode,'plugin_slider');
}
public function postConnect() {
$this->Lexer->addExitPattern('</slider>', 'plugin_slider');
}
/**
* Handle matches of the slider 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 $conf;
$first = false;
$img = '';
// the enter state is the very first slide only
if($state == DOKU_LEXER_ENTER){
$first = true;
}
// handle intermediate slider calls just like new entry patterns
if($state == DOKU_LEXER_UNMATCHED && substr($match, 0, 7) == '<slider'){
$state = DOKU_LEXER_ENTER;
}
// handle unclosed slide opener, by getting the image url from the last call
if($this->inslideopener && $state == DOKU_LEXER_UNMATCHED && substr($match, 0, 7) == '>'){
$lastcall = array_pop($handler->calls);
if(preg_match('/^https?:\/\//i', $lastcall[1][0])) $img = $lastcall[1][0];
$state = DOKU_LEXER_ENTER;
$this->inslideopener = false;
}
// handle states
switch ($state) {
case DOKU_LEXER_ENTER:
if(substr($match, -1) != '>'){
// we have a slide opener, but it contained some other syntax (probably a link)
// happens because we parse our own syntax from LEXER_UNMATCHED calls
$this->inslideopener = true;
return false;
}elseif(!$img){
$img = trim(substr($match, 7,-1));
}
return array($state, $img, $first);
case DOKU_LEXER_UNMATCHED:
// check if $match is a == header ==
$headerMatch = preg_grep('/([ \t]*={2,}[^\n]+={2,}[ \t]*(?=))/msSi', array($match));
if (empty($headerMatch)) {
$handler->_addCall('cdata', array($match), $pos);
} else {
// if it's a == header ==, use the core header() renderer
// (copied from core header() in inc/parser/handler.php)
$title = trim($match);
$level = 7 - strspn($title,'=');
if($level < 1) $level = 1;
$title = trim($title,'=');
$title = trim($title);
$handler->_addCall('header',array($title,$level,$pos), $pos);
// close the section edit the header could open
if ($title && $level <= $conf['maxseclevel']) {
$handler->addPluginCall('wrap_closesection', array(), DOKU_LEXER_SPECIAL, $pos, '');
}
}
return false;
case DOKU_LEXER_EXIT:
return array($state, false, false);
}
}
/**
* Render xhtml output or metadata
*
* @param string $mode Renderer mode (supported modes: xhtml)
* @param Doku_Renderer $R The renderer
* @param array $data The data from the handler() function
* @return bool If rendering was successful.
*/
public function render($mode, Doku_Renderer $R, $data) {
if($mode != 'xhtml') return false;
list($state, $img, $first) = $data;
if($state == DOKU_LEXER_ENTER){
if($first){
// open the list
$R->doc .= '<div class="clearer"></div>';
$R->doc .= '<ul class="plugin_slider">';
}else{
// close previous item
$this->close_slide($R);
}
// new item
$this->open_slide($R, $img);
}elseif($state == DOKU_LEXER_EXIT){
// close previous item
$this->close_slide($R);
// close list
$R->doc .= '</ul>';
$R->doc .= '<div class="clearer"></div>';
}
return true;
}
/**
* Opens as slide and remounts the renderer document
*
* @param Doku_Renderer $R
* @param string $img
*/
private function open_slide($R, $img){
// open the list item
if($img){
$R->doc .= '<li class="plugin_slider_hasimg">';
$R->doc .= '<img src="'.ml($img, array('w'=>$this->getConf('width'))).'" class="plugin_slider_img" alt="" />';
}else{
$R->doc .= '<li class="plugin_slider_noimg">';
}
// remount the doc
$R->keepdoc = $R->doc;
$R->doc = '';
}
/**
* Closes the previous slide and removes dangling paragraphs and empty content divs
*
* @param Doku_Renderer $R
*/
private function close_slide($R){
// clean up dangling paragraphs
$R->doc = preg_replace('/<p>\s*$/','',$R->doc);
$R->doc = preg_replace('/^\s*<\/p>\s*/','',$R->doc);
$R->doc = trim($R->doc);
// wrap content if any
if($R->doc){
$R->doc = '<div class="plugin_slider_content li">'.$R->doc.'</div>';
}
// mount doument back
$R->doc = $R->keepdoc . $R->doc;
unset($R->keepdoc);
// close item
$R->doc .= '</li>';
}
}
// vim:ts=4:sw=4:et: