From 758aee465370157ffbc49abb7ea0f7bba2c66248 Mon Sep 17 00:00:00 2001 From: nex3 Date: Wed, 8 Nov 2006 03:27:37 +0000 Subject: [PATCH] Added tabulation-management methods too Haml::Helpers. git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@129 7063305b-7217-0410-af8c-cdc13e5119b9 --- lib/haml/helpers.rb | 19 +++++++++++++++++++ test/helper_test.rb | 12 ++++++++++++ test/results/helpers.xhtml | 6 ++++++ test/templates/helpers.haml | 7 +++++++ 4 files changed, 44 insertions(+) diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 1d5a5004..b839dc47 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -30,5 +30,24 @@ module Haml def list_of(array) # :yields: item (array.collect { |i| "
  • #{yield(i)}
  • " }).join("\n") end + + # Increments the tabulation modifier of the buffer. This is the + # number of tabs the buffer automatically adds to the lines of the + # template. + def tab_up(i = 1) + buffer.tabulation += i + end + + # Decrements the tabulation modifier of the buffer. This is the + # number of tabs the buffer automatically adds to the lines of the + # template. + def tab_down(i = 1) + buffer.tabulation -= i + end + + # Gets a reference to the current Haml::Buffer object. + def buffer # :nodoc: + @haml_stack[-1] + end end end diff --git a/test/helper_test.rb b/test/helper_test.rb index 91eb7da1..724b7bbd 100644 --- a/test/helper_test.rb +++ b/test/helper_test.rb @@ -6,6 +6,10 @@ require File.dirname(__FILE__) + '/../lib/haml/helpers' class HelperTest < Test::Unit::TestCase include Haml::Helpers + def render(text, options = {}) + Haml::Engine.new(text, options).to_html + end + def test_flatten assert_equal(flatten("FooBar"), "FooBar") @@ -21,4 +25,12 @@ class HelperTest < Test::Unit::TestCase assert_equal("
  • 1
  • \n
  • 2
  • ", (list_of([1, 2]) { |i| i.to_s})) assert_equal("
  • 1
  • ", (list_of([[1]]) { |i| i.first})) end + + def test_buffer_access + assert(render("= buffer") =~ /#/) + end + + def test_tabs + assert_equal(render("foo\n- tab_up\nbar\n- tab_down\nbaz"), "foo\n bar\nbaz\n") + end end diff --git a/test/results/helpers.xhtml b/test/results/helpers.xhtml index c477300f..7249f160 100644 --- a/test/results/helpers.xhtml +++ b/test/results/helpers.xhtml @@ -23,3 +23,9 @@ man.

    +

    foo

    +

    + reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong +

    +

    baz

    +

    boom

    diff --git a/test/templates/helpers.haml b/test/templates/helpers.haml index d8d41f2c..b33627b9 100644 --- a/test/templates/helpers.haml +++ b/test/templates/helpers.haml @@ -8,3 +8,10 @@ man. - 3.times do = foo +%p foo +- tab_up +%p reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong +- tab_down +%p baz +- buffer.tabulation = 10 +%p boom