forked from jrmehle/sinatra-baseapp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.rb
63 lines (49 loc) · 1.89 KB
/
helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module Helpers
include Rack::Utils
alias_method :h, :escape_html
def image_tag(path_to_file, options = {})
path_to_file = File.join '/images', path_to_file
"<img src=\"#{path_to_file}\" width=\"#{options[:width]}\" height=\"#{options[:height]}\" alt=\"#{options[:alt]}\" #{'id="' + options[:id] + '"' if options[:id]} #{'class="' + options[:class] + '"' if options[:class]} #{'title="' + options[:title] + '"' if options[:title]} />"
end
def current_page?(path)
path == request.path_info
end
def link_to_unless_current(text, path = "#")
if current_page? path
"<span id=\"current\">#{text}</span>"
else
"<a href=\"#{path}\">#{text}</a>"
end
end
def strip_tags(str)
str.gsub(/<\/?[^>]*>/, "")
end
# Ripped off this functionality from the Rails helpers
def mail_to(email_address = '', text = nil, options = {})
string = ''
email_address = email_address.to_s
email_address_obfuscated = email_address.dup
email_address_encoded = ''
email_address_obfuscated.each_byte do |c|
email_address_encoded << sprintf("&#%d;", c)
end
protocol = 'mailto:'
protocol.each_byte { |c| string << sprintf("&#%d;", c) }
email_address.each_byte do |c|
char = c.chr
string << (char =~ /\w/ ? sprintf("%%%x", c) : char)
end
"<a href=\"#{string}\" class=\"email #{options[:class] if options.include?(:class)}\">#{text || email_address_encoded}</a>"
end
def stylesheet_for_current_page
stylesheet = request.path_info.gsub('/', '')
stylesheet = 'index' if request.path_info == '/'
return "<link rel=\"stylesheet\" href=\"/stylesheets/#{stylesheet}.css\" type=\"text/css\" media=\"screen\" charset=\"utf-8\">"
end
def stylesheet_for_current_page_if(condition)
stylesheet_for_current_page if condition
end
def use_page_stylesheet?
%w{ }.include? request.path_info
end
end