]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/url.js
Make channel, nick and auth links less obvious.
[irc/quakenet/qwebirc.git] / js / ui / url.js
1 qwebirc.ui.urlificate = function(element, text, execfn, cmdfn, window) {
2 var punct_re = /(\.*|\,|;|\])$/;
3
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);
12
13 var before = text.substring(0, index);
14 var matched = match[0];
15 var after = text.substring(index + matched.length);
16
17 appendfn(before);
18 var more = matchfn(matched, appendfn);
19 if(!more)
20 more = "";
21 text = more + after;
22 }
23 };
24
25 var appendText = function(text) {
26 qwebirc.util.NBSPCreate(text, element);
27 };
28
29 var appendChan = function(text) {
30 var newtext = text.replace(punct_re, "");
31 var punct = text.substring(newtext.length);
32
33 var a = new Element("span");
34 a.href = "#";
35 a.addClass("hyperlink-channel");
36 a.addEvent("click", function(e) {
37 new Event(e).stop();
38 execfn("/JOIN " + newtext);
39 });
40 a.appendChild(document.createTextNode(newtext));
41 element.appendChild(a);
42
43 return punct;
44 };
45
46 var appendURL = function(text, appendfn) {
47 var url = text.replace(punct_re, "");
48 var punct = text.substring(url.length);
49
50 var href = "";
51 var fn = null;
52 var target = "new";
53 var disptext = url;
54 var elementType = "a";
55 var addClass;
56
57 var ma = url.match(/^qwebirc:\/\/(.*)$/);
58 if(ma) {
59 var m = ma[1].match(/^([^\/]+)\/([^\/]+)\/?(.*)$/);
60 if(!m) {
61 appendfn(text);
62 return;
63 }
64
65 var cmd = cmdfn(m[1], window);
66 if(cmd) {
67 addClass = m[1];
68 elementType = cmd[0];
69 if(cmd[0] != "a") {
70 url = null;
71 } else {
72 url = "#";
73 }
74 fn = cmd[1];
75 disptext = unescape(m[2]);
76 target = null;
77 } else {
78 appendfn(text);
79 return;
80 }
81 if(m[3])
82 punct = m[3] + punct;
83 } else {
84 if(url.match(/^www\./))
85 url = "http://" + url;
86 }
87
88 var a = new Element(elementType);
89 if(addClass)
90 a.addClass("hyperlink-" + addClass);
91
92 if(url) {
93 a.href = url;
94
95 if(target)
96 a.target = target;
97 }
98 a.appendChild(document.createTextNode(disptext));
99
100 element.appendChild(a);
101 if($defined(fn))
102 a.addEvent("click", function(e) { new Event(e).stop(); fn(disptext); });
103
104 return punct;
105 };
106
107 txtprocess(text, /\b((https?|ftp|qwebirc):\/\/|www\.)[^ ]+/, function(text) {
108 txtprocess(text, /\B#[^ ,]+/, appendText, appendChan);
109 }, appendURL);
110 }