From 90c4923bf4633c63d38943a86f2984ded5d8fba2 Mon Sep 17 00:00:00 2001 From: Pat Nakajima Date: Tue, 14 Apr 2009 15:18:01 -0400 Subject: [PATCH] Added coverage for textilize and markdown helpers. --- actionpack/test/template/text_helper_test.rb | 12 ++++++++++++ actionpack/test/testing_sandbox.rb | 9 +++++++++ 2 files changed, 21 insertions(+), 0 deletions(-) diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index be71638..b37ac48 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -402,6 +402,18 @@ class TextHelperTest < ActionView::TestCase :link => :all, :html => { :class => "menu", :target => "_blank" }) end + def test_markdown + with_lib('BlueCloth') do + assert_dom_equal '

A header

', markdown('#A header') + end + end + + def test_textilize + with_lib('RedCloth') do + assert_dom_equal '

A header

', textilize('h1. A header') + end + end + def test_cycle_class value = Cycle.new("one", 2, "3") assert_equal("one", value.to_s) diff --git a/actionpack/test/testing_sandbox.rb b/actionpack/test/testing_sandbox.rb index c365851..409b046 100644 --- a/actionpack/test/testing_sandbox.rb +++ b/actionpack/test/testing_sandbox.rb @@ -12,4 +12,13 @@ module TestingSandbox yield end end + + def with_lib(name) + begin + require name + yield + rescue LoadError + pending "Skipping tests that rely on #{name}" + end + end end -- 1.6.0.6 From 3653189d5ebad8166166e2ba0d5e26ada2ad2cc6 Mon Sep 17 00:00:00 2001 From: Pat Nakajima Date: Tue, 14 Apr 2009 16:31:36 -0400 Subject: [PATCH] Added ActionView::Base.text_helper. The text_helper method allows you to specify which classes should handle text formattings logic for methods such as Markdown and Textile, or even register your own. --- actionpack/lib/action_view/base.rb | 8 +++++ actionpack/lib/action_view/helpers/text_helper.rb | 33 +++++++++++++++++++- actionpack/test/template/text_helper_test.rb | 5 +++ actionpack/test/testing_sandbox.rb | 10 ++++++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index efed19a..220a802 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -187,6 +187,14 @@ module ActionView #:nodoc: @@cache_template_loading = nil cattr_accessor :cache_template_loading + # Register classes to be used for various text helpers + @@text_helper_classes = {} + cattr_accessor :text_helper_classes + + # Defaults for text helper classes + text_helper :markdown, :BlueCloth + text_helper :textile, :RedCloth + def self.cache_template_loading? ActionController::Base.allow_concurrency || (cache_template_loading.nil? ? !ActiveSupport::Dependencies.load? : cache_template_loading) end diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 573b99b..f7f531c 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -2,6 +2,35 @@ require 'action_view/helpers/tag_helper' module ActionView module Helpers #:nodoc: + module ClassMethods + # Allows custom text helper formatters to be registered. By default, Rails + # has formatters for Textile (RedCloth) and Markdown (BlueCloth). You can add + # new classes using the text_helper method: + # + # class SyntaxHighlighter + # def initialize(text) + # @text = text + # end + # + # def to_html + # `pygmentize #{@text}` + # end + # end + # + # ActionView::Base.text_helper :syntax_highlight, SyntaxHighlighter + # + def text_helper(name, klass=nil) + case klass + when nil + ActionView::Base.text_helper_classes[name].to_s.constantize + when Symbol, String + ActionView::Base.text_helper_classes[name] = klass + else Class + ActionView::Base.text_helper_classes[name] = klass.to_s + end + end + end + # The TextHelper module provides a set of methods for filtering, formatting # and transforming strings, which can reduce the amount of inline Ruby code in # your views. These helper methods extend ActionView making them callable @@ -238,7 +267,7 @@ module ActionView if text.blank? "" else - textilized = RedCloth.new(text, [ :hard_breaks ]) + textilized = ActionView::Base.text_helper(:textile).new(text, [ :hard_breaks ]) textilized.hard_breaks = true if textilized.respond_to?(:hard_breaks=) textilized.to_html end @@ -288,7 +317,7 @@ module ActionView # markdown('![The ROR logo](http://rubyonrails.com/images/rails.png "Ruby on Rails")') # # => '

The ROR logo

' def markdown(text) - text.blank? ? "" : BlueCloth.new(text).to_html + text.blank? ? "" : ActionView::Base.text_helper(:markdown).new(text).to_html end # Returns +text+ transformed into HTML using simple formatting rules. diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index b37ac48..da5ca46 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -12,6 +12,11 @@ class TextHelperTest < ActionView::TestCase @_cycles = nil if (defined? @_cycles) end + def test_register_different_formatter_class + ActionView::Base.text_helper :markdown, FakeMarkdown + assert_equal 'FAKE MARKDOWN: # A header', markdown('# A header') + end + def test_concat self.output_buffer = 'foo' assert_equal 'foobar', concat('bar') diff --git a/actionpack/test/testing_sandbox.rb b/actionpack/test/testing_sandbox.rb index 409b046..15fd5de 100644 --- a/actionpack/test/testing_sandbox.rb +++ b/actionpack/test/testing_sandbox.rb @@ -1,4 +1,14 @@ module TestingSandbox + class FakeMarkdown + def initialize(text) + @text = text + end + + def to_html + "FAKE MARKDOWN: #{@text}" + end + end + # Temporarily replaces KCODE for the block def with_kcode(kcode) if RUBY_VERSION < '1.9' -- 1.6.0.6