7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/zfben_extend/string.rb', line 7
def self.to_html text, options={}
options = {
link: true,
link_regexp: /[a-zA-Z]{3,}:\/\/\S+/,
tag: true,
tag_regexp: /&?#[a-zA-Z0-9]+/,
tag_url: '/tags/',
mail: true,
mail_regexp: /[a-zA-Z0-9.-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9.]+/,
user: true,
user_regexp: /@[a-zA-Z0-9]+/,
user_url: '/users/'
}.merge(options)
data = {}
if options[:link]
data[:link] = []
text = text.gsub(options[:link_regexp]){ |link|
data[:link].push('<a href="' + link + '">' + link + '</a>')
'ZFBENLINK' + (data[:link].length - 1).to_s + 'ZFBENLINK'
}
end
if options[:tag]
data[:tag] = []
text = text.gsub(options[:tag_regexp]){ |tag|
if tag[0] != '&'
data[:tag].push('<a href="' + options[:tag_url] + tag.gsub('#', '') + '">' + tag + '</a>')
'ZFBENTAG' + (data[:tag].length - 1).to_s + 'ZFBENTAG'
else
tag
end
}
end
if options[:mail]
data[:mail] = []
text = text.gsub(options[:mail_regexp]){ |mail|
data[:mail].push('<a href="mailto:' + mail + '">' + mail + '</a>')
'ZFBENMAIL' + (data[:mail].length - 1).to_s + 'ZFBENMAIL'
}
end
if options[:user]
data[:user] = []
text = text.gsub(options[:user_regexp]){ |user|
data[:user].push('<a href="' + options[:user_url] + user.gsub('@', '') + '">' + user + '</a>')
'ZFBENUSER' + (data[:user].length - 1).to_s + 'ZFBENUSER'
}
end
[:link, :tag, :mail, :user].each do |name|
if options[name]
text = text.gsub(Regexp.new('ZFBEN' + name.to_s.upcase + '[0-9]+ZFBEN' + name.to_s.upcase)){ |t| data[name][t.match(/[0-9]+/)[0].to_i] }
end
end
self.new text.html_safe
end
|