Skip to content

Commit

Permalink
custom templates, set config
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Mar 19, 2010
1 parent e6d3ee8 commit a2e9a3a
Show file tree
Hide file tree
Showing 27 changed files with 85 additions and 77 deletions.
34 changes: 14 additions & 20 deletions lib/slick/builder/base.rb
@@ -1,4 +1,5 @@
require 'fileutils'
require 'logger'
require 'abstract_controller'

module Slick::Builder
Expand All @@ -10,6 +11,7 @@ def controller_path
end

include AbstractController::Layouts
include AbstractController::Helpers

self.view_paths = [File.expand_path('../../views', __FILE__)]
self.layout 'application'
Expand All @@ -19,45 +21,37 @@ def controller_path
def initialize(data, parent = nil)
@parent = parent
@data = data
@config = data[:config] || {}
@config = ActiveSupport::InheritableOptions.new(data[:config] || {})
end

protected

def root_dir
@config[:root] ||= File.expand_path('../../../..', __FILE__)
end

def public_dir
"#{root_dir}/public"
end
def local_path(content)
content.path.to_s.gsub("#{config.root_dir}/public", '')
end
helper_method :local_path

def view_paths
p "KEKSE"
end
protected

def render(template, target)
html = super(:template => template, :layout => layout, :locals => data)
write(target, html) && html
end

def write(file_name, html)
path = [public_dir, path, file_name].compact.join('/')
path = [config.root_dir, 'public', path, file_name].compact.join('/')
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w+') { |f| f.write(html) }
end

public

# TODO can this be made smarter in AbstractController::Layouts? doesn't even seem to be used anywhere?
# def config
# {}
# end

# TODO can this be made smarter in AbstractController::Layouts? just pass it through for now
def response_body=(body)
body
end

# TODO can this be just be a default in ActionView if !controller.respond_to?(:logger)
def logger
@logger ||= Logger.new(STDOUT)
end
end
end

2 changes: 1 addition & 1 deletion lib/slick/builder/section.rb
Expand Up @@ -15,7 +15,7 @@ def build
protected

def build_index
render("#{section.type}/index", "#{path}.html")
render("#{section.type}/#{section.template || 'index'}", "#{path}.html")
end

def build_children
Expand Down
12 changes: 8 additions & 4 deletions lib/slick/model/base.rb
Expand Up @@ -26,6 +26,10 @@ def layout
attributes[:layout]
end

def template
attributes[:template]
end

def slug # TODO
title.underscore.gsub(/[\W]/, '_')
end
Expand Down Expand Up @@ -61,12 +65,12 @@ def title_from_path
title = File.basename(path.to_s).gsub(extname, '').titleize
end

def parse(content)
parse_yaml_preamble(content).merge(:content => content)
def parse(body)
parse_yaml_preamble(body).merge(:body => body)
end

def parse_yaml_preamble(content)
content.gsub!(YAML_PREAMBLE, '')
def parse_yaml_preamble(body)
body.gsub!(YAML_PREAMBLE, '')
$1 ? YAML.load($1).symbolize_keys : {}
end
end
Expand Down
10 changes: 9 additions & 1 deletion lib/slick/model/section.rb
Expand Up @@ -5,7 +5,7 @@ def initialize(path)
end

def contents
@contents ||= type == 'blog' ? content_paths.map { |path| Content.new(path) } : []
@contents ||= type == 'blog' ? build_posts : []
end

def children
Expand All @@ -18,6 +18,14 @@ def read

protected

def build_posts
build_contents.sort { |lft, rgt| rgt.published_at <=> lft.published_at }
end

def build_contents
content_paths.map { |path| Content.new(path) }
end

def content_paths
@content_paths ||= Dir["#{dirname.to_s}/*"].select { |path| path =~ Content::PUBLISHED_AT_PATTERN }
end
Expand Down
10 changes: 9 additions & 1 deletion lib/slick/views/blog/index.html.erb
@@ -1 +1,9 @@
<%= section.title %>
<%= content_tag :h1, 'Recent posts' %>
<%= content_tag :ul do %>
<% section.contents.each do |content| %>
<%= content_tag :li do %>
<%= link_to(content.title, local_path(content)) %>
<% end %>
<% end %>
<% end %>
3 changes: 2 additions & 1 deletion lib/slick/views/blog/show.html.erb
@@ -1,2 +1,3 @@
Post: <%= content.title %>
<%= content_tag :h1, content.title %>
<%= content.body.html_safe %>

13 changes: 11 additions & 2 deletions lib/slick/views/layouts/application.html.erb
@@ -1,2 +1,11 @@
application layout:
<%= yield %>
<!DOCTYPE html>
<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 %>
</head>
<body>
<%= yield %>
</body>
</html>
3 changes: 2 additions & 1 deletion lib/slick/views/page/index.html.erb
@@ -1 +1,2 @@
Page: <%= section.title %>
<%= content_tag :h1, content.title %>
<%= content.body.html_safe %>
1 change: 0 additions & 1 deletion lib/slick/views/page/show.html.erb

This file was deleted.

Empty file.
3 changes: 2 additions & 1 deletion test/_root/data/articles.html
@@ -1,5 +1,6 @@
---
title: Articles
layout: page
template: custom
---
Articles Content
Articles body

This file was deleted.

4 changes: 0 additions & 4 deletions test/_root/public/2009/07/06/using_ruby_1_9_ripper.html

This file was deleted.

4 changes: 0 additions & 4 deletions test/_root/public/articles.html

This file was deleted.

4 changes: 0 additions & 4 deletions test/_root/public/articles/articles_child.html

This file was deleted.

5 changes: 0 additions & 5 deletions test/_root/public/contact.html

This file was deleted.

3 changes: 0 additions & 3 deletions test/_root/public/index.html

This file was deleted.

4 changes: 0 additions & 4 deletions test/_root/public/projects.html

This file was deleted.

4 changes: 0 additions & 4 deletions test/_root/public/projects/i18n.html

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions test/_root/views/page/custom.html.erb
@@ -0,0 +1 @@
custom template: <%= section.title %>
2 changes: 2 additions & 0 deletions test/_root/views/page/index.html.erb
@@ -0,0 +1,2 @@
Custom Page: <%= section.title %>

23 changes: 18 additions & 5 deletions test/builder_test.rb
Expand Up @@ -4,10 +4,13 @@
class BuilderTest < Test::Unit::TestCase
def setup
@config = {
:root => TEST_ROOT
:title => "Sven Fuchs",
:root_dir => TEST_ROOT,
:assets_dir => ASSETS_DIR,
:stylesheets_dir => ASSETS_DIR + '/stylesheets'
}
@section = Slick::Model::Section.new(DATA_DIR)
# FileUtils.rm_r(PUBLIC_DIR) rescue Errno::ENOENT
FileUtils.rm_r(PUBLIC_DIR) rescue Errno::ENOENT
FileUtils.mkdir_p(PUBLIC_DIR)
end

Expand Down Expand Up @@ -49,16 +52,26 @@ def setup

test 'build w/ the application (default) layout' do
@section.attributes['layout'] = 'does_not_exist'
assert_match /application layout/, Slick::Builder.create(nil, @section, :section => @section).send(:build_index)
assert_match /DOCTYPE html/, Slick::Builder.create(nil, @section, :config => @config, :section => @section).send(:build_index)
end

test 'build w/ a builder specific layout' do
section = @section.children.detect { |child| child.layout == nil }
assert_match /page layout/, Slick::Builder.create(nil, section, :section => section).send(:build_index)
assert_match /page layout/, Slick::Builder.create(nil, section, :config => @config, :section => section).send(:build_index)
end

test 'build w/ a custom layout' do
section = @section.children.detect { |child| child.layout == 'custom' }
assert_match /custom layout/, Slick::Builder.create(nil, section, :section => section).send(:build_index)
assert_match /custom layout/, Slick::Builder.create(nil, section, :config => @config, :section => section).send(:build_index)
end

test 'build w/ a custom template' do
section = @section.children.detect { |child| child.template == 'custom' }
assert_match /custom template/, Slick::Builder.create(nil, section, :config => @config, :section => section).send(:build_index)
end

test 'build w/ an overwritten page template' do
section = @section.children.detect { |child| child.title == 'Contact' }
assert_match /Custom Page/, Slick::Builder.create(nil, section, :config => @config, :section => section).send(:build_index)
end
end
4 changes: 2 additions & 2 deletions test/model/base_test.rb
Expand Up @@ -21,8 +21,8 @@ def model(path)
assert_equal 'page', model.layout
end

test "read stores the file's main content to the content attribute" do
test "read stores the file's main body to the body attribute" do
model = self.model(DATA_DIR + '/articles.html')
assert_equal 'Articles Content', model.content
assert_equal 'Articles body', model.body
end
end
3 changes: 0 additions & 3 deletions test/model/content_test.rb
@@ -1,9 +1,6 @@
require File.expand_path('../../test_helper', __FILE__)

class ModelContentTest < Test::Unit::TestCase
def content
end

test "published_at_from_file_name parses a date from the file's basename" do
content = Slick::Model::Content.new(DATA_DIR + '/_posts/2009-07-05-foo.html')
date = content.send(:published_at_from_filename)
Expand Down
5 changes: 3 additions & 2 deletions test/model/section_test.rb
Expand Up @@ -19,7 +19,7 @@ class ModelSectionTest < Test::Unit::TestCase
model = Section.new(DATA_DIR + '/contact.html')
assert_equal 'page', model.type
assert_equal 'custom', model.layout
assert_equal 'http://github.com/svenfuchs', model.content
assert_equal 'http://github.com/svenfuchs', model.body
end

test 'building a Page from a page directory' do
Expand All @@ -45,7 +45,8 @@ class ModelSectionTest < Test::Unit::TestCase
assert_equal 'blog', model.type

assert_equal 2, model.contents.size
assert_equal 'Ripper2Ruby: modify and recompile your Ruby code', model.contents.first.title
assert_equal 'Using Ruby 1.9 Ripper', model.contents.first.title
assert_equal 'Ripper2Ruby: modify and recompile your Ruby code', model.contents.last.title

assert_equal 3, model.children.size
assert_equal 'Articles', model.children.first.title
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Expand Up @@ -14,6 +14,7 @@

TEST_ROOT = File.expand_path('../_root', __FILE__)
DATA_DIR = TEST_ROOT + '/data'
ASSETS_DIR = TEST_ROOT + '/assets'
PUBLIC_DIR = TEST_ROOT + '/public'
VIEWS_DIR = TEST_ROOT + '/views'

Expand Down

0 comments on commit a2e9a3a

Please sign in to comment.