Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

custom templates, set config

  • Loading branch information...
commit a2e9a3a7175c443b8c29e727731d2499f578d8d7 1 parent e6d3ee8
@svenfuchs authored
Showing with 85 additions and 77 deletions.
  1. +14 −20 lib/slick/builder/base.rb
  2. +1 −1  lib/slick/builder/section.rb
  3. +8 −4 lib/slick/model/base.rb
  4. +9 −1 lib/slick/model/section.rb
  5. +9 −1 lib/slick/views/blog/index.html.erb
  6. +2 −1  lib/slick/views/blog/show.html.erb
  7. +11 −2 lib/slick/views/layouts/application.html.erb
  8. +2 −1  lib/slick/views/page/index.html.erb
  9. +0 −1  lib/slick/views/page/show.html.erb
  10. 0  test/_root/assets/stylesheets/application.css
  11. +2 −1  test/_root/data/articles.html
  12. +0 −4 test/_root/public/2009/07/05/ripper2_ruby__modify_and_recompile_your_ruby_code.html
  13. +0 −4 test/_root/public/2009/07/06/using_ruby_1_9_ripper.html
  14. +0 −4 test/_root/public/articles.html
  15. +0 −4 test/_root/public/articles/articles_child.html
  16. +0 −5 test/_root/public/contact.html
  17. +0 −3  test/_root/public/index.html
  18. +0 −4 test/_root/public/projects.html
  19. +0 −4 test/_root/public/projects/i18n.html
  20. 0  {lib/slick → test/_root}/views/layouts/page.html.erb
  21. +1 −0  test/_root/views/page/custom.html.erb
  22. +2 −0  test/_root/views/page/index.html.erb
  23. +18 −5 test/builder_test.rb
  24. +2 −2 test/model/base_test.rb
  25. +0 −3  test/model/content_test.rb
  26. +3 −2 test/model/section_test.rb
  27. +1 −0  test/test_helper.rb
View
34 lib/slick/builder/base.rb
@@ -1,4 +1,5 @@
require 'fileutils'
+require 'logger'
require 'abstract_controller'
module Slick::Builder
@@ -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'
@@ -19,22 +21,15 @@ 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)
@@ -42,22 +37,21 @@ def render(template, target)
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
-
View
2  lib/slick/builder/section.rb
@@ -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
View
12 lib/slick/model/base.rb
@@ -26,6 +26,10 @@ def layout
attributes[:layout]
end
+ def template
+ attributes[:template]
+ end
+
def slug # TODO
title.underscore.gsub(/[\W]/, '_')
end
@@ -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
View
10 lib/slick/model/section.rb
@@ -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
@@ -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
View
10 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 %>
View
3  lib/slick/views/blog/show.html.erb
@@ -1,2 +1,3 @@
-Post: <%= content.title %>
+<%= content_tag :h1, content.title %>
+<%= content.body.html_safe %>
View
13 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>
View
3  lib/slick/views/page/index.html.erb
@@ -1 +1,2 @@
-Page: <%= section.title %>
+<%= content_tag :h1, content.title %>
+<%= content.body.html_safe %>
View
1  lib/slick/views/page/show.html.erb
@@ -1 +0,0 @@
-Article: <%= content.title %>
View
0  test/_root/assets/stylesheets/application.css
No changes.
View
3  test/_root/data/articles.html
@@ -1,5 +1,6 @@
---
title: Articles
layout: page
+template: custom
---
-Articles Content
+Articles body
View
4 test/_root/public/2009/07/05/ripper2_ruby__modify_and_recompile_your_ruby_code.html
@@ -1,4 +0,0 @@
-application layout:
-Post: Ripper2Ruby: modify and recompile your Ruby code
-
-
View
4 test/_root/public/2009/07/06/using_ruby_1_9_ripper.html
@@ -1,4 +0,0 @@
-application layout:
-Post: Using Ruby 1.9 Ripper
-
-
View
4 test/_root/public/articles.html
@@ -1,4 +0,0 @@
-page layout:
-Page: Articles
-
-
View
4 test/_root/public/articles/articles_child.html
@@ -1,4 +0,0 @@
-page layout:
-Page: Articles Child
-
-
View
5 test/_root/public/contact.html
@@ -1,5 +0,0 @@
-custom layout:
-Page: Contact
-
-
-
View
3  test/_root/public/index.html
@@ -1,3 +0,0 @@
-application layout:
-Data
-
View
4 test/_root/public/projects.html
@@ -1,4 +0,0 @@
-page layout:
-Page: Projects
-
-
View
4 test/_root/public/projects/i18n.html
@@ -1,4 +0,0 @@
-page layout:
-Page: I18n
-
-
View
0  lib/slick/views/layouts/page.html.erb → test/_root/views/layouts/page.html.erb
File renamed without changes
View
1  test/_root/views/page/custom.html.erb
@@ -0,0 +1 @@
+custom template: <%= section.title %>
View
2  test/_root/views/page/index.html.erb
@@ -0,0 +1,2 @@
+Custom Page: <%= section.title %>
+
View
23 test/builder_test.rb
@@ -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
@@ -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
View
4 test/model/base_test.rb
@@ -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
View
3  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)
View
5 test/model/section_test.rb
@@ -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
@@ -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
View
1  test/test_helper.rb
@@ -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'
Please sign in to comment.
Something went wrong with that request. Please try again.