summaryrefslogtreecommitdiff
path: root/vrsite/page.go
blob: 2974eec61cfcef2d1230bdd4c6eb5b142dd34c7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// page.go — what a template gets, and how links are formed.
//
// The same templates render two ways: as a static export (relative links,
// ".html" suffixes, no server-side features) and as the live web app
// (absolute routes, htmx-driven filters, comments and jj queries). Pages carry
// Live so a template can ask for the difference in one place.
package main

import (
	"html/template"
	"net/url"
	"strconv"
	"strings"
)

type page struct {
	Site     *Site
	Title    string
	Kind     string // index | file | files | notes | jj | change | session
	Root     string // static: relative prefix; live: "/"
	Live     bool
	Rev      string // revision being browsed ("" = the change the log points at)
	ChangeID string // resolved change id for Rev — what pages name and link
	Commit   string // the commit under it — what content is read and cached at
	Default  string // the change the log points at
	Path     string // this request's path, so the change bar can re-aim it
	Current  string // current file path, for the tree
	User     string // who web comments are attributed to

	// what this view is scoped to. Scope is "change" (only what was recorded
	// at ChangeID) or "all"; ScopeEmpty means "change" was asked for but
	// nothing was ever recorded there, so the whole log is showing instead.
	Scope      string
	ScopeEmpty bool
	Here       int // notes recorded at this change
	Elsewhere  int // notes recorded at any other change
	ChangeInfo *Row

	Tree  template.HTML
	File  *File
	Files []*File // every file at this change, for counts

	Notes    []*Note
	Filter   NoteFilter
	Facets   *Facets
	Sessions []*Session
	Compact  bool
	Window   string

	JJ     *JJView
	Change *Change
	Conv   *Conv
	Err    string
}

// JJView is one rendered jj query: what was asked, and what came back.
type JJView struct {
	Kind   string // log | op
	Revset string
	AtOp   string
	Limit  int
	Rows   []Row
	Err    string
}

// params carries the two things every link must not lose: which change you
// are looking at, and how wide the view is.
func (p *page) params() string { return p.paramsWith(p.Scope) }

func (p *page) paramsWith(scope string) string {
	var q []string
	// "@" is a revset like any other now that the default change is the log's,
	// so dropping it here would slide the reader off the change they picked
	if p.Rev != "" {
		q = append(q, "rev="+url.QueryEscape(p.Rev))
	}
	if scope == "all" {
		q = append(q, "scope=all")
	}
	if len(q) == 0 {
		return ""
	}
	return "?" + strings.Join(q, "&")
}

// ScopeHref is this same page seen with the other scope.
func (p *page) ScopeHref(scope string) string {
	path := p.Path
	if path == "" {
		path = "/"
	}
	return path + p.paramsWith(scope)
}

// BackHref is this page again at the change the log points at.
func (p *page) BackHref() string {
	q := ""
	if p.Scope == "all" {
		q = "?scope=all"
	}
	return p.JumpPath() + q
}

// JumpPath is where the change picker submits: your own page when it is one
// that can be read at any change, the file tree otherwise.
func (p *page) JumpPath() string {
	switch p.Kind {
	case "index", "file", "files", "notes":
		if p.Path != "" {
			return p.Path
		}
	}
	return "/files"
}

// Href maps a logical destination to a URL for this page's mode. Kinds the
// static export has no page for return "", and templates guard on .Live.
func (p *page) Href(kind string, arg ...string) string {
	a := ""
	if len(arg) > 0 {
		a = arg[0]
	}
	if p.Live {
		switch kind {
		case "":
			return "/" + p.params()
		case "notes":
			return "/notes" + p.params()
		case "files":
			return "/files" + p.params()
		case "tree":
			return "/tree" + p.params()
		case "jj":
			return "/jj"
		case "code":
			return "/code/" + escPath(a) + p.params()
		case "change":
			return "/change/" + a
		case "session":
			return "/session/" + escPath(a)
		case "asset":
			return "/" + a
		}
		return "/"
	}
	switch kind {
	case "":
		return p.Root + "index.html"
	case "notes":
		return p.Root + "notes.html"
	case "files":
		return p.Root + "files.html"
	case "tree":
		return p.Root + "tree.html"
	case "code":
		return p.Root + "code/" + a + ".html"
	case "asset":
		return p.Root + a
	}
	return ""
}

// codeLink is the tree's per-file linker for this page's mode.
func (p *page) codeLink() func(string) string {
	return func(path string) string { return p.Href("code", path) }
}

func escPath(p string) string {
	parts := strings.Split(p, "/")
	for i, s := range parts {
		parts[i] = url.PathEscape(s)
	}
	return strings.Join(parts, "/")
}

// ConvHref opens the conversation a note was written in, anchored on it. It
// needs the transcripts on this machine, so the static export has none — and
// the conversation itself does not link back to where you already are.
func (p *page) ConvHref(n *Note) string {
	if !p.Live || p.Kind == "session" || n.Session == "" || safeSession(n.Session) != nil {
		return ""
	}
	id := strconv.Itoa(n.ID)
	return p.Href("session", n.Session) + "?note=" + id + "#note-" + id
}

// NoteHref points at the noted line, or the note list when it is unpinned.
// The ?note= is what turns a jump into an arrival: the page opens in notes
// focus with the card flashed and its lines marked (see app.js).
func (p *page) NoteHref(n *Note) string {
	if n.File == "" {
		return p.Href("notes")
	}
	id := strconv.Itoa(n.ID)
	h := p.Href("code", n.File)
	sep := "?"
	if strings.Contains(h, "?") {
		sep = "&"
	}
	h += sep + "note=" + id
	if n.Start > 0 {
		h += "#L" + strconv.Itoa(n.Start)
	}
	return h
}

func newTemplates() *template.Template {
	funcs := template.FuncMap{
		"short": short,
		"band":  func(a, b uint8) bool { return b != 0 && a&b != 0 },
		"lines": func(n *Note) string {
			if n.Start <= 0 {
				return "whole file"
			}
			if n.End > n.Start {
				return ":" + strconv.Itoa(n.Start) + "-" + strconv.Itoa(n.End)
			}
			return ":" + strconv.Itoa(n.Start)
		},
		"base": func(p string) string {
			if i := strings.LastIndex(p, "/"); i >= 0 {
				return p[i+1:]
			}
			return p
		},
		"dict": func(kv ...any) map[string]any {
			m := map[string]any{}
			for i := 0; i+1 < len(kv); i += 2 {
				m[kv[i].(string)] = kv[i+1]
			}
			return m
		},
	}
	tpl, err := template.New("layout.tmpl").Funcs(funcs).ParseFS(tfs, "templates/*.tmpl")
	if err != nil {
		fatal("%v", err)
	}
	return tpl
}

func short(s string) string {
	if len(s) > 8 {
		return s[:8]
	}
	return s
}