apkdoc @ master

 1package main
 2
 3import (
 4	"errors"
 5	html "html/template"
 6	"io"
 7	"os"
 8	"strings"
 9	text "text/template"
10)
11
12type Templater interface {
13	Execute(wr io.Writer, data any) error
14}
15
16var (
17	templateFunc = map[string]any{
18		"DerefI": func(i *int) int { return *i },
19		"DerefS": func(i *string) string { return *i },
20		"Format": func(e *Entry, format string) string {
21			p := e.Properties()
22			p["commit"] = strings.Replace(*e.Commit, "-dirty", "", -1)
23			return tsprintf(format, p)
24		},
25	}
26)
27
28func GetTemplate(templateType, filePath string) (Templater, error) {
29	file, err := os.Open(filePath)
30	if err != nil {
31		return nil, err
32	}
33
34	tmpl, err := io.ReadAll(file)
35	if err != nil {
36		return nil, err
37	}
38
39	switch templateType {
40	case "text":
41		return text.New("text").
42			Funcs(templateFunc).
43			Parse(string(tmpl))
44	case "html":
45		return html.New("html").
46			Funcs(templateFunc).
47			Parse(string(tmpl))
48	default:
49		return nil, errors.New("Invalid template type")
50	}
51}
52
53func tsprintf(format string, params map[string]string) string {
54	for key, val := range params {
55		format = strings.Replace(format, "%{"+key+"}s", val, -1)
56	}
57	return format
58}