/
kind.rb
70 lines (65 loc) · 2.08 KB
/
kind.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
64
65
66
67
68
69
70
require 'vanilla/dynasnip'
require 'atom'
require 'date'
class Kind < Dynasnip
def handle(kind, limit=10, as=:html)
as = as.to_sym
snips = Soup.sieve(:kind => kind)
entries = snips.sort_by { |s| s.created_at || '' }.reverse[0...limit.to_i].map do |snip|
render_entry_in_template(snip, as, kind)
end
render_entry_collection(snips, entries, as, kind)
end
def render_entry_in_template(snip, as, kind)
rendered_contents = prepare_snip_contents(snip)
case as
when :html
snip_template.
gsub('SNIP_KIND', kind).
gsub('SNIP_NAME', snip.name).
gsub('CREATED_AT', snip.created_at || '').
gsub('SNIP_CONTENT', rendered_contents)
when :xml
Atom::Entry.new do |e|
e.published = DateTime.parse(snip.created_at)
e.updated = DateTime.parse(snip.updated_at || snip.created_at)
e.content = Atom::Content::Html.new(rendered_contents)
e.title = snip.name
e.authors = [Atom::Person.new(:name => snip.author || domain)]
e.links << Atom::Link.new(:href => "http://#{domain}#{Vanilla::Routes.url_to(snip.name)}")
e.id = "tag:#{domain},#{(snip.created_at || Date.today.to_s).split[0]}:/#{snip.name}"
end
end
end
def prepare_snip_contents(snip)
rendered_snip = app.render(snip)
# make all the links absolute
rendered_snip.gsub(/href="\//, "href=\"http://#{domain}/")
end
def render_entry_collection(snips, entries, as, kind)
case as
when :html
entries.join
when :xml
Atom::Feed.new do |f|
f.title = feed_title
f.updated = DateTime.parse(snips[0].updated_at)
f.id = "tag:#{domain},2008-06-01:kind/#{kind}"
f.entries = entries
end.to_xml
end
end
attribute :feed_title, "Your Blog"
attribute :domain, "yourdomain.com"
attribute :snip_template, %{
<div class="snip SNIP_KIND">
<div class="details">
#{Vanilla::Routes.link_to '#', 'SNIP_NAME'}
<p class="created_at">CREATED_AT</p>
</div>
<div class="content">
SNIP_CONTENT
</div>
</div>
}
end