dongyoufo5672 2018-04-15 03:11
浏览 214

git差异到HTML

I'm writing a webhook in Go that sends me an email with the diff for every commit to certain repository. Right now I am sending the diff as raw text like this:

https://github.com/ee92/go-lambda/commit/ac56fc2cfe86c50e9d73ecb0f8db74c672e205cd.diff

I wish to send it as pretty formatted HTML with color like you see on github or bitbucket so it will be easier to read what changed. Really stumped on how to go about this. Appreciate any advice. Thanks.

  • 写回答

1条回答 默认 最新

  • douquan3294 2018-04-16 15:41
    关注

    You could use the stdlib html/template library to make a pretty HTML template and pass your raw string as a param:

    https://golang.org/pkg/html/template/

    import "text/template"
    ...
    t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
    err = t.ExecuteTemplate(out, "T", "<script>alert('you have been pwned')</script>")
    

    will produce this:

    Hello, <script>alert('you have been pwned')</script>!
    

    So in your case, define a template in a separate file, read it in, and then call

    t, err := template.ParseFiles("./path/to/template.html")
    if err != nil {
        log.Fatal(err)
    }
    
    err = t.ExecuteTemplate(out, "T", rawDiffString)
    if err != nil {
        log.Fatal(err)
    }
    

    which will take your diff string and stick it into the template wherever you defined the template variable.

    You'll have to read the spec for how Go parses through the HTML template to properly format your HTML file.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥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做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序