forked from wikiLingo/wikiLingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WikiLingoWYSIWYG.php
337 lines (300 loc) · 9.82 KB
/
WikiLingoWYSIWYG.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
<?php
class WikiLingoWYSIWYG extends WikiLingo
{
/* wiki syntax type tracking */
static $typeIndex = array();
static $typeShorthandName = array();
static $typeShorthand = array(
"preFormattedText" => "pp",
"bold" => "b",
"box" => "bx",
"center" => "c",
"noParse" => "np",
"code" => "cd",
"color" => "clr",
"italic" => "i",
"l2r" => "l2r",
"r2l" => "r2l",
"header" => "hdr",
"horizontalRow" => "hr",
"listParent" => "lp",
"listUnordered" => "lu",
"listOrdered" => "lh",
"listToggleUnordered" => "ltu",
"listToggleOrdered" => "lto",
"listBreak" => "lb",
"listDefinitionParent" => "ldp",
"listDefinition" => "ld",
"listDefinitionDescription" => "ldd",
"listEmpty" => "le",
"line" => "ln",
"forcedLineEnd" => "fln",
"unlink" => "ul",
"link" => "l",
"linkWord" => "lw",
"linkNp" => "lnp",
"linkExternal" => "el",
"wikiLink" => "wl",
"strike" => "stk",
"doubleDash" => "dd",
"table" => "t",
"tableBody" => "tbd",
"tableRow" => "tr",
"tableData" => "td",
"titleBar" => "tb",
"underscore" => "u",
"comment" => "cm",
"plugin" => "pl",
);
public static function staticConstruct()
{
if (empty(self::$typeShorthandName)) {
foreach (self::$typeShorthand as $type => $shorthand) {
self::$typeShorthandName[$shorthand] = $type;
}
}
}
//end state handlers
//Wiki Syntax Objects Parsing Start
/**
* syntax handler: noparse, ~np~$content~/np~
*
* @access public
* @param $content string parsed string found inside detected syntax
* @return string $content desired output from syntax
*/
public function noParse($content)
{
return $this->createWikiTag(
"noParse",
"span",
parent::noParse($content),
array(
"class" => "noParse"
)
);
}
/**
* Handles plugins directly from the wiki parser. A plugin can be on a different level of the current parser, and
* if so, the execution is delayed until the parser reaches that level.
*
* @access private
* @param array &$pluginDetails plugins details in an array
* @return string either returns $key or block from execution message
*/
public function plugin(&$name, &$parameters, &$end = null, &$body = null)
{
$plugin = parent::plugin($name, $parameters, $end, $body);
$plugin->attributes["data-syntax"] = rawurlencode($plugin->syntax);
$plugin->attributes["data-body"] = rawurlencode($plugin->bodySyntax);
$plugin->attributes["data-parameters"] = rawurlencode(json_encode($plugin->parameters));
$plugin->attributes["data-name"] = $plugin->name;
$plugin->attributes["contenteditable"] = "false";
$plugin->attributes["data-t"] = self::$typeShorthand["plugin"];
return $plugin;
}
/**
* syntax handler: double dynamic variable, %%$content%%
*
* @access public
* @param $content string parsed string found inside detected syntax
* @return string $content desired output from syntax
*/
function doubleDynamicVar($content)
{
return "%%" . $content . "%%";
}
/**
* syntax handler: single dynamic variable, %$content%
*
* @access public
* @param $content string parsed string found inside detected syntax
* @return string $content desired output from syntax
*/
function singleDynamicVar($content)
{
return "%" . $content . "%";
}
/**
* syntax handler: unlink, [[$content|$content]]
*
* @access public
* @param $content parsed string found inside detected syntax
* @return string $content desired output from syntax
*/
function unlink($content) //[[content|content]
{
$contentLength = strlen($content);
if ($content[$contentLength - 3] == "@" &&
$content[$contentLength - 2] == "n" &&
$content[$contentLength - 1] == "p"
) {
$content = substr($content, 0, -3);
}
return $this->createWikiTag("unlink", "span", $content);
}
/**
* syntax handler: link, [$content|$content], ((Page)), ((Page|$content)), (type(Page)), (type(Page|$content)), ((external:Page)), ((external:Page|$content))
*
* @access public
* @param $type string type, np, wiki, alias (or whatever is "(here(", word
* @param $content string found inside detected syntax
* @return string $content desired output from syntax
*/
function link($type, $page, $description = '') //[content|content]
{
if ($type == 'word') {
return $page;
}
return parent::link($type, $page, $description, true);
}
/**
* syntax handler: tiki comment, ~tc~$content~/tc~
*
* @access public
* @param $content parsed string found inside detected syntax
* @return string $content desired output from syntax
*/
function comment($content)
{
return $this->createWikiTag(
"comment",
"span",
substr($content, 4, -5),
array(
"class" => "wikiComment"
)
);
}
/**
* syntax handler: header, \n!$content
* <p>
* Uses $this->Parser->header as a processor. Is called from $this->block().
*
* @access public
* @param $content parsed string found inside detected syntax
* @return string $content desired output from syntax
*/
function header($content, $trackExclamationCount = true) //!content
{
return parent::header($content, true);
}
/**
* Increments the html tag
* are static, so that all index are unique
*
* @access private
* @param string $name plugin name
* @return string $index
*/
static public function incrementTypeIndex($name)
{
$name = strtolower($name);
if (isset(self::$typeIndex[$name]) == false) self::$typeIndex[$name] = 0;
self::$typeIndex[$name]++;
return self::$typeIndex[$name];
}
/**
* Gets wiki syntax type symbol shorthand, cuts down on information needed to send to browser, used in translating html to wiki
*
* @access private
* @param string $name type name
* @return string $index
*/
static public function typeShorthand($name)
{
if (!isset(self::$typeShorthand[$name])) {
throw new Exception("Type Doesn't Exists");
}
return self::$typeShorthand[$name];
}
/**
* Gets wiki syntax type name from shorthand, cuts down on information needed to send to browser, used in translating html to wiki
*
* @access private
* @param string $name type shorthand
* @return string $index
*/
static public function typeFromShorthand($shorthand)
{
if (!isset(self::$typeShorthandName[$shorthand])) {
return "";
}
return self::$typeShorthandName[$shorthand];
}
/**
* syntax handler: characters
*
* @access public
* @param $content char handler, upper or lower case
* @return string output of char
*/
function char($content)
{
return $content;
}
/**
* syntax handler: new line, \n
* <p>
* Detects if a line break is needed and returns it. If $this->skipBr is set to true, skips output of <br /> and
* sets it back to false for the next line to process
*
* @access public
* @param $ch line line character
* @return string $result of line process
*/
function line($ch)
{
//TODO: We want to handle the items that we needed to select the br just after the syntax needing the br to go away and hide it using css because we need to maintain it when parsing back from html
$this->skipBr = false;
return parent::line($ch);
}
/**
* tag helper creation, noise items that will be disposed
*
* @access public
* @param $syntaxType string from what syntax type
* @param $tagType string what output tag type
* @param $content string what is inside the tag
* @param $params array what params to add to the tag, array, key = param, value = value
* @param $type default is "standard", of types : standard, inline, open, close
* @return string $tag desired output from syntax
*/
public function createWikiHelper($syntaxType, $tagType, $content = "", $attributes = array(), $type = "standard")
{
if (!isset($attributes['class'])) {
$attributes['class'] = "";
}
$attributes['class'] .= " wlwh"; //WikiLingo WYSIWYG Helper tag
$attributes['class'] = trim($attributes['class']);
return parent::createWikiHelper($syntaxType, $tagType, $content, $attributes, $type);
}
/**
* tag creation, should only be used with items that are directly related to wiki syntax, buttons etc, should use createWikiHelper
*
* @access public
* @param $syntaxType string from what syntax type
* @param $tagType string what output tag type
* @param $content string what is inside the tag
* @param $params array what params to add to the tag, array, key = param, value = value
* @param $type string the content to be ignored and for tag to close, ie <tag />
* @return string $tag desired output from syntax
*/
public function createWikiTag($syntaxType, $tagType, $content = "", $params = array(), $type = "standard")
{
if ($type != "close") {
$params['data-i'] = self::incrementTypeIndex($syntaxType);
$params['data-t'] = self::typeShorthand($syntaxType);
if (!isset($params['class'])) {
$params['class'] = "";
}
$params['class'] .= " wlw"; //WikiLingo WYSIWYG tag
$params['class'] = trim($params['class']);
}
if ($this->isRepairing($syntaxType) == true) {
$params['data-repair'] = true;
}
return parent::createWikiTag($syntaxType, $tagType, $content, $params, $type);
}
}
WikiLingoWYSIWYG::staticConstruct();