-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.php
417 lines (377 loc) · 13.8 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/**
* Plugin monthcal: Display monthly calendar
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Milosz Galazka <[email protected]>
*/
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
require_once (DOKU_INC . 'inc/html.php');
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_monthcal extends DokuWiki_Syntax_Plugin {
/**
* Get the type of syntax this plugin defines.
*
*/
function getType(){
return 'substition';
}
/**
* Define how this plugin is handled regarding paragraphs.
*
*/
function getPType(){
return 'block';
}
/**
* Where to sort in?
*
* Doku_Parser_Mode_html 190
* --> execute here <--
* Doku_Parser_Mode_code 200
*/
function getSort(){
return 199;
}
/**
* Connect lookup pattern to lexer.
*
*/
function connectTo($mode) {
$this->Lexer->addSpecialPattern('{{monthcal.*?}}',$mode,'plugin_monthcal');
}
/**
* Handler to prepare matched data for the rendering process.
*
*/
function handle($match, $state, $pos, Doku_Handler $handler){
$data = array();
// get page info
$INFO = pageinfo();
// default vaues
$data['month'] = date('m');
$data['year'] = date('Y');
$data['namespace'] = $INFO['namespace'];
$data['week_start_on'] = 0;
$data['display_weeks'] = 1;
$data['create_links_on_days'] = 0;
$data['create_links_on_weeks'] = 1;
$data['do_not_create_past_links'] = 1;
$data['mark_today'] = 1;
$data['borders'] = 1;
$data['align'] = 0;
$provided_data = substr($match, 11, -2);
$arguments = explode (',', $provided_data);
foreach ($arguments as $argument) {
list($key, $value) = explode('=', $argument);
switch($key) {
case 'year':
$data['year'] = $value;
break;
case 'month':
$data['month'] = $value;
break;
case 'namespace':
$data['namespace'] = (strpos($value, ':') === false) ? ':' . $value : $value;
break;
case 'week_start_on':
if (strtolower($value) == "sunday")
$data['week_start_on'] = 1;
else
$data['week_start_on'] = 0;
break;
case 'display_weeks':
switch(strtolower($value)) {
case 'no':
$data['display_weeks'] = 0;
break;
default:
$data['display_weeks'] = 1;
break;
}
break;
case 'create_links_on_days':
switch(strtolower($value)) {
case 'no':
$data['create_links_on_days'] = 0;
break;
case 'local':
$data['create_links_on_days'] = 2;
break;
default:
$data['create_links_on_days'] = 1;
break;
}
break;
case 'create_links_on_weeks':
switch(strtolower($value)) {
case 'no':
$data['create_links_on_weeks'] = 0;
break;
case 'local':
$data['create_links_on_weeks'] = 2;
break;
default:
$data['create_links_on_weeks'] = 1;
break;
}
break;
case 'do_not_create_past_links':
switch(strtolower($value)) {
case 'no':
$data['do_not_create_past_links'] = 0;
break;
default:
$data['do_not_create_past_links'] = 1;
break;
}
break;
case 'mark_today':
switch(strtolower($value)) {
case 'no':
$data['mark_today'] = 0;
break;
default:
$data['mark_today'] = 1;
break;
}
break;
case 'borders':
switch(strtolower($value)) {
case 'all':
$data['borders'] = 1;
break;
case 'table':
$data['borders'] = 2;
break;
default:
$data['borders'] = 0;
break;
}
break;
case 'align':
switch(strtolower($value)) {
case 'left':
$data['align'] = 1;
break;
case 'right':
$data['align'] = 2;
break;
default:
$data['align'] = 0;
break;
}
break;
}
}
return $data;
}
/**
* Handle the actual output creation.
*
*/
function render($mode, Doku_Renderer $renderer, $data) {
if ($mode == 'xhtml'){
$renderer->doc .= $this->create_calendar($data);
return true;
}
return false;
}
/**
* Create calendar
*
*/
function create_calendar($data) {
// date today
$date_today = new DateTime();
// date yesterday
$date_yesterday = (new DateTime($date_today->format('Y-m-d')))->modify('-1 day');
// date: from -> to
$date_from = new DateTime($data['year'] . "-" . $data['month'] . "-01");
$date_to = (new DateTime($date_from->format('Y-m-d')))->modify('+1 month');
// date prev/next month
$date_prev_month = (new DateTime($date_from->format('Y-m-d')))->modify('-1 month');
$date_next_month = $date_to; //(new DateTime($date_to->format('Y-m-d')))->modify('+1 month');
$date_interval = new DateInterval('P1D');
$date_range = new DatePeriod($date_from, $date_interval, $date_to);
// first day in on ...
$date_from_on_weekday = $date_from->format('N');
// language specific
$weekdays = $this->getLang('monthcal_weekdays');
$months = $this->getLang('monthcal_months');
// weekday variable which is used inside each loop
$wday = 1;
// move by one element to the right if week starts at Sunday
if ($data['week_start_on'] == 1) {
if ($date_from_on_weekday <= 6)
$date_from_on_weekday += 1;
else
$date_from_on_weekday = 1;
}
// border css
switch($data['align']) {
case 1:
$css_align = 'left';
break;
case 2:
$css_align = 'right';
break;
default:
$css_align = '';
break;
}
// border css
switch($data['borders']) {
case 1:
$css_table_border = 'withborder';
$css_td_border = 'withborder';
break;
case 2:
$css_table_border = 'withborder';
$css_td_border = 'borderless';
break;
case 0:
$css_table_border = 'borderless';
$css_td_border = 'borderless';
break;
}
// html code
$html = '<table class="monthcal ' . $css_table_border . ' ' . $css_align . '">';
// colspan for month/year
$colspan_month = 4;
if ($data['display_weeks'] == '1') {
$colspan_year= 4;
} else {
$colspan_year= 3;
}
// header
$html .= '<tr class="description">';
$html .= '<td class="month ' . $css_td_border . '" colspan="' . $colspan_month . '">' . $months[$date_from->format('m')-1] . ' ';
$html .= '</td>';
$html .= '<td class="year ' . $css_td_border . '" colspan="' . $colspan_year . '">' . $date_from->format('Y') . '</td></tr>';
// swap weekdays if week starts at Sunday
if ($data['week_start_on'] == 1) { $weekdays=array($weekdays[6],$weekdays[0],$weekdays[1],$weekdays[2],$weekdays[3],$weekdays[4],$weekdays[5]);}
// append empty header for week numbers
if ($data['display_weeks'] == '1') {
array_unshift($weekdays,$this->getLang('monthcal_week'));
}
// weekdays
$html .= '<tr>';
foreach($weekdays as $weekday) {
$html .= '<th class="' . $css_td_border . '">' . $weekday . '</th>';
}
$html .= '</tr>';
$html .= '<tr>';
// initial week number
if ($data['display_weeks'] == '1') {
$date = $date_from;
if ($data['create_links_on_weeks'] == '1' ) {
if ($conf['lang'] == 'zh') {
setlocale(LC_TIME, 'zh_CN.UTF-8');
}
$date_1st = (new DateTime($date->format('Ymd')))->modify('this week');
$date_7nd = (new DateTime($date->format('Ymd')))->modify('this week +6 days');
$pagename = strftime($this->getLang('monthcal_week_link'), strtotime($date->format('Ymd'))).'-'.$date_1st->format('m.d').'-'.$date_7nd->format('m.d');
$pagelink = $data['namespace'] . ':' . $pagename;
if (($data['do_not_create_past_links'] == '1') and ($date_7nd->format('Ymd') < $date_today->format('Ymd'))) {
$page_exists = null;
resolve_pageid($data['namespace'], $pagename, $page_exists);
if ($page_exists) {
$html_week = html_wikilink($pagelink, $date->format('W'));
} else {
$html_week = $date->format('W');
}
} else {
$html_week = html_wikilink($pagelink, $date->format('W'));
}
} else if ($data['create_links_on_weeks'] == '2' ) {
$html_week = '<a href="#section' . $pagename . '">' . $date->format('W') . '</a>';
} else {
$html_week = $date->format('W');
}
$html .= '<td class="' . $css_td_border . '">' . $html_week . '</td>';
}
// first empty days
if ($date_from_on_weekday > 1) {
for($wday;$wday < $date_from_on_weekday;$wday++) {
$html .= '<td class="' . $css_td_border . '"></td>';
}
}
// month days
foreach($date_range as $date) {
if ($wday > 7) {
$wday = 1;
$html .= "</tr>";
$html .= "<tr>";
if ($data['display_weeks'] == '1') {
if ($data['create_links_on_weeks'] == '1' ) {
if ($conf['lang'] == 'zh') {
setlocale(LC_TIME, 'zh_CN.UTF-8');
}
$date_1st = (new DateTime($date->format('Ymd')))->modify('this week');
$date_7nd = (new DateTime($date->format('Ymd')))->modify('this week +6 days');
$pagename = strftime($this->getLang('monthcal_week_link'), strtotime($date->format('Ymd'))).'-'.$date_1st->format('m.d').'-'.$date_7nd->format('m.d');
$pagelink = $data['namespace'] . ':' . $pagename;
if (($data['do_not_create_past_links'] == '1') and ($date_7nd->format('Ymd') < $date_today->format('Ymd'))) {
$page_exists = null;
resolve_pageid($data['namespace'], $pagename, $page_exists);
if ($page_exists) {
$html_week = html_wikilink($pagelink, $date->format('W'));
} else {
$html_week = $date->format('W');
}
} else {
$html_week = html_wikilink($pagelink, $date->format('W'));
}
} else if ($data['create_links_on_weeks'] == '2' ) {
$html_week = '<a href="#section' . $pagename . '">' . $date->format('W') . '</a>';
} else {
$html_week = $date->format('W');
}
$html .= '<td class="' . $css_td_border . '">' . $html_week . '</td>';
}
}
if ($date->format('Ymd') == $date_today->format('Ymd') and $data['mark_today'] == 1)
$css_today='today';
else
$css_today='';
if ($data['create_links_on_days'] == '1' ) {
$pagename = $date->format('Y-m-d');
$pagelink = $data['namespace'] . ':' . $pagename;
if (($data['do_not_create_past_links'] == '1') and ($date->format('Ymd') < $date_today->format('Ymd'))) {
$page_exists = null;
resolve_pageid($data['namespace'], $pagename, $page_exists);
if ($page_exists) {
$html_day = html_wikilink($pagelink, $date->format('d'));
} else {
$html_day = $date->format('d');
}
} else {
$html_day = html_wikilink($pagelink, $date->format('d'));
}
} else if ($data['create_links_on_days'] == '2' ) {
$html_day = '<a href="#section' . $pagename . '">' . $date->format('d') . '</a>';
} else {
$html_day = $date->format('d');
}
$html .= '<td class="' . $css_td_border . ' ' . $css_today . '">' . $html_day . '</td>';
$wday++;
}
// last empty days
if ($wday < 8) {
for($wday;$wday<8;$wday++) {
$html .= '<td class="' . $css_td_border . '"></td>';
}
}
// close table
$html .= '</table>';
// return table
return $html;
}
}
?>