1 module ApplicationHelper
2 require 'rexml/document'
6 Rinku.auto_link(text, :urls, tag_options(:rel => "nofollow")).html_safe
8 Rinku.auto_link(text, :urls, tag_options(:rel => "nofollow"))
12 def html_escape_unicode(text)
13 chars = ActiveSupport::Multibyte::Unicode.u_unpack(text).map do |c|
14 c < 127 ? c.chr : "&##{c.to_s};"
20 def rss_link_to(*args)
21 return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
24 def atom_link_to(*args)
25 return link_to(image_tag("RSS.gif", :size => "16x16", :border => 0), Hash[*args], { :class => "rsssmall" });
28 def javascript_strings
31 js << "<script type='text/javascript'>\n"
32 js << "i18n_strings = new Array();\n"
33 js << javascript_strings_for_key("javascripts")
42 css << ".hidden { display: none }";
43 css << ".hide_unless_logged_in { display: none }" unless @user;
44 css << ".hide_if_logged_in { display: none }" if @user;
45 css << ".hide_if_user_#{@user.id} { display: none }" if @user;
46 css << ".show_if_user_#{@user.id} { display: inline }" if @user;
47 css << ".hide_unless_administrator { display: none }" unless @user and @user.administrator?;
49 return content_tag(:style, css, :type => "text/css")
52 def if_logged_in(tag = :div, &block)
53 content_tag(tag, capture(&block), :class => "hide_unless_logged_in")
56 def if_not_logged_in(tag = :div, &block)
57 content_tag(tag, capture(&block), :class => "hide_if_logged_in")
60 def if_user(user, tag = :div, &block)
62 content_tag(tag, capture(&block), :class => "hidden show_if_user_#{user.id}")
66 def unless_user(user, tag = :div, &block)
68 content_tag(tag, capture(&block), :class => "hide_if_user_#{user.id}")
70 content_tag(tag, capture(&block))
74 def if_administrator(tag = :div, &block)
75 content_tag(tag, capture(&block), :class => "hide_unless_administrator")
78 def describe_location(lat, lon, zoom = nil, language = nil)
80 language = language || request.user_preferred_languages.join(',')
81 url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
84 response = OSM::Timer.timeout(4) do
85 REXML::Document.new(Net::HTTP.get(URI.parse(url)))
91 if response and result = response.get_text("reversegeocode/result")
94 "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}"
98 def user_image(user, options = {})
99 options[:class] ||= "user_image"
101 image_tag user.image.url(:large), options
104 def user_thumbnail(user, options = {})
105 options[:class] ||= "user_thumbnail"
107 image_tag user.image.url(:small), options
113 elsif @user and @user.preferred_editor
114 @user.preferred_editor
120 def scale_to_zoom(scale)
121 Math.log(360.0 / (scale.to_f * 512.0)) / Math.log(2.0)
124 def richtext_area(object_name, method, options = {})
125 id = "#{object_name.to_s}_#{method.to_s}"
126 format = options.delete(:format) || "markdown"
128 content_tag(:div, :id => "#{id}_container", :class => "richtext_container") do
129 output_buffer << content_tag(:div, :id => "#{id}_content", :class => "richtext_content") do
130 output_buffer << text_area(object_name, method, options.merge("data-preview-url" => preview_url(:format => format)))
131 output_buffer << content_tag(:div, "", :id => "#{id}_preview", :class => "richtext_preview")
134 output_buffer << content_tag(:div, :id => "#{id}_help", :class => "richtext_help") do
135 output_buffer << render("site/#{format}_help")
136 output_buffer << submit_tag(I18n.t("site.richtext_area.edit"), :id => "#{id}_doedit", :class => "richtext_doedit", :disabled => true)
137 output_buffer << submit_tag(I18n.t("site.richtext_area.preview"), :id => "#{id}_dopreview", :class => "richtext_dopreview")
144 def javascript_strings_for_key(key)
146 value = I18n.t(key, :locale => "en")
148 if value.is_a?(String)
149 js << "i18n_strings['#{key}'] = '" << escape_javascript(t(key)) << "';\n"
151 value.each_key do |k|
152 js << javascript_strings_for_key("#{key}.#{k}")