Module: UserText::Helper

Included in:
UserText
Defined in:
lib/user_text/helper.rb

Instance Method Summary collapse

Instance Method Details

#email2a(text) ⇒ Object



17
18
19
# File 'lib/user_text/helper.rb', line 17

def email2a(text)
  text.gsub(/[\w+\-.]+@[a-z\d\-.]+\.[a-z]+/i) { %(<a href="mailto:#{Regexp.last_match(0)}">#{Regexp.last_match(0)}</a>) }
end

#nl2br(text) ⇒ Object



7
8
9
# File 'lib/user_text/helper.rb', line 7

def nl2br(text)
  text.gsub(/\R/) { %(<br>) }
end

#url2a(text) ⇒ Object



11
12
13
14
15
# File 'lib/user_text/helper.rb', line 11

def url2a(text)
  text.gsub(URI::DEFAULT_PARSER.make_regexp(%w[http https])) do
    %(<a href="#{Regexp.last_match(0)}" target="_blank">#{Regexp.last_match(0)}</a>)
  end
end

#user_text(text) ⇒ Object



3
4
5
# File 'lib/user_text/helper.rb', line 3

def user_text(text)
  email2a(url2a(nl2br(user_text_sanitize(text.to_s)))).to_s.html_safe
end

#user_text_sanitize(text) ⇒ Object



21
22
23
# File 'lib/user_text/helper.rb', line 21

def user_text_sanitize(text)
  user_text_sanitizer.sanitize(text)
end

#user_text_sanitizerObject



25
26
27
# File 'lib/user_text/helper.rb', line 25

def user_text_sanitizer
  @user_text_sanitizer ||= Rails::HTML5::FullSanitizer.new
end