【问题标题】:How can I insert 'string' in HTML body?如何在 HTML 正文中插入“字符串”?
【发布时间】:2015-04-04 20:57:22
【问题描述】:

我正在尝试获取任何 HTML 并将特定字符串插入正文。

这是我的脚本的一部分:

  orig_comments = defect.Field(@td_columns[:"Comments"])      
  line_comments = "________________________________________"
  reopened_comments = "reopened"  
  reason_comments = "reason"      

  new_comments = "#{orig_comments}\n#{line_comments}\n#{reopened_comments}\n#{reason_comments}\n"
  new_comments.gsub(/\n/, '<br>')

orig_comments 是 HTML 格式:

<html>
<body>
<div align="left"><font face="Arial"><span style="font-size:9pt">&nbsp;&nbsp;</span></font></div>
<div align="left"><font face="Arial" color="#000080" size="1"><span style="font-size:8pt"><b>Hello&lt;hello.hello&gt;, 
2015-02-05:</b></span></font><font face="Arial"><span style="font-size:9pt"> </span></font></div>
<div align="left"><font face="Arial"><span style="font-size:9pt">1st line</span></font></div>
</body>
</html>

如果我只是将line_commentsreopend_commentsreason_comments 附加到orig_comments,HTML 视图中的结果是:

Hello<hello.hello>, 2015-02-05: 
1st line
________________________________________ reopened

我看不到“原因”cmets 和正确的换行符。

您能告诉我在这种情况下如何插入换行符和其他字符串吗?

更新

我实现了我想要的脚本并且运行良好。

另外,我还有一个问题。

当我追加一个新的孩子时,我想在值中使用变量。

例如,

date=Time.now.strftime("%Y-%m-%d") 

tmp1 = '<div align="left"><font face="Arial"><span style="font-size:9pt">#{date}</span></font></div>'

但是,“日期”变量没有按我的预期显示。 “变量”不起作用。

你能给我一些建议吗?

修改后的脚本

  orig_html = defect.Field(@td_columns[:"Comments"])
  #XML module only provides html parsing without the DOCTYPE tag.
  doc = Nokogiri::XML::DocumentFragment.parse(orig_html)  

  tmp1 = '<div align="left"><font face="Arial"><span style="font-size:9pt">________________________________________</span></font></div>'
  tmp2 = '<div align="left"><font face="Arial"><span style="font-size:9pt">This issue has been reopend. please, check it again referring following one of the reasons.</span></font></div>'
  tmp3 = '<div align="left"><font face="Arial"><span style="font-size:9pt">&lt;reopen reasons&gt;</span></font></div>'
  tmp4 = '<div align="left"><font face="Arial"><span style="font-size:9pt">1. This issue may have reoccur in the latest source codes.</span></font></div>'
  tmp5 = '<div align="left"><font face="Arial"><span style="font-size:9pt">2. The commit may not have been applied.</span></font></div>'
  tmp6 = '<div align="left"><font face="Arial"><span style="font-size:9pt">3. This issue may not have been fixed in the right way.</span></font></div>'
  tmp7 = '<div align="left"><font face="Arial"><span style="font-size:9pt">4. The tool may not have recognized your well-modified source codes.</span></font></div>'
  child1 = Nokogiri::XML::fragment(tmp1)
  child2 = Nokogiri::XML::fragment(tmp2)
  child3 = Nokogiri::XML::fragment(tmp3)
  child4 = Nokogiri::XML::fragment(tmp4)
  child5 = Nokogiri::XML::fragment(tmp5)
  child6 = Nokogiri::XML::fragment(tmp6)
  child7 = Nokogiri::XML::fragment(tmp7)
  body = doc.at('body')
  body.add_child(child1)
  body.add_child(child2)
  body.add_child(child3)
  body.add_child(child4)
  body.add_child(child5)
  body.add_child(child6)
  body.add_child(child7)      

【问题讨论】:

  • 您不应该尝试使用正则表达式来操作 HTML。虽然有可能让它工作,但它会非常脆弱。相反,学习使用 HTML/XML 解析器,例如 Nokogiri,这是 Ruby 的事实标准。您不能简单地附加到 HTML 文档。结束的&lt;/html&gt; 标记标志着文档的结束。相反,您需要搜索 inside &lt;html&gt;&lt;body&gt; 块中要插入新内容的位置,然后在该处进行操作。我建议你更多地了解 HTML 的结构,然后学习如何使用 Nokogiri。
  • 我已经安装了 Nokogiri。我会更多地研究它。感谢您的建议@the Tin Man

标签: html ruby string


【解决方案1】:

这是一个关于如何操作 HTML 的快速而肮脏的示例:

require 'nokogiri'

doc = Nokogiri::HTML(<<EOT)
<html>
<body>
<div align="left"><font face="Arial"><span style="font-size:9pt">&nbsp;&nbsp;</span></font></div>
<div align="left"><font face="Arial" color="#000080" size="1"><span style="font-size:8pt"><b>Hello&lt;hello.hello&gt;, 
2015-02-05:</b></span></font><font face="Arial"><span style="font-size:9pt"> </span></font></div>
<div align="left"><font face="Arial"><span style="font-size:9pt">1st line</span></font></div>
</body>
</html>
EOT

body = doc.at('body')

body.add_child(
  [
    '________________________________________', 
    'reopened', 
    'reason'
  ].join("<br>\n")
)

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>
# >> <div align="left"><font face="Arial"><span style="font-size:9pt">&#160;&#160;</span></font></div>
# >> <div align="left">
# >> <font face="Arial" color="#000080" size="1"><span style="font-size:8pt"><b>Hello&lt;hello.hello&gt;, 
# >> 2015-02-05:</b></span></font><font face="Arial"><span style="font-size:9pt"> </span></font>
# >> </div>
# >> <div align="left"><font face="Arial"><span style="font-size:9pt">1st line</span></font></div>
# >> ________________________________________<br>
# >> reopened<br>
# >> reason</body>
# >> </html>

Nokogiri 非常灵活,可以轻松修改 HTML 和 XML。诀窍是找到您想要的节点然后对其进行操作,或者如果节点只是一个地标,则可能是它周围的节点。

at('body') 方法搜索第一个body 标记并将其作为Nokogiri::XML::Node 返回。一旦我有了它,我就可以轻松地修改它或它的孩子。

add_child 可以接受几种不同类型的参数,但最容易传递它的是包含您要添加的 HTML 的字符串。在这种情况下,我在一个数组中创建了三个字符串,使用join 将它们与中间的&lt;br&gt;\n 一起附加,并将它们作为&lt;body&gt; 标记的最后一个子元素附加。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-20
    • 2018-08-14
    • 2011-05-04
    • 2020-09-22
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多