-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaction.php
More file actions
134 lines (111 loc) · 3.75 KB
/
Copy pathaction.php
File metadata and controls
134 lines (111 loc) · 3.75 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
<?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]);
$tableAlign = preg_match('/[<>]+/', $width[0]) == 1 ? array_shift($width) : '-';
$tableWidth = array_shift($width);
if ($tableWidth != '-' || $tableAlign != '-') {
$table = $this->styleTable($data[2][0], $tableWidth, $tableAlign);
}
else {
$table = $data[2][0];
}
return $table . $this->renderColumns($width) . $data[3][0];
}
/**
* Add width and align styles to the table
*/
private function styleTable($html, $width, $align) {
preg_match('/^([^\n]*<table)(.*?)(>)$/', $html, $match);
$entry = $match[1];
$attributes = $match[2];
$exit = $match[3];
$widthStyle = $this->getTableWidthStyle($width);
$alignStyle = $this->getTableAlignStyle($align);
$tableStyle = implode(' ', array_filter([$widthStyle, $alignStyle]));
if (preg_match('/(.*?style\s*=\s*(["\']).*?)(\2.*)/', $attributes, $match) == 1) {
$attributes = $match[1] . '; ' . $tableStyle . $match[3];
}
else {
$attributes .= ' style="' . $tableStyle . '"';
}
return $entry . $attributes . $exit;
}
/**
* Return table width style
*/
private function getTableWidthStyle($width) {
if ($width != '-') {
return 'min-width: 0; width: ' . $width . ';';
}
return '';
}
/**
* Return table align style
*/
private function getTableAlignStyle($align) {
switch ($align) {
case '><':
return 'margin-left: auto; margin-right: auto;';
case '>':
return 'margin-left: auto; margin-right: 0;';
case '<':
return 'margin-left: 0; margin-right: auto;';
}
return '';
}
/**
* Render column tags
*/
private function renderColumns($width) {
$html = DOKU_LF;
if (!empty($width)) {
$html .= '<colgroup>';
foreach ($width as $w) {
if ($w != '-') {
$html .= '<col style="width: ' . $w . '" />';
}
else {
$html .= '<col />';
}
}
$html .= '</colgroup>';
}
return $html;
}
}