我正在尝试获取任何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"> </span></font></div>
<div align="left"><font face="Arial" color="#000080" size="1"><span style="font-size:8pt"><b>Hello<hello.hello>,
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_comments
,reopend_comments
和reason_comments
附加到orig_comments
,则HTML视图中的结果为:
Hello<hello.hello>, 2015-02-05:
1st line
________________________________________ reopened
我无法看到&#39;原因&#39;评论和适当的新行。
你可以告诉我如何在这种情况下插入换行符和其他字符串吗?
更新
我实现了我想要的脚本并且运行良好。
此外,我还有一个问题。
当我追加一个新孩子时,我想在值中使用变量。
例如,
date=Time.now.strftime("%Y-%m-%d")
tmp1 = '<div align="left"><font face="Arial"><span style="font-size:9pt">#{date}</span></font></div>'
但是,&#39; date&#39;变量未按预期显示。 &#39;可变&#39;没有用。
你可以告诉我任何建议吗?修改过的脚本
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"><reopen reasons></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)
答案 0 :(得分:2)
以下是如何操作HTML的快速而肮脏的示例:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<html>
<body>
<div align="left"><font face="Arial"><span style="font-size:9pt"> </span></font></div>
<div align="left"><font face="Arial" color="#000080" size="1"><span style="font-size:8pt"><b>Hello<hello.hello>,
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">  </span></font></div>
# >> <div align="left">
# >> <font face="Arial" color="#000080" size="1"><span style="font-size:8pt"><b>Hello<hello.hello>,
# >> 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
将它们与干预的<br>\n
一起追加,并将它们作为<body>
标记的最后一个子项附加。