package pages
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"html"
"html/template"
"log"
"math"
"math/rand"
"net/url"
"path/filepath"
"reflect"
"strings"
"time"
"github.com/alecthomas/chroma/v2"
chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/dustin/go-humanize"
"github.com/dustin/go-humanize/english"
"github.com/go-enry/go-enry/v2"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
"tangled.org/core/appview/cache"
"tangled.org/core/appview/db"
"tangled.org/core/appview/models"
"tangled.org/core/appview/oauth"
"tangled.org/core/appview/pages/markup"
"tangled.org/core/appview/pages/markup/sanitizer"
"tangled.org/core/appview/pages/repoinfo"
"tangled.org/core/crypto"
"tangled.org/core/idresolver"
"tangled.org/core/orm"
"tangled.org/core/types"
)
type tab map[string]string
func (p *Pages) ownerSlashRepo(repo *models.Repo) string {
ownerId, err := p.resolver.ResolveIdent(context.Background(), repo.Did)
if err != nil {
return repo.RepoIdentifier()
}
handle := ownerId.Handle
if handle != "" && !handle.IsInvalidHandle() {
return string(handle) + "/" + repo.Slug()
}
return repo.RepoIdentifier()
}
func (p *Pages) funcMap() template.FuncMap {
return template.FuncMap{
"split": func(s string) []string {
return strings.Split(s, "\n")
},
"capitalize": func(s string) string {
if s == "" {
return s
}
return strings.ToUpper(s[:1]) + s[1:]
},
"trimPrefix": func(s, prefix string) string {
return strings.TrimPrefix(s, prefix)
},
"join": func(elems []string, sep string) string {
return strings.Join(elems, sep)
},
"contains": func(s string, target string) bool {
return strings.Contains(s, target)
},
"stripPort": func(hostname string) string {
if strings.Contains(hostname, ":") {
return strings.Split(hostname, ":")[0]
}
return hostname
},
"mapContains": func(m any, key any) bool {
mapValue := reflect.ValueOf(m)
if mapValue.Kind() != reflect.Map {
return false
}
keyValue := reflect.ValueOf(key)
return mapValue.MapIndex(keyValue).IsValid()
},
"resolve": func(s string) string {
return p.DisplayHandle(context.Background(), s)
},
"resolver": func() *idresolver.Resolver {
return p.resolver
},
"primaryHandle": func(s string) string {
return primaryHandle(p.resolver, s)
},
"resolvePds": func(s string) string {
identity, err := p.resolver.ResolveIdent(context.Background(), s)
if err != nil {
return ""
}
return identity.PDSEndpoint()
},
"ownerSlashRepo": p.ownerSlashRepo,
"pipelineCommitPath": func(repoInfo repoinfo.RepoInfo, pipeline types.Pipeline) string {
sha := pipeline.Sha()
if sourceRepo := pipeline.SourceRepo(); sourceRepo != nil {
if repo, err := db.GetRepoByDid(p.db, *sourceRepo); err == nil && repo != nil {
return "/" + p.ownerSlashRepo(repo) + "/commit/" + sha
}
}
return "/" + repoInfo.FullName() + "/commit/" + sha
},
"pipelineSourceLabel": func(pipeline types.Pipeline) string {
branch := pipeline.Trigger().PRSourceBranch()
if branch == "" {
return branch
}
sourceRepo := pipeline.SourceRepo()
if sourceRepo == nil {
return branch
}
repo, err := db.GetRepoByDid(p.db, *sourceRepo)
if err != nil || repo == nil {
return branch
}
return p.ownerSlashRepo(repo) + "/" + branch
},
"pipelinePullPath": func(pipeline types.Pipeline) string {
pullAtStr := pipeline.Trigger().PRUri()
if pullAtStr == "" {
return ""
}
// GetPull's reverse-mapping already populates pull.Repo
pull, err := db.GetPull(p.db, orm.FilterEq("at_uri", pullAtStr))
if err != nil || pull == nil || pull.Repo == nil {
return ""
}
return fmt.Sprintf("/%s/pulls/%d", p.ownerSlashRepo(pull.Repo), pull.PullId)
},
"truncateAt30": func(s string) string {
if len(s) <= 30 {
return s
}
return s[:30] + "…"
},
// short prefix of a commit hash or jj change id, safe on short input
"shortId": func(s string) string {
if len(s) <= 8 {
return s
}
return s[:8]
},
"splitOn": func(s, sep string) []string {
return strings.Split(s, sep)
},
"string": func(v any) string {
return fmt.Sprint(v)
},
"int64": func(a int) int64 {
return int64(a)
},
"add": func(a, b int) int {
return a + b
},
"now": func() time.Time {
return time.Now()
},
// the absolute state of go templates
"add64": func(a, b int64) int64 {
return a + b
},
"sub": func(a, b int) int {
return a - b
},
"mul": func(a, b int) int {
return a * b
},
"div": func(a, b int) int {
return a / b
},
"mod": func(a, b int) int {
return a % b
},
"randInt": func(bound int) int {
return rand.Intn(bound)
},
"f64": func(a int) float64 {
return float64(a)
},
"addf64": func(a, b float64) float64 {
return a + b
},
"subf64": func(a, b float64) float64 {
return a - b
},
"mulf64": func(a, b float64) float64 {
return a * b
},
"divf64": func(a, b float64) float64 {
if b == 0 {
return 0
}
return a / b
},
"negf64": func(a float64) float64 {
return -a
},
"cond": func(cond any, a, b string) string {
if cond == nil {
return b
}
if boolean, ok := cond.(bool); boolean && ok {
return a
}
return b
},
"assoc": func(values ...string) ([][]string, error) {
if len(values)%2 != 0 {
return nil, fmt.Errorf("invalid assoc call, must have an even number of arguments")
}
pairs := make([][]string, 0)
for i := 0; i < len(values); i += 2 {
pairs = append(pairs, []string{values[i], values[i+1]})
}
return pairs, nil
},
"append": func(s []any, values ...any) []any {
s = append(s, values...)
return s
},
// scale numerics over 1000 to 1k
"scaleFmt": func(n any) string {
var v float64
switch x := n.(type) {
case int:
v = float64(x)
case int32:
v = float64(x)
case int64:
v = float64(x)
case float64:
v = x
default:
return fmt.Sprintf("%v", n)
}
if v < 1000 {
return fmt.Sprintf("%d", int(v))
}
k := v / 1000
if k < 10 {
return fmt.Sprintf("%.1fk", k)
}
return fmt.Sprintf("%dk", int(k))
},
"commaFmt": humanize.Comma,
"plural": english.Plural,
"relTimeFmt": humanize.Time,
"shortRelTimeFmt": func(t time.Time) string {
if t.Unix() == 0 {
return "at the beginning of time"
}
return humanize.CustomRelTime(t, time.Now(), "", "", []humanize.RelTimeMagnitude{
{D: time.Second, Format: "now", DivBy: time.Second},
{D: 2 * time.Second, Format: "1s %s", DivBy: 1},
{D: time.Minute, Format: "%ds %s", DivBy: time.Second},
{D: 2 * time.Minute, Format: "1min %s", DivBy: 1},
{D: time.Hour, Format: "%dmin %s", DivBy: time.Minute},
{D: 2 * time.Hour, Format: "1hr %s", DivBy: 1},
{D: humanize.Day, Format: "%dhrs %s", DivBy: time.Hour},
{D: 2 * humanize.Day, Format: "1d %s", DivBy: 1},
{D: 20 * humanize.Day, Format: "%dd %s", DivBy: humanize.Day},
{D: 8 * humanize.Week, Format: "%dw %s", DivBy: humanize.Week},
{D: humanize.Year, Format: "%dmo %s", DivBy: humanize.Month},
{D: 18 * humanize.Month, Format: "1y %s", DivBy: 1},
{D: 2 * humanize.Year, Format: "2y %s", DivBy: 1},
{D: humanize.LongTime, Format: "%dy %s", DivBy: humanize.Year},
{D: math.MaxInt64, Format: "a long while %s", DivBy: 1},
})
},
"shortTimeFmt": func(t time.Time) string {
return t.Format("Jan 2, 2006")
},
"longTimeFmt": func(t time.Time) string {
return t.Format("Jan 2, 2006, 3:04 PM MST")
},
"iso8601DateTimeFmt": func(t time.Time) string {
return t.Format("2006-01-02T15:04:05-07:00")
},
"iso8601DurationFmt": func(duration time.Duration) string {
days := int64(duration.Hours() / 24)
hours := int64(math.Mod(duration.Hours(), 24))
minutes := int64(math.Mod(duration.Minutes(), 60))
seconds := int64(math.Mod(duration.Seconds(), 60))
return fmt.Sprintf("P%dD%dH%dM%dS", days, hours, minutes, seconds)
},
"durationFmt": func(duration time.Duration) string {
return durationFmt(duration, [4]string{"d", "h", "m", "s"})
},
"longDurationFmt": func(duration time.Duration) string {
return durationFmt(duration, [4]string{"days", "hours", "minutes", "seconds"})
},
"byteFmt": humanize.Bytes,
"length": func(slice any) int {
v := reflect.ValueOf(slice)
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
return v.Len()
}
return 0
},
"splitN": func(s, sep string, n int) []string {
return strings.SplitN(s, sep, n)
},
"escapeHtml": func(s string) template.HTML {
if s == "" {
return template.HTML("
")
}
return template.HTML(s)
},
"unescapeHtml": func(s string) string {
return html.UnescapeString(s)
},
"nl2br": func(text string) template.HTML {
return template.HTML(strings.ReplaceAll(template.HTMLEscapeString(text), "\n", "
"))
},
"unwrapText": func(text string) string {
paragraphs := strings.Split(text, "\n\n")
for i, p := range paragraphs {
lines := strings.Split(p, "\n")
paragraphs[i] = strings.Join(lines, " ")
}
return strings.Join(paragraphs, "\n\n")
},
"sequence": func(n int) []struct{} {
return make([]struct{}, n)
},
// take atmost N items from this slice
"take": func(slice any, n int) any {
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
return nil
}
if v.Len() == 0 {
return nil
}
return v.Slice(0, min(n, v.Len())).Interface()
},
"markdown": func(text string) template.HTML {
rctx := p.rctx.Clone()
rctx.RendererType = markup.RendererTypeDefault
htmlString := rctx.RenderMarkdown(text)
sanitized := sanitizer.SanitizeDefault(htmlString)
return template.HTML(sanitized)
},
"description": func(text string) template.HTML {
rctx := p.rctx.Clone()
rctx.RendererType = markup.RendererTypeDefault
htmlString := rctx.RenderMarkdownWith(text, goldmark.New(
goldmark.WithExtensions(
emoji.Emoji,
),
))
sanitized := sanitizer.SanitizeDescription(htmlString)
return template.HTML(sanitized)
},
"readme": func(text string) template.HTML {
rctx := p.rctx.Clone()
rctx.RendererType = markup.RendererTypeRepoMarkdown
htmlString := rctx.RenderMarkdown(text)
sanitized := sanitizer.SanitizeDefault(htmlString)
return template.HTML(sanitized)
},
"code": func(content, path string) string {
var style *chroma.Style = styles.Get("catpuccin-latte")
formatter := chromahtml.New(
chromahtml.InlineCode(false),
chromahtml.WithLineNumbers(true),
chromahtml.WithLinkableLineNumbers(true, "L"),
chromahtml.Standalone(false),
chromahtml.WithClasses(true),
)
lexer := lexers.Get(filepath.Base(path))
if lexer == nil {
if firstLine, _, ok := strings.Cut(content, "\n"); ok && strings.HasPrefix(firstLine, "#!") {
// extract interpreter from shebang (handles "#!/usr/bin/env nu", "#!/usr/bin/nu", etc.)
fields := strings.Fields(firstLine[2:])
if len(fields) > 0 {
interp := filepath.Base(fields[len(fields)-1])
lexer = lexers.Get(interp)
}
}
}
if lexer == nil {
lexer = lexers.Analyse(content)
}
if lexer == nil {
lexer = lexers.Fallback
}
iterator, err := lexer.Tokenise(nil, content)
if err != nil {
p.logger.Error("chroma tokenize", "err", "err")
return ""
}
var code bytes.Buffer
err = formatter.Format(&code, style, iterator)
if err != nil {
p.logger.Error("chroma format", "err", "err")
return ""
}
return code.String()
},
"trimUriScheme": func(text string) string {
text = strings.TrimPrefix(text, "https://")
text = strings.TrimPrefix(text, "http://")
return text
},
"isNil": func(t any) bool {
// returns false for other "zero" values
return t == nil
},
"hasPrefix": strings.HasPrefix,
"list": func(args ...any) []any {
return args
},
"dict": func(values ...any) (map[string]any, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]any, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
},
"queryParams": func(params ...any) (url.Values, error) {
if len(params)%2 != 0 {
return nil, errors.New("invalid queryParams call")
}
vals := make(url.Values, len(params)/2)
for i := 0; i < len(params); i += 2 {
key, ok := params[i].(string)
if !ok {
return nil, errors.New("queryParams keys must be strings")
}
v, ok := params[i+1].(string)
if !ok {
return nil, errors.New("queryParams values must be strings")
}
vals.Add(key, v)
}
return vals, nil
},
"deref": func(v any) any {
val := reflect.ValueOf(v)
if val.Kind() == reflect.Pointer && !val.IsNil() {
return val.Elem().Interface()
}
return nil
},
"i": func(name string, classes ...string) template.HTML {
data, err := p.icon(name, classes)
if err != nil {
log.Printf("icon %s does not exist", name)
data, _ = p.icon("airplay", classes)
}
return template.HTML(data)
},
"cssContentHash": p.CssContentHash,
"pathEscape": func(s string) string {
return url.PathEscape(s)
},
"pathUnescape": func(s string) string {
u, _ := url.PathUnescape(s)
return u
},
"safeUrl": func(s string) template.URL {
return template.URL(s)
},
"tinyAvatar": func(handle string) string {
return p.AvatarUrl(handle, "tiny")
},
"fullAvatar": func(handle string) string {
return p.AvatarUrl(handle, "")
},
"placeholderAvatar": func(size string) template.HTML {
sizeClass := "size-6"
iconSize := "size-4"
switch size {
case "tiny":
sizeClass = "size-6"
iconSize = "size-4"
case "small":
sizeClass = "size-8"
iconSize = "size-5"
default:
sizeClass = "size-12"
iconSize = "size-8"
}
icon, _ := p.icon("user-round", []string{iconSize, "text-gray-400", "dark:text-gray-500"})
return template.HTML(fmt.Sprintf(`