// Package templates embeds the shipped typst templates so the mopac-pdf // binary is self-contained: the engine container only ever sees the // prepared compile root. package templates import ( "embed" "fmt" "os" "path/filepath" "strings" ) //go:embed *.typ var embedded embed.FS // Names lists the shipped template names ("report", "brief"). func Names() []string { entries, err := embedded.ReadDir(".") if err != nil { return nil } var names []string for _, e := range entries { if !e.IsDir() && strings.HasSuffix(e.Name(), ".typ") { names = append(names, strings.TrimSuffix(e.Name(), ".typ")) } } return names } // Source returns the template source for name: from dir when set (custom // template drop-in), else the embedded copy. func Source(dir, name string) (string, error) { if strings.ContainsAny(name, `/\.`) || name == "" { return "", fmt.Errorf("template: invalid name %q", name) } if dir != "" { data, err := os.ReadFile(filepath.Join(dir, name+".typ")) if err == nil { return string(data), nil } if !os.IsNotExist(err) { return "", err } } data, err := embedded.ReadFile(name + ".typ") if err != nil { return "", fmt.Errorf("template %q not found (shipped: %s)", name, strings.Join(Names(), ", ")) } return string(data), nil }