-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.php
More file actions
99 lines (81 loc) · 2.72 KB
/
action.php
File metadata and controls
99 lines (81 loc) · 2.72 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
<?php
/**
* Plugin TableWidth
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Mykola Ostrovskyy <dwpforge@gmail.com>
*/
class action_plugin_tablewidth extends DokuWiki_Action_Plugin {
/**
* Register callbacks
*/
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('RENDERER_CONTENT_POSTPROCESS', 'AFTER', $this, 'replaceComments');
}
/**
* Replace table-width comments by HTML
*/
public function replaceComments(&$event, $param) {
if ($event->data[0] == 'xhtml') {
$pattern = '/(<!-- table-width [^\n]+? -->\n)([^\n]*<table.*?>)(\s*<t)/';
$flags = PREG_SET_ORDER | PREG_OFFSET_CAPTURE;
if (preg_match_all($pattern, $event->data[1], $match, $flags) > 0) {
$start = 0;
$html = '';
foreach ($match as $data) {
$html .= substr($event->data[1], $start, $data[0][1] - $start);
$html .= $this->processTable($data);
$start = $data[0][1] + strlen($data[0][0]);
}
$event->data[1] = $html . substr($event->data[1], $start);;
}
}
}
/**
* Convert table-width comments and table mark-up into final HTML
*/
private function processTable($data) {
preg_match('/<!-- table-width ([^\n]+?) -->/', $data[1][0], $match);
$width = preg_split('/\s+/', $match[1]);
$tableWidth = array_shift($width);
if ($tableWidth != '-') {
$table = $this->styleTable($data[2][0], $tableWidth);
}
else {
$table = $data[2][0];
}
return $table . $this->renderColumns($width) . $data[3][0];
}
/**
* Add width style to the table
*/
private function styleTable($html, $width) {
preg_match('/^([^\n]*<table)(.*?)(>)$/', $html, $match);
$entry = $match[1];
$attributes = $match[2];
$exit = $match[3];
$widthStyle = 'min-width: 0px; width: ' . $width . ';';
if (preg_match('/(.*?style\s*=\s*(["\']).*?)(\2.*)/', $attributes, $match) == 1) {
$attributes = $match[1] . '; ' . $widthStyle . $match[3];
}
else {
$attributes .= ' style="' . $widthStyle . '"';
}
return $entry . $attributes . $exit;
}
/**
* Render column tags
*/
private function renderColumns($width) {
$html = DOKU_LF;
foreach ($width as $w) {
if ($w != '-') {
$html .= '<col style="width: ' . $w . '" />';
}
else {
$html .= '<col />';
}
}
return $html;
}
}