1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-08-10 15:53:49 +00:00

Highlight primitive diff additions and deletions

This commit is contained in:
Umar Getagazov 2022-11-15 01:30:04 +03:00 committed by Timur Ismagilov
parent 4e6adec81a
commit 40dbbf5376
4 changed files with 77 additions and 4 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/viewutil" "github.com/bouncepaw/mycorrhiza/viewutil"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"html/template"
"log" "log"
"net/http" "net/http"
"path/filepath" "path/filepath"
@ -125,6 +126,7 @@ var (
{{define "diff for at title"}}Разница для {{beautifulName .HyphaName}} для {{.Hash}}{{end}} {{define "diff for at title"}}Разница для {{beautifulName .HyphaName}} для {{.Hash}}{{end}}
{{define "diff for at heading"}}Разница для <a href="/hypha/{{.HyphaName}}">{{beautifulName .HyphaName}}</a> для {{.Hash}}{{end}} {{define "diff for at heading"}}Разница для <a href="/hypha/{{.HyphaName}}">{{beautifulName .HyphaName}}</a> для {{.Hash}}{{end}}
{{define "no text diff available"}}Нет текстовой разницы.{{end}}
{{define "count pre"}}Отобразить{{end}} {{define "count pre"}}Отобразить{{end}}
{{define "count post"}}свежих правок.{{end}} {{define "count post"}}свежих правок.{{end}}
@ -158,15 +160,49 @@ type primitiveDiffData struct {
*viewutil.BaseData *viewutil.BaseData
HyphaName string HyphaName string
Hash string Hash string
Text string Text template.HTML
} }
func primitiveDiff(meta viewutil.Meta, h hyphae.Hypha, hash, text string) { func primitiveDiff(meta viewutil.Meta, h hyphae.Hypha, hash, text string) {
hunks := history.SplitPrimitiveDiff(text)
if len(hunks) > 0 {
var buf strings.Builder
for _, hunk := range hunks {
lines := strings.Split(hunk, "\n")
buf.WriteString(`<pre class="codeblock">`)
for i, line := range lines {
line = strings.Trim(line, "\r")
var class string
if len(line) > 0 {
switch line[0] {
case '+':
class = "primitive-diff__addition"
case '-':
class = "primitive-diff__deletion"
case '@':
class = "primitive-diff__context"
}
}
if i > 0 {
buf.WriteString("\n")
}
line = template.HTMLEscapeString(line)
fmt.Fprintf(&buf, `<code class="%s">%s</code>`,
class, line)
}
buf.WriteString(`</pre>`)
}
text = buf.String()
} else if text != "" {
text = template.HTMLEscapeString(text)
text = fmt.Sprintf(
`<pre class="codeblock"><code>%s</code></pre>`, text)
}
viewutil.ExecutePage(meta, chainPrimitiveDiff, primitiveDiffData{ viewutil.ExecutePage(meta, chainPrimitiveDiff, primitiveDiffData{
BaseData: &viewutil.BaseData{}, BaseData: &viewutil.BaseData{},
HyphaName: h.CanonicalName(), HyphaName: h.CanonicalName(),
Hash: hash, Hash: hash,
Text: text, Text: template.HTML(text),
}) })
} }

View File

@ -1,11 +1,12 @@
{{define "diff for at title"}}Diff of {{beautifulName .HyphaName}} at {{.Hash}}{{end}} {{define "diff for at title"}}Diff of {{beautifulName .HyphaName}} at {{.Hash}}{{end}}
{{define "diff for at heading"}}Diff of <a href="/hypha/{{.HyphaName}}">{{beautifulName .HyphaName}}</a> at {{.Hash}}{{end}} {{define "diff for at heading"}}Diff of <a href="/hypha/{{.HyphaName}}">{{beautifulName .HyphaName}}</a> at {{.Hash}}{{end}}
{{define "no text diff available"}}No text diff available.{{end}}
{{define "title"}}{{template "diff for at title" .}}{{end}} {{define "title"}}{{template "diff for at title" .}}{{end}}
{{define "body"}} {{define "body"}}
<main class="main-width"> <main class="main-width">
<article> <article>
<h1>{{template "diff for at heading" .}}</h1> <h1>{{template "diff for at heading" .}}</h1>
<pre class="codeblock"><code>{{.Text}}</code></pre> {{if .Text}}{{.Text}}{{else}}{{template "no text diff available" .}}{{end}}
</article> </article>
</main> </main>
{{end}} {{end}}

View File

@ -252,3 +252,21 @@ func PrimitiveDiffAtRevision(filepath, hash string) (string, error) {
} }
return out.String(), err return out.String(), err
} }
// SplitPrimitiveDiff splits a primitive diff of a single file into hunks.
func SplitPrimitiveDiff(text string) (result []string) {
idx := strings.Index(text, "@@ -")
if idx < 0 {
return
}
text = text[idx:]
for {
idx = strings.Index(text, "\n@@ -")
if idx < 0 {
result = append(result, text)
return
}
result = append(result, text[:idx+1])
text = text[idx+1:]
}
}

View File

@ -911,3 +911,21 @@ body[data-rrh-addr^="/interwiki"] main form + form {
.img-gallery img { max-width: 100%; max-height: 50vh; } .img-gallery img { max-width: 100%; max-height: 50vh; }
figure { margin: 0; } figure { margin: 0; }
figcaption { padding-bottom: .5rem; } figcaption { padding-bottom: .5rem; }
/*
* Primitive diff
*/
.primitive-diff__addition {
color: green;
}
.primitive-diff__deletion {
color: red;
}
.primitive-diff__context {
opacity: .5;
}
@media (prefers-color-scheme: dark) {
.primitive-diff__addition {
color: #4cd74c;
}
}