-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhelpers.rb
More file actions
193 lines (156 loc) · 5 KB
/
helpers.rb
File metadata and controls
193 lines (156 loc) · 5 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
helpers do
include ERB::Util
# Grab a posts title or the default title
def page_title
case
when @post
"#{@post.title} - Nathan Hoad"
when @tag
"Tagged with ##{@tag} - Nathan Hoad"
else
"Nathan Hoad"
end
end
# Get the page description
def page_description
if defined?(@post) && ! @post.nil?
paragraphs = @post.body.split("\n\n")
if paragraphs.first =~ /^\!/
description = paragraphs[1]
else
description = paragraphs.first
end
description.gsub(/<\/?p>/, '').gsub(/\[(.*?)\]\(.*?\)/){ |match| $1 }
elsif defined?(@tag) && ! @tag.nil?
"Posts tagged with ##{@tag}"
else
"I'm Nathan Hoad and I'm a software geek that loves the beauty of simple things. I blog about Ruby, Rails, Sinatra, Git, and Graphic Design"
end
end
# Grab the full url of the current site
def url
request.scheme + '://' + request.host
end
# Render a partial template
#
# eg.
# partial :posts => @posts
# partial :post => post
def partial (template, options = {})
options = { :layout => false }.merge options
if template.is_a? Hash
options[:locals] = template[:locals] || {}
template.delete(:locals) if template[:locals]
options[:locals].merge! template
template = template.reject{ |key, value| [:locals].include? key }.keys.first.to_s
end
haml("_#{template}".to_sym, options)
end
# Convert text to html
def text_to_html (text, args = {})
args = { :emotes => true, :map_headings => 2 }.merge args
html = Moredown.text_to_html(text, args)
html.gsub!('src="media/', 'src="/media/')
html.gsub!(/<pre><code>#! (.*?)\n([^<]*?)<\/code><\/pre>/) { |match| "<pre><code class=\"#{$1.downcase.strip}\">#{$2.strip}</code></pre>" }
if args[:teaser]
paragraphs = html.scan(/<p.*?p>/)
# ignore the first paragraph if it contains only an image
if paragraphs.first =~ /<p><img.+?\/><\/p>/ or paragraphs.first =~ /<p><object.+?<\/object><\/p>/
teaser = paragraphs[1]
else
teaser = paragraphs.first
end
if teaser != nil and teaser != html.strip and args[:slug]
teaser += "<p><a href='/#{args[:slug]}'>Read more...</a></p>"
html = teaser
end
end
html
end
# Pluralise a string
def pluralise (count, one, many = nil)
many = "#{one}s" unless many
if count == 1
"1 #{one}"
else
"#{count} #{many}"
end
end
# Distance of time in words
def time_ago_in_words (time)
t = (Time.now - time).to_i
case
when t < 50.minutes
"#{pluralise((t / 1.minute).round, "minute")} ago"
when t < 90.minutes
"About an hour ago"
when t < 18.hours
"#{pluralise((t / 1.hour).round, "hour")} ago"
when t < 2.day
"About a day ago"
when t < 20.days
"#{pluralise((t / 1.day).round, "day")} ago"
when t < 1.month
"About a month ago"
else
time.strftime("%B %Y")
end
end
# Link to a post unless the post is a link itself
def post_title (post)
if post.link
"<a href='#{post.link}' title='#{post.link}'>★ #{post.title}</a>"
else
link_to_post(post)
end
end
# Link to a url
def link_to (label, url)
"<a href='#{url}'>#{label}</a>"
end
# Link to a post
def link_to_post (post, label = nil)
label ||= post.title
"<a href='/#{post.slug}'>#{label}</a>"
end
# Render the tags for a post
def tags (tags)
tags = [tags] if tags.is_a? String
tags = tags.map{ |tag| "<li><a href=\"/tags/#{tag}\">##{tag}</a></li>" }
"<ul>#{tags.sort.join(' ')}</ul>"
end
# Render links to the next and previous things
def where_to_now? (relative_to)
if relative_to.class == Post
newer_post = Post.previous_before(relative_to)
older_post = Post.next_after(relative_to)
newer_post = (newer_post)? "#{link_to_post(newer_post)} is next" : "<span>Nothing is next</span>"
older_post = (older_post)? "Previously, #{link_to_post(older_post)}" : "<span>Previously, nothing</span>"
options = "<ul data-context=\"posts\"><li>#{newer_post}</li><li>#{older_post}</li></ul>"
else # page number
older_posts = (Post.page(relative_to + 1).empty?)? "<span>No older posts</span>" : "<a href='/?page=#{relative_to + 1}'>Older posts</a>"
newer_posts = (relative_to > 1)? "<a href='/?page=#{relative_to - 1}'>Newer posts</a>" : "<span>No newer posts</span>"
options = "<ul data-context=\"pages\"><li>#{older_posts}</li><li>#{newer_posts}</li></ul>"
end
"<h4>Where to now?</h4>\n#{options}"
end
end
# Extend Fixnum to add some time stuff (like Active Support)
class Fixnum
def minute
self * 60
end
alias :minutes :minute
def hour
self.minutes * 60
end
alias :hours :hour
def day
self.hours * 24
end
alias :days :day
def month
self.days * 30
end
alias :months :month
end