source: aowow/templates/wowhead/js/Markup.js

Last change on this file was 170, checked in by maron, 16 years ago
  • Property svn:executable set to *
File size: 13.5 KB
Line 
1var Markup = {
2 MODE_COMMENT: 1,
3 MODE_ARTICLE: 2,
4 MODE_QUICKFACTS: 3,
5 MODE_SIGNATURE: 4,
6 simpleTags: {
7 img: 1,
8 pad: 1,
9 item: 1,
10 spell: 1
11 },
12 _prepare: function(D, C) {
13 Markup.tags = [];
14 Markup.links = [];
15 Markup.nTags = 0;
16 Markup.nLinks = 0;
17 D = D.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
18 D = D.replace(/^(\s*)|\r|(\s*)$/g, "");
19 D = D.replace(/\n(\s+)\n/g, "\n\n");
20 var B = "b|i|u|s|small";
21 var A = "b|i|u|s|small|url";
22 switch (C) {
23 case Markup.MODE_ARTICLE:
24 Markup.maps = [];
25 D = D.replace(/(.)?\[toggler\s*[\s+=:]\s*(.+?)\]/gi,
26 function(F, E, H) {
27 if (E == "\\") {
28 return F.substr(1)
29 } else {
30 var G = Markup._yank(H, [["id", "[a-z0-9]+"]]);
31 Markup.tags.push({
32 name: "toggler",
33 close: false,
34 other: G
35 });
36 return (E || "") + "<t" + (Markup.nTags++) + ">"
37 }
38 });
39 D = D.replace(/(.)?\[div\s*[\s+=:]\s*(.+?)\]/gi,
40 function(F, E, H) {
41 if (E == "\\") {
42 return F.substr(1)
43 } else {
44 var G = Markup._yank(H, [["id", "[a-z0-9]+"], ["hidden", ""]]);
45 Markup.tags.push({
46 name: "div",
47 close: false,
48 other: G
49 });
50 return (E || "") + "<t" + (Markup.nTags++) + ">"
51 }
52 });
53 D = D.replace(/(.)?\[img\s*[\s+=:]\s*(.+?)\]/gi,
54 function(F, E, H) {
55 if (E == "\\") {
56 return F.substr(1)
57 } else {
58 var G = Markup._yank(H, [["src", "\\S+"], ["width", "[0-9]+"], ["height", "[0-9]+"], ["float", "left|right"]]);
59 Markup.tags.push({
60 name: "img",
61 close: false,
62 other: G
63 });
64 return (E || "") + "<t" + (Markup.nTags++) + ">"
65 }
66 });
67 D = D.replace(/(.)?\[map\s*[\s+=:]\s*(.+?)\]/gi,
68 function(F, E, H) {
69 if (E == "\\") {
70 return F.substr(1)
71 } else {
72 var G = Markup._yank(H, [["zone", "[0-9]+[a-z]?"], ["source", "\\S+"]]);
73 Markup.tags.push({
74 name: "map",
75 close: false,
76 other: G
77 });
78 return (E || "") + "<t" + (Markup.nTags++) + ">"
79 }
80 });
81 D = D.replace(/(.)?\[pin\s*[\s+=:]\s*(.+?)\]/gi,
82 function(F, E, H) {
83 if (E == "\\") {
84 return F.substr(1)
85 } else {
86 var G = Markup._yank(H, [["x", "[0-9]{1,2}(\\.[0-9])?"], ["y", "[0-9]{1,2}(\\.[0-9])?"], ["url", "\\S+"], ["type", "[0-9]+"]]);
87 Markup.tags.push({
88 name: "pin",
89 close: false,
90 other: G
91 });
92 return (E || "") + "<t" + (Markup.nTags++) + ">"
93 }
94 });
95 B += "|h3|minibox";
96 A += "|h3|minibox|toggler|div|map|pin";
97 case Markup.MODE_QUICKFACTS:
98 D = D.replace(/(.)?\[color\s*[\s+=:]\s*(aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|purple|red|silver|teal|white|yellow|q[0-8]?|#[a-f0-9]{6})\]/gi,
99 function(F, E, G) {
100 if (E == "\\") {
101 return F.substr(1)
102 } else {
103 Markup.tags.push({
104 name: "color",
105 color: false,
106 other: (G).toLowerCase()
107 });
108 return (E || "") + "<t" + (Markup.nTags++) + ">"
109 }
110 });
111 D = D.replace(/(.)?\[(item|spell)\s*[\s+=:]\s*(\d+?)\]/gi,
112 function(F, E, I, H) {
113 if (E == "\\") {
114 return F.substr(1)
115 } else {
116 var G = Markup.tags.push({
117 name: (I).toLowerCase(),
118 close: false,
119 other: H
120 });
121 return (E || "") + "<t" + (Markup.nTags++) + ">"
122 }
123 });
124 B += "|pad";
125 A += "|color";
126 case Markup.MODE_COMMENT:
127 D = D.replace(/(.)?\[quote(\s*[\s+=:]\s*([^\]]+?))?\]/gi,
128 function(F, E, I, H) {
129 if (E == "\\") {
130 return F.substr(1)
131 } else {
132 var G = Markup.tags.push({
133 name: "quote",
134 close: false,
135 other: H
136 });
137 return (E || "") + "<t" + (Markup.nTags++) + ">"
138 }
139 });
140 B += "|code|ul|ol|li";
141 A += "|code|quote|ul|ol|li";
142 default:
143 D = D.replace(/(.)?\[url\s*[\s+=:]\s*([^\]]+?)\]/gi,
144 function(F, E, G) {
145 if (E == "\\") {
146 return F.substr(1)
147 } else {
148 Markup.tags.push({
149 name: "url",
150 close: false,
151 other: G
152 });
153 return (E || "") + "<t" + (Markup.nTags++) + ">"
154 }
155 })
156 }
157 D = D.replace(new RegExp("(.)?\\[(" + B + ")\\]", "gi"),
158 function(F, E, G) {
159 if (E == "\\") {
160 return F.substr(1)
161 } else {
162 Markup.tags.push({
163 name: (G).toLowerCase(),
164 close: false,
165 other: ""
166 });
167 return (E || "") + "<t" + (Markup.nTags++) + ">"
168 }
169 });
170 D = D.replace(new RegExp("(.)?\\[/(" + A + ")\\]", "gi"),
171 function(F, E, G) {
172 if (E == "\\") {
173 return F.substr(1)
174 } else {
175 Markup.tags.push({
176 name: (G).toLowerCase(),
177 close: true,
178 other: ""
179 });
180 return (E || "") + "<t" + (Markup.nTags++) + ">"
181 }
182 });
183 D = D.replace(/(https?:\/\/|www\.)([\/_a-z0-9\%\?#@\-\+~&=;:'\(\)]|\.\S|,\S)+/gi,
184 function(E) {
185 Markup.links.push(E);
186 return "<l" + (Markup.nLinks++) + ">"
187 });
188 return D
189 },
190 _parseCode: function(G, K, E) {
191 var B = [],
192 I = [],
193 D = "",
194 C;
195 var F = G.length;
196 while (E < F) {
197 if (G.charAt(E) == "<" && G.charAt(E + 1) == "t") {
198 if (D.length) {
199 if (K == "ol" || K == "ul") {
200 if (trim(D).length) {
201 I.push(["<text>", D])
202 }
203 } else {
204 if (K != "map") {
205 B.push(["<text>", D])
206 }
207 }
208 D = ""
209 }
210 E += 2;
211 C = "";
212 while (G.charAt(E) != ">") {
213 C += G.charAt(E); ++E
214 }++E;
215 var L = Markup.tags[parseInt(C)];
216 if (L.close) {
217 if (L.name == K) {
218 break
219 }
220 } else {
221 var H = Markup.simpleTags[L.name] != null;
222 var J = [];
223 if (!H) {
224 var A = Markup._parseCode(G, L.name, E);
225 J = A[0];
226 E = A[1]
227 }
228 if (J.length || H) {
229 if (L.name == "url" && !Markup._isUrlSafe(L.other)) {
230 L.name = "<node>"
231 }
232 if (K == "ol" || K == "ul") {
233 if (L.name == "li") {
234 I = Markup._cleanNodes(I);
235 if (I.length) {
236 B.push(["li", I]);
237 I = []
238 }
239 B.push(["li", J])
240 } else {
241 I.push([L.name, J, L.other])
242 }
243 } else {
244 if (K == "map") {
245 if (L.name == "pin") {
246 B.push(["pin", J, L.other])
247 }
248 } else {
249 if (L.name == "li") {
250 B.push(["<node>", J, L.other])
251 } else {
252 if (L.name != "pin") {
253 B.push([L.name, J, L.other])
254 }
255 }
256 }
257 }
258 }
259 }
260 } else {
261 D += G.charAt(E); ++E
262 }
263 }
264 if (D.length) {
265 if (K == "ol" || K == "ul") {
266 if (trim(D).length) {
267 I.push(["<text>", D])
268 }
269 } else {
270 if (K != "map") {
271 B.push(["<text>", D])
272 }
273 }
274 }
275 I = Markup._cleanNodes(I);
276 if (I.length) {
277 B.push(["li", I]);
278 I = []
279 }
280 return [B, E]
281 },
282 _cleanNodes: function(B) {
283 var D = [];
284 for (var C = 0, A = B.length; C < A; ++C) {
285 if (B[C][0] == "<text>") {
286 if (C == 0) {
287 B[C][1] = B[C][1].replace(/^\n+/g, "")
288 }
289 if (C == B.length - 1) {
290 B[C][1] = B[C][1].replace(/\n+$/g, "")
291 }
292 if (B[C][1] != "") {
293 D.push(B[C])
294 }
295 } else {
296 D.push(B[C])
297 }
298 }
299 return D
300 },
301 _yank: function(D, C) {
302 var E = {};
303 D = " " + D + " ";
304 for (var B = 0, A = C.length; B < A; ++B) {
305 if (C[B][1] == "") {
306 D = D.replace(new RegExp("\\s" + C[B][0] + "\\s", "ig"),
307 function(F) {
308 E[C[B][0]] = true;
309 return " "
310 })
311 } else {
312 D = D.replace(new RegExp("\\s" + C[B][0] + "\\s*[\\s=:]\\s*(" + C[B][1] + ")\\s", "ig"),
313 function(G, F) {
314 E[C[B][0]] = F;
315 return " "
316 })
317 }
318 }
319 return E
320 },
321 _isUrlSafe: function(A) {
322 if (!A) {
323 return true
324 }
325 return ! A.match(/^[a-z0-9]+?:/i) || A.match(/^https?:/i)
326 },
327 _htmlmize: function(B) {
328 var G = "";
329 for (var D = 0, A = B.length; D < A; ++D) {
330 switch (B[D][0]) {
331 case "<text>":
332 G += B[D][1];
333 break;
334 case "<node>":
335 G += Markup._htmlmize(B[D][1]);
336 break;
337 case "minibox":
338 G += '<div class="minibox">' + Markup._htmlmize(B[D][1]) + "</div>";
339 break;
340 case "code":
341 G += '<pre class="code">' + Markup._htmlmize(B[D][1]) + "</pre>";
342 break;
343 case "quote":
344 G += '<div class="quote">';
345 if (B[D][2]) {
346 var H = trim(B[D][2]);
347 if (H.length > 0) {
348 G += "<small><b>";
349 if (H.match(/[^a-z0-9]/i) == null && H.length >= 4 && H.length <= 16) {
350 G += '<a href="?user=' + H + '">' + H + "</a>"
351 } else {
352 G += H
353 }
354 G += "</b> " + LANG.markup_said + '</small><div class="pad"></div>'
355 }
356 }
357 G += Markup._htmlmize(B[D][1]) + "</div>";
358 break;
359 case "url":
360 G += '<a href="' + B[D][2] + '"';
361 if (B[D][2].indexOf("wowhead.com") == -1 && B[D][2].substr(0, 5) == "http:") {
362 G += ' target="_blank"'
363 }
364 G += ">" + Markup._htmlmize(B[D][1]) + "</a>";
365 break;
366 case "li":
367 G += "<li><div>" + Markup._htmlmize(B[D][1]) + "</div></li>";
368 break;
369 case "u":
370 G += "<ins>" + Markup._htmlmize(B[D][1]) + "</ins>";
371 break;
372 case "s":
373 G += "<del>" + Markup._htmlmize(B[D][1]) + "</del>";
374 break;
375 case "pad":
376 G += '<div class="pad">' + Markup._htmlmize(B[D][1]) + "</div>";
377 break;
378 case "color":
379 G += "<span " + ((B[D][2].charAt(0) == "q") ? 'class="': 'style="color: ') + B[D][2] + '">' + Markup._htmlmize(B[D][1]) + "</span>";
380 break;
381 case "toggler":
382 var F = B[D][2];
383 if (F.id != null) {
384 G += '<a href="javascript:;" class="disclosure-' + (F.hidden ? "on": "off") + '" onmousedown="return false" onclick="this.className = \'disclosure-\' + (g_toggleDisplay(ge(\'ov4io23-' + F.id + "')) ? 'on' : 'off')\">" + Markup._htmlmize(B[D][1]) + "</a>"
385 }
386 break;
387 case "div":
388 var F = B[D][2];
389 if (F.id != null) {
390 G += '<div id="ov4io23-' + F.id + '"';
391 if (F.hidden) {
392 G += ' style="display: none"'
393 }
394 G += ">" + Markup._htmlmize(B[D][1]) + "</div>"
395 }
396 break;
397 case "img":
398 var F = B[D][2];
399 if (F.src != "" && Markup._isUrlSafe(F.src)) {
400 G += '<img src="' + F.src + '" alt="" class="border"';
401 if (F.width) {
402 G += ' width="' + F.width + '"'
403 }
404 if (F.height) {
405 G += ' height="' + F.height + '"'
406 }
407 G += ' style="margin: 8px;';
408 if (F["float"]) {
409 G += " float: " + F["float"]
410 }
411 G += '" />'
412 }
413 break;
414 case "item":
415 G += '<a href="?item=' + B[D][2] + '">(Item #' + B[D][2] + ")</a>";
416 break;
417 case "spell":
418 G += '<a href="?spell=' + B[D][2] + '">(Spell #' + B[D][2] + ")</a>";
419 break;
420 case "map":
421 var F = B[D][2];
422 if (F.zone != null) {
423 var E = Markup._htmlmize(B[D][1]);
424 G += '<a href="javascript:;" onclick="Markup.mapperPreview(' + Markup.maps.length + ')">(Map: Zone #' + parseInt(F.zone) + ", " + E.length + " pin" + (E.length == 1 ? "": "s") + ")</a>";
425 Markup.maps.push([F.zone, E])
426 }
427 break;
428 case "pin":
429 var F = B[D][2];
430 if (!Markup._isUrlSafe(F.url)) {
431 F.url = ""
432 }
433 if (G == "") {
434 G = []
435 }
436 var C = Markup._htmlmize(B[D][1]).replace(/\n/g, "<br />");
437 if (F.url && F.url.indexOf("?npc=") != -1) {
438 C = '<b class="q">' + C + '</b><br/ ><span class="q2">Click to view this NPC</span>'
439 }
440 G.push([parseFloat(F.x || 0), parseFloat(F.y || 0), {
441 label: C,
442 url: F.url,
443 type: F.type
444 }]);
445 break;
446 default:
447 G += "<" + B[D][0] + ">" + Markup._htmlmize(B[D][1]) + "</" + B[D][0] + ">"
448 }
449 }
450 return G
451 },
452 _tagless: function(B) {
453 var E = "";
454 for (var C = 0, A = B.length; C < A; ++C) {
455 switch (B[C][0]) {
456 case "<text>":
457 E += B[C][1];
458 break;
459 case "img":
460 var D = B[C][2];
461 if (D.src != "" && Markup._isUrlSafe(D.src)) {
462 E += "(Image: " + D.src + ")"
463 }
464 break;
465 case "item":
466 E += "(Item #" + B[C][2] + ")";
467 break;
468 case "spell":
469 E += "(Spell #" + B[C][2] + ")";
470 break;
471 case "map":
472 E += "(Map)";
473 break;
474 case "pin":
475 break;
476 default:
477 E += Markup._tagless(B[C][1])
478 }
479 }
480 return E
481 },
482 toHtml: function(D, C) {
483 C = C || {};
484 C.root = C.root || "";
485 if (C.mode != null && C.mode != Markup.MODE_SIGNATURE) {
486 D = D.replace(/\s*\[(ol|ul|h3)\]/ig,
487 function(F, E) {
488 return "[" + E + "]"
489 });
490 D = D.replace(/\[\/(ol|ul|h3|minibox|code|quote)\]\s*/ig,
491 function(F, E) {
492 return "[/" + E + "]"
493 });
494 D = D.replace(/\s*\[(pad)\]\s*/ig,
495 function(F, E) {
496 return "[" + E + "]"
497 })
498 }
499 D = Markup._prepare(D, C.mode);
500 var A = Markup._parseCode(D, C.root, 0)[0];
501 var B = Markup._htmlmize(A);
502 B = B.replace(/<l([0-9]+)>/g,
503 function(G, E) {
504 var F = Markup.links[parseInt(E)];
505 return '<a href="' + F.replace(/^www/, "http://www") + (F.indexOf("wowhead.com") == -1 ? '" target="_blank">': '">') + (F.length > 100 ? F.substr(0, 97) + "...": F) + "</a>"
506 });
507 B = B.replace(/\n/g, "<br />");
508 return B
509 },
510 removeTags: function(D, C) {
511 C = C || {};
512 C.root = C.root || "";
513 if (C.mode != null && C.mode != Markup.MODE_SIGNATURE) {
514 D = D.replace(/\s*\[(ol|ul|h3)\]\s?/ig,
515 function(F, E) {
516 return " [" + E + "]"
517 });
518 D = D.replace(/\s?\[\/(ol|ul|h3|minibox|code|quote)\]\s*/ig,
519 function(F, E) {
520 return "[/" + E + "]"
521 });
522 D = D.replace(/\s*\[(pad)\]\s*/ig,
523 function(F, E) {
524 return "[" + E + "]"
525 })
526 }
527 D = Markup._prepare(D, C.mode);
528 var A = Markup._parseCode(D, C.root, 0)[0];
529 var B = Markup._tagless(A);
530 B = B.replace(/<l([0-9]+)>/g,
531 function(G, E) {
532 var F = Markup.links[parseInt(E)];
533 return F
534 });
535 B = B.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"');
536 return B
537 },
538 mapperPreview: function(C) {
539 try {
540 window.mapper = Markup.maps[C];
541 var B = window.open("?edit=mapperpreview", "mapperpreview", "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=no,width=800,height=540");
542 B.focus()
543 } catch(A) {}
544 }
545};
Note: See TracBrowser for help on using the repository browser.