Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
add link_text_block option
Browse files Browse the repository at this point in the history
  • Loading branch information
keita committed Jun 6, 2012
1 parent a456d1e commit c1e30b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/twitter-text/autolink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions spec/autolinking_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit c1e30b1

Please sign in to comment.