MCPcopy Create free account
hub / github.com/gogs/gogs / diffsToHTML

Function diffsToHTML

internal/gitutil/diff.go:73–106  ·  view source on GitHub ↗
(diffs []diffmatchpatch.Diff, lineType git.DiffLineType)

Source from the content-addressed store, hash-verified

71}
72
73func diffsToHTML(diffs []diffmatchpatch.Diff, lineType git.DiffLineType) template.HTML {
74 buf := bytes.NewBuffer(nil)
75
76 // Reproduce signs which are cut for inline diff before.
77 switch lineType {
78 case git.DiffLineAdd:
79 buf.WriteByte('+')
80 case git.DiffLineDelete:
81 buf.WriteByte('-')
82 }
83
84 const (
85 addedCodePrefix = `<span class="added-code">`
86 removedCodePrefix = `<span class="removed-code">`
87 codeTagSuffix = `</span>`
88 )
89
90 for i := range diffs {
91 switch {
92 case diffs[i].Type == diffmatchpatch.DiffInsert && lineType == git.DiffLineAdd:
93 buf.WriteString(addedCodePrefix)
94 buf.WriteString(html.EscapeString(diffs[i].Text))
95 buf.WriteString(codeTagSuffix)
96 case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == git.DiffLineDelete:
97 buf.WriteString(removedCodePrefix)
98 buf.WriteString(html.EscapeString(diffs[i].Text))
99 buf.WriteString(codeTagSuffix)
100 case diffs[i].Type == diffmatchpatch.DiffEqual:
101 buf.WriteString(html.EscapeString(diffs[i].Text))
102 }
103 }
104
105 return template.HTML(buf.Bytes())
106}
107
108// DiffFile is a wrapper to git.DiffFile with helper methods.
109type DiffFile struct {

Callers 2

ComputedInlineDiffForMethod · 0.85
Test_diffsToHTMLFunction · 0.85

Calls 2

HTMLMethod · 0.80
BytesMethod · 0.80

Tested by 1

Test_diffsToHTMLFunction · 0.68