summaryrefslogtreecommitdiff
path: root/lib/page.rb
blob: b349dc8f5a5b9c60f803060ae945dd5499660ac1 (plain)
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
# coding: utf-8
require 'erb'
require 'set'

require 'category'
require 'sitegen'

class Page
	# Page is an abstract class.
	#
	# Implementors must implement several methods:
	#
	#     def url => URI
	#     def atom_title => String
	#     def atom_author => Person
	#     def atom_content => html | nil
	#     def atom_rights => html | nil
	#
	#     def page_categories => String | Enumerable<String>
	#
	#     def page_published => Time | nil
	#     def page_updated => Time | nil
	#     def page_years => Enumerable<Fixnum>
	def initialize
		Sitegen::add(self)
	end

	def atom_categories # => Enumerable<Category>
		if @categories.nil?
			raw = page_categories
			if raw.is_a?(String)
				raw = raw.split
			end
			@categories = raw.map{|abbr|Category.new(abbr)}
		end
		@categories
	end

	def atom_published # => Time | nil
		if @published.nil?
			unless page_published.nil?
				@published = page_published
			else
				unless page_updated.nil?
					@published = page_updated
				end
			end
			# sanity check
			unless page_published.nil? or page_updated.nil?
				if page_updated < page_published
					@published = page_updated
				end
			end
		end
		@published
	end

	def atom_updated # => Time | nil
		if @updated.nil?
			unless page_updated.nil?
				@updated = page_updated
			else
				unless page_published.nil?
					@updated = page_published
				end
			end
		end
		@updated
	end

	def years # => Enumerable<Fixnum>
		if @years.nil?
			if atom_published.nil? || atom_updated.nil?
				@years = Set[]
			else
				first = atom_published.year
				last = atom_updated.year

				years = page_years
				years.add(first)
				years.add(last)

				@years = Set[*years.select{|i|i >= first && i <= last}]
			end
		end
		@years
	end

	def index_class
		return ''
	end
	def index_link(cururl, depth)
		# FIXME: This code is super gross.
		ret = " * <span><a class=\"#{index_class}\" href=\"#{cururl.route_to(url)}\" title=\"Published on #{atom_published.strftime('%Y-%m-%d')}"
		if atom_updated != atom_published
			ret += " (updated on #{atom_updated.strftime('%Y-%m-%d')})"
		end
		ret += "\">#{atom_title}</a></span><span>"
		atom_categories.each do |t|
			ret += t.html
		end
		ret += "</span>\n"
	end
end

ERB::new(File::read("tmpl/page.atom.erb")).def_method(Page, 'atom()', "tmpl/page.atom.erb")
ERB::new(File::read("tmpl/page.html.erb")).def_method(Page, 'html()', "tmpl/page.html.erb")