From 6a2587f5cebc7ad45b249bcbe6641221542ba5eb Mon Sep 17 00:00:00 2001 From: Aki Kareha Date: Sun, 14 Sep 2025 13:50:57 +0900 Subject: [PATCH] Print diff more pretty --- internal/action/static/style.css | 18 +++++++++++ internal/data/data.go | 12 ++++--- internal/format/diff.go | 55 ++++++++++++++++++++++++++++++++ templates/revisions.html | 2 +- 4 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 internal/format/diff.go diff --git a/internal/action/static/style.css b/internal/action/static/style.css index 5a70c68..2b7d65e 100644 --- a/internal/action/static/style.css +++ b/internal/action/static/style.css @@ -20,3 +20,21 @@ em { color: #25e; font-size: 1.125em; } + +.plus { + color: #8cf; + font-size: 1.125em; +} + +.plus-line { + color: #36f; +} + +.minus { + color: #fc8; + font-size: 1.125em; +} + +.minus-line { + color: #f63; +} diff --git a/internal/data/data.go b/internal/data/data.go index 434c2ae..5240d07 100644 --- a/internal/data/data.go +++ b/internal/data/data.go @@ -3,6 +3,7 @@ package data import ( "context" "fmt" + "html/template" "log" "net/url" "strings" @@ -11,6 +12,7 @@ import ( "github.com/jackc/pgx/v5/pgxpool" "github.com/akikareha/himewiki/internal/config" + "github.com/akikareha/himewiki/internal/format" ) var db *pgxpool.Pool @@ -168,11 +170,12 @@ func LoadAll() ([]Name, error) { } type Revision struct { - ID int - Name string - Diff string + ID int + Name string + Diff string CreatedAt time.Time - Escaped string + Escaped string + DiffHTML template.HTML } func LoadRevisions(name string) ([]Revision, error) { @@ -193,6 +196,7 @@ func LoadRevisions(name string) ([]Revision, error) { return nil, err } r.Escaped = url.PathEscape(r.Name) + r.DiffHTML = template.HTML(format.Diff(r.Diff)) revs = append(revs, r) } return revs, nil diff --git a/internal/format/diff.go b/internal/format/diff.go new file mode 100644 index 0000000..968bd5d --- /dev/null +++ b/internal/format/diff.go @@ -0,0 +1,55 @@ +package format + +import ( + "bytes" + "html/template" +) + +func detectLine(data []byte) (int, int) { + lineFeed := bytes.IndexByte(data, '\n') + if lineFeed == -1 { + lineFeed = len(data) + } + lineEnd := lineFeed + if lineEnd > 0 { + c := data[lineEnd - 1] + if c == '\r' { + lineEnd -= 1 + } + } + nextLine := lineFeed + 1 + return lineEnd, nextLine +} + +func Diff(text string) string { + data := []byte(text) + index := 0 + var html bytes.Buffer + + for index < len(data) { + lineEnd, nextLine := detectLine(data[index:]) + lineEnd += index + nextLine += index + line := data[index:lineEnd] + + if len(line) < 1 { + html.WriteString("\n"); + } else { + c := line[0] + htmlLine := template.HTMLEscapeString(string(line[1:])) + if c == '+' { + html.WriteString("+") + html.WriteString("" + htmlLine + "\n") + } else if c == '-' { + html.WriteString("-") + html.WriteString("" + htmlLine + "\n") + } else { + html.WriteString(" ") + html.WriteString(htmlLine + "\n") + } + } + index = nextLine + } + + return html.String() +} diff --git a/templates/revisions.html b/templates/revisions.html index 31cf7b9..55cbff3 100644 --- a/templates/revisions.html +++ b/templates/revisions.html @@ -24,7 +24,7 @@ -
{{.Diff}}
+
{{.DiffHTML}}
{{else}}
  • No revisions found.