Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby linkify用于字符串中的URL_Ruby_Regex_Linkify - Fatal编程技术网

Ruby linkify用于字符串中的URL

Ruby linkify用于字符串中的URL,ruby,regex,linkify,Ruby,Regex,Linkify,有一些帖子是关于使用正则表达式链接文本的。最受欢迎的 但是,我的规范有点棘手: describe TextFormatter do def l(input) TextFormatter.gsub_links!(input){|link| "!!#{link}!!"} end it "should detect simple links" do l("http://www.cnn.com").should == "!!http://www.cnn.com!!"

有一些帖子是关于使用正则表达式链接文本的。最受欢迎的

但是,我的规范有点棘手:

describe TextFormatter do 

  def l(input) 
    TextFormatter.gsub_links!(input){|link| "!!#{link}!!"}
  end

  it "should detect simple links" do
    l("http://www.cnn.com").should == "!!http://www.cnn.com!!"
  end

  it "should detect multi links" do
    l("http://www.cnn.com http://boats.com?help.asp").should == "!!http://www.cnn.com!! !!http://boats.com?help.asp!!"
  end

  it "should compensate for parans properly" do 
    l("(http://this.is?hello_world)").should == "(!!http://this.is?hello_world!!)"
  end

  it "should ignore existing links" do 
    s = "<A HREF='http://sam.com'> http://sam.com </A>"
    l(s.dup).should == s
  end

  it "should allow parans" do 
    l("http://sam.com.au?(red)").should == "!!http://sam.com.au?(red)!!"
  end

end

我可能遗漏了一些上下文,但为什么要重新发明轮子呢?您是否在
actionpack
中尝试了
auto_link

$ gem install actionpack

$ irb -f --prompt simple
>> require 'action_view'
>> include ActionView::Helpers

>> auto_link("abc http://google.com xyz")
=> "abc <a href=\"http://google.com\">http://google.com</a> xyz"
>> auto_link("abc <a href='http://google.com'>google</a> xyz")
=> "abc <a href='http://google.com'>google</a> xyz"
$gem安装actionpack
$irb-f——提示简单
>>需要“操作视图”
>>包括ActionView::Helpers
>>自动链接(“abchttp://google.com xyz“)
=>“abc xyz”
>>自动链接(“abc xyz”)
=>“abc xyz”

yerp,自动链接似乎通过了我所有的测试。。。感谢您指出为什么它可能无法在应用程序中工作?尝试执行此操作时,我收到一个错误“错误的参数数(2对1)文件:tag_helper.rb位置:tag_选项行:113”。虽然自3.1版开始,Rails就不再使用了
auto_link
,但它在控制台中工作得非常好。有关更新的答案,请参阅。
$ gem install actionpack

$ irb -f --prompt simple
>> require 'action_view'
>> include ActionView::Helpers

>> auto_link("abc http://google.com xyz")
=> "abc <a href=\"http://google.com\">http://google.com</a> xyz"
>> auto_link("abc <a href='http://google.com'>google</a> xyz")
=> "abc <a href='http://google.com'>google</a> xyz"