-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathoutput.php
220 lines (191 loc) · 6.54 KB
/
output.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
<?php
/**
* DokuWiki Plugin struct (Syntax Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr, Michael Große <[email protected]>
*/
use dokuwiki\Extension\SyntaxPlugin;
use dokuwiki\Extension\Event;
use dokuwiki\plugin\struct\meta\AccessTable;
use dokuwiki\plugin\struct\meta\Assignments;
use dokuwiki\plugin\struct\meta\StructException;
class syntax_plugin_struct_output extends SyntaxPlugin
{
protected $hasBeenRendered = array('metadata'=>false, 'xhtml'=>false);
protected const XHTML_OPEN = '<div id="plugin__struct_output">';
protected const XHTML_CLOSE = '</div>';
/**
* Regexp to check on which actions the struct data may be rendered
*/
protected const WHITELIST_ACTIONS = '/^(show|export_.*)$/';
/**
* @return string Syntax mode type
*/
public function getType()
{
return 'substition';
}
/**
* @return string Paragraph type
*/
public function getPType()
{
return 'block';
}
/**
* @return int Sort order - Low numbers go before high numbers
*/
public function getSort()
{
return 155;
}
/**
* Connect lookup pattern to lexer.
*
* We do not connect any pattern here, because the call to this plugin is not
* triggered from syntax but our action component
*
* @asee action_plugin_struct_output
* @param string $mode Parser mode
*/
public function connectTo($mode)
{
}
/**
* Handle matches of the struct 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)
{
// this is never called
return [];
}
/**
* Render schema data
*
* Currently completely renderer agnostic
*
* @param string $format Renderer format
* @param Doku_Renderer $renderer The renderer
* @param array $data The data from the handler() function
* @return bool If rendering was successful.
*/
public function render($format, Doku_Renderer $renderer, $data)
{
global $ACT;
global $ID;
global $INFO;
global $REV;
foreach (helper_plugin_struct::BLACKLIST_RENDERER as $blacklisted) {
if ($renderer instanceof $blacklisted) {
return true;
}
}
if (!isset($INFO) || $format == "metadata") {
$pagename = pageinfo()['id'];
} else {
$pagename = $INFO['id'];
}
if ($ID != $pagename) return true;
if (!page_exists($pagename)) return true;
if ($this->hasBeenRendered['metadata'] && $format == 'metadata') return true;
if ($this->hasBeenRendered['xhtml'] && $format == 'xhtml') return true;
if (!preg_match(self::WHITELIST_ACTIONS, act_clean($ACT))) return true;
// do not render the output twice on the same page, e.g. when another page has been included
if ($format == 'metadata') {
$this->hasBeenRendered['metadata'] = true;
}
else if ($format == 'xhtml') {
$this->hasBeenRendered['xhtml'] = true;
}
try {
$assignments = Assignments::getInstance();
} catch (StructException $e) {
return false;
}
$tables = $assignments->getPageAssignments($ID);
if (!$tables) return true;
if ($format == 'xhtml') $renderer->doc .= self::XHTML_OPEN;
$hasdata = false;
foreach ($tables as $table) {
try {
$schemadata = AccessTable::getPageAccess($table, $ID, (int)$REV);
} catch (StructException $ignored) {
continue; // no such schema at this revision
}
$rendercontext = [
'renderer' => $renderer,
'format' => $format,
'meta' => p_get_metadata($ID),
'schemadata' => $schemadata,
'hasdata' => &$hasdata
];
$event = new Event(
'PLUGIN_STRUCT_RENDER_SCHEMA_DATA',
$rendercontext
);
$event->trigger([$this, 'renderSchemaData']);
}
if ($format == 'xhtml') $renderer->doc .= self::XHTML_CLOSE;
// if no data has been output, remove empty wrapper again
if ($format == 'xhtml' && !$hasdata) {
$renderer->doc = substr($renderer->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE));
}
return true;
}
/**
* Default schema data rendering (simple table view)
*
* @param array The render context including renderer and data
*/
public function renderSchemaData($rendercontext)
{
$schemadata = $rendercontext['schemadata'];
$renderer = $rendercontext['renderer'];
$format = $rendercontext['format'];
$schemadata->optionSkipEmpty(true);
$data = $schemadata->getData();
if (!count($data))
return;
$rendercontext['hasdata'] = true;
if ($format == 'xhtml') {
$renderer->doc .= '<div class="struct_output_' . $schemadata->getSchema()->getTable() . '">';
}
$renderer->table_open();
$renderer->tablethead_open();
$renderer->tablerow_open();
$renderer->tableheader_open(2);
$renderer->cdata($schemadata->getSchema()->getTranslatedLabel());
$renderer->tableheader_close();
$renderer->tablerow_close();
$renderer->tablethead_close();
$renderer->tabletbody_open();
foreach ($data as $field) {
$renderer->tablerow_open();
$renderer->tableheader_open();
$renderer->cdata($field->getColumn()->getTranslatedLabel());
$renderer->tableheader_close();
$renderer->tablecell_open();
if ($format == 'xhtml') {
$renderer->doc = substr($renderer->doc, 0, -1) .
' data-struct="' . hsc($field->getColumn()->getFullQualifiedLabel()) .
'">';
}
$field->render($renderer, $format);
$renderer->tablecell_close();
$renderer->tablerow_close();
}
$renderer->tabletbody_close();
$renderer->table_close();
if ($format == 'xhtml') {
$renderer->doc .= '</div>';
}
}
}
// vim:ts=4:sw=4:et: