[PATCH] Highlight primitive diff additions and deletions
Export this patch
---
history/histweb/histview.go | 39 ++++++++++++++++++++++ --
history/histweb/view_primitive_diff.html | 4 + --
static/default.css | 18 +++++++++++
3 files changed, 57 insertions(+), 4 deletions(-)
diff --git a/history/histweb/histview.go b/history/histweb/histview.go
index 4737817..46c7040 100644
--- a/history/histweb/histview.go
+++ b/history/histweb/histview.go
@@ -11,6 +11,7 @@ import (
"github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/viewutil"
"github.com/gorilla/mux"
+ "html/template"
"log"
"net/http"
"path/filepath"
@@ -158,15 +159,49 @@ type primitiveDiffData struct {
*viewutil.BaseData
HyphaName string
Hash string
- Text string
+ Text template.HTML
}
func primitiveDiff(meta viewutil.Meta, h hyphae.Hypha, hash, text string) {
+ var (
+ lines = strings.Split(strings.TrimSpace(text), "\n")[4:]
+ result strings.Builder
+ )
+ for i, line := range lines {
+ line = strings.Trim(line, "\r\n")
+ var className string
+ if len(line) > 0 {
+ switch line[0] {
+ case '+':
+ className = "primitive-diff__addition"
+ case '-':
+ className = "primitive-diff__deletion"
+ case '@':
+ className = "primitive-diff__context"
+ if i > 0 {
+ result.WriteString("</pre>")
+ }
+ result.WriteString(`<pre class="codeblock">`)
+ }
+ }
+ if i > 0 {
+ result.WriteString("\n")
+ }
+ escapedLine := template.HTMLEscapeString(line)
+ if className != "" {
+ fmt.Fprintf(&result, `<code class="%s">%s</code>`,
+ className, escapedLine)
+ } else {
+ fmt.Fprintf(&result, `<code>%s</code>`, escapedLine)
+ }
+ }
+ result.WriteString("</pre>")
+ text = result.String()
viewutil.ExecutePage(meta, chainPrimitiveDiff, primitiveDiffData{
BaseData: &viewutil.BaseData{},
HyphaName: h.CanonicalName(),
Hash: hash,
- Text: text,
+ Text: template.HTML(text),
})
}
diff --git a/history/histweb/view_primitive_diff.html b/history/histweb/view_primitive_diff.html
index c2025a0..b9a834a 100644
--- a/history/histweb/view_primitive_diff.html
+++ b/history/histweb/view_primitive_diff.html
@@ -5,7 +5,7 @@
<main class="main-width">
<article>
<h1>{{template "diff for at heading" .}}</h1>
- <pre class="codeblock"><code>{{.Text}}</code></pre>
+ {{.Text}}
</article>
</main>
- {{end}}
\ No newline at end of file
+ {{end}}
diff --git a/static/default.css b/static/default.css
index e966da0..8d09d53 100644
--- a/static/default.css
+++ b/static/default.css
@@ -911,3 +911,21 @@ body[data-rrh-addr^="/interwiki"] main form + form {
.img-gallery img { max-width: 100%; max-height: 50vh; }
figure { margin: 0; }
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;
+ }
+ }
--
2.37.0 (Apple Git-136)
Thanks! I've found some bugs:
Bugs:
* For media hypha with no text, primitive diff simply fails.
* For media hypha with the most recent revision introducing text, and the one before it having no text, a text that should not appear appears.
* For textual hyphae, revisions introducing text (i/e the first revisions ever) show this thing too.