Print diff more pretty
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
55
internal/format/diff.go
Normal file
55
internal/format/diff.go
Normal file
@@ -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("<span class=\"plus\">+</span>")
|
||||
html.WriteString("<span class=\"plus-line\">" + htmlLine + "</span>\n")
|
||||
} else if c == '-' {
|
||||
html.WriteString("<span class=\"minus\">-</span>")
|
||||
html.WriteString("<span class=\"minus-line\">" + htmlLine + "</span>\n")
|
||||
} else {
|
||||
html.WriteString(" ")
|
||||
html.WriteString(htmlLine + "\n")
|
||||
}
|
||||
}
|
||||
index = nextLine
|
||||
}
|
||||
|
||||
return html.String()
|
||||
}
|
||||
@@ -24,7 +24,7 @@
|
||||
<input type="hidden" name="i" value="{{.ID}}" />
|
||||
<input type="submit" value="View" />
|
||||
</form>
|
||||
<pre>{{.Diff}}</pre>
|
||||
<pre>{{.DiffHTML}}</pre>
|
||||
</li>
|
||||
{{else}}
|
||||
<li>No revisions found.</li>
|
||||
|
||||
Reference in New Issue
Block a user