]> git.openstreetmap.org Git - rails.git/blob - app/helpers/application_helper.rb
Merge branch 'master' into openid
[rails.git] / app / helpers / application_helper.rb
1 module ApplicationHelper
2   require 'rexml/document'
3
4   def sanitize(text)
5     Sanitize.clean(text, Sanitize::Config::OSM)
6   end
7
8   def htmlize(text)
9     return linkify(sanitize(simple_format(text)))
10   end
11
12   def linkify(text)
13     return auto_link(text, :link => :urls, :html => { :rel => "nofollow" })
14   end
15
16   def html_escape_unicode(text)
17     chars = ActiveSupport::Multibyte::Chars.u_unpack(text).map do |c|
18       c < 127 ? c.chr : "&##{c.to_s};"
19     end
20
21     return chars.join("")
22   end
23
24   def rss_link_to(*args)
25     return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
26   end
27
28   def atom_link_to(*args)
29     return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
30   end
31
32   def javascript_strings
33     js = ""
34
35     js << "<script type='text/javascript'>\n"
36     js << "i18n_strings = new Array();\n"
37     js << javascript_strings_for_key("javascripts")
38     js << "</script>\n"
39
40     return js
41   end
42
43   def describe_location(lat, lon, zoom = nil, language = nil)
44     zoom = zoom || 14
45     language = language || request.user_preferred_languages.join(',')
46     url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
47     response = REXML::Document.new(Net::HTTP.get(URI.parse(url)))
48
49     if result = response.get_text("reversegeocode/result")
50       result.to_s
51     else
52       "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}"
53     end
54   end
55
56   def user_image(user, options = {})
57     options[:class] ||= "user_image"
58
59     if user.image
60       image_tag url_for_file_column(user, "image"), options
61     else
62       image_tag "anon_large.png", options
63     end
64   end
65
66   def user_thumbnail(user, options = {})
67     options[:class] ||= "user_thumbnail"
68
69     if user.image
70       image_tag url_for_file_column(user, "image"), options
71     else
72       image_tag "anon_small.png", options
73     end
74   end
75
76 private
77
78   def javascript_strings_for_key(key)
79     js = ""
80     value = t(key, :locale => "en")
81
82     if value.is_a?(String)
83       js << "i18n_strings['#{key}'] = '" << escape_javascript(t(key)) << "';\n"
84     else
85       value.each_key do |k|
86         js << javascript_strings_for_key("#{key}.#{k}")
87       end
88     end
89
90     return js
91   end
92 end