duanpin5168 2012-04-09 03:22
浏览 31
已采纳

使用go-html-transform预处理HTML:替换失败

Following on from this question on whitelisting HTML tags, I've been experimenting with Jeremy Wall's go-html-transform. In the hopes of improving searchable documentation I'm asking this here rather than pestering the author directly... hopefully this isn't too tool-specific for SO.

App Engine, latest SDK. Post.Body is a []byte. This works:

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())
}

Result:

<strong>Blarg.<em>Foo</em></strong>

However, if instead of AppendChildren() I use something like the following:

t.Apply(transform.Replace(h5.Text("<em>Foo</em>")), "strong")

I get an internal server error. Have I misunderstood the use of Replace()? The existing documentation suggests this sort of thing should be possible.

  • 写回答

1条回答 默认 最新

  • dongwolu5275 2012-04-09 16:46
    关注

    Running your transform code outside of App Engine, it panics and you can see a TODO in the source at that point. Then it's not too much harder to read the code and see that it's going to panic if given a root node.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器