-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeLink.rb
21 lines (21 loc) · 1.19 KB
/
makeLink.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def link(text)
if (text[/((http:\/\/|ftp:\/\/|https:\/\/|ssh:\/\/|file:\/\/)(([\p{Letter}\p{Number}]+(\-([\p{Letter}\p{Number}]+))?\.)?)([\p{Letter}\p{Number}]+(\-([\p{Letter}\p{Number}]+))?)((\.)(\p{Letter}{2,})){1,2}(\/[\S]*)?)($|\s|\z)/])
if (text[/(.*)(#{$1})(.*)/])
return "#{$1}<a href=\"#{$2}\">#{$2}</a>#{$3}"
end
elsif (text[/((mailto:)([\p{Letter}\p{Number}\-\.\_]+)(@)(([\p{Letter}\p{Number}]+(\-([\p{Letter}\p{Number}]+))?\.)?)([\p{Letter}\p{Number}]+(\-([\p{Letter}\p{Number}]+))?)((\.)(\p{Letter}{2,})){1,2})($|\s|\z)/])
if (text[/(.*)(#{$1})(.*)/])
return "#{$1}<a href=\"#{$2}\">#{$2}</a>#{$3}"
end
elsif (text[/((www\.)([\p{Letter}\p{Number}]+(\-([\p{Letter}\p{Number}]+))?)((\.)(\p{Letter}{2,})){1,2}{1,2}(\/[\S]*)?)($|\s|\z)/])
if (text[/(.*)(#{$1})(.*)/])
return "#{$1}<a href=\"http://#{$2}\">#{$2}</a>#{$3}"
end
elsif (text[/(([\p{Letter}\p{Number}\-\.\_]+)(@)(([\p{Letter}\p{Number}]+(\-([\p{Letter}\p{Number}]+))?\.)?)([\p{Letter}\p{Number}]+(\-([\p{Letter}\p{Number}]+))?)((\.)(\p{Letter}{2,})){1,2})($|\s|\z)/])
if (text[/(.*)(#{$1})(.*)/])
return "#{$1}<a href=\"mailto:#{$2}\">#{$2}</a>#{$3}"
end
else
return text
end
end