1 # frozen_string_literal: true
4 SECONDARY_WIKI_PREFIX_PATTERN = /[a-z:_-]+:/
5 QID_PATTERN = /[Qq][1-9][0-9]*/
7 # regex to match all wikipedia locale project identifiers
8 WIKIPEDIA_PROJECT_IDENTIFIER_PATTERN = /[a-z]{2,3}(?:-[a-z]{2,3})?|be-tarask|roa-tara|simple|zh-classical|zh-min-nan/
10 def self.parse_key(key)
11 wiki_link("key", key)&.first ||
12 { :text => ERB::Util.html_escape(key) }
15 def self.parse_value(key, value)
16 wikipedia_links(key, value) ||
17 wikidata_links(key, value) ||
18 wikimedia_commons_link(key, value) ||
19 email_link(key, value) ||
20 telephone_links(key, value) ||
21 colour_preview(key, value) ||
22 wiki_link("tag", key, value) ||
23 split_value(key, value).map { |x| basic_link(key, x) }
26 def self.wiki_link(type, *components)
27 # update-wiki-pages does s/ /_/g on keys before saving them, we
28 # have to replace spaces with underscore so we'll link
29 # e.g. `source=Isle of Man Government aerial imagery (2001)' to
31 lookup = components.join("=").tr(" ", "_")
33 page = @wiki_pages_dict.dig(I18n.locale.to_s, type, lookup) ||
34 @wiki_pages_dict.dig("en", type, lookup)
38 hash = { :text => ERB::Util.html_escape(components.last), :url => wiki_url("wiki.openstreetmap.org", page), :type => :"wiki_link.#{type}", :key => components.first }
39 hash[:value] = components[1] if components.size > 1
44 def self.wikipedia_links(key, value)
45 # Some k/v's are wikipedia=http://en.wikipedia.org/wiki/Full%20URL
46 return nil if %r{^https?://}.match?(value)
48 if key =~ /^(?:#{SECONDARY_WIKI_PREFIX_PATTERN})?wikipedia(?::(#{WIKIPEDIA_PROJECT_IDENTIFIER_PATTERN}))?$/o
49 lang = Regexp.last_match(1)
54 # Value could be a semicolon-separated list of Wikipedia pages
55 value.split(";").map do |wiki_value|
56 wiki_value = wiki_value.strip
58 if wiki_value =~ /^(#{WIKIPEDIA_PROJECT_IDENTIFIER_PATTERN}):(.+)$/oi
59 page_lang = Regexp.last_match(1)
60 title_section = Regexp.last_match(2)
63 return nil unless page_lang
65 title_section = wiki_value
68 title, section = title_section.split("#", 2).map { |s| ERB::Util.url_encode s.tr(" ", "_") }
69 url = wiki_url("#{page_lang}.wikipedia.org", title)
70 url += "##{section}" if section
72 { :text => ERB::Util.html_escape(wiki_value), :url => url, :type => :wikipedia_link, :page => wiki_value }
76 private_class_method def self.wiki_url(project, title, ns = "wiki", locale = I18n.locale)
77 "https://#{project}/#{ns}/#{title.tr(' ', '_')}?uselang=#{locale}"
80 def self.wikidata_links(key, value)
81 # The simple wikidata-tag (this is limited to only one value)
82 if key == "wikidata" && value =~ /^#{QID_PATTERN}$/o
84 elsif key =~ /^#{SECONDARY_WIKI_PREFIX_PATTERN}wikidata$/o &&
85 # Value has to be a semicolon-separated list of wikidata-IDs (whitespaces allowed before and after semicolons)
86 value =~ /^#{QID_PATTERN}(?:\s*;\s*#{QID_PATTERN})*$/o
87 # Splitting at every semicolon to get a separate hash for each wikidata-ID
88 qids = value.split(";")
92 widget = { :type => :html, :widget => :wikidata_preview, :qids => qids.map(&:strip), :count => qids.length }
93 qids.map { |id| { :text => id, :url => wiki_url("www.wikidata.org", id.strip, "entity"), :type => :wikidata_link, :page => id.strip } }.unshift(widget)
96 def self.wikimedia_commons_link(key, value)
97 if key =~ /^(?:#{SECONDARY_WIKI_PREFIX_PATTERN})?wikimedia_commons$/o && value =~ /^(file|category):([^#]+)/i
98 namespace = Regexp.last_match(1)
99 title = Regexp.last_match(2)
101 :text => ERB::Util.html_escape(value),
102 :url => wiki_url("commons.wikimedia.org", "#{namespace}:#{ERB::Util.url_encode title}"),
103 :type => :wikimedia_commons_link,
110 private_class_method def self.split_value(key, value)
111 if %w[opening_hours collection_times service_times].include?(key)
118 def self.basic_link(key, value)
119 return { :text => RichText.auto_link(value) } if %r{\Ahttps?://}.match?(value)
121 url_template = @tag2link_dict[key]
122 return { :text => RichText.auto_link(value) } unless url_template
124 link = url_template.gsub("$1", value.sub(/^#/, ""))
125 { :text => value, :url => link, :type => :tag2link_link, :key => key }
128 def self.email_link(key, value)
129 # Avoid converting conditional tags into emails, since EMAIL_REGEXP is quite permissive
130 return nil unless %w[email contact:email].include? key
132 # Does the value look like an email? eg "someone@domain.tld"
134 # Uses Ruby built-in regexp to validate email.
135 # This will not catch certain valid emails containing comments, whitespace characters,
136 # and quoted strings.
137 # (see: https://github.com/ruby/ruby/blob/master/lib/uri/mailto.rb)
139 # remove any leading and trailing whitespace
142 return unless email.match?(URI::MailTo::EMAIL_REGEXP)
144 [{ :text => ERB::Util.html_escape(email), :url => email, :type => :email_link, :email => email }]
147 def self.telephone_links(_key, value)
148 # Does it look like a global phone number? eg "+1 (234) 567-8901 "
149 # or a list of alternate numbers separated by ;
151 # Per RFC 3966, this accepts the visual separators -.() within the number,
152 # which are displayed and included in the tel: URL, and accepts whitespace,
153 # which is displayed but not included in the tel: URL.
154 # (see: http://tools.ietf.org/html/rfc3966#section-5.1.1)
156 # Also accepting / as a visual separator although not given in RFC 3966,
157 # because it is used as a visual separator in OSM data in some countries.
158 if value.match?(%r{^\s*\+[\d\s()/.-]{6,25}\s*(;\s*\+[\d\s()/.-]{6,25}\s*)*$})
159 return value.split(";").map do |phone_number|
160 # for display, remove leading and trailing whitespace
161 phone_number = phone_number.strip
163 # for tel: URL, remove all whitespace
164 # "+1 (234) 567-8901 " -> "tel:+1(234)567-8901"
165 phone_no_whitespace = phone_number.gsub(/\s+/, "")
166 { :text => ERB::Util.html_escape(phone_number), :url => "tel:#{phone_no_whitespace}", :type => :telephone_link, :phone_number => phone_number }
172 def self.colour_preview(key, value)
173 return nil unless key =~ /^(?>.+:)?colour$/ && !value.nil? # see discussion at https://github.com/openstreetmap/openstreetmap-website/pull/1779
175 # does value look like a colour? ( 3 or 6 digit hex code or w3c colour name)
177 %w[aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate
178 coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgrey darkgreen darkkhaki darkmagenta darkolivegreen
179 darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey darkturquoise darkviolet deeppink deepskyblue
180 dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray grey green greenyellow honeydew
181 hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray
182 lightgrey lightgreen lightpink lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen
183 linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise
184 mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod
185 palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple red rosybrown royalblue saddlebrown salmon
186 sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow springgreen steelblue tan teal thistle tomato turquoise
187 violet wheat white whitesmoke yellow yellowgreen]
188 return nil unless value =~ /^#([0-9a-fA-F]{3}){1,2}$/ || w3c_colors.include?(value.downcase)
190 [{ :type => :html, :widget => :colour_preview, :colour_value => value }, { :text => value }]
194 @tag2link_dict = build_tag2link_dict(JSON.parse(paths[:tag2link].read)).freeze
195 @wiki_pages_dict = YAML.load_file(paths[:wiki_pages]).freeze
198 def self.build_tag2link_dict(data)
200 # exclude deprecated, third-party, and non-HTTP URLs
201 .reject { |item| item["rank"] == "deprecated" || item["source"] == "wikidata:P3303" || !item["url"].match?(%r{\Ahttps?://[^$]}) }
202 .group_by { |item| item["key"].sub(/^Key:/, "") }
203 .transform_values { |items| choose_best_tag2link_item(items) }
205 .transform_values { |items| items["url"] }
208 private_class_method def self.choose_best_tag2link_item(items)
209 return nil if items.blank?
211 return items.first if items.size == 1
213 # move preferred to the start of the array
214 ranked = items.sort_by { |item| item["rank"] == "preferred" ? 0 : 1 }.uniq { |item| item["url"] }
215 top_rank = ranked.first["rank"]
216 top_items = ranked.select { |i| i["rank"] == top_rank }
218 # if only one top-ranked item, prefer that
219 return top_items.first if top_items.size == 1
221 grouped = top_items.group_by { |i| i["source"] }
222 return nil if grouped.size > 2
224 # if both sources have exactly one preferred, prefer osmwiki
225 return grouped["osmwiki:P8"]&.first || grouped.values.flatten.first if grouped.all? { |_s, vals| vals.size == 1 }
227 # if one source has multiple preferreds and the other has one, prefer the single one
228 return grouped.min_by { |_s, vals| vals.size }.last.first if grouped.any? { |_s, vals| vals.size == 1 }
230 # exclude any that are ambiguous