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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
gem 'activesupport', ">= 2.3.2"
require 'active_support'
require 'rubypants'
module Helpers
module EscapeHelper
HTML_ESCAPE = { '&' => '& ', '>' => '>', '<' => '<', '"' => '"' }
JSON_ESCAPE = { '&' => '\u0026 ', '>' => '\u003E', '<' => '\u003C' }
# A utility method for escaping HTML tag characters.
# This method is also aliased as <tt>h</tt>.
#
# In your ERb templates, use this method to escape any unsafe content. For example:
# <%=h @person.name %>
#
# ==== Example:
# puts html_escape("is a > 0 & a < 10?")
# # => is a > 0 & a < 10?
def html_escape(html)
html.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }
end
def escape_once(html)
html.to_s.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| HTML_ESCAPE[special] }
end
alias h escape_once
# A utility method for escaping HTML entities in JSON strings.
# This method is also aliased as <tt>j</tt>.
#
# In your ERb templates, use this method to escape any HTML entities:
# <%=j @person.to_json %>
#
# ==== Example:
# puts json_escape("is a > 0 & a < 10?")
# # => is a \u003E 0 \u0026 a \u003C 10?
def json_escape(s)
s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] }
end
alias j json_escape
end
include EscapeHelper
module ParamsHelper
def params
@params ||= begin
q = request.query.dup
q.each { |(k,v)| q[k.to_s.intern] = v }
q
end
end
end
include ParamsHelper
module TagHelper
def content_tag(name, content, html_options={})
%{<#{name}#{html_attributes(html_options)}>#{content}</#{name}>}
end
def tag(name, html_options={})
%{<#{name}#{html_attributes(html_options)} />}
end
def image_tag(src, html_options = {})
tag(:img, html_options.merge({:src=>src}))
end
def javascript_tag(content = nil, html_options = {})
content_tag(:script, javascript_cdata_section(content), html_options.merge(:type => "text/javascript"))
end
def link_to(name, href, html_options = {})
html_options = html_options.stringify_keys
confirm = html_options.delete("confirm")
onclick = "if (!confirm('#{html_escape(confirm)}')) return false;" if confirm
content_tag(:a, name, html_options.merge(:href => href, :onclick=>onclick))
end
def link_to_function(name, *args, &block)
html_options = {}
html_options = args.pop if args.last.is_a? Hash
function = args[0] || ''
onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;"
href = html_options[:href] || '#'
content_tag(:a, name, html_options.merge(:href => href, :onclick => onclick))
end
private
def cdata_section(content)
"<![CDATA[#{content}]]>"
end
def javascript_cdata_section(content) #:nodoc:
"\n//#{cdata_section("\n#{content}\n//")}\n"
end
def html_attributes(options)
unless options.blank?
attrs = []
options.each_pair do |key, value|
if value == true
attrs << %(#{key}="#{key}") if value
else
attrs << %(#{key}="#{value}") unless value.nil?
end
end
" #{attrs.sort * ' '}" unless attrs.empty?
end
end
end
include TagHelper
# My added helpers
def to_html_email(address)
email = string_to_html(address)
"<a href=\"#{string_to_html('mailto:')}#{email}\">#{email}</a>"
end
def string_to_html(s)
s.strip.unpack("C*").map{|ch| "&#" + ch.to_s + ";" }.to_s
end
def show_part (file)
data = ''
f = File.open(Dir.pwd+"/source/"+file)
f.each_line do |line|
data += line
end
data
end
def shorten_words (string, word_limit = 25)
words = string.split(/\s/)
if words.size >= word_limit
words[0,(word_limit-1)].join(" ") + '…'
else
string
end
end
def shorten (string, char_limit = 55)
chars = string.scan(/.{1,1}/)
if chars.size >= char_limit
chars[0,(char_limit-1)].join + '…'
else
"blah2"
end
end
def absolute_url(input, url)
input.gsub(/(href|src)(\s*=\s*)(["'])(\/.*?)\3/) { $1 + $2 + $3 + url + $4 + $3 }
end
def rp(input)
RubyPants.new(input).to_html
end
def style_amp(input)
input.gsub(" & "," <span class='amp'>&</span> ")
end
end
|