我已经尝试了很多这样的排列:
builder = Nokogiri::HTML::Builder.new do |doc|
doc.html {
doc.body {
links.each do |i|
doc.p {
doc.text "#{i.text}"
}
doc.a["href"] = i[:href]
end
}
}
end
其中 links
是一个数组,其中包含 test
和 :href
所需的值。
这产生的是(为简洁起见缩写):
This is the HTML generated
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<p>10 ∙ Progamer Lim Yohwan, the E-Sports Icon</p>
<a href="http://boxerbiography.blogspot.com/2006/12/10-progamer-lim-yohwan-e-sports-icon.html"></a>
我希望它产生的地方是:
<p><a href="http://boxerbiography.blogspot.com/2006/12/10-progamer-lim-yohwan-e-sports-icon.html">10 ∙ Progamer Lim Yohwan, the E-Sports Icon</a></p>
我该怎么做?
最佳答案
随着builder interface ,属性作为参数提供给 doc.tagname
调用,内容进入 block 内。所以像这样的事情应该可以解决问题:
builder = Nokogiri::HTML::Builder.new do |doc|
doc.html {
doc.body {
links.each do |i|
doc.p {
doc.a(:href => i[:href]) {
doc.text i.text # or maybe i[:text]
}
}
end
}
}
end
关于ruby - 如果我想要 HTML 输出,如何使用 Nokogiri 向某些文本添加 a'href' 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8025598/