Files
pages/internal/server/revision_routes.go
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

227 lines
2.9 KiB
Go

package server
import (
"fmt"
"html"
"html/template"
"net/http"
"strings"
)
func (s *Server) handleRevisionView(
w http.ResponseWriter,
r *http.Request,
) {
revisionUUID := strings.TrimPrefix(
r.URL.Path,
"/r/view/",
)
revision, err :=
s.store.GetRevision(revisionUUID)
if err != nil {
http.Error(
w,
err.Error(),
500,
)
return
}
contentView := revisionContentView(revision, "")
contentView.RawURL = rawProjectionURL("rev:" + revision.UUID)
rendered := template.HTML("")
if contentView.Kind == "html" {
rendered = template.HTML(s.safeHTMLProjection(r, revision.Body, "", false))
}
s.renderWithContext(
w,
r,
"revision_view.gohtml",
map[string]any{
"NavItems": resolveNavItems(
s.getSystemNavItems(s.domainUUIDForRequest(r)),
NavContext{},
),
"Title": "Revision View",
"Revision": revision,
"ContentView": contentView,
"RenderedHTML": rendered,
},
)
}
func renderColoredDiff(
before string,
after string,
) string {
beforeLines := strings.Split(
before,
"\n",
)
afterLines := strings.Split(
after,
"\n",
)
max := len(beforeLines)
if len(afterLines) > max {
max = len(afterLines)
}
var out []string
out = append(
out,
`<pre class="diff-view">`,
)
for i := 0; i < max; i++ {
lineNo := i + 1
var b string
var a string
if i < len(beforeLines) {
b = beforeLines[i]
}
if i < len(afterLines) {
a = afterLines[i]
}
switch {
case b == a:
out = append(
out,
fmt.Sprintf(
`<div class="diff-same"><span class="diff-line-no">%4d</span> %s</div>`,
lineNo,
html.EscapeString(a),
),
)
default:
if b != "" {
out = append(
out,
fmt.Sprintf(
`<div class="diff-del"><span class="diff-line-no">%4d</span> - %s</div>`,
lineNo,
html.EscapeString(b),
),
)
}
if a != "" {
out = append(
out,
fmt.Sprintf(
`<div class="diff-add"><span class="diff-line-no">%4d</span> + %s</div>`,
lineNo,
html.EscapeString(a),
),
)
}
}
}
out = append(
out,
"</pre>",
)
return strings.Join(
out,
"\n",
)
}
func (s *Server) handleRevisionDiff(
w http.ResponseWriter,
r *http.Request,
) {
path := strings.TrimPrefix(
r.URL.Path,
"/r/diff/",
)
parts := strings.SplitN(
path,
"/",
2,
)
if len(parts) != 2 {
http.Error(
w,
"bad diff path",
400,
)
return
}
beforeRev, err :=
s.store.GetRevision(parts[0])
if err != nil {
http.Error(
w,
err.Error(),
500,
)
return
}
afterRev, err :=
s.store.GetRevision(parts[1])
if err != nil {
http.Error(
w,
err.Error(),
500,
)
return
}
diffHTML := renderColoredDiff(
beforeRev.Body,
afterRev.Body,
)
s.renderWithContext(
w,
r,
"revision_diff.gohtml",
map[string]any{
"Title": "Revision Diff",
"Before": beforeRev,
"After": afterRev,
"DiffHTML": template.HTML(
diffHTML,
),
},
)
}