]> git.openstreetmap.org Git - rails.git/blob - app/helpers/browse_helper.rb
Improve styling of "signup blocked" page
[rails.git] / app / helpers / browse_helper.rb
1 require "uri"
2
3 module BrowseHelper
4   def printable_name(object, version = false)
5     id = if object.id.is_a?(Array)
6            object.id[0]
7          else
8            object.id
9          end
10     name = t "printable_name.with_id", :id => id.to_s
11     if version
12       name = t "printable_name.with_version", :id => name, :version => object.version.to_s
13     end
14
15     # don't look at object tags if redacted, so as to avoid giving
16     # away redacted version tag information.
17     unless object.redacted?
18       locale = I18n.locale.to_s
19
20       while locale =~ /-[^-]+/ && !object.tags.include?("name:#{I18n.locale}")
21         locale = locale.sub(/-[^-]+/, "")
22       end
23
24       if object.tags.include? "name:#{locale}"
25         name = t "printable_name.with_name_html", :name => content_tag(:bdi, object.tags["name:#{locale}"].to_s), :id => content_tag(:bdi, name)
26       elsif object.tags.include? "name"
27         name = t "printable_name.with_name_html", :name => content_tag(:bdi, object.tags["name"].to_s), :id => content_tag(:bdi, name)
28       end
29     end
30
31     name
32   end
33
34   def link_class(type, object)
35     classes = [type]
36
37     if object.redacted?
38       classes << "deleted"
39     else
40       classes += icon_tags(object).flatten.map { |t| h(t) }
41       classes << "deleted" unless object.visible?
42     end
43
44     classes.join(" ")
45   end
46
47   def link_title(object)
48     if object.redacted?
49       ""
50     else
51       h(icon_tags(object).map { |k, v| k + "=" + v }.to_sentence)
52     end
53   end
54
55   def link_follow(object)
56     "nofollow" if object.tags.empty?
57   end
58
59   def format_key(key)
60     if url = wiki_link("key", key)
61       link_to h(key), url, :title => t("browse.tag_details.wiki_link.key", :key => key)
62     else
63       h(key)
64     end
65   end
66
67   def format_value(key, value)
68     if wp = wikipedia_link(key, value)
69       link_to h(wp[:title]), wp[:url], :title => t("browse.tag_details.wikipedia_link", :page => wp[:title])
70     elsif wdt = wikidata_link(key, value)
71       link_to h(wdt[:title]), wdt[:url], :title => t("browse.tag_details.wikidata_link", :page => wdt[:title])
72     elsif url = wiki_link("tag", "#{key}=#{value}")
73       link_to h(value), url, :title => t("browse.tag_details.wiki_link.tag", :key => key, :value => value)
74     elsif url = telephone_link(key, value)
75       link_to h(value), url, :title => t("browse.tag_details.telephone_link", :phone_number => value)
76     else
77       linkify h(value)
78     end
79   end
80
81   def type_and_paginated_count(type, pages)
82     if pages.page_count == 1
83       t "browse.changeset.#{type}",
84         :count => pages.item_count
85     else
86       t "browse.changeset.#{type}_paginated",
87         :x => pages.current_page.first_item,
88         :y => pages.current_page.last_item,
89         :count => pages.item_count
90     end
91   end
92
93   private
94
95   ICON_TAGS = %w(aeroway amenity barrier building highway historic landuse leisure man_made natural railway shop tourism waterway).freeze
96
97   def icon_tags(object)
98     object.tags.find_all { |k, _v| ICON_TAGS.include? k }.sort
99   end
100
101   def wiki_link(type, lookup)
102     locale = I18n.locale.to_s
103
104     # update-wiki-pages does s/ /_/g on keys before saving them, we
105     # have to replace spaces with underscore so we'll link
106     # e.g. `source=Isle of Man Government aerial imagery (2001)' to
107     # the correct page.
108     lookup_us = lookup.tr(" ", "_")
109
110     if page = WIKI_PAGES[locale][type][lookup_us] rescue nil
111       url = "http://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
112     elsif page = WIKI_PAGES["en"][type][lookup_us] rescue nil
113       url = "http://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
114     end
115
116     url
117   end
118
119   def wikipedia_link(key, value)
120     # Some k/v's are wikipedia=http://en.wikipedia.org/wiki/Full%20URL
121     return nil if value =~ %r{^https?://}
122
123     if key == "wikipedia"
124       # This regex should match Wikipedia language codes, everything
125       # from de to zh-classical
126       lang = if value =~ /^([a-z-]{2,12}):(.+)$/i
127                # Value is <lang>:<title> so split it up
128                # Note that value is always left as-is, see: https://trac.openstreetmap.org/ticket/4315
129                $1
130              else
131                # Value is <title> so default to English Wikipedia
132                "en"
133              end
134     elsif key =~ /^wikipedia:(\S+)$/
135       # Language is in the key, so assume value is the title
136       lang = $1
137     else
138       # Not a wikipedia key!
139       return nil
140     end
141
142     if value =~ /^([^#]*)#(.*)/
143       # Contains a reference to a section of the wikipedia article
144       # Must break it up to correctly build the url
145       value = $1
146       section = "#" + $2
147       encoded_section = "#" + URI.encode($2.gsub(/ +/, "_"), /[^A-Za-z0-9:_]/).tr("%", ".")
148     else
149       section = ""
150       encoded_section = ""
151     end
152
153     {
154       :url => "http://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}#{encoded_section}",
155       :title => value + section
156     }
157   end
158
159   def wikidata_link(key, value)
160     if key == "wikidata" && value =~ /^[Qq][1-9][0-9]*$/
161       return {
162         :url => "//www.wikidata.org/wiki/#{value}?uselang=#{I18n.locale}",
163         :title => value
164       }
165     end
166     nil
167   end
168
169   def telephone_link(_key, value)
170     # does it look like a phone number? eg "+1 (234) 567-8901 " ?
171     return nil unless value =~ %r{^\s*\+[\d\s\(\)/\.-]{6,25}\s*$}
172
173     # remove all whitespace instead of encoding it http://tools.ietf.org/html/rfc3966#section-5.1.1
174     # "+1 (234) 567-8901 " -> "+1(234)567-8901"
175     value_no_whitespace = value.gsub(/\s+/, "")
176
177     "tel:#{value_no_whitespace}"
178   end
179 end