从 this question on whitelisting HTML tags 开始,我一直在试验 Jeremy Wall 的 go-html-transform .为了改进可搜索的文档,我在这里提出这个问题而不是直接纠缠作者......希望这对于 SO 来说不是太特定于工具。
App Engine,最新的 SDK。 Post.Body 是一个 []byte。这有效:
package posts
import (
// ...
"html/template"
"code.google.com/p/go-html-transform/html/transform"
"code.google.com/p/go-html-transform/h5"
)
// ...
// Pre-process post body, then return it to the template as HTML()
// to avoid html/template's escaping allowable tags
func (p *Post) BodyHTML() template.HTML {
doc, _ := transform.NewDoc(string(p.Body))
t := transform.NewTransform(doc)
// Add some text to the end of any <strong></strong> nodes.
t.Apply(transform.AppendChildren(h5.Text("<em>Foo</em>")), "strong")
return template.HTML(t.String())
}
结果:
<strong>Blarg.<em>Foo</em></strong>
但是,如果我不使用 AppendChildren(),而是使用如下内容:
t.Apply(transform.Replace(h5.Text("<em>Foo</em>")), "strong")
我收到一个内部服务器错误。我是否误解了 Replace() 的使用? The existing documentation表明这种事情应该是可能的。
最佳答案
在 App Engine 之外运行您的转换代码时,它会出现困惑,此时您可以在源代码中看到一个 TODO。然后阅读代码并看到如果给定根节点它会 panic 就不会太难了。
关于html-parsing - 使用 go-html-transform 预处理 HTML : Replace fails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10068552/