Permalink
Browse files

add archive and feed

  • Loading branch information...
1 parent a2e9a3a commit 53cd74228ff95cd06d75c5a872cc741824520ca6 Sven Fuchs committed Mar 20, 2010
View
@@ -16,23 +16,35 @@ def controller_path
self.view_paths = [File.expand_path('../../views', __FILE__)]
self.layout 'application'
- attr_reader :config
+ attr_reader :config, :request
+ helper_method :local_path, :url, :page_title, :request
def initialize(data, parent = nil)
- @parent = parent
- @data = data
- @config = ActiveSupport::InheritableOptions.new(data[:config] || {})
+ @parent = parent
+ @data = data
+ @config = ActiveSupport::InheritableOptions.new(data[:config] || {})
+ @request = ActionDispatch::Request.new({ 'HTTP_HOST' => 'svenfuchs.com' })
+ end
+
+ def url(content)
+ config[:url] + '/' + local_path(content)
end
def local_path(content)
- content.path.to_s.gsub("#{config.root_dir}/public", '')
+ "#{content.permalink}.html" # TODO
+ end
+
+ def page_title
+ content = data[:content] || section
+ [content.title, config[:title]].compact.join(' - ')
end
- helper_method :local_path
protected
- def render(template, target)
- html = super(:template => template, :layout => layout, :locals => data)
+ def render(template, target, options = {})
+ # TODO lookup_context, at least formats, seem messed up after we've rendered once
+ @lookup_context = ActionView::LookupContext.new(self.class._view_paths, details_for_lookup)
+ html = super(options.merge(:template => template, :layout => layout, :locals => data))
write(target, html) && html
end
View
@@ -2,17 +2,28 @@ module Slick::Builder
class Blog < Section
def build
super
- build_contents
+ build_posts
+ build_feed
+ build_archive
end
protected
- def build_contents
+ def build_posts
section.contents.each do |content|
data.merge!(:content => content)
render("#{section.type}/show", "#{content.permalink}.html")
end
end
+
+ def build_feed
+ # TODO curiously rails would not find the template if named index.atom.builder
+ render("#{section.type}/feed", "#{path}.atom", :format => :atom)
+ end
+
+ def build_archive
+ render("#{section.type}/archive", "#{File.dirname(path)}/archive.html")
+ end
end
end
View
@@ -30,8 +30,8 @@ def template
attributes[:template]
end
- def slug # TODO
- title.underscore.gsub(/[\W]/, '_')
+ def slug
+ basename.to_s.gsub(extname, '')
end
def dirname
@@ -63,6 +63,7 @@ def read
def title_from_path
title = File.basename(path.to_s).gsub(extname, '').titleize
+ title == 'Data' ? nil : title
end
def parse(body)
View
@@ -4,14 +4,17 @@
module Slick::Model
class Content < Base
- PUBLISHED_AT_PATTERN = %r((?:^|\/)([\d]{4}[-_][\d]{2}[-_][\d]{2}))
+ PUBLISHED_AT_PATTERN = %r((?:^|\/)([\d]{4}[-_][\d]{1,2}[-_][\d]{1,2})[-_])
- def permalink # TODO
+ def permalink
date = published_at.to_s.gsub('-', '/')
- slug = title.underscore.gsub(/[\W]/, '_')
[date, slug].join('/')
end
+ def slug
+ super.gsub(PUBLISHED_AT_PATTERN, '')
+ end
+
protected
def read
@@ -20,6 +23,12 @@ def read
attributes
end
+ # should overwrite and strip the date
+ # def title_from_path
+ # title = File.basename(path.to_s).gsub(extname, '').titleize
+ # title == 'Data' ? nil : title
+ # end
+
def published_at_from_filename
basename.to_s =~ PUBLISHED_AT_PATTERN
Date.parse($1) rescue nil
@@ -12,6 +12,12 @@ def children
@children ||= child_paths.map { |path| Section.new(path) }
end
+ def archive
+ contents.group_by { |content| content.published_at.year }.tap do |archive|
+ archive.each { |year, contents| archive[year] = contents.group_by { |c| c.published_at.month } }
+ end
+ end
+
def read
super.merge(:type => determine_type)
end
@@ -0,0 +1,20 @@
+<h1>Archive</h1>
+<ul id="archive">
+ <% section.archive.each do |year, posts| %>
+ <li>
+ <h2><%= year %></h2>
+ <ul>
+ <% posts.each do |month, posts| %>
+ <li>
+ <h3><%= Date::MONTHNAMES[month.to_i] %></h3>
+ <ul>
+ <% posts.each do |post| %>
+ <li><%= link_to(post.title, local_path(post)) %></li>
+ <% end %>
+ </ul>
+ </li>
+ <% end %>
+ </ul>
+ </li>
+ <% end %>
+</ul>
@@ -0,0 +1,16 @@
+atom_feed do |feed|
+ feed.title(page_title)
+ feed.updated(section.contents.first.published_at)
+
+ section.contents[0, 25].each do |post|
+ feed.entry(post, :url => url(post), :id => url(post)) do |entry|
+ entry.title(post.title)
+ entry.content(post.body, :type => 'html')
+ entry.updated(post.published_at.to_time.xmlschema)
+ entry.published(post.published_at.to_time.xmlschema)
+ entry.author do |author|
+ author.name(config[:author])
+ end
+ end
+ end
+end
@@ -1,9 +1,8 @@
-<%= content_tag :h1, 'Recent posts' %>
+<h2>Recent Posts</h2>
-<%= content_tag :ul do %>
- <% section.contents.each do |content| %>
- <%= content_tag :li do %>
- <%= link_to(content.title, local_path(content)) %>
- <% end %>
+<ul class="posts">
+ <% section.contents.slice(0, 15).each do |content| %>
+ <li><%= link_to(content.title, local_path(content)) %></li>
<% end %>
-<% end %>
+</ul>
+<p><%= link_to('Older posts', 'archive.html') %> &rarr;</p>
@@ -1,3 +1,4 @@
-<%= content_tag :h1, content.title %>
-<%= content.body.html_safe %>
-
+<div id="post">
+ <%= content_tag :h1, content.title %>
+ <%= content.body.html_safe %>
+</div>
@@ -2,8 +2,9 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <title><%#= config.title %></title>
- <%= stylesheet_link_tag :all %>
+ <title><%= config['title'] %></title>
+ <%= stylesheet_link_tag :all, :recursive => true, :cache => true %>
+ <%= javascript_include_tag :all, :recursive => true, :cache => true %>
</head>
<body>
<%= yield %>
@@ -0,0 +1,3 @@
+<div id="error">
+ <%= section.body.html_safe %>
+</div>
@@ -1,2 +1,4 @@
-<%= content_tag :h1, content.title %>
-<%= content.body.html_safe %>
+<div id="page">
+ <%= content_tag :h1, section.title %>
+ <%= section.body.html_safe %>
+</div>

0 comments on commit 53cd742

Please sign in to comment.