1 module ApplicationHelper
2 require 'rexml/document'
5 Sanitize.clean(text, Sanitize::Config::OSM)
9 return linkify(sanitize(simple_format(text)))
13 return auto_link(text, :link => :urls, :html => { :rel => "nofollow" })
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};"
24 def rss_link_to(*args)
25 return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
28 def atom_link_to(*args)
29 return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
32 def javascript_strings
35 js << "<script type='text/javascript'>\n"
36 js << "i18n_strings = new Array();\n"
37 js << javascript_strings_for_key("javascripts")
46 css << ".hidden { display: none }";
47 css << ".hide_unless_logged_in { display: none }" unless @user;
48 css << ".hide_if_logged_in { display: none }" if @user;
49 css << ".hide_if_user_#{@user.id} { display: none }" if @user;
50 css << ".show_if_user_#{@user.id} { display: inline }" if @user;
51 css << ".hide_unless_administrator { display: none }" unless @user and @user.administrator?;
53 return content_tag(:style, css)
56 def if_logged_in(tag = :div, &block)
57 concat(content_tag(tag, capture(&block), :class => "hide_unless_logged_in"))
60 def if_not_logged_in(tag = :div, &block)
61 concat(content_tag(tag, capture(&block), :class => "hide_if_logged_in"))
64 def if_user(user, tag = :div, &block)
66 concat(content_tag(tag, capture(&block), :class => "hidden show_if_user_#{user.id}"))
70 def unless_user(user, tag = :div, &block)
72 concat(content_tag(tag, capture(&block), :class => "hide_if_user_#{user.id}"))
74 concat(content_tag(tag, capture(&block)))
78 def if_administrator(tag = :div, &block)
79 concat(content_tag(tag, capture(&block), :class => "hide_unless_administrator"))
82 def describe_location(lat, lon, zoom = nil, language = nil)
84 language = language || request.user_preferred_languages.join(',')
85 url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
88 response = Timeout::timeout(4) do
89 REXML::Document.new(Net::HTTP.get(URI.parse(url)))
95 if response and result = response.get_text("reversegeocode/result")
98 "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}"
102 def user_image(user, options = {})
103 options[:class] ||= "user_image"
106 image_tag url_for_file_column(user, "image"), options
108 image_tag "anon_large.png", options
112 def user_thumbnail(user, options = {})
113 options[:class] ||= "user_thumbnail"
116 image_tag url_for_file_column(user, "image"), options
118 image_tag "anon_small.png", options
125 elsif @user and @user.preferred_editor
126 @user.preferred_editor
134 def javascript_strings_for_key(key)
136 value = I18n.t(key, :locale => "en")
138 if value.is_a?(String)
139 js << "i18n_strings['#{key}'] = '" << escape_javascript(t(key)) << "';\n"
141 value.each_key do |k|
142 js << javascript_strings_for_key("#{key}.#{k}")