-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDiagram.php
367 lines (344 loc) · 9.57 KB
/
Diagram.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
<?php
/*
* Diagram
*
* NOTE: DO NOT INCLUDE THIS DIRECTLY VIA LocalSettings.php. This is NOT the main extension
* file. Include DFDiagram.php instead.
*/
require_once 'Char.php';
require_once 'Color.php';
require_once 'Grid.php';
require_once 'VarDict.php';
function DFDParseTokens($string){
/*
* Takes a string and returns an array of tokens (e.g. Color tags, tiles, etc.)
*/
// Convert to UTF-8
$string = mb_convert_encoding($string, 'UTF-8');
// True when inside a tag ([...])
$in_tag = false;
// Index where current tag begins
$tag_start = 0;
$tokens = array();
for ($index = 0; $index < strlen($string); $index++) {
$char = mb_substr($string, $index, 1, 'UTF-8');
if (!mb_strlen($char)) {
// mb_ functions sometimes end up with empty characters
continue;
}
if ($char == "[" && !$in_tag) {
// starts a tag
$in_tag = true;
$tag_start = $index;
}
elseif ($char == "]" && $in_tag) {
// closes a tag
$in_tag = false;
// Use the substring from $tag_start to the current character (INCLUSIVE) as the token
$tokens[] = mb_substr($string, $tag_start, $index - $tag_start + 1);
}
else if (!$in_tag) {
$tokens[] = $char;
}
}
return $tokens;
}
class DFDTable {
/**
* Represents the table created by a diagram
*/
private $text;
private $opts;
private $fg;
private $bg;
private $lines;
private $grid;
private $vars;
public function __construct($text, $a_opts) {
// Opt-independent stuff
$this->vars = new VarDict();
// Default options
$opts = array(
'fg' => '7:1',
'bg' => '0:0'
);
foreach($opts as $key => $val){
if(array_key_exists($key, $a_opts)){
$opts[$key] = $a_opts[$key];
}
}
$this->text = $text;
$this->opts = $opts;
$this->fg = $opts['fg'];
$this->bg = $opts['bg'];
$this->setUp();
}
public function setUp(){
/*
* Set up table
*/
$fgcolor = $this->fg;
$bgcolor = $this->bg;
// Parse variables first - otherwise, characters won't be displayed properly
$var_tags = array();
preg_match_all('/\[\$[^]]+\]/', $this->text, $var_tags);
if (count($var_tags) && $var_tags[0] != null) {
foreach ($var_tags[0] as $i => $tag) {
// preg escaped
$esc_tag = '\[\$' . substr($tag, 2, -1) . '\]';
if (preg_match('/=/', $tag)) {
// Assignment
$parts = preg_split('/=/', $tag, 2);
$vname = substr($parts[0], 2);
$val = substr($parts[1], 0, -1);
$this->vars->set($vname, $val);
$this->text = preg_replace('/' . $esc_tag . '/', '', $this->text);
}
else {
// variable name
$vname = substr($tag, 2, -1);
// Replace 1 occurence of the tag (therefore the first occurence) with the variable's value
$this->text = preg_replace('/\[\$' . $vname . '\]/', $this->vars->get($vname), $this->text, 1);
}
}
}
// Clean up blank lines
$this->text = preg_replace('/\n+/', "\n", $this->text);
// And possible lines at the beginning
$this->text = preg_replace('/^\n+/', '', $this->text);
// Set up grid
$this->grid = new DGrid();
$this->lines = preg_split('/\n/', $this->text);
// Parse tokens
$this->tokens = array();
for ($row = 0; $row < count($this->lines); $row++) {
$this->tokens[$row] = DFDParseTokens($this->lines[$row]);
}
for ($row = 0; $row < count($this->tokens); $row++) {
$tokens = $this->tokens[$row];
$col = -1;
for ($i = 0; $i < count($tokens); $i++) {
$token = $tokens[$i];
if(mb_strlen($token) == 1){
// Character
$col++;
$cell = new DFDTableCell($token, $fgcolor, $bgcolor);
$this->grid->set($row, $col, $cell);
}
else {
// tag
if ($token == '[#]') {
// Reset foreground color
$fgcolor = $this->fg;
}
elseif ($token == '[@]') {
// Reset bg color
$bgcolor = $this->bg;
}
elseif ($token == '[#@]' || $token == '[@#]') {
// Reset fg and bg
$bgcolor = $this->bg;
$fgcolor = $this->fg;
}
elseif ($token[1] == '#') {
// Set fg color
$fgcolor = substr($token, 2, strlen($token) - 3);
}
elseif ($token[1] == '@') {
// Set bg color
$bgcolor = substr($token, 2, strlen($token) - 3);
}
elseif ($token[1] == '%') {
// Character
$col++;
$char = new Char(substr($token, 2, strlen($token) - 3));
$cell = new DFDTableCell($char->text, $fgcolor, $bgcolor);
$this->grid->set($row, $col, $cell);
}
}
}
}
}
public function render(){
$html = "\n<table>\n";
for($r = 0; $r < $this->grid->height; $r++) {
$html .= "\t<tr>";
// Initial bg/fg colors
$fg = $this->fg;
$bg = $this->bg;
for ($c = 0; $c < $this->grid->width; $c++) {
$cell = $this->grid->get($r, $c);
if($cell === false){
// No cell exists at this row/col; create a blank black cell
$cell = new DFDTableCell(' ', "$fg", "$bg");
}
else {
// Set fg/bg to current cell's colors
$fg = $cell->fg;
$bg = $cell->bg;
}
$html .= "<td>{$cell->render()}</td>";
}
$html .= "</tr>\n";
}
$html .= "</table>";
return $html;
}
}
class DFDTableCell {
/**
* An individual cell in a DFDTable
*
* @property Color $fg The cell's foreground color
* @property Color $bg The cell's background color
* @property string $text The cell's text (should be one character, but this is not enforced)
*/
public $text;
public $fg;
public $bg;
public function __construct($text, $fg, $bg) {
$this->text = $text;
$this->fg = new Color($fg);
$this->bg = new Color($bg);
}
public function render(){
$char = $this->text;
// is a non-breaking space; without it, the cell doesn't align with other cells properly.
if($char == ' '){
$char = ' ';
}
return "<span style=\"display:block; color:{$this->fg}; background-color:{$this->bg};\">{$char}</span>";
}
}
class DFDiagram {
/**
* Diagram wrapper
*
* Note that this class uses DFDTable to render the body of the diagram.
*/
// A DFDTable
private $table;
public static function parseXMLAttrs($string) {
$string = $string . ' ';
$attrString = preg_replace('/<\w+\s*([^>]*)>/', "$1", $string);
$attrString = preg_replace('/(\w+?)=([^"\'\s]+)\s+/', "$1=\"$2\" ", $attrString);
$attrString = str_replace("'", '"', $attrString);
$attrs = array();
$current = "";
$state = 'name';
$attrName = '';
for ($i = 0; $i < mb_strlen($string); $i++) {
$char = mb_substr($attrString, $i, 1);
if ($char == '"') {
if ($state == 'name') {
$state = 'value';
$attrName = $current;
}
elseif ($state == 'value') {
$state = 'name';
$attrs[$attrName] = $current;
}
$current = '';
continue;
}
if (preg_match('/\s/', $char) && $state == 'name') {
continue;
}
if ($char == '=' && $state == 'name') {
continue;
}
$current .= $char;
}
return $attrs;
}
public static function XMLAttrsToDataAttrs($attrs) {
$text = '';
foreach ($attrs as $name => $value) {
$text .= "data-$name=\"$value\" ";
}
return $text;
}
public function __construct($text, $opts) {
// Initialize the table with the provided text and options (no processing here)
//$this->table = new DFDTable($text, $opts);
if (!array_key_exists('display', $opts))
$opts['display'] = 'block';
$opts['display'] = preg_replace('/[^A-Za-z-]/', '', $opts['display']);
$this->opts = $opts;
$this->tables = array();
$frames = array();
if (!preg_match_all('/\n*<(frame[^>]*)>([\s\S]*?)<\/frame>\n*/', $text, $frames, PREG_SET_ORDER)) {
// no frames explicitly specified; fall back to a single frame
$frames[0] = array("<frame>$text</frame>", 'frame', $text); // simulates a regex match
}
foreach ($frames as $f) {
$frameText = $f[2];
$table = new DFDTable($frameText, $opts);
$table->frameOptions = DFDiagram::parseXMLAttrs("<{$f[1]}>");
$this->tables[] = $table;
}
}
public function render() {
// Render the rendered table, wrapped with render()
//return $this->format($this->table->render());
$text = '';
foreach ($this->tables as $table) {
$text .= '<div class="dfdiagram-frame" ' . DFDiagram::XMLAttrsToDataAttrs($table->frameOptions)
. '>' . $table->render() . '</div>';
}
return $this->format($text);
}
public function format($html) {
$style = "";
if ($this->opts['display'] != 'block')
$style .= "display: {$this->opts['display']};";
return <<< HTML
<div class="dfdiagram" style="$style">
$html
</div>
HTML;
}
}
class DFDMWHooks {
/**
* Hooks for integrating DFDiagram extension functionality with MediaWiki
*/
static public function init($parser) {
// Register the <diagram> tag
$parser->setHook('diagram', 'DFDMWHooks::create');
return true;
}
static public function create($text, $args, $parser, $frame) {
// HTML-style ignoring of whitespace
if(preg_match('/\S/', $text) === 0){ // no match
// Include the default diagram
global $wgDFDDefaultDiagramPath;
$text = file_get_contents($wgDFDDefaultDiagramPath);
}
// Remove leading newlines
$text = preg_replace('/^\n+/', '', $text);
// Create new DFDiagram
$diagram = new DFDiagram($text, $args);
return $diagram->render();
}
static public function includeModules($outPage, $skin) {
/*
* Include the resources in $wgResourceModules
*/
$user = $skin->getContext()->getUser();
$outPage->addModuleStyles(array('ext.DFDiagram'));
if($user->getOption('dfdiagram-use-canvas')) {
$outPage->addModules('ext.DFDiagram.canvas');
}
return true;
}
static public function getPreferences($user, &$preferences) {
// Create preferences
$preferences['dfdiagram-use-canvas'] = array(
'type' => 'toggle',
'label-message' => 'dfdiagram-use-canvas',
'section' => 'dfdiagram/dfdiagram-canvas'
);
return true;
}
}