diff options
Diffstat (limited to 'lib/page.rb')
-rw-r--r-- | lib/page.rb | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/lib/page.rb b/lib/page.rb new file mode 100644 index 0000000..20f9701 --- /dev/null +++ b/lib/page.rb @@ -0,0 +1,103 @@ +# coding: utf-8 +require 'erb' +require 'set' + +require 'category' + +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 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") |