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| wiki_link("tag", key, x)&.first || basic_link(key, x) }
26 private_class_method 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 private_class_method 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 private_class_method 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 private_class_method 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 private_class_method 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_value = value.sub(/^#/, "")
125 path, separator, query = url_template.partition("?")
126 path.gsub!("$1", link_value)
127 # Values substituted into the query must be fully encoded, since
128 # otherwise a "+" would be decoded as a space, and "&" or "#"
129 # would break up the query.
130 query.gsub!("$1", ERB::Util.url_encode(link_value))
131 { :text => value, :url => path + separator + query, :type => :tag2link_link, :key => key }
134 private_class_method def self.email_link(key, value)
135 # Avoid converting conditional tags into emails, since EMAIL_REGEXP is quite permissive
136 return nil unless %w[email contact:email].include? key
138 # Does the value look like an email? eg "someone@domain.tld"
140 # Uses Ruby built-in regexp to validate email.
141 # This will not catch certain valid emails containing comments, whitespace characters,
142 # and quoted strings.
143 # (see: https://github.com/ruby/ruby/blob/master/lib/uri/mailto.rb)
145 # remove any leading and trailing whitespace
148 return unless email.match?(URI::MailTo::EMAIL_REGEXP)
150 [{ :text => ERB::Util.html_escape(email), :url => email, :type => :email_link, :email => email }]
153 private_class_method def self.telephone_links(_key, value)
154 # Does it look like a global phone number? eg "+1 (234) 567-8901 "
155 # or a list of alternate numbers separated by ;
157 # Per RFC 3966, this accepts the visual separators -.() within the number,
158 # which are displayed and included in the tel: URL, and accepts whitespace,
159 # which is displayed but not included in the tel: URL.
160 # (see: http://tools.ietf.org/html/rfc3966#section-5.1.1)
162 # Also accepting / as a visual separator although not given in RFC 3966,
163 # because it is used as a visual separator in OSM data in some countries.
164 if value.match?(%r{^\s*\+[\d\s()/.-]{6,25}\s*(;\s*\+[\d\s()/.-]{6,25}\s*)*$})
165 return value.split(";").map do |phone_number|
166 # for display, remove leading and trailing whitespace
167 phone_number = phone_number.strip
169 # for tel: URL, remove all whitespace
170 # "+1 (234) 567-8901 " -> "tel:+1(234)567-8901"
171 phone_no_whitespace = phone_number.gsub(/\s+/, "")
172 { :text => ERB::Util.html_escape(phone_number), :url => "tel:#{phone_no_whitespace}", :type => :telephone_link, :phone_number => phone_number }
178 private_class_method def self.colour_preview(key, value)
179 return nil unless key =~ /^(?>.+:)?colour$/ && !value.nil? # see discussion at https://github.com/openstreetmap/openstreetmap-website/pull/1779
181 # does value look like a colour? ( 3 or 6 digit hex code or w3c colour name)
183 %w[aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate
184 coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgrey darkgreen darkkhaki darkmagenta darkolivegreen
185 darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey darkturquoise darkviolet deeppink deepskyblue
186 dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray grey green greenyellow honeydew
187 hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray
188 lightgrey lightgreen lightpink lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen
189 linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise
190 mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod
191 palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple red rosybrown royalblue saddlebrown salmon
192 sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow springgreen steelblue tan teal thistle tomato turquoise
193 violet wheat white whitesmoke yellow yellowgreen]
194 return nil unless value =~ /^#([0-9a-fA-F]{3}){1,2}$/ || w3c_colors.include?(value.downcase)
196 [{ :type => :html, :widget => :colour_preview, :colour_value => value }, { :text => value }]
200 @tag2link_dict = build_tag2link_dict(JSON.parse(paths[:tag2link].read)).freeze
201 @wiki_pages_dict = YAML.load_file(paths[:wiki_pages]).freeze
204 def self.build_tag2link_dict(data)
206 "wikipedia.org" => "wikipedia",
207 "wikidata.org" => "wikidata",
208 "commons.wikimedia.org" => "wikimedia_commons",
209 "wiki.openstreetmap.org" => "language"
212 # exclude deprecated, third-party, already handled, static, and non-HTTP URLs
213 .reject { |item| item["rank"] == "deprecated" || item["source"] == "wikidata:P3303" || excluded.any? { |domain, subkey| item["url"].include?(domain) && item["key"].include?(subkey) } }
214 .filter { |item| item["url"].include?("$1") && item["url"].match?(%r{\Ahttps?://[^$]}) }
215 .group_by { |item| item["key"].sub(/^Key:/, "") }
216 .transform_values { |items| choose_best_tag2link_item(items) }
218 .transform_values { |items| items["url"] }
221 private_class_method def self.choose_best_tag2link_item(items)
222 return nil if items.blank?
224 return items.first if items.size == 1
226 # move preferred to the start of the array
227 ranked = items.sort_by { |item| item["rank"] == "preferred" ? 0 : 1 }.uniq { |item| item["url"] }
228 top_rank = ranked.first["rank"]
229 top_items = ranked.select { |i| i["rank"] == top_rank }
231 # if only one top-ranked item, prefer that
232 return top_items.first if top_items.size == 1
234 grouped = top_items.group_by { |i| i["source"] }
235 return nil if grouped.size > 2
237 # if both sources have exactly one preferred, prefer osmwiki
238 return grouped["osmwiki:P8"]&.first || grouped.values.flatten.first if grouped.all? { |_s, vals| vals.size == 1 }
240 # if one source has multiple preferreds and the other has one, prefer the single one
241 return grouped.min_by { |_s, vals| vals.size }.last.first if grouped.any? { |_s, vals| vals.size == 1 }
243 # exclude any that are ambiguous