link_to h(wmc[:title]), wmc[:url], :title => t("browse.tag_details.wikimedia_commons_link", :page => wmc[:title])
elsif url = wiki_link("tag", "#{key}=#{value}")
link_to h(value), url, :title => t("browse.tag_details.wiki_link.tag", :key => key, :value => value)
+ elsif email = email_link(key, value)
+ link_to(h(email[:email]), email[:url], :title => t("browse.tag_details.email_link", :email => email[:email]))
elsif phones = telephone_links(key, value)
# similarly, telephone_links() returns an array of phone numbers
phones = phones.map do |p|
nil
end
+ def email_link(_key, value)
+ # Does the value look like an email? eg "someone@domain.tld"
+
+ # Uses Ruby built-in regexp to validate email.
+ # This will not catch certain valid emails containing comments, whitespace characters,
+ # and quoted strings.
+ # (see: https://github.com/ruby/ruby/blob/master/lib/uri/mailto.rb)
+
+ # remove any leading and trailing whitespace
+ email = value.strip
+
+ if email.match?(URI::MailTo::EMAIL_REGEXP)
+ # add 'mailto:'' prefix
+ return { :email => email, :url => "mailto:#{email}" }
+ end
+
+ nil
+ end
+
def telephone_links(_key, value)
# Does it look like a global phone number? eg "+1 (234) 567-8901 "
# or a list of alternate numbers separated by ;
assert_nil link
end
+ def test_email_link
+ email = email_link("foo", "Test")
+ assert_nil email
+
+ email = email_link("email", "123")
+ assert_nil email
+
+ email = email_link("email", "Abc.example.com")
+ assert_nil email
+
+ email = email_link("email", "a@b@c.com")
+ assert_nil email
+
+ email = email_link("email", "just\"not\"right@example.com")
+ assert_nil email
+
+ email = email_link("email", "123 abcdefg@space.com")
+ assert_nil email
+
+ email = email_link("email", "test@ abc")
+ assert_nil email
+
+ email = email_link("email", "using;semicolon@test.com")
+ assert_nil email
+
+ email = email_link("email", "x@example.com")
+ assert_equal "x@example.com", email[:email]
+ assert_equal "mailto:x@example.com", email[:url]
+
+ email = email_link("email", "other.email-with-hyphen@example.com")
+ assert_equal "other.email-with-hyphen@example.com", email[:email]
+ assert_equal "mailto:other.email-with-hyphen@example.com", email[:url]
+
+ email = email_link("email", "user.name+tag+sorting@example.com")
+ assert_equal "user.name+tag+sorting@example.com", email[:email]
+ assert_equal "mailto:user.name+tag+sorting@example.com", email[:url]
+
+ email = email_link("email", "dash-in@both-parts.com")
+ assert_equal "dash-in@both-parts.com", email[:email]
+ assert_equal "mailto:dash-in@both-parts.com", email[:url]
+
+ email = email_link("email", "example@s.example")
+ assert_equal "example@s.example", email[:email]
+ assert_equal "mailto:example@s.example", email[:url]
+
+ # Strips whitespace at ends
+ email = email_link("email", " test@email.com ")
+ assert_equal "test@email.com", email[:email]
+ assert_equal "mailto:test@email.com", email[:url]
+ end
+
def test_telephone_links
links = telephone_links("foo", "Test")
assert_nil links