From: Andy Allan Date: Wed, 8 Feb 2023 17:30:25 +0000 (+0000) Subject: Merge pull request #3691 from AntonKhorev/wikipedia-secondary-links X-Git-Tag: live~1532 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/47362f432a9bd10196b27aa1667e6166b76f4b66?hp=-c Merge pull request #3691 from AntonKhorev/wikipedia-secondary-links Link prefixed wikipedia tags --- 47362f432a9bd10196b27aa1667e6166b76f4b66 diff --combined app/helpers/browse_tags_helper.rb index f95003aae,3a952ccc9..2bd547fde --- a/app/helpers/browse_tags_helper.rb +++ b/app/helpers/browse_tags_helper.rb @@@ -1,4 -1,8 +1,8 @@@ module BrowseTagsHelper + # https://wiki.openstreetmap.org/wiki/Key:wikipedia#Secondary_Wikipedia_links + # https://wiki.openstreetmap.org/wiki/Key:wikidata#Secondary_Wikidata_links + SECONDARY_WIKI_PREFIXES = "architect|artist|brand|flag|genus|name:etymology|network|operator|species|subject".freeze + def format_key(key) if url = wiki_link("key", key) link_to h(key), url, :title => t("browse.tag_details.wiki_link.key", :key => key) @@@ -22,7 -26,7 +26,7 @@@ 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])) + mail_to(email, :title => t("browse.tag_details.email_link", :email => email)) elsif phones = telephone_links(key, value) # similarly, telephone_links() returns an array of phone numbers phones = phones.map do |p| @@@ -32,7 -36,7 +36,7 @@@ elsif colour_value = colour_preview(key, value) tag.span("", :class => "colour-preview-box", :"data-colour" => colour_value, :title => t("browse.tag_details.colour_preview", :colour_value => colour_value)) + colour_value else - linkify h(value) + safe_join(value.split(";").map { |x| linkify(h(x)) }, ";") end end @@@ -60,7 -64,7 +64,7 @@@ return nil if %r{^https?://}.match?(value) case key - when "wikipedia" + when "wikipedia", /^(#{SECONDARY_WIKI_PREFIXES}):wikipedia/o # This regex should match Wikipedia language codes, everything # from de to zh-classical lang = if value =~ /^([a-z-]{2,12}):(.+)$/i @@@ -104,7 -108,7 +108,7 @@@ :title => value }] # Key has to be one of the accepted wikidata-tags - elsif key =~ /(architect|artist|brand|name:etymology|network|operator|subject):wikidata/ && + elsif key =~ /(#{SECONDARY_WIKI_PREFIXES}):wikidata/o && # Value has to be a semicolon-separated list of wikidata-IDs (whitespaces allowed before and after semicolons) value =~ /^[Qq][1-9][0-9]*(\s*;\s*[Qq][1-9][0-9]*)*$/ # Splitting at every semicolon to get a separate hash for each wikidata-ID @@@ -125,10 -129,7 +129,10 @@@ nil end - def email_link(_key, value) + def email_link(key, value) + # Avoid converting conditional tags into emails, since EMAIL_REGEXP is quite permissive + return nil unless %w[email contact:email].include? key + # Does the value look like an email? eg "someone@domain.tld" # Uses Ruby built-in regexp to validate email. @@@ -139,7 -140,10 +143,7 @@@ # 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 + return email if email.match?(URI::MailTo::EMAIL_REGEXP) nil end diff --combined test/helpers/browse_tags_helper_test.rb index f9f977c49,b90ad1e0b..a0f716974 --- a/test/helpers/browse_tags_helper_test.rb +++ b/test/helpers/browse_tags_helper_test.rb @@@ -22,9 -22,6 +22,9 @@@ class BrowseTagsHelperTest < ActionView html = format_value("unknown", "unknown") assert_dom_equal "unknown", html + html = format_value("addr:street", "Rue de l'Amigo") + assert_dom_equal "Rue de l'Amigo", html + html = format_value("phone", "+1234567890") assert_dom_equal "+1234567890", html @@@ -49,15 -46,6 +49,15 @@@ html = format_value("colour", "#f00") assert_dom_equal %(#f00), html + + html = format_value("email", "foo@example.com") + assert_dom_equal "foo@example.com", html + + html = format_value("source", "https://example.com") + assert_dom_equal "https://example.com", html + + html = format_value("source", "https://example.com;hello;https://example.net") + assert_dom_equal "https://example.com;hello;https://example.net", html end def test_wiki_link @@@ -132,6 -120,10 +132,10 @@@ assert_equal "//www.wikidata.org/entity/Q24?uselang=en", links[0][:url] assert_equal "Q24", links[0][:title] + links = wikidata_links("species:wikidata", "Q26899") + assert_equal "//www.wikidata.org/entity/Q26899?uselang=en", links[0][:url] + assert_equal "Q26899", links[0][:title] + # Another allowed key, this time with multiple values and I18n I18n.with_locale "dsb" do links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346") @@@ -194,6 -186,10 +198,10 @@@ assert_equal "zh-classical:Test#Section", link[:title] end + link = wikipedia_link("subject:wikipedia", "en:Catherine McAuley") + assert_equal "https://en.wikipedia.org/wiki/en:Catherine McAuley?uselang=en", link[:url] + assert_equal "en:Catherine McAuley", link[:title] + link = wikipedia_link("foo", "Test") assert_nil link end @@@ -252,29 -248,29 +260,29 @@@ 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] + assert_equal "x@example.com", email 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] + assert_equal "other.email-with-hyphen@example.com", email 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] + assert_equal "user.name+tag+sorting@example.com", email 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] + assert_equal "dash-in@both-parts.com", email email = email_link("email", "example@s.example") - assert_equal "example@s.example", email[:email] - assert_equal "mailto:example@s.example", email[:url] + assert_equal "example@s.example", email # 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] + assert_equal "test@email.com", email + + email = email_link("contact:email", "example@example.com") + assert_equal "example@example.com", email + + email = email_link("maxweight:conditional", "none@agricultural") + assert_nil email end def test_telephone_links