]> git.openstreetmap.org Git - rails.git/blob - app/helpers/browse_helper.rb
Rename css_class to link_class for consistency
[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     name = t 'printable_name.with_id', :id => object.id.to_s
8     if version
9       name = t 'printable_name.with_version', :id => name, :version => object.version.to_s
10     end
11     if object.tags.include? "name:#{I18n.locale}"
12       name = t 'printable_name.with_name',  :name => object.tags["name:#{I18n.locale}"].to_s, :id => name
13     elsif object.tags.include? 'name'
14       name = t 'printable_name.with_name',  :name => object.tags['name'].to_s, :id => name
15     end
16     return name
17   end
18
19   def link_class(type, object)
20     return type + " " + h(icon_tags(object).join(' '))
21   end
22
23   def link_title(object)
24     return h(icon_tags(object).map { |k,v| k + '=' + v }.to_sentence)
25   end
26
27   def format_key(key)
28     if url = wiki_link("key", key)
29       link_to h(key), url, :title => t('browse.tag_details.wiki_link.key', :key => key)
30     else
31       h(key)
32     end
33   end
34
35   def format_value(key, value)
36     if wp = wikipedia_link(key, value)
37       link_to h(wp[:title]), wp[:url], :title => t('browse.tag_details.wikipedia_link', :page => wp[:title])
38     elsif url = wiki_link("tag", "#{key}=#{value}")
39       link_to h(value), url, :title => t('browse.tag_details.wiki_link.tag', :key => key, :value => value)
40     else
41       linkify h(value)
42     end
43   end
44
45 private
46
47   ICON_TAGS = [ 
48     "aeroway", "amenity", "barrier", "building", "highway", "landuse",
49     "leisure", "man_made", "natural", "railway", "shop", "tourism", "waterway"
50   ]
51
52   def icon_tags(object)
53     object.tags.find_all { |k,v| ICON_TAGS.include? k }
54   end
55
56   def wiki_link(type, lookup)
57     locale = I18n.locale.to_s
58
59     if page = WIKI_PAGES[locale][type][lookup] rescue nil
60       url = "http://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
61     elsif page = WIKI_PAGES["en"][type][lookup] rescue nil
62       url = "http://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
63     end
64
65     return url
66   end
67
68   def wikipedia_link(key, value)
69     # Some k/v's are wikipedia=http://en.wikipedia.org/wiki/Full%20URL
70     return nil if value =~ /^http:\/\//
71
72     if key == "wikipedia"
73       # This regex should match Wikipedia language codes, everything
74       # from de to zh-classical
75       if value =~ /^([a-z-]{2,12}):(.+)$/
76         # Value is <lang>:<title> so split it up
77         lang  = $1
78         value = $2
79       else
80         # Value is <title> so default to English Wikipedia
81         lang = 'en'
82       end
83     elsif key =~ /^wikipedia:(\S+)$/
84       # Language is in the key, so assume value is a simple title
85       lang = $1
86     else
87       # Not a wikipedia key!
88       return nil
89     end
90
91     return {
92       :url => "http://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}",
93       :title => value
94     }
95   end
96 end