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