X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/9b39577f4646f3624cf24aaa04b06b6f053c3270..HEAD:/app/helpers/browse_tags_helper.rb?ds=inline diff --git a/app/helpers/browse_tags_helper.rb b/app/helpers/browse_tags_helper.rb index f95003aae..867fe1273 100644 --- a/app/helpers/browse_tags_helper.rb +++ b/app/helpers/browse_tags_helper.rb @@ -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|buried|flag|genus|manufacturer|model|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) @@ -30,9 +34,13 @@ 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 + svg = tag.svg :width => 14, :height => 14, :class => "float-end m-1" do + concat tag.title t("browse.tag_details.colour_preview", :colour_value => colour_value) + concat tag.rect :x => 0.5, :y => 0.5, :width => 13, :height => 13, :fill => colour_value, :stroke => "#2222" + end + svg + colour_value else - safe_join(value.split(";").map { |x| linkify(h(x)) }, ";") + safe_join(value.split(";", -1).map { |x| linkify(h(x)) }, ";") end end @@ -60,40 +68,32 @@ module BrowseTagsHelper return nil if %r{^https?://}.match?(value) case key - when "wikipedia" - # This regex should match Wikipedia language codes, everything - # from de to zh-classical - lang = if value =~ /^([a-z-]{2,12}):(.+)$/i - # Value is : so split it up - # Note that value is always left as-is, see: https://trac.openstreetmap.org/ticket/4315 - Regexp.last_match(1) - else - # Value is <title> so default to English Wikipedia - "en" - end + when "wikipedia", /^(#{SECONDARY_WIKI_PREFIXES}):wikipedia/o + lang = "en" when /^wikipedia:(\S+)$/ - # Language is in the key, so assume value is the title lang = Regexp.last_match(1) else - # Not a wikipedia key! return nil end - if value =~ /^([^#]*)#(.*)/ - # Contains a reference to a section of the wikipedia article - # Must break it up to correctly build the url - value = Regexp.last_match(1) - section = "##{Regexp.last_match(2)}" - encoded_section = "##{CGI.escape(Regexp.last_match(2).gsub(/ +/, '_'))}" + # This regex should match Wikipedia language codes, everything + # from de to zh-classical + if value =~ /^([a-z-]{2,12}):(.+)$/i + lang = Regexp.last_match(1) + title_section = Regexp.last_match(2) else - section = "" - encoded_section = "" + title_section = value end - { - :url => "https://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}#{encoded_section}", - :title => value + section - } + title, section = title_section.split("#", 2) + url = "https://#{lang}.wikipedia.org/wiki/#{wiki_encode(title)}?uselang=#{I18n.locale}" + url += "##{wiki_encode(section)}" if section + + { :url => url, :title => value } + end + + def wiki_encode(s) + u s.tr(" ", "_") end def wikidata_links(key, value) @@ -104,7 +104,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