简体   繁体   English

Ruby字符串插值以将图像添加到链接,在图像src之前加一个斜杠

[英]Ruby string interpolation to add image to a link prefixes a slash to image src

I am using Ruby 2.3.1p112 and I am trying to use string interpolation to produce an image link. 我正在使用Ruby 2.3.1p112,并且正在尝试使用字符串插值法生成图像链接。 However, it incorrectly escapes the link src quotes, like this: src=\\" http://localhost:3000/t \\" . 但是,它错误地转义了链接src引号,例如: src = \\“ http:// localhost:3000 / t \\” The example is shown below: 示例如下所示:

 <a href=www.google.com target='_blank'> google.com \"<img src=\"http://localhost:3000/t\" width='1' height='1'>\"</a>

This is not view code; 这不是查看代码; it happens on the backend and here is the class extracted out and simplified to show the problem 它发生在后端,这是提取并简化以显示问题的类

class Link
  require 'uri'

  def self.link_name(url)
    uri = URI.parse(url)
    uri = URI.parse("http://#{url}") if uri.scheme.nil?
    host = uri.host.downcase
    host.start_with?('www.') ? host[4..-1] : host
  end

  def self.url_regex
    /(http:|www.)[a-zA-Z0-9\/:\.\?]*/
  end

  def self.link_image(e)
    email = ['b@y.com', 'x@go.com']
      email.map do |p|
        token = new.generate_email_click_tracking_img
        e.gsub(url_regex) do |url|

        puts "token inloop is <a href=#{url}>#{link_name(url)}  #{token} </a>"

        "<a href=#{url} target='_blank'> #{link_name(url)} \"#{token}\"</a>"
      end   
    end
  end

  def generate_email_click_tracking_img
    url = "http://localhost:3000/t"
    "<img src=\"#{url}\" width='1' height='1'>"
  end

end  

You can reproduce it by running the code below in irb: 您可以通过在irb中运行以下代码来重现它:

a =  "me in www.google.com, you in http://www.facebook.com"
Link.link_image(a)

If you run the code above, you will see that the puts statement logs the correct thing and the image src is : 如果运行上面的代码,您将看到puts语句记录了正确的内容,图像src为:

<a href=http://www.facebook.com>facebook.com  <img src="http://localhost:3000/t" width='1' height='1'> </a>

But without the puts statement the image src is surrounded with escaped quotes: http://localhost:3000/t\\" 但是,如果没有puts语句,则图像src会包含转义的引号:http:// localhost:3000 / t \\“

<a href=http://www.facebook.com target='_blank'> facebook.com \"<img src=\"http://localhost:3000/t\" width='1' height='1'>\"</a>

What is the best way to to remove the quote escapes in the image src? 删除图像src中的引号转义的最佳方法是什么?

There is no backslash. 没有反斜杠。 Your code is working just fine. 您的代码工作正常。

You can reproduce it by running the code below in irb 您可以通过在irb中运行以下代码来重现它

Try running this in irb : 尝试在irb运行它:

puts '"hello"'
# => "hello"
'"hello"'
# => "\"hello\""

All you are seeing is that when directly outputting a variable, irb is displaying the raw string. 您所看到的是,当直接输出变量时, irb显示原始字符串。 And, because the string is terminated by " characters, it is therefore necessary to escape any " characters inside the output, upon display. 并且,由于字符串以"字符结尾,因此在显示时必须在输出中转义任何"字符。

If the string really did contain literal backslashes, what you would see instead of 如果该字符串确实确实包含文字反斜杠,那么您将看到的不是

<a href=http://www.facebook.com target='_blank'> facebook.com \"<img src=\"http://localhost:3000/t\" width='1' height='1'>\"</a>

Would be: 将会:

<a href=http://www.facebook.com target='_blank'> facebook.com \\\"<img src=\\\"http://localhost:3000/t\\\" width='1' height='1'>\\\"</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM