forked from tangled.org/core
Monorepo for Tangled — https://tangled.org
1package pages 2 3import ( 4 "errors" 5 "fmt" 6 "html" 7 "html/template" 8 "log" 9 "math" 10 "path/filepath" 11 "reflect" 12 "strings" 13 "time" 14 15 "github.com/dustin/go-humanize" 16) 17 18func funcMap() template.FuncMap { 19 return template.FuncMap{ 20 "split": func(s string) []string { 21 return strings.Split(s, "\n") 22 }, 23 "truncateAt30": func(s string) string { 24 if len(s) <= 30 { 25 return s 26 } 27 return s[:30] + "…" 28 }, 29 "splitOn": func(s, sep string) []string { 30 return strings.Split(s, sep) 31 }, 32 "add": func(a, b int) int { 33 return a + b 34 }, 35 "sub": func(a, b int) int { 36 return a - b 37 }, 38 "cond": func(cond interface{}, a, b string) string { 39 if cond == nil { 40 return b 41 } 42 43 if boolean, ok := cond.(bool); boolean && ok { 44 return a 45 } 46 47 return b 48 }, 49 "didOrHandle": func(did, handle string) string { 50 if handle != "" { 51 return fmt.Sprintf("@%s", handle) 52 } else { 53 return did 54 } 55 }, 56 "assoc": func(values ...string) ([][]string, error) { 57 if len(values)%2 != 0 { 58 return nil, fmt.Errorf("invalid assoc call, must have an even number of arguments") 59 } 60 pairs := make([][]string, 0) 61 for i := 0; i < len(values); i += 2 { 62 pairs = append(pairs, []string{values[i], values[i+1]}) 63 } 64 return pairs, nil 65 }, 66 "append": func(s []string, values ...string) []string { 67 s = append(s, values...) 68 return s 69 }, 70 "timeFmt": humanize.Time, 71 "shortTimeFmt": func(t time.Time) string { 72 return humanize.CustomRelTime(t, time.Now(), "", "", []humanize.RelTimeMagnitude{ 73 {time.Second, "now", time.Second}, 74 {2 * time.Second, "1s %s", 1}, 75 {time.Minute, "%ds %s", time.Second}, 76 {2 * time.Minute, "1min %s", 1}, 77 {time.Hour, "%dmin %s", time.Minute}, 78 {2 * time.Hour, "1hr %s", 1}, 79 {humanize.Day, "%dhrs %s", time.Hour}, 80 {2 * humanize.Day, "1d %s", 1}, 81 {20 * humanize.Day, "%dd %s", humanize.Day}, 82 {8 * humanize.Week, "%dw %s", humanize.Week}, 83 {humanize.Year, "%dmo %s", humanize.Month}, 84 {18 * humanize.Month, "1y %s", 1}, 85 {2 * humanize.Year, "2y %s", 1}, 86 {humanize.LongTime, "%dy %s", humanize.Year}, 87 {math.MaxInt64, "a long while %s", 1}, 88 }) 89 }, 90 "byteFmt": humanize.Bytes, 91 "length": func(slice any) int { 92 v := reflect.ValueOf(slice) 93 if v.Kind() == reflect.Slice || v.Kind() == reflect.Array { 94 return v.Len() 95 } 96 return 0 97 }, 98 "splitN": func(s, sep string, n int) []string { 99 return strings.SplitN(s, sep, n) 100 }, 101 "escapeHtml": func(s string) template.HTML { 102 if s == "" { 103 return template.HTML("<br>") 104 } 105 return template.HTML(s) 106 }, 107 "unescapeHtml": func(s string) string { 108 return html.UnescapeString(s) 109 }, 110 "nl2br": func(text string) template.HTML { 111 return template.HTML(strings.Replace(template.HTMLEscapeString(text), "\n", "<br>", -1)) 112 }, 113 "unwrapText": func(text string) string { 114 paragraphs := strings.Split(text, "\n\n") 115 116 for i, p := range paragraphs { 117 lines := strings.Split(p, "\n") 118 paragraphs[i] = strings.Join(lines, " ") 119 } 120 121 return strings.Join(paragraphs, "\n\n") 122 }, 123 "sequence": func(n int) []struct{} { 124 return make([]struct{}, n) 125 }, 126 "subslice": func(slice any, start, end int) any { 127 v := reflect.ValueOf(slice) 128 if v.Kind() != reflect.Slice && v.Kind() != reflect.Array { 129 return nil 130 } 131 if start < 0 || start > v.Len() || end > v.Len() || start > end { 132 return nil 133 } 134 return v.Slice(start, end).Interface() 135 }, 136 "markdown": func(text string) template.HTML { 137 return template.HTML(renderMarkdown(text)) 138 }, 139 "isNil": func(t any) bool { 140 // returns false for other "zero" values 141 return t == nil 142 }, 143 "list": func(args ...any) []any { 144 return args 145 }, 146 "dict": func(values ...any) (map[string]any, error) { 147 if len(values)%2 != 0 { 148 return nil, errors.New("invalid dict call") 149 } 150 dict := make(map[string]any, len(values)/2) 151 for i := 0; i < len(values); i += 2 { 152 key, ok := values[i].(string) 153 if !ok { 154 return nil, errors.New("dict keys must be strings") 155 } 156 dict[key] = values[i+1] 157 } 158 return dict, nil 159 }, 160 "i": func(name string, classes ...string) template.HTML { 161 data, err := icon(name, classes) 162 if err != nil { 163 log.Printf("icon %s does not exist", name) 164 data, _ = icon("airplay", classes) 165 } 166 return template.HTML(data) 167 }, 168 } 169} 170 171func icon(name string, classes []string) (template.HTML, error) { 172 iconPath := filepath.Join("static", "icons", name) 173 174 if filepath.Ext(name) == "" { 175 iconPath += ".svg" 176 } 177 178 data, err := Files.ReadFile(iconPath) 179 if err != nil { 180 return "", fmt.Errorf("icon %s not found: %w", name, err) 181 } 182 183 // Convert SVG data to string 184 svgStr := string(data) 185 186 svgTagEnd := strings.Index(svgStr, ">") 187 if svgTagEnd == -1 { 188 return "", fmt.Errorf("invalid SVG format for icon %s", name) 189 } 190 191 classTag := ` class="` + strings.Join(classes, " ") + `"` 192 193 modifiedSVG := svgStr[:svgTagEnd] + classTag + svgStr[svgTagEnd:] 194 return template.HTML(modifiedSVG), nil 195}