1 module ApplicationHelper
2 require 'rexml/document'
5 Sanitize.clean(text, Sanitize::Config::OSM).html_safe
9 return linkify(sanitize(simple_format(text)))
14 Rinku.auto_link(text, :urls, tag_options(:rel => "nofollow")).html_safe
16 Rinku.auto_link(text, :urls, tag_options(:rel => "nofollow"))
20 def html_escape_unicode(text)
21 chars = ActiveSupport::Multibyte::Unicode.u_unpack(text).map do |c|
22 c < 127 ? c.chr : "&##{c.to_s};"
28 def rss_link_to(*args)
29 return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
32 def atom_link_to(*args)
33 return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
36 def javascript_strings
39 js << "<script type='text/javascript'>\n"
40 js << "i18n_strings = new Array();\n"
41 js << javascript_strings_for_key("javascripts")
50 css << ".hidden { display: none }";
51 css << ".hide_unless_logged_in { display: none }" unless @user;
52 css << ".hide_if_logged_in { display: none }" if @user;
53 css << ".hide_if_user_#{@user.id} { display: none }" if @user;
54 css << ".show_if_user_#{@user.id} { display: inline }" if @user;
55 css << ".hide_unless_administrator { display: none }" unless @user and @user.administrator?;
57 return content_tag(:style, css, :type => "text/css")
60 def if_logged_in(tag = :div, &block)
61 content_tag(tag, capture(&block), :class => "hide_unless_logged_in")
64 def if_not_logged_in(tag = :div, &block)
65 content_tag(tag, capture(&block), :class => "hide_if_logged_in")
68 def if_user(user, tag = :div, &block)
70 content_tag(tag, capture(&block), :class => "hidden show_if_user_#{user.id}")
74 def unless_user(user, tag = :div, &block)
76 content_tag(tag, capture(&block), :class => "hide_if_user_#{user.id}")
78 content_tag(tag, capture(&block))
82 def if_administrator(tag = :div, &block)
83 content_tag(tag, capture(&block), :class => "hide_unless_administrator")
86 def describe_location(lat, lon, zoom = nil, language = nil)
88 language = language || request.user_preferred_languages.join(',')
89 url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
92 response = OSM::Timer.timeout(4) do
93 REXML::Document.new(Net::HTTP.get(URI.parse(url)))
99 if response and result = response.get_text("reversegeocode/result")
102 "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}"
106 def user_image(user, options = {})
107 options[:class] ||= "user_image"
110 image_tag url_for_file_column(user, "image"), options
112 image_tag "anon_large.png", options
116 def user_thumbnail(user, options = {})
117 options[:class] ||= "user_thumbnail"
120 image_tag url_for_file_column(user, "image"), options
122 image_tag "anon_small.png", options
129 elsif @user and @user.preferred_editor
130 @user.preferred_editor
138 def javascript_strings_for_key(key)
140 value = I18n.t(key, :locale => "en")
142 if value.is_a?(String)
143 js << "i18n_strings['#{key}'] = '" << escape_javascript(t(key)) << "';\n"
145 value.each_key do |k|
146 js << javascript_strings_for_key("#{key}.#{k}")