diff --git a/lib/slick/builder/base.rb b/lib/slick/builder/base.rb index 13cd652..6cb498a 100644 --- a/lib/slick/builder/base.rb +++ b/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 - diff --git a/lib/slick/builder/section.rb b/lib/slick/builder/section.rb index 20d4fc0..a2e5879 100644 --- a/lib/slick/builder/section.rb +++ b/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 diff --git a/lib/slick/model/base.rb b/lib/slick/model/base.rb index dae31e3..3c41287 100644 --- a/lib/slick/model/base.rb +++ b/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 diff --git a/lib/slick/model/section.rb b/lib/slick/model/section.rb index 7d1e5e9..7fb0f71 100644 --- a/lib/slick/model/section.rb +++ b/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 diff --git a/lib/slick/views/blog/index.html.erb b/lib/slick/views/blog/index.html.erb index 85936c1..c06e356 100644 --- a/lib/slick/views/blog/index.html.erb +++ b/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 %> diff --git a/lib/slick/views/blog/show.html.erb b/lib/slick/views/blog/show.html.erb index 0a063d4..ed210ad 100644 --- a/lib/slick/views/blog/show.html.erb +++ b/lib/slick/views/blog/show.html.erb @@ -1,2 +1,3 @@ -Post: <%= content.title %> +<%= content_tag :h1, content.title %> +<%= content.body.html_safe %> diff --git a/lib/slick/views/layouts/application.html.erb b/lib/slick/views/layouts/application.html.erb index 4227962..c27e33d 100644 --- a/lib/slick/views/layouts/application.html.erb +++ b/lib/slick/views/layouts/application.html.erb @@ -1,2 +1,11 @@ -application layout: -<%= yield %> + + + + + <%#= config.title %> + <%= stylesheet_link_tag :all %> + + + <%= yield %> + + diff --git a/lib/slick/views/page/index.html.erb b/lib/slick/views/page/index.html.erb index 4aecf97..22345eb 100644 --- a/lib/slick/views/page/index.html.erb +++ b/lib/slick/views/page/index.html.erb @@ -1 +1,2 @@ -Page: <%= section.title %> +<%= content_tag :h1, content.title %> +<%= content.body.html_safe %> diff --git a/lib/slick/views/page/show.html.erb b/lib/slick/views/page/show.html.erb deleted file mode 100644 index 8a80170..0000000 --- a/lib/slick/views/page/show.html.erb +++ /dev/null @@ -1 +0,0 @@ -Article: <%= content.title %> diff --git a/test/_root/assets/stylesheets/application.css b/test/_root/assets/stylesheets/application.css new file mode 100644 index 0000000..e69de29 diff --git a/test/_root/data/articles.html b/test/_root/data/articles.html index 5e1a8d2..59dbab6 100644 --- a/test/_root/data/articles.html +++ b/test/_root/data/articles.html @@ -1,5 +1,6 @@ --- title: Articles layout: page +template: custom --- -Articles Content +Articles body diff --git a/test/_root/public/2009/07/05/ripper2_ruby__modify_and_recompile_your_ruby_code.html b/test/_root/public/2009/07/05/ripper2_ruby__modify_and_recompile_your_ruby_code.html deleted file mode 100644 index e2ff711..0000000 --- a/test/_root/public/2009/07/05/ripper2_ruby__modify_and_recompile_your_ruby_code.html +++ /dev/null @@ -1,4 +0,0 @@ -application layout: -Post: Ripper2Ruby: modify and recompile your Ruby code - - diff --git a/test/_root/public/2009/07/06/using_ruby_1_9_ripper.html b/test/_root/public/2009/07/06/using_ruby_1_9_ripper.html deleted file mode 100644 index b9925df..0000000 --- a/test/_root/public/2009/07/06/using_ruby_1_9_ripper.html +++ /dev/null @@ -1,4 +0,0 @@ -application layout: -Post: Using Ruby 1.9 Ripper - - diff --git a/test/_root/public/articles.html b/test/_root/public/articles.html deleted file mode 100644 index 3e71bed..0000000 --- a/test/_root/public/articles.html +++ /dev/null @@ -1,4 +0,0 @@ -page layout: -Page: Articles - - diff --git a/test/_root/public/articles/articles_child.html b/test/_root/public/articles/articles_child.html deleted file mode 100644 index 85717f6..0000000 --- a/test/_root/public/articles/articles_child.html +++ /dev/null @@ -1,4 +0,0 @@ -page layout: -Page: Articles Child - - diff --git a/test/_root/public/contact.html b/test/_root/public/contact.html deleted file mode 100644 index 4abaa37..0000000 --- a/test/_root/public/contact.html +++ /dev/null @@ -1,5 +0,0 @@ -custom layout: -Page: Contact - - - diff --git a/test/_root/public/index.html b/test/_root/public/index.html deleted file mode 100644 index 23cadfe..0000000 --- a/test/_root/public/index.html +++ /dev/null @@ -1,3 +0,0 @@ -application layout: -Data - diff --git a/test/_root/public/projects.html b/test/_root/public/projects.html deleted file mode 100644 index 9d9057b..0000000 --- a/test/_root/public/projects.html +++ /dev/null @@ -1,4 +0,0 @@ -page layout: -Page: Projects - - diff --git a/test/_root/public/projects/i18n.html b/test/_root/public/projects/i18n.html deleted file mode 100644 index c33ff9d..0000000 --- a/test/_root/public/projects/i18n.html +++ /dev/null @@ -1,4 +0,0 @@ -page layout: -Page: I18n - - diff --git a/lib/slick/views/layouts/page.html.erb b/test/_root/views/layouts/page.html.erb similarity index 100% rename from lib/slick/views/layouts/page.html.erb rename to test/_root/views/layouts/page.html.erb diff --git a/test/_root/views/page/custom.html.erb b/test/_root/views/page/custom.html.erb new file mode 100644 index 0000000..062c255 --- /dev/null +++ b/test/_root/views/page/custom.html.erb @@ -0,0 +1 @@ +custom template: <%= section.title %> diff --git a/test/_root/views/page/index.html.erb b/test/_root/views/page/index.html.erb new file mode 100644 index 0000000..5a3339c --- /dev/null +++ b/test/_root/views/page/index.html.erb @@ -0,0 +1,2 @@ +Custom Page: <%= section.title %> + diff --git a/test/builder_test.rb b/test/builder_test.rb index 232b3aa..d7a3ba2 100644 --- a/test/builder_test.rb +++ b/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 diff --git a/test/model/base_test.rb b/test/model/base_test.rb index c0786b7..c8f074e 100644 --- a/test/model/base_test.rb +++ b/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 diff --git a/test/model/content_test.rb b/test/model/content_test.rb index 200aed8..3206672 100644 --- a/test/model/content_test.rb +++ b/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) diff --git a/test/model/section_test.rb b/test/model/section_test.rb index 1a89964..eec9ccc 100644 --- a/test/model/section_test.rb +++ b/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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 4202ac8..ec27c48 100644 --- a/test/test_helper.rb +++ b/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'