3 module BrowseTagsHelper
5 if url = wiki_link("key", key)
6 link_to h(key), url, :title => t("browse.tag_details.wiki_link.key", :key => key)
12 def format_value(key, value)
13 if wp = wikipedia_link(key, value)
14 link_to h(wp[:title]), wp[:url], :title => t("browse.tag_details.wikipedia_link", :page => wp[:title])
15 elsif wdt = wikidata_links(key, value)
16 # IMPORTANT: Note that wikidata_links() returns an array of hashes, unlike for example wikipedia_link(),
17 # which just returns one such hash.
19 link_to(w[:title], w[:url], :title => t("browse.tag_details.wikidata_link", :page => w[:title].strip))
22 elsif url = wiki_link("tag", "#{key}=#{value}")
23 link_to h(value), url, :title => t("browse.tag_details.wiki_link.tag", :key => key, :value => value)
24 elsif phones = telephone_links(key, value)
25 # similarly, telephone_links() returns an array of phone numbers
26 phones = phones.map do |p|
27 link_to(h(p[:phone_number]), p[:url], :title => t("browse.tag_details.telephone_link", :phone_number => p[:phone_number]))
29 safe_join(phones, "; ")
37 def wiki_link(type, lookup)
38 locale = I18n.locale.to_s
40 # update-wiki-pages does s/ /_/g on keys before saving them, we
41 # have to replace spaces with underscore so we'll link
42 # e.g. `source=Isle of Man Government aerial imagery (2001)' to
44 lookup_us = lookup.tr(" ", "_")
46 if page = WIKI_PAGES.dig(locale, type, lookup_us)
47 url = "https://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
48 elsif page = WIKI_PAGES.dig("en", type, lookup_us)
49 url = "https://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
55 def wikipedia_link(key, value)
56 # Some k/v's are wikipedia=http://en.wikipedia.org/wiki/Full%20URL
57 return nil if value =~ %r{^https?://}
60 # This regex should match Wikipedia language codes, everything
61 # from de to zh-classical
62 lang = if value =~ /^([a-z-]{2,12}):(.+)$/i
63 # Value is <lang>:<title> so split it up
64 # Note that value is always left as-is, see: https://trac.openstreetmap.org/ticket/4315
67 # Value is <title> so default to English Wikipedia
70 elsif key =~ /^wikipedia:(\S+)$/
71 # Language is in the key, so assume value is the title
72 lang = Regexp.last_match(1)
74 # Not a wikipedia key!
78 if value =~ /^([^#]*)#(.*)/
79 # Contains a reference to a section of the wikipedia article
80 # Must break it up to correctly build the url
81 value = Regexp.last_match(1)
82 section = "#" + Regexp.last_match(2)
83 encoded_section = "#" + CGI.escape(Regexp.last_match(2).gsub(/ +/, "_")).tr("%", ".")
90 :url => "https://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}#{encoded_section}",
91 :title => value + section
95 def wikidata_links(key, value)
96 # The simple wikidata-tag (this is limited to only one value)
97 if key == "wikidata" && value =~ /^[Qq][1-9][0-9]*$/
99 :url => "//www.wikidata.org/entity/#{value}?uselang=#{I18n.locale}",
102 # Key has to be one of the accepted wikidata-tags
103 elsif key =~ /(architect|artist|brand|name:etymology|network|operator|subject):wikidata/ &&
104 # Value has to be a semicolon-separated list of wikidata-IDs (whitespaces allowed before and after semicolons)
105 value =~ /^[Qq][1-9][0-9]*(\s*;\s*[Qq][1-9][0-9]*)*$/
106 # Splitting at every semicolon to get a separate hash for each wikidata-ID
107 return value.split(";").map do |id|
108 { :title => id, :url => "//www.wikidata.org/entity/#{id.strip}?uselang=#{I18n.locale}" }
114 def telephone_links(_key, value)
115 # Does it look like a global phone number? eg "+1 (234) 567-8901 "
116 # or a list of alternate numbers separated by ;
118 # Per RFC 3966, this accepts the visual separators -.() within the number,
119 # which are displayed and included in the tel: URL, and accepts whitespace,
120 # which is displayed but not included in the tel: URL.
121 # (see: http://tools.ietf.org/html/rfc3966#section-5.1.1)
123 # Also accepting / as a visual separator although not given in RFC 3966,
124 # because it is used as a visual separator in OSM data in some countries.
125 if value =~ %r{^\s*\+[\d\s\(\)/\.-]{6,25}\s*(;\s*\+[\d\s\(\)/\.-]{6,25}\s*)*$}
126 return value.split(";").map do |phone_number|
127 # for display, remove leading and trailing whitespace
128 phone_number = phone_number.strip
130 # for tel: URL, remove all whitespace
131 # "+1 (234) 567-8901 " -> "tel:+1(234)567-8901"
132 phone_no_whitespace = phone_number.gsub(/\s+/, "")
133 { :phone_number => phone_number, :url => "tel:#{phone_no_whitespace}" }