Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure the pretty functions. Add markdown pretty function. Diff result with old and new pretty color function #131

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 123 additions & 15 deletions diffmatchpatch/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ type Diff struct {
Text string
}

type PrettyInfo struct {
PrefixInsert string
SuffixInsert string
PrefixDelete string
SuffixDelete string
PrefixEqual string
SuffixEqual string
Escape bool
}

// splice removes amount elements from slice at index index, replacing them with elements.
func splice(slice []Diff, index int, amount int, elements ...Diff) []Diff {
if len(elements) == amount {
Expand Down Expand Up @@ -1122,44 +1132,142 @@ func (dmp *DiffMatchPatch) DiffXIndex(diffs []Diff, loc int) int {
// DiffPrettyHtml converts a []Diff into a pretty HTML report.
// It is intended as an example from which to write one's own display functions.
func (dmp *DiffMatchPatch) DiffPrettyHtml(diffs []Diff) string {
return dmp.DiffPretty(diffs, PrettyInfo{
PrefixInsert: "<ins style=\"background:#e6ffe6;\">",
SuffixInsert: "</ins>",
PrefixDelete: "<del style=\"background:#ffe6e6;\">",
SuffixDelete: "</del>",
PrefixEqual: "<span>",
SuffixEqual: "</span>",
Escape: true,
})
}

// DiffPrettyText converts a []Diff into a colored text report.
func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string {
return dmp.DiffPretty(diffs, PrettyInfo{
PrefixInsert: "\x1b[32m",
SuffixInsert: "\x1b[0m",
PrefixDelete: "\x1b[31m",
SuffixDelete: "\x1b[0m",
PrefixEqual: "",
SuffixEqual: "",
})
}

// DiffPrettyMarkdown converts a []Diff into a colored makdown report.
func (dmp *DiffMatchPatch) DiffPrettyMarkdown(diffs []Diff) string {
return dmp.DiffPretty(diffs, PrettyInfo{
PrefixInsert: "<font color=\"#00ff00\">",
SuffixInsert: "</font>",
PrefixDelete: "<font color=\"#ff0000\">",
SuffixDelete: "</font>",
PrefixEqual: "",
SuffixEqual: "",
})
}

func (dmp *DiffMatchPatch) DiffPrettyHtmlAll(diffs []Diff) (old, new string) {
prettyInfo := PrettyInfo{
PrefixInsert: "<ins style=\"background:#e6ffe6;\">",
SuffixInsert: "</ins>",
PrefixDelete: "<del style=\"background:#ffe6e6;\">",
SuffixDelete: "</del>",
PrefixEqual: "<span>",
SuffixEqual: "</span>",
Escape: true,
}
return dmp.DiffPrettyOld(diffs, prettyInfo), dmp.DiffPrettyNew(diffs, prettyInfo)
}

func (dmp *DiffMatchPatch) DiffPrettyTextAll(diffs []Diff) (old, new string) {
prettyInfo := PrettyInfo{
PrefixInsert: "\x1b[32m",
SuffixInsert: "\x1b[0m",
PrefixDelete: "\x1b[31m",
SuffixDelete: "\x1b[0m",
PrefixEqual: "",
SuffixEqual: "",
}
return dmp.DiffPrettyOld(diffs, prettyInfo), dmp.DiffPrettyNew(diffs, prettyInfo)
}

func (dmp *DiffMatchPatch) DiffPrettyMarkdownAll(diffs []Diff) (old, new string) {
prettyInfo := PrettyInfo{
PrefixInsert: "<font color=\"#00ff00\">",
SuffixInsert: "</font>",
PrefixDelete: "<font color=\"#ff0000\">",
SuffixDelete: "</font>",
PrefixEqual: "",
SuffixEqual: "",
}
return dmp.DiffPrettyOld(diffs, prettyInfo), dmp.DiffPrettyNew(diffs, prettyInfo)
}

func (dmp *DiffMatchPatch) DiffPretty(diffs []Diff, PrettyInfo PrettyInfo) string {
var buff bytes.Buffer
for _, diff := range diffs {
text := strings.Replace(html.EscapeString(diff.Text), "\n", "&para;<br>", -1)
text := diff.Text
if PrettyInfo.Escape {
text = strings.Replace(html.EscapeString(diff.Text), "\n", "&para;<br>", -1)
}
switch diff.Type {
case DiffInsert:
_, _ = buff.WriteString("<ins style=\"background:#e6ffe6;\">")
_, _ = buff.WriteString(PrettyInfo.PrefixInsert)
_, _ = buff.WriteString(text)
_, _ = buff.WriteString("</ins>")
_, _ = buff.WriteString(PrettyInfo.SuffixInsert)
case DiffDelete:
_, _ = buff.WriteString("<del style=\"background:#ffe6e6;\">")
_, _ = buff.WriteString(PrettyInfo.PrefixDelete)
_, _ = buff.WriteString(text)
_, _ = buff.WriteString("</del>")
_, _ = buff.WriteString(PrettyInfo.SuffixDelete)
case DiffEqual:
_, _ = buff.WriteString("<span>")
_, _ = buff.WriteString(PrettyInfo.PrefixEqual)
_, _ = buff.WriteString(text)
_, _ = buff.WriteString("</span>")
_, _ = buff.WriteString(PrettyInfo.SuffixEqual)
}
}

return buff.String()
}

// DiffPrettyText converts a []Diff into a colored text report.
func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string {
func (dmp *DiffMatchPatch) DiffPrettyOld(diffs []Diff, PrettyInfo PrettyInfo) string {
var buff bytes.Buffer
for _, diff := range diffs {
text := diff.Text
if PrettyInfo.Escape {
text = strings.Replace(html.EscapeString(diff.Text), "\n", "&para;<br>", -1)
}
switch diff.Type {
case DiffDelete:
_, _ = buff.WriteString(PrettyInfo.PrefixDelete)
_, _ = buff.WriteString(text)
_, _ = buff.WriteString(PrettyInfo.SuffixDelete)
case DiffEqual:
_, _ = buff.WriteString(PrettyInfo.PrefixEqual)
_, _ = buff.WriteString(text)
_, _ = buff.WriteString(PrettyInfo.SuffixEqual)
}
}

return buff.String()
}

func (dmp *DiffMatchPatch) DiffPrettyNew(diffs []Diff, PrettyInfo PrettyInfo) string {
var buff bytes.Buffer
for _, diff := range diffs {
text := diff.Text
if PrettyInfo.Escape {
text = strings.Replace(html.EscapeString(diff.Text), "\n", "&para;<br>", -1)
}
switch diff.Type {
case DiffInsert:
_, _ = buff.WriteString("\x1b[32m")
_, _ = buff.WriteString(text)
_, _ = buff.WriteString("\x1b[0m")
case DiffDelete:
_, _ = buff.WriteString("\x1b[31m")
_, _ = buff.WriteString(PrettyInfo.PrefixInsert)
_, _ = buff.WriteString(text)
_, _ = buff.WriteString("\x1b[0m")
_, _ = buff.WriteString(PrettyInfo.SuffixInsert)
case DiffEqual:
_, _ = buff.WriteString(PrettyInfo.PrefixEqual)
_, _ = buff.WriteString(text)
_, _ = buff.WriteString(PrettyInfo.SuffixEqual)
}
}

Expand Down
106 changes: 106 additions & 0 deletions diffmatchpatch/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,112 @@ func TestDiffPrettyText(t *testing.T) {
}
}

func TestDiffPrettyMarkdown(t *testing.T) {
type TestCase struct {
Diffs []Diff

Expected string
}

dmp := New()

for i, tc := range []TestCase{
{
Diffs: []Diff{
{DiffEqual, "a\n"},
{DiffDelete, "<B>b</B>"},
{DiffInsert, "c&d"},
},

Expected: "a\n<font color=\"#ff0000\"><B>b</B></font><font color=\"#00ff00\">c&d</font>",
},
} {
actual := dmp.DiffPrettyMarkdown(tc.Diffs)
assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
}
}

func TestDiffPrettyHtmlAll(t *testing.T) {
type TestCase struct {
Diffs []Diff

ExpectedOld string
ExpectedNew string
}

dmp := New()

for i, tc := range []TestCase{
{
Diffs: []Diff{
{DiffEqual, "a\n"},
{DiffDelete, "<B>b</B>"},
{DiffInsert, "c&d"},
},
ExpectedOld: "<span>a&para;<br></span><del style=\"background:#ffe6e6;\">&lt;B&gt;b&lt;/B&gt;</del>",
ExpectedNew: "<span>a&para;<br></span><ins style=\"background:#e6ffe6;\">c&amp;d</ins>",
},
} {
actualOld, actualNew := dmp.DiffPrettyHtmlAll(tc.Diffs)
assert.Equal(t, tc.ExpectedOld, actualOld, fmt.Sprintf("Test case #%d, %#v", i, tc))
assert.Equal(t, tc.ExpectedNew, actualNew, fmt.Sprintf("Test case #%d, %#v", i, tc))
}
}

func TestDiffPrettyTextAll(t *testing.T) {
type TestCase struct {
Diffs []Diff

ExpectedOld string
ExpectedNew string
}

dmp := New()

for i, tc := range []TestCase{
{
Diffs: []Diff{
{DiffEqual, "a\n"},
{DiffDelete, "<B>b</B>"},
{DiffInsert, "c&d"},
},
ExpectedOld: "a\n\x1b[31m<B>b</B>\x1b[0m",
ExpectedNew: "a\n\x1b[32mc&d\x1b[0m",
},
} {
actualOld, actualNew := dmp.DiffPrettyTextAll(tc.Diffs)
assert.Equal(t, tc.ExpectedOld, actualOld, fmt.Sprintf("Test case #%d, %#v", i, tc))
assert.Equal(t, tc.ExpectedNew, actualNew, fmt.Sprintf("Test case #%d, %#v", i, tc))
}
}

func TestDiffPrettyMarkdownAll(t *testing.T) {
type TestCase struct {
Diffs []Diff

ExpectedOld string
ExpectedNew string
}

dmp := New()

for i, tc := range []TestCase{
{
Diffs: []Diff{
{DiffEqual, "a\n"},
{DiffDelete, "<B>b</B>"},
{DiffInsert, "c&d"},
},
ExpectedOld: "a\n<font color=\"#ff0000\"><B>b</B></font>",
ExpectedNew: "a\n<font color=\"#00ff00\">c&d</font>",
},
} {
actualOld, actualNew := dmp.DiffPrettyMarkdownAll(tc.Diffs)
assert.Equal(t, tc.ExpectedOld, actualOld, fmt.Sprintf("Test case #%d, %#v", i, tc))
assert.Equal(t, tc.ExpectedNew, actualNew, fmt.Sprintf("Test case #%d, %#v", i, tc))
}
}

func TestDiffText(t *testing.T) {
type TestCase struct {
Diffs []Diff
Expand Down