-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathImageMapHandler.php
192 lines (173 loc) · 5.9 KB
/
ImageMapHandler.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
<?php
namespace dokuwiki\plugin\imagemapping;
use dokuwiki\Parsing\Handler\CallWriterInterface;
/**
* Custom CallWriter for the imagemapping plugin
*/
class ImageMapHandler implements CallWriterInterface
{
/** @var CallWriterInterface the parent call writer */
public $CallWriter;
/** @var array the local call stack */
protected $calls = [];
/** @var string Name of this imagemap FIXME currenlty unsused?*/
private $mapname;
/**
* Constructor
*
* @param CallWriterInterface $CallWriter the "parent" call writer
*/
public function __construct($mapname, CallWriterInterface $CallWriter)
{
$this->mapname = $mapname;
$this->CallWriter = $CallWriter;
}
/** @inheritdoc */
public function writeCall($call)
{
$this->calls[] = $call;
}
/** @inheritdoc */
public function writeCalls($calls)
{
$this->calls = array_merge($this->calls, $calls);
}
/** @inheritdoc */
public function finalise()
{
$last_call = end($this->calls);
$this->process();
$this->addPluginCall([DOKU_LEXER_EXIT], $last_call[2]);
$this->CallWriter->finalise(); // FIXME do we really need to call it?
}
/**
* Get the parent call writer
*
* @return CallWriterInterface
*/
public function getCallWriter()
{
return $this->CallWriter;
}
/**
* Process the local call stack
*
* @return void
*/
public function process()
{
$last_call = end($this->calls);
$first_call = array_shift($this->calls);
$this->CallWriter->writeCall($first_call);
$this->processLinks($first_call[2]);
if (!empty($this->calls)) {
$this->addPluginCall([DOKU_LEXER_MATCHED, 'divstart'], $first_call[2]);
//Force a new paragraph
$this->CallWriter->writeCall(['eol', [], $this->calls[0][2]]);
$this->CallWriter->writeCalls($this->calls);
$this->addPluginCall([DOKU_LEXER_MATCHED, 'divend'], $last_call[2]);
}
}
/**
* Adds a call to the imagemap plugin with the given data
*
* The syntax component will be called with the given data and handle the various sub modes
*
* @param array $args [DOKU_LEXER_*, 'submode', params...]
* @param int $pos
* @return void
*/
protected function addPluginCall($args, $pos)
{
$this->CallWriter->writeCall(['plugin', ['imagemapping', $args, $args[0]], $pos]);
}
/**
* Add a new area call to the call stack
*
* @param int $pos The position in the source
* @param string $type The type of the link (internallink, externallink, ...)
* @param string $title The link title including the coordinates
* @param string $url The link part of the link
* @param string $wiki The interwiki identifier for interwiki links
* @return string The title without the coordinates
*/
protected function addArea($pos, $type, $title, $url, $wiki = null)
{
if (preg_match('/^(.*)@([^@]+)$/u', $title, $match)) {
$coords = explode(',', $match[2]);
if (count($coords) == 3) {
$shape = 'circle';
} elseif (count($coords) == 4) {
$shape = 'rect';
} elseif (count($coords) >= 6) {
$shape = 'poly';
} else {
return $title;
}
$coords = array_map('trim', $coords);
$title = trim($match[1]);
$coords = join(',', $coords);
$coords = trim($coords);
$this->addPluginCall(
[DOKU_LEXER_MATCHED, 'area', $shape, $coords, $type, $title, $url, $wiki],
$pos
);
}
return $title;
}
/**
* Walk through the call stack and process all links
*
* This will add the imagemap areas to the call stack and remove the coordinates from the link titles
*
* @todo simplify more and add tests
* @param int $pos The source position
* @return void
*/
protected function processLinks($pos)
{
for ($n = 0; $n < count($this->calls); $n++) {
$data =& $this->calls[$n][1];
$type = $this->calls[$n][0];
switch ($type) {
case 'plugin':
// support for other plugins that use the imagemap syntax (e.g. the popupviewer plugin)
$plugin = plugin_load('syntax', $data[0]);
if ($plugin != null && method_exists($plugin, 'convertToImageMapArea')) {
$plugin->convertToImageMapArea($this, $data[1], $pos);
}
break;
case 'internallink':
case 'locallink':
case 'externallink':
case 'emaillink':
case 'windowssharelink':
if (is_array($data[1])) {
$title = $data[1]['title'] ?? '';
} else {
$title = $data[1];
}
$title = $this->addArea($pos, $type, $title, $data[0]);
if (is_array($data[1])) {
$data[1]['title'] = $title;
} else {
$data[1] = $title;
}
break;
case 'interwikilink':
if (is_array($data[1])) {
$title = $data[1]['title'];
} else {
$title = $data[1];
}
$title = $this->addArea($pos, $type, $title, $data[3], $data[2]);
if (is_array($data[1])) {
$data[1]['title'] = $title;
} else {
$data[1] = $title;
}
break;
}
}
}
}