Permalink
Browse files

Converter, with layouts

  • Loading branch information...
1 parent addc9cf commit 767d01a2b8d68f8239db3c342afd1ad6466b82e0 @holizz committed Sep 19, 2010
Showing with 129 additions and 1 deletion.
  1. +1 −0 lib/lavender.rb
  2. +34 −0 lib/lavender/converter.rb
  3. +1 −1 lib/lavender/renderer.rb
  4. +93 −0 spec/converter_spec.rb
View
@@ -2,3 +2,4 @@
require 'haml'
require 'lavender/renderer'
+require 'lavender/converter'
View
@@ -0,0 +1,34 @@
+module Lavender
+ class Converter
+ def initialize options
+ @options = options
+ end
+
+ def render
+ yaml = YAML.load_stream @options[:page]
+ case yaml[0]
+ when String
+ yaml[0]
+ when Hash
+ conf = yaml[0]
+
+ r = Renderer.new(conf['processor'].to_sym, yaml[1])
+ page = r.render(conf)
+ output = page
+
+ layout = conf['layout']
+ if layout
+ hsh = @options[:layouts][layout.to_sym]
+ processor = hsh.keys.first
+ content = hsh[processor]
+ r = Renderer.new(processor, content)
+ output = r.render(conf) { page }
+ end
+
+ output
+ else
+ raise ArgumentError, 'first document must be Hash or String'
+ end
+ end
+ end
+end
View
@@ -13,7 +13,7 @@ def render vars = {}, &block
@input
when :erb
def method_missing name
- @vars[name]
+ @vars[name] || @vars[name.to_s]
end
ERB.new(@input).result(binding)
when :haml
View
@@ -0,0 +1,93 @@
+require 'spec_helper'
+
+describe Lavender::Converter do
+ before do
+ end
+
+ it "should render anything that isn't YAML as-is" do
+ page = "<p>My paragraph</p>"
+ c = Lavender::Converter.new(:page => page)
+ c.render.should == "<p>My paragraph</p>"
+ end
+
+ it "should render pages with the given template processor" do
+ page = <<END
+---
+processor: erb
+layout: null
+text: content
+---
+<p>Document <%= text %>.</p>
+END
+
+ c = Lavender::Converter.new(:page => page)
+
+ c.render.should == "<p>Document content.</p>"
+ end
+
+ it "should render pages with a layout" do
+ page = <<END
+---
+processor: haml
+layout: default
+title: your face
+---
+%p Document content.
+END
+ layout = <<END
+%html
+ %head
+ %title= title
+ %body
+ = yield
+END
+
+ c = Lavender::Converter.new(:page => page, :layouts => {:default => {:haml => layout}})
+
+ c.render.should == <<END
+<html>
+ <head>
+ <title>your face</title>
+ </head>
+ <body>
+ <p>Document content.</p>
+ </body>
+</html>
+END
+ end
+
+ it "should allow mixing templating languages" do
+ page = <<END
+---
+processor: haml
+layout: page
+title: your face
+---
+%p Document content.
+END
+ layout = <<END
+<html>
+ <head>
+ <title><%= title %></title>
+ </head>
+ <body>
+ <%= yield %>
+ </body>
+</html>
+END
+
+ c = Lavender::Converter.new(:page => page, :layouts => {:page => {:erb => layout}})
+
+ c.render.should == <<END
+<html>
+ <head>
+ <title>your face</title>
+ </head>
+ <body>
+ <p>Document content.</p>
+
+ </body>
+</html>
+END
+ end
+end

0 comments on commit 767d01a

Please sign in to comment.