]>
Commit | Line | Data |
---|---|---|
83f9b8b2 | 1 | var url_re = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/; |
dbd61667 | 2 | var chan_re = /\B#[^ ,]+/; |
83f9b8b2 | 3 | |
dbd61667 CP |
4 | function txtprocess(text, regex, appendfn, matchfn) { |
5 | for(;;) { | |
6 | var index = text.search(regex); | |
7 | if(index == -1) { | |
8 | appendfn(text); | |
9 | break; | |
10 | } | |
11 | var match = text.match(regex); | |
12 | ||
13 | var before = text.substring(0, index); | |
14 | var matched = match[0]; | |
15 | var after = text.substring(index + matched.length); | |
16 | ||
17 | text = after; | |
18 | appendfn(before); | |
19 | matchfn(matched); | |
20 | } | |
21 | } | |
22 | ||
23 | function urlificate(element, text, execfn) { | |
83f9b8b2 | 24 | function appendText(text) { |
dbd61667 | 25 | chanficate(element, text, execfn); |
83f9b8b2 CP |
26 | } |
27 | function appendA(text) { | |
28 | var a = document.createElement("a"); | |
29 | a.href = text; | |
30 | a.target = "new"; | |
31 | a.appendChild(document.createTextNode(text)); | |
32 | ||
33 | element.appendChild(a); | |
34 | } | |
dbd61667 CP |
35 | txtprocess(text, url_re, appendText, appendA); |
36 | } | |
37 | ||
38 | function chanficate(element, text, execfn) { | |
39 | function appendText(text) { | |
40 | element.appendChild(document.createTextNode(text)); | |
41 | } | |
42 | function appendA(text) { | |
43 | var a = document.createElement("a"); | |
44 | a.href = "#"; | |
45 | a.addEvent("click", function(e) { | |
46 | new Event(e).stop(); | |
47 | execfn("/JOIN " + text); | |
48 | }); | |
49 | a.appendChild(document.createTextNode(text)); | |
50 | ||
51 | element.appendChild(a); | |
83f9b8b2 | 52 | } |
dbd61667 | 53 | txtprocess(text, chan_re, appendText, appendA); |
83f9b8b2 | 54 | } |