【发布时间】: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"> </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
我看不到“原因”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"><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)
【问题讨论】:
-
您不应该尝试使用正则表达式来操作 HTML。虽然有可能让它工作,但它会非常脆弱。相反,学习使用 HTML/XML 解析器,例如 Nokogiri,这是 Ruby 的事实标准。您不能简单地附加到 HTML 文档。结束的
</html>标记标志着文档的结束。相反,您需要搜索 inside<html><body>块中要插入新内容的位置,然后在该处进行操作。我建议你更多地了解 HTML 的结构,然后学习如何使用 Nokogiri。 -
我已经安装了 Nokogiri。我会更多地研究它。感谢您的建议@the Tin Man