| 71 | } |
| 72 | |
| 73 | func 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. |
| 109 | type DiffFile struct { |