github.com/blend/go-sdk@v1.20220411.3/diff/pretty_html.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package diff
     9  
    10  import (
    11  	"bytes"
    12  	"html"
    13  	"strings"
    14  )
    15  
    16  // PrettyHTML converts a []Diff into a pretty HTML report.
    17  // It is intended as an example from which to write one's own display functions.
    18  func PrettyHTML(diffs []Diff) string {
    19  	var buff bytes.Buffer
    20  	for _, diff := range diffs {
    21  		text := strings.Replace(html.EscapeString(diff.Text), "\n", "&para;<br>", -1)
    22  		switch diff.Type {
    23  		case DiffInsert:
    24  			_, _ = buff.WriteString("<ins style=\"background:#e6ffe6;\">")
    25  			_, _ = buff.WriteString(text)
    26  			_, _ = buff.WriteString("</ins>")
    27  		case DiffDelete:
    28  			_, _ = buff.WriteString("<del style=\"background:#ffe6e6;\">")
    29  			_, _ = buff.WriteString(text)
    30  			_, _ = buff.WriteString("</del>")
    31  		case DiffEqual:
    32  			_, _ = buff.WriteString("<span>")
    33  			_, _ = buff.WriteString(text)
    34  			_, _ = buff.WriteString("</span>")
    35  		}
    36  	}
    37  	return buff.String()
    38  }