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 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端