Permalink
Browse files

Add a project index. Refactor out YAMLable and Templates.

  • Loading branch information...
1 parent 33346e5 commit 68d39f1ca43257f474450e9c9f7e723e93dd9737 @mikedouglas committed Aug 17, 2011
Showing with 65 additions and 22 deletions.
  1. +5 −0 lib/bamboo.rb
  2. +3 −11 lib/bamboo/page.rb
  3. +7 −11 lib/bamboo/project.rb
  4. +20 −0 lib/bamboo/template.rb
  5. +13 −0 lib/bamboo/yamlable.rb
  6. +1 −0 projects/reverseHN.md
  7. +2 −0 templates/project.html
  8. +14 −0 templates/projects.html
View
@@ -1,5 +1,6 @@
require 'sinatra'
require_relative 'bamboo/project'
+require_relative 'bamboo/template'
class Bamboo < Sinatra::Base
set :public, 'static'
@@ -13,6 +14,10 @@ def initialize
'Hello world!'
end
+ get '/projects' do
+ Template.new("projects").render({'projects' => @projects.values})
+ end
+
get '/projects/:name' do |name|
@projects[name].html
end
View
@@ -1,7 +1,9 @@
require_relative 'markdown'
-require 'yaml'
+require_relative 'yamlable'
class Page
+ include YAMLable
+
attr_reader :config, :body
# returns a map of stubs to page-like objects
@@ -23,14 +25,4 @@ def html
def to_liquid
@config
end
-
- protected
- # returns [map of headers, body]
- def filter_config(text)
- if text =~ /^---\n(.*)^---\n(.*)/m
- return YAML.load($1), $2
- else
- return {}, text
- end
- end
end
View
@@ -1,24 +1,20 @@
require_relative 'page'
-require 'yaml'
-require 'liquid'
+require_relative 'template'
class Project < Page
def html
- content = super
- config, template = filter_config(File.read('templates/project.html'))
- html = Liquid::Template.parse(template).render({'page' => self, 'content' => content})
-
- if config
- template = File.read('templates/' + (config['layout'] || 'layout') + '.html')
- Liquid::Template.parse(template).render(config.merge({'page' => self, 'content' => html}))
+ if @config
+ template = Template.new("project")
+ template.render({'content' => super, 'page' => self})
else
- html
+ super
end
end
def to_liquid
super.merge({
- 'paths' => ['projects']
+ 'paths' => ['projects'],
+ 'url' => '/projects/' + @config['title']
})
end
end
View
@@ -0,0 +1,20 @@
+require 'liquid'
+
+class Template
+ include YAMLable
+
+ def initialize(fname)
+ @config, @body = filter_config(File.read("templates/" + fname + ".html"))
+ @body = Liquid::Template.parse(@body)
+ end
+
+ def render(hash)
+ out = @body.render(hash)
+ if @config
+ template = Template.new(@config['layout'] || 'layout')
+ template.render(hash.merge(@config.merge({'content' => out})))
+ else
+ out
+ end
+ end
+end
View
@@ -0,0 +1,13 @@
+require 'yaml'
+
+module YAMLable
+ protected
+ # returns [map of headers, body]
+ def filter_config(text)
+ if text =~ /^---\n(.*)^---\n(.*)/m
+ return YAML.load($1) || {}, $2
+ else
+ return nil, text
+ end
+ end
+end
View
@@ -2,6 +2,7 @@
title: reverseHN
date: 2011 July 10
author: Mike Douglas
+description: Find the news.yc discussion thread for any page on the web.
---
If you’re at all like me, you’ve probably got 16+ browser tabs open at
View
@@ -1,3 +1,5 @@
+---
+---
<article>
<header>
<h1>{{ page.title }}</h1>
View
@@ -0,0 +1,14 @@
+---
+page:
+ title: projects
+---
+<div class="posts">
+ {% for project in projects %}
+ <article>
+ <header>
+ <h1><a href="{{ project.url }}">{{ project.title }}</a></h1>
+ <p>{{ project.description }}</p>
+ </header>
+ </article>
+ {% endfor %}
+</div>

0 comments on commit 68d39f1

Please sign in to comment.