html-parsing - 使用 go-html-transform 预处理 HTML : Replace fails

标签 html-parsing go

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/

相关文章:

PHP 脚本读取外部 HTML 源代码并列出标签之间的代码

从网站下载图像并显示文件大小的 Java 程序

go - 当谷歌更改包裹的网址时该怎么办?

json - 将 Steam Rest API 与 JSON 结合使用

go - cgo调用共享库: cannot find lib or function?

python - 用美汤加父标签

python - BeautifulSoup 在使用 find 时挂起

go - 在 go 中通过 SSH 发送文件

go - 我们如何在 kafka 中快速写入单条消息(不是批量消息)?

python - 漂亮的汤无法解析这个 HTML