]>
Commit | Line | Data |
---|---|---|
8e327eaa CP |
1 | function urlificate(element, text, execfn) { |
2 | var punct_re = /(\.*|\,|;)$/; | |
83f9b8b2 | 3 | |
8e327eaa CP |
4 | var txtprocess = function(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); | |
dbd61667 | 12 | |
8e327eaa CP |
13 | var before = text.substring(0, index); |
14 | var matched = match[0]; | |
15 | var after = text.substring(index + matched.length); | |
83f9b8b2 | 16 | |
8e327eaa CP |
17 | appendfn(before); |
18 | var more = matchfn(matched); | |
19 | if(!more) | |
20 | more = ""; | |
21 | text = more + after; | |
22 | } | |
23 | }; | |
24 | ||
25 | var appendText = function(text) { | |
dbd61667 | 26 | element.appendChild(document.createTextNode(text)); |
8e327eaa CP |
27 | }; |
28 | ||
29 | var appendChan = function(text) { | |
30 | var newtext = text.replace(punct_re, ""); | |
31 | var punct = text.substring(newtext.length); | |
32 | ||
dbd61667 CP |
33 | var a = document.createElement("a"); |
34 | a.href = "#"; | |
35 | a.addEvent("click", function(e) { | |
36 | new Event(e).stop(); | |
8e327eaa | 37 | execfn("/JOIN " + newtext); |
dbd61667 | 38 | }); |
8e327eaa CP |
39 | a.appendChild(document.createTextNode(newtext)); |
40 | element.appendChild(a); | |
41 | ||
42 | return punct; | |
43 | }; | |
44 | ||
45 | var appendURL = function(text) { | |
46 | var newtext = text.replace(punct_re, ""); | |
47 | var punct = text.substring(newtext.length); | |
dbd61667 | 48 | |
8e327eaa CP |
49 | var a = document.createElement("a"); |
50 | a.href = newtext; | |
51 | a.target = "new"; | |
52 | a.appendChild(document.createTextNode(newtext)); | |
dbd61667 | 53 | element.appendChild(a); |
8e327eaa CP |
54 | |
55 | return punct; | |
56 | }; | |
57 | ||
58 | txtprocess(text, /\bhttp:\/\/[^ ]+/, function(text) { | |
59 | txtprocess(text, /\B#[^ ,]+/, appendText, appendChan); | |
60 | }, appendURL); | |
83f9b8b2 | 61 | } |