this repo has no description
1package pages
2
3import (
4 "bytes"
5 "embed"
6 "fmt"
7 "html"
8 "html/template"
9 "io"
10 "io/fs"
11 "log"
12 "net/http"
13 "path"
14 "path/filepath"
15 "strings"
16
17 "github.com/alecthomas/chroma/v2"
18 chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
19 "github.com/alecthomas/chroma/v2/lexers"
20 "github.com/alecthomas/chroma/v2/styles"
21 "github.com/dustin/go-humanize"
22 "github.com/sotangled/tangled/appview/auth"
23 "github.com/sotangled/tangled/appview/db"
24 "github.com/sotangled/tangled/types"
25)
26
27//go:embed templates/* static/*
28var files embed.FS
29
30type Pages struct {
31 t map[string]*template.Template
32}
33
34func funcMap() template.FuncMap {
35 return template.FuncMap{
36 "split": func(s string) []string {
37 return strings.Split(s, "\n")
38 },
39 "splitOn": func(s, sep string) []string {
40 return strings.Split(s, sep)
41 },
42 "add": func(a, b int) int {
43 return a + b
44 },
45 "sub": func(a, b int) int {
46 return a - b
47 },
48 "cond": func(cond interface{}, a, b string) string {
49 if cond == nil {
50 return b
51 }
52
53 if boolean, ok := cond.(bool); boolean && ok {
54 return a
55 }
56
57 return b
58 },
59 "didOrHandle": func(did, handle string) string {
60 if handle != "" {
61 return fmt.Sprintf("@%s", handle)
62 } else {
63 return did
64 }
65 },
66 "assoc": func(values ...string) ([][]string, error) {
67 if len(values)%2 != 0 {
68 return nil, fmt.Errorf("invalid assoc call, must have an even number of arguments")
69 }
70 pairs := make([][]string, 0)
71 for i := 0; i < len(values); i += 2 {
72 pairs = append(pairs, []string{values[i], values[i+1]})
73 }
74 return pairs, nil
75 },
76 "append": func(s []string, values ...string) []string {
77 s = append(s, values...)
78 return s
79 },
80 "timeFmt": humanize.Time,
81 "byteFmt": humanize.Bytes,
82 "length": func(v []string) int {
83 return len(v)
84 },
85 "splitN": func(s, sep string, n int) []string {
86 return strings.SplitN(s, sep, n)
87 },
88 "escapeHtml": func(s string) template.HTML {
89 if s == "" {
90 return template.HTML("<br>")
91 }
92 return template.HTML(s)
93 },
94 "unescapeHtml": func(s string) string {
95 return html.UnescapeString(s)
96 },
97 "nl2br": func(text string) template.HTML {
98 return template.HTML(strings.Replace(template.HTMLEscapeString(text), "\n", "<br>", -1))
99 },
100 "unwrapText": func(text string) string {
101 paragraphs := strings.Split(text, "\n\n")
102
103 for i, p := range paragraphs {
104 lines := strings.Split(p, "\n")
105 paragraphs[i] = strings.Join(lines, " ")
106 }
107
108 return strings.Join(paragraphs, "\n\n")
109 },
110 "sequence": func(n int) []struct{} {
111 return make([]struct{}, n)
112 },
113 }
114}
115
116func NewPages() *Pages {
117 templates := make(map[string]*template.Template)
118
119 // Walk through embedded templates directory and parse all .html files
120 err := fs.WalkDir(files, "templates", func(path string, d fs.DirEntry, err error) error {
121 if err != nil {
122 return err
123 }
124
125 if !d.IsDir() && strings.HasSuffix(path, ".html") {
126 name := strings.TrimPrefix(path, "templates/")
127 name = strings.TrimSuffix(name, ".html")
128
129 if !strings.HasPrefix(path, "templates/layouts/") {
130 // Add the page template on top of the base
131 tmpl, err := template.New(name).
132 Funcs(funcMap()).
133 ParseFS(files, "templates/layouts/*.html", path)
134 if err != nil {
135 return fmt.Errorf("setting up template: %w", err)
136 }
137
138 templates[name] = tmpl
139 log.Printf("loaded template: %s", name)
140 }
141
142 return nil
143 }
144 return nil
145 })
146 if err != nil {
147 log.Fatalf("walking template dir: %v", err)
148 }
149
150 log.Printf("total templates loaded: %d", len(templates))
151
152 return &Pages{
153 t: templates,
154 }
155}
156
157type LoginParams struct {
158}
159
160func (p *Pages) execute(name string, w io.Writer, params any) error {
161 return p.t[name].ExecuteTemplate(w, "layouts/base", params)
162}
163
164func (p *Pages) executePlain(name string, w io.Writer, params any) error {
165 return p.t[name].Execute(w, params)
166}
167
168func (p *Pages) executeRepo(name string, w io.Writer, params any) error {
169 return p.t[name].ExecuteTemplate(w, "layouts/repobase", params)
170}
171
172func (p *Pages) Login(w io.Writer, params LoginParams) error {
173 return p.executePlain("user/login", w, params)
174}
175
176type TimelineParams struct {
177 LoggedInUser *auth.User
178 Timeline []db.TimelineEvent
179 DidHandleMap map[string]string
180}
181
182func (p *Pages) Timeline(w io.Writer, params TimelineParams) error {
183 return p.execute("timeline", w, params)
184}
185
186type SettingsParams struct {
187 LoggedInUser *auth.User
188 PubKeys []db.PublicKey
189}
190
191func (p *Pages) Settings(w io.Writer, params SettingsParams) error {
192 return p.execute("settings/keys", w, params)
193}
194
195type KnotsParams struct {
196 LoggedInUser *auth.User
197 Registrations []db.Registration
198}
199
200func (p *Pages) Knots(w io.Writer, params KnotsParams) error {
201 return p.execute("knots", w, params)
202}
203
204type KnotParams struct {
205 LoggedInUser *auth.User
206 Registration *db.Registration
207 Members []string
208 IsOwner bool
209}
210
211func (p *Pages) Knot(w io.Writer, params KnotParams) error {
212 return p.execute("knot", w, params)
213}
214
215type NewRepoParams struct {
216 LoggedInUser *auth.User
217 Knots []string
218}
219
220func (p *Pages) NewRepo(w io.Writer, params NewRepoParams) error {
221 return p.execute("repo/new", w, params)
222}
223
224type ProfilePageParams struct {
225 LoggedInUser *auth.User
226 UserDid string
227 UserHandle string
228 Repos []db.Repo
229 CollaboratingRepos []db.Repo
230 ProfileStats ProfileStats
231 FollowStatus db.FollowStatus
232}
233
234type ProfileStats struct {
235 Followers int
236 Following int
237}
238
239func (p *Pages) ProfilePage(w io.Writer, params ProfilePageParams) error {
240 return p.execute("user/profile", w, params)
241}
242
243type RepoInfo struct {
244 Name string
245 OwnerDid string
246 OwnerHandle string
247 Description string
248 SettingsAllowed bool
249}
250
251func (r RepoInfo) OwnerWithAt() string {
252 if r.OwnerHandle != "" {
253 return fmt.Sprintf("@%s", r.OwnerHandle)
254 } else {
255 return r.OwnerDid
256 }
257}
258
259func (r RepoInfo) FullName() string {
260 return path.Join(r.OwnerWithAt(), r.Name)
261}
262
263func (r RepoInfo) GetTabs() [][]string {
264 tabs := [][]string{
265 {"overview", "/"},
266 {"issues", "/issues"},
267 {"pulls", "/pulls"},
268 }
269
270 if r.SettingsAllowed {
271 tabs = append(tabs, []string{"settings", "/settings"})
272 }
273
274 return tabs
275}
276
277type RepoIndexParams struct {
278 LoggedInUser *auth.User
279 RepoInfo RepoInfo
280 Active string
281 types.RepoIndexResponse
282}
283
284func (p *Pages) RepoIndexPage(w io.Writer, params RepoIndexParams) error {
285 params.Active = "overview"
286 if params.IsEmpty {
287 return p.executeRepo("repo/empty", w, params)
288 }
289 return p.executeRepo("repo/index", w, params)
290}
291
292type RepoLogParams struct {
293 LoggedInUser *auth.User
294 RepoInfo RepoInfo
295 types.RepoLogResponse
296 Active string
297}
298
299func (p *Pages) RepoLog(w io.Writer, params RepoLogParams) error {
300 params.Active = "overview"
301 return p.execute("repo/log", w, params)
302}
303
304type RepoCommitParams struct {
305 LoggedInUser *auth.User
306 RepoInfo RepoInfo
307 Active string
308 types.RepoCommitResponse
309}
310
311func (p *Pages) RepoCommit(w io.Writer, params RepoCommitParams) error {
312 params.Active = "overview"
313 return p.executeRepo("repo/commit", w, params)
314}
315
316type RepoTreeParams struct {
317 LoggedInUser *auth.User
318 RepoInfo RepoInfo
319 Active string
320 BreadCrumbs [][]string
321 BaseTreeLink string
322 BaseBlobLink string
323 types.RepoTreeResponse
324}
325
326type RepoTreeStats struct {
327 NumFolders uint64
328 NumFiles uint64
329}
330
331func (r RepoTreeParams) TreeStats() RepoTreeStats {
332 numFolders, numFiles := 0, 0
333 for _, f := range r.Files {
334 if !f.IsFile {
335 numFolders += 1
336 } else if f.IsFile {
337 numFiles += 1
338 }
339 }
340
341 return RepoTreeStats{
342 NumFolders: uint64(numFolders),
343 NumFiles: uint64(numFiles),
344 }
345}
346
347func (p *Pages) RepoTree(w io.Writer, params RepoTreeParams) error {
348 params.Active = "overview"
349 return p.execute("repo/tree", w, params)
350}
351
352type RepoBranchesParams struct {
353 LoggedInUser *auth.User
354 RepoInfo RepoInfo
355 types.RepoBranchesResponse
356}
357
358func (p *Pages) RepoBranches(w io.Writer, params RepoBranchesParams) error {
359 return p.executeRepo("repo/branches", w, params)
360}
361
362type RepoTagsParams struct {
363 LoggedInUser *auth.User
364 RepoInfo RepoInfo
365 types.RepoTagsResponse
366}
367
368func (p *Pages) RepoTags(w io.Writer, params RepoTagsParams) error {
369 return p.executeRepo("repo/tags", w, params)
370}
371
372type RepoBlobParams struct {
373 LoggedInUser *auth.User
374 RepoInfo RepoInfo
375 Active string
376 BreadCrumbs [][]string
377 types.RepoBlobResponse
378}
379
380func (p *Pages) RepoBlob(w io.Writer, params RepoBlobParams) error {
381 style := styles.Get("bw")
382 b := style.Builder()
383 b.Add(chroma.LiteralString, "noitalic")
384 style, _ = b.Build()
385
386 if params.Lines < 5000 {
387 c := params.Contents
388 formatter := chromahtml.New(
389 chromahtml.InlineCode(true),
390 chromahtml.WithLineNumbers(true),
391 chromahtml.WithLinkableLineNumbers(true, "L"),
392 chromahtml.Standalone(false),
393 )
394
395 lexer := lexers.Get(filepath.Base(params.Path))
396 if lexer == nil {
397 lexer = lexers.Fallback
398 }
399
400 iterator, err := lexer.Tokenise(nil, c)
401 if err != nil {
402 return fmt.Errorf("chroma tokenize: %w", err)
403 }
404
405 var code bytes.Buffer
406 err = formatter.Format(&code, style, iterator)
407 if err != nil {
408 return fmt.Errorf("chroma format: %w", err)
409 }
410
411 params.Contents = code.String()
412 }
413
414 params.Active = "overview"
415 return p.executeRepo("repo/blob", w, params)
416}
417
418type Collaborator struct {
419 Did string
420 Handle string
421 Role string
422}
423
424type RepoSettingsParams struct {
425 LoggedInUser *auth.User
426 RepoInfo RepoInfo
427 Collaborators []Collaborator
428 Active string
429 IsCollaboratorInviteAllowed bool
430}
431
432func (p *Pages) RepoSettings(w io.Writer, params RepoSettingsParams) error {
433 params.Active = "settings"
434 return p.executeRepo("repo/settings", w, params)
435}
436
437type RepoIssuesParams struct {
438 LoggedInUser *auth.User
439 RepoInfo RepoInfo
440 Active string
441 Issues []db.Issue
442 DidHandleMap map[string]string
443}
444
445func (p *Pages) RepoIssues(w io.Writer, params RepoIssuesParams) error {
446 params.Active = "issues"
447 return p.executeRepo("repo/issues/issues", w, params)
448}
449
450type RepoSingleIssueParams struct {
451 LoggedInUser *auth.User
452 RepoInfo RepoInfo
453 Active string
454 Issue db.Issue
455 Comments []db.Comment
456 IssueOwnerHandle string
457 DidHandleMap map[string]string
458
459 State string
460}
461
462func (p *Pages) RepoSingleIssue(w io.Writer, params RepoSingleIssueParams) error {
463 params.Active = "issues"
464 if params.Issue.Open {
465 params.State = "open"
466 } else {
467 params.State = "closed"
468 }
469 return p.execute("repo/issues/issue", w, params)
470}
471
472type RepoNewIssueParams struct {
473 LoggedInUser *auth.User
474 RepoInfo RepoInfo
475 Active string
476}
477
478func (p *Pages) RepoNewIssue(w io.Writer, params RepoNewIssueParams) error {
479 params.Active = "issues"
480 return p.executeRepo("repo/issues/new", w, params)
481}
482
483func (p *Pages) Static() http.Handler {
484 sub, err := fs.Sub(files, "static")
485 if err != nil {
486 log.Fatalf("no static dir found? that's crazy: %v", err)
487 }
488 return http.StripPrefix("/static/", http.FileServer(http.FS(sub)))
489}
490
491func (p *Pages) Error500(w io.Writer) error {
492 return p.execute("errors/500", w, nil)
493}
494
495func (p *Pages) Error404(w io.Writer) error {
496 return p.execute("errors/404", w, nil)
497}
498
499func (p *Pages) Error503(w io.Writer) error {
500 return p.execute("errors/503", w, nil)
501}