From 75e07dc5b8407b121b6228c4ee1ed595944a536a Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=A0tefan=20Baebler?= Date: Sun, 15 Jun 2014 09:30:17 +0100 Subject: [PATCH] Added telephone links into tag detail browser International phone numbers become links using the official URI scheme as per http://tools.ietf.org/html/rfc3966. It is not limited to key=phone, so it can be used in other keys as well. --- app/helpers/browse_helper.rb | 13 +++++++++ config/locales/en.yml | 1 + test/helpers/browse_helper_test.rb | 47 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/app/helpers/browse_helper.rb b/app/helpers/browse_helper.rb index bfc7db80b..e70ec204e 100644 --- a/app/helpers/browse_helper.rb +++ b/app/helpers/browse_helper.rb @@ -63,6 +63,8 @@ module BrowseHelper link_to h(wp[:title]), wp[:url], :title => t('browse.tag_details.wikipedia_link', :page => wp[: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 url = telephone_link(key, value) + link_to h(value), url, :title => t('browse.tag_details.telephone_link', :phone_number => value) else linkify h(value) end @@ -146,4 +148,15 @@ private :title => value + section } end + + def telephone_link(key, value) + # does it look like a phone number? eg "+1 (234) 567-8901 " ? + return nil unless value =~ /^\s*\+[\d\s\(\)\/\.-]{6,25}\s*$/ + + # remove all whitespace instead of encoding it http://tools.ietf.org/html/rfc3966#section-5.1.1 + # "+1 (234) 567-8901 " -> "+1(234)567-8901" + valueNoWhitespace = value.gsub(/\s+/, '') + + return "tel:#{valueNoWhitespace}" + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index db644920e..fe5f58d51 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -181,6 +181,7 @@ en: key: "The wiki description page for the %{key} tag" tag: "The wiki description page for the %{key}=%{value} tag" wikipedia_link: "The %{page} article on Wikipedia" + telephone_link: "Call %{phone_number}" note: title: "Note: %{id}" new_note: "New Note" diff --git a/test/helpers/browse_helper_test.rb b/test/helpers/browse_helper_test.rb index 830674940..d5f7a91e6 100644 --- a/test/helpers/browse_helper_test.rb +++ b/test/helpers/browse_helper_test.rb @@ -83,6 +83,9 @@ class BrowseHelperTest < ActionView::TestCase html = format_value("unknown", "unknown") assert_equal "unknown", html + + html = format_value("phone", "+1234567890") + assert_equal "+1234567890", html end def test_icon_tags @@ -157,4 +160,48 @@ class BrowseHelperTest < ActionView::TestCase link = wikipedia_link("foo", "Test") assert_nil link end + + def test_telephone_link + link = telephone_link("foo", "Test") + assert_nil link + + link = telephone_link("phone", "+123") + assert_nil link + + link = telephone_link("phone", "123") + assert_nil link + + link = telephone_link("phone", "123 abcdefg") + assert_nil link + + link = telephone_link("phone", "+1234567890 abc") + assert_nil link + + link = telephone_link("phone", "+1234567890; +22334455667788") + assert_nil link + + link = telephone_link("phone", "1234567890") + assert_nil link + + link = telephone_link("phone", "+1234567890") + assert_equal "tel:+1234567890", link + + link = telephone_link("phone", "+1234-567-890") + assert_equal "tel:+1234-567-890", link + + link = telephone_link("phone", "+1234/567/890") + assert_equal "tel:+1234/567/890", link + + link = telephone_link("phone", "+1234.567.890") + assert_equal "tel:+1234.567.890", link + + link = telephone_link("phone", " +1234 567-890 ") + assert_equal "tel:+1234567-890", link + + link = telephone_link("phone", "+1 234-567-890") + assert_equal "tel:+1234-567-890", link + + link = telephone_link("phone", "+1 (234) 567-890") + assert_equal "tel:+1(234)567-890", link + end end -- 2.43.2