]> git.openstreetmap.org Git - rails.git/blob - app/helpers/browse_helper.rb
Serve OpenSearch definition though the assets pipeline
[rails.git] / app / helpers / browse_helper.rb
1 module BrowseHelper
2   def link_to_page(page, page_param)
3     return link_to(page, page_param => page)
4   end
5
6   def printable_name(object, version=false)
7     if object.id.is_a?(Array)
8       id = object.id[0]
9     else
10       id = object.id
11     end
12     name = t 'printable_name.with_id', :id => id.to_s
13     if version
14       name = t 'printable_name.with_version', :id => name, :version => object.version.to_s
15     end
16     if object.tags.include? "name:#{I18n.locale}"
17       name = t 'printable_name.with_name',  :name => object.tags["name:#{I18n.locale}"].to_s, :id => name
18     elsif object.tags.include? 'name'
19       name = t 'printable_name.with_name',  :name => object.tags['name'].to_s, :id => name
20     end
21     return name
22   end
23
24   def link_class(type, object)
25     return type + " " + h(icon_tags(object).join(' ')) + (object.visible == false ? ' deleted' : '')
26   end
27
28   def link_title(object)
29     return h(icon_tags(object).map { |k,v| k + '=' + v }.to_sentence)
30   end
31
32   def format_key(key)
33     if url = wiki_link("key", key)
34       link_to h(key), url, :title => t('browse.tag_details.wiki_link.key', :key => key)
35     else
36       h(key)
37     end
38   end
39
40   def format_value(key, value)
41     if wp = wikipedia_link(key, value)
42       link_to h(wp[:title]), wp[:url], :title => t('browse.tag_details.wikipedia_link', :page => wp[:title])
43     elsif url = wiki_link("tag", "#{key}=#{value}")
44       link_to h(value), url, :title => t('browse.tag_details.wiki_link.tag', :key => key, :value => value)
45     else
46       linkify h(value)
47     end
48   end
49
50 private
51
52   ICON_TAGS = [ 
53     "aeroway", "amenity", "barrier", "building", "highway", "historic", "landuse",
54     "leisure", "man_made", "natural", "railway", "shop", "tourism", "waterway"
55   ]
56
57   def icon_tags(object)
58     object.tags.find_all { |k,v| ICON_TAGS.include? k }
59   end
60
61   def wiki_link(type, lookup)
62     locale = I18n.locale.to_s
63
64     # update-wiki-pages does s/ /_/g on keys before saving them, we
65     # have to replace spaces with underscore so we'll link
66     # e.g. `source=Isle of Man Government aerial imagery (2001)' to
67     # the correct page.
68     lookup_us = lookup.tr(" ", "_")
69
70     if page = WIKI_PAGES[locale][type][lookup_us] rescue nil
71       url = "http://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
72     elsif page = WIKI_PAGES["en"][type][lookup_us] rescue nil
73       url = "http://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
74     end
75
76     return url
77   end
78
79   def wikipedia_link(key, value)
80     # Some k/v's are wikipedia=http://en.wikipedia.org/wiki/Full%20URL
81     return nil if value =~ /^http:\/\//
82
83     if key == "wikipedia"
84       # This regex should match Wikipedia language codes, everything
85       # from de to zh-classical
86       if value =~ /^([a-z-]{2,12}):(.+)$/
87         # Value is <lang>:<title> so split it up
88         lang  = $1
89         value = $2
90       else
91         # Value is <title> so default to English Wikipedia
92         lang = 'en'
93       end
94     elsif key =~ /^wikipedia:(\S+)$/
95       # Language is in the key, so assume value is a simple title
96       lang = $1
97     else
98       # Not a wikipedia key!
99       return nil
100     end
101
102     return {
103       :url => "http://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}",
104       :title => value
105     }
106   end
107 end