如何添加商标符号

时间:2015-09-02 15:24:53

标签: ruby nokogiri

我正在尝试在HTML文档中的“Imagination Playground”的所有实例中添加商标符号。但是我最终得到了这样的东西:

<i class="fa fa-trademark"></i>

似乎我正在使用的符号被转换为HTML字符。我该怎么逃避呢?

这是我原来的Ruby代码:

body = "<p>Whether you want to build a playground, make play a priority in your community, or learn more about Imagination Playground , we've got webinars for you in March!</p>
  <p>As always, all our webinars are FREE. All you need to participate is a phone and a computer with an Internet connection.</p>"
new_body = Nokogiri::HTML(body)
new_body.encoding = 'UTF-8'
new_body.css('p','a').each{ |p|
p.content =  p.content.gsub(/Imagination Playground\s/,'Imagination Playground<i class="fa fa-trademark"></i>');
puts new_body

这就是我得到的:

<p>Whether you want to build a playground, make play a priority in your community, or learn more about Imagination Playground&lt;i class="fa fa-trademark"&gt;&lt;/i&gt;, we've got webinars for you in March!</p>
<p>As always, all our webinars are FREE. All you need to participate is a phone and a computer with an Internet connection.</p>

如何替换该HTML段落并转义&符号和特殊字符?

1 个答案:

答案 0 :(得分:2)

我是这样做的:

require 'nokogiri'

doc = Nokogiri::HTML(<<EOT)
<p>Whether you want to build a playground, make play a priority in your community, or learn more about Imagination Playground , we've got webinars for you in March!</p>
<p>As always, all our webinars are FREE. All you need to participate is a phone and a computer with an Internet connection.</p>
EOT

doc.encoding = 'UTF-8'
doc.css('p').each do |p|
  p.children = p.content.gsub(/Imagination Playground\s/, 'Imagination Playground<i class="fa fa-trademark"></i>')
end
puts doc

结果是:

# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body>
# >> <p>Whether you want to build a playground, make play a priority in your community, or learn more about Imagination Playground<i class="fa fa-trademark"></i>, we've got webinars for you in March!</p>
# >> <p>As always, all our webinars are FREE. All you need to participate is a phone and a computer with an Internet connection.</p>
# >> </body></html>

Nokogiri非常聪明。当它看到children=时,它会查看它是否正在接收字符串。如果是这样,它会解析该字符串并将其转换为Node,然后用新节点替换现有子节点。这与使用Nokogiri知道应该是文本的content=有很大的不同,然后将嵌入的标签编码为&lt;等。文档中对此进行了介绍。

children=

  

为此节点设置内部html node_or_tags node_or_tags可以是Nokogiri :: XML :: Node,Nokogiri :: XML :: DocumentFragment,或包含标记的字符串。

content=

  

将Node的内容设置为包含字符串的Text节点。字符串获取XML转义,而不是解释为标记。

  

如果我想保留段落中的html标记,尝试对<p>fsome test and then <b>bold</b></p>

执行此操作,则无效

您正在更改要求。不要那样做。请具体说明您的需求,以便我们能够回答一次真正的问题。

需要进行少量更改以获取所需标记的内容。使用children.to_html获取嵌入节点的HTML字符串,然后gsub并使用其结果:

require 'nokogiri'

doc = Nokogiri::HTML('<p>Imagination Playground<b>foo</b></p>')
puts doc.to_html

这看起来像这样:

# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body><p>Imagination Playground<b>foo</b></p></body></html>

修改DOM:

doc.search('p').each do |p|
  p.children = p.children.to_html.gsub(/Imagination Playground\s?/, 'Imagination Playground<i class="fa fa-trademark"></i>')
end
puts doc

现在看起来像:

# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body><p>Imagination Playground<i class="fa fa-trademark"></i><b>foo</b></p></body></html>

请注意,我使用search代替css。使用通用方法而不是更具体的方法。如果需要,它可以更轻松地切换到XPath。

另外,我在gsub中使用更智能的模式来有条件地抓取单个尾随空格(如果可用)。用HTML做这件事并不是必不可少的,因为浏览器会消耗空白,但如果你处理常规文本文档或预先格式化的文本,这将是正确的方法。

而且,关于Nokogiri所看到的更多细节:

doc.search('p').first 
# => #(Element:0x3fd222462204 {
#      name = "p",
#      children = [
#        #(Text "Imagination Playground"),
#        #(Element:0x3fd2224608f0 { name = "b", children = [ #(Text "foo")] })]
#      })
doc.search('p').first.children 
# => [#<Nokogiri::XML::Text:0x3fd222461688 "Imagination Playground">, #<Nokogiri::XML::Element:0x3fd2224608f0 name="b" children=[#<Nokogiri::XML::Text:0x3fd22245fe64 "foo">]>]