]> git.openstreetmap.org Git - rails.git/blobdiff - app/helpers/browse_tags_helper.rb
Merge remote-tracking branch 'upstream/pull/4297'
[rails.git] / app / helpers / browse_tags_helper.rb
index e7562081dfd0415b2684ec0c0516607b11582a77..014b8262d050b947de09416b15a8020191f0bb26 100644 (file)
@@ -1,4 +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 @@ module BrowseTagsHelper
     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|
@@ -30,9 +34,9 @@ module BrowseTagsHelper
       end
       safe_join(phones, "; ")
     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
+      tag.span("", :class => "colour-preview-box float-end m-1 border border-dark border-opacity-10", :"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 @@ module BrowseTagsHelper
     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 @@ module BrowseTagsHelper
         :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,7 +129,10 @@ module BrowseTagsHelper
     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.
@@ -136,10 +143,7 @@ module BrowseTagsHelper
     # 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