Permalink
Browse files

wip

  • Loading branch information...
1 parent ed34d9a commit cc9eeaf4842fdc4eb700620549e517d5795a2292 @maca committed Jun 26, 2012
Showing with 58 additions and 139 deletions.
  1. +0 −92 lib/markup_helpers.rb
  2. +45 −34 lib/tiny.rb
  3. +0 −3 lib/tiny/haml.rb
  4. +7 −4 spec/erb_spec.rb
  5. +5 −5 spec/haml_spec.rb
  6. +1 −1 spec/pure_ruby_spec.rb
View
92 lib/markup_helpers.rb
@@ -1,92 +0,0 @@
-require "tilt"
-require "rack/utils"
-
-require "tiny/version"
-require "tiny/tilt"
-
-module Tiny
- include Haml::Helpers if const_defined? :Haml
-
- class Context
- include MarkupHelpers
- attr_reader :buffer
-
- def initialize scope, &block
- @buffer, @scope = '', scope
- instance_eval &block
- end
-
- def tag *args
- @buffer << super(*args)
- end
-
- def text content
- @buffer << Rack::Utils.escape_html(content)
- end
-
- def text! content
- @buffer << content
- end
-
- def method_missing *args
- @scope.send *args
- end
- end
-
- def tag tag_name, content_or_attrs = {}, attrs = nil, &block
- if attrs.nil? && Hash === content_or_attrs
- attrs = content_or_attrs
- content = nil
- else
- attrs ||= {}
- content = content_or_attrs
- end
-
- attrs = attrs.map do |name, val|
- next if val.nil? || val == []
- val == true ? name : %{#{name}="#{[*val].join(' ')}"}
- end.compact.join(' ')
-
- attrs = " #{attrs}" unless attrs.empty?
- content = capture(&block) if block_given?
- output = %{<#{tag_name}#{attrs}>#{content}</#{tag_name}>}
- block_given? ? concat(output) : output
- end
-
- private
- def capture &block
- case @__tilt_context
- when Tilt::ErubisTemplate, Tilt::ERBTemplate
- erb_capture &block
- when Tilt::HamlTemplate
- capture_haml &block
- else
- scope = Context === self ? @scope : self
- Context.new(scope, &block).buffer
- end
- end
-
- def concat content
- case @__tilt_context
- when Tilt::ErubisTemplate, Tilt::ERBTemplate
- erb_buffer << content
- when Tilt::HamlTemplate
- haml_concat content
- else
- content
- end
- end
-
- def erb_capture
- old_buffer = erb_buffer.dup
- erb_buffer.clear and yield
- content = erb_buffer.dup
- erb_buffer.replace(old_buffer) and content
- end
-
- def erb_buffer
- outvar = @__tilt_context.instance_variable_get(:@outvar)
- outvar or raise('Please pass the outvar option when instantiating the erb template')
- instance_variable_get outvar
- end
-end
View
79 lib/tiny.rb
@@ -1,4 +1,5 @@
require "tilt"
+require 'yaml'
require "rack/utils"
require "tiny/version"
@@ -7,27 +8,7 @@
module Tiny
include Haml::Helpers if const_defined? :Haml
- module Helpers
- def tag tag_name, content_or_attrs = {}, attrs = nil, &block
- if attrs.nil? && Hash === content_or_attrs
- attrs = content_or_attrs
- content = nil
- else
- attrs ||= {}
- content = content_or_attrs
- end
-
- attrs = attrs.map do |name, val|
- next if val.nil? || val == []
- val == true ? name : %{#{name}="#{[*val].join(' ')}"}
- end.compact.join(' ')
-
- attrs = " #{attrs}" unless attrs.empty?
- content = capture(&block) if block_given?
- output = %{<#{tag_name}#{attrs}>#{content}</#{tag_name}>}
- block_given? ? concat(output) : output
- end
-
+ module CaptureHelpers
private
def capture &block
case @__tilt_context
@@ -36,43 +17,73 @@ def capture &block
when Tilt::HamlTemplate
capture_haml &block
else
- scope = Context === self ? @scope : self
- Context.new(scope, &block).buffer
+ scope = Tag === self ? @scope : self
+ Tag.new(scope, &block).buffer
end
end
def concat content
case @__tilt_context
when Tilt::ErubisTemplate, Tilt::ERBTemplate
erb_buffer << content
- when Tilt::HamlTemplate
- haml_concat content
else
content
end
end
def erb_capture
- old_buffer = erb_buffer.dup
+ buffer = erb_buffer.dup
erb_buffer.clear and yield
- content = erb_buffer.dup
- erb_buffer.replace(old_buffer) and content
+ return erb_buffer.dup
+ ensure
+ erb_buffer.replace(buffer)
end
def erb_buffer
outvar = @__tilt_context.instance_variable_get(:@outvar)
- outvar or raise('Please pass the outvar option when instantiating the erb template')
instance_variable_get outvar
end
end
- class Context
+ module Helpers
+ def tag name, content_or_attrs = {}, attrs = nil, &block
+ Tag.new(self, name, content_or_attrs, attrs, &block).render
+ end
+
+ class << self
+ def included base
+ base.send :include, CaptureHelpers
+ end
+ end
+ end
+
+ class Tag
include Helpers
- attr_reader :buffer
+ attr_reader :tag_name, :attrs
+
+ def initialize scope, tag_name, content_or_attrs = {}, attrs = nil, &block
+ @buffer, @scope, @tag_name, @block = '', scope, tag_name, block
+ if attrs.nil? && Hash === content_or_attrs
+ @attrs = content_or_attrs
+ else
+ @attrs = attrs || {}
+ @content = content_or_attrs
+ end
+ end
- def initialize scope, &block
- @buffer, @scope = '', scope
- instance_eval &block
+ def render
+ tag_attrs = attrs.map do |name, val|
+ next if val.nil? || val == []
+ val == true ? name : %{#{name}="#{[*val].join(' ')}"}
+ end.compact.join(' ')
+
+ tag_attrs = " #{tag_attrs}" unless tag_attrs.empty?
+ %{<#{tag_name}#{tag_attrs}>#{content}</#{tag_name}>}
+ end
+
+ private
+ def content
+ @block and instance_exec(self, &@block) or @content
end
def tag *args
View
3 lib/tiny/haml.rb
@@ -1,3 +0,0 @@
-module MarkupHelpers
- include Haml::Helpers
-end
View
11 spec/erb_spec.rb
@@ -1,5 +1,6 @@
# encoding: utf-8
require 'spec_helper'
+require 'erubis'
describe 'markup helpers' do
include Tiny::Helpers
@@ -11,28 +12,30 @@
describe 'tag' do
describe 'basic' do
before do
- @output = Tilt['erb'].new { '<%= tag(:li, "Hello") %>' }.render(self)
+ @output = Tilt['erb'].new(:outvar => '@_out_buf') do
+ '<%= tag(:li, "Hello") %>'
+ end.render(self)
end
it { output.should have_css 'li', :text => "Hello" }
end
describe 'shallow blocks' do
before do
- @output = Tilt['erb'].new do
+ @output = Tilt['erb'].new(:outvar => '@_out_buf') do
<<-ERB
<% tag(:li) do %>
<%= tag(:a, 'Hello') %>
<% end %>
ERB
- end.render(self).tap { |e| puts e }
+ end.render(self)
end
it { output.should have_css 'li' }
it { output.should have_css 'li > a', :text => 'Hello' }
end
describe 'deeper blocks' do
before do
- @output = Tilt['erb'].new do
+ @output = Tilt['erb'].new(:outvar => '@_out_buf') do
<<-ERB
<% tag(:li) do %>
<% tag(:a, 'Hello') do %>
View
10 spec/haml_spec.rb
@@ -18,9 +18,9 @@
describe 'shallow blocks' do
before do
- @output = Tilt['haml'].new(:outvar => '@_out_buf') do
+ @output = Tilt['haml'].new do
<<-ERB
-- tag(:li) do
+= tag(:li) do
= tag(:a, 'Hello')
ERB
end.render(self)
@@ -31,10 +31,10 @@
describe 'deeper blocks' do
before do
- @output = Tilt['haml'].new(:outvar => '@_out_buf') do
+ @output = Tilt['haml'].new do
<<-ERB
-- tag(:li) do
- - tag(:a, 'Hello') do
+= tag(:li) do
+ = tag(:a, 'Hello') do
= tag(:img)
ERB
end.render(self)
View
2 spec/pure_ruby_spec.rb
@@ -61,7 +61,7 @@
end
describe 'buffering' do
- describe 'basic' do
+ describe 'concatenation' do
before do
@output = tag(:ul) do
tag(:li, 'One')

0 comments on commit cc9eeaf

Please sign in to comment.