Permalink
Browse files

add link_text_block option

  • Loading branch information...
1 parent a456d1e commit c1e30b1f56fa8679946f190468051cab392d4577 @keitaf keitaf committed Jun 6, 2012
Showing with 22 additions and 1 deletion.
  1. +3 −1 lib/twitter-text/autolink.rb
  2. +19 −0 spec/autolinking_spec.rb
@@ -187,7 +187,8 @@ def html_escape(text)
:username_url_base, :list_url_base, :hashtag_url_base, :cashtag_url_base,
:username_url_block, :list_url_block, :hashtag_url_block, :link_url_block,
:username_include_symbol, :suppress_lists, :suppress_no_follow, :url_entities,
- :invisible_tag_attrs, :symbol_tag, :text_with_symbol_tag, :url_target, :link_attribute_block
+ :invisible_tag_attrs, :symbol_tag, :text_with_symbol_tag, :url_target,
+ :link_attribute_block, :link_text_block
]).freeze
def extract_html_attrs_from_options!(options)
@@ -382,6 +383,7 @@ def link_to_text_with_symbol(entity, symbol, text, href, attributes = {}, option
def link_to_text(entity, text, href, attributes = {}, options = {})
attributes[:href] = href
options[:link_attribute_block].call(entity, attributes) if options[:link_attribute_block]
+ text = options[:link_text_block].call(entity, text) if options[:link_text_block]
%(<a#{tag_attrs(attributes)}>#{text}</a>)
end
View
@@ -702,6 +702,25 @@ def original_text; "Single char file ext http://www.bestbuy.com/site/Currie+Tech
linked.should match(/<a[^>]+dummy-url-attr=\"http:\/\/twitter.com\/\"/)
end
+ it "should modify link text by link_text_block" do
+ linked = @linker.auto_link("#hash @mention",
+ :link_text_block => lambda{|entity, text|
+ entity[:hashtag] ? "#replaced" : "pre_#{text}_post"
+ }
+ )
+ linked.should match(/<a[^>]+>#replaced<\/a>/)
+ linked.should match(/<a[^>]+>pre_mention_post<\/a>/)
+
+ linked = @linker.auto_link("#hash @mention", {
+ :link_text_block => lambda{|entity, text|
+ "pre_#{text}_post"
+ },
+ :symbol_tag => "s", :text_with_symbol_tag => "b", :username_include_symbol => true
+ })
+ linked.should match(/<a[^>]+>pre_<s>#<\/s><b>hash<\/b>_post<\/a>/)
+ linked.should match(/<a[^>]+>pre_<s>@<\/s><b>mention<\/b>_post<\/a>/)
+ end
+
it "should apply :url_target only to auto-linked URLs" do
auto_linked = @linker.auto_link("#hashtag @mention http://test.com/", {:url_target => '_blank'})
auto_linked.should have_autolinked_hashtag('#hashtag')

0 comments on commit c1e30b1

Please sign in to comment.