diff --git a/generate.go b/generate.go
index 31c9155..5eee79d 100644
--- a/generate.go
+++ b/generate.go
@@ -6,6 +6,73 @@
package main
+import (
+ _ "embed"
+ "errors"
+ "fmt"
+ "html/template"
+ "os"
+ "path/filepath"
+ "strings"
+ "time"
+)
+
+//go:embed news.xml.gotpl
+var newsTemplateStr string
+
func Generate(scan ScanResult) {
- fail("unimplemented")
+ //the XML prelude gets mangled by html.Template, so we print it manually
+ fmt.Println(``)
+
+ t, err := template.New("news.xml").Funcs(funcs).Parse(newsTemplateStr)
+ must(err)
+ must(t.Execute(os.Stdout, scan))
+}
+
+//Functions for the news.xml.gotpl template.
+var funcs = map[string]interface{}{
+ "now": func() string {
+ return time.Now().Local().Format("2006-01-02T15:04:05")
+ },
+
+ "filesize": func(file string) (int64, error) {
+ fi, err := os.Stat(file)
+ if err != nil {
+ return 0, err
+ }
+ return fi.Size(), nil
+ },
+
+ "onlymp3": func(files []string) (string, error) {
+ for _, file := range files {
+ if strings.HasSuffix(file, ".mp3") {
+ return file, nil
+ }
+ }
+ return "", errors.New("no .mp3 audio file supplied")
+ },
+
+ "exceptmp3": func(files []string) (result []string) {
+ for _, file := range files {
+ if !strings.HasSuffix(file, ".mp3") {
+ result = append(result, file)
+ }
+ }
+ return
+ },
+
+ "mimetype": func(file string) (string, error) {
+ switch filepath.Ext(file) {
+ case ".mp3":
+ return "audio/mpeg", nil
+ case ".m4a":
+ return "audio/mp4", nil
+ case ".ogg":
+ return "audio/ogg", nil
+ case ".opus":
+ return "audio/opus", nil
+ default:
+ return "", errors.New("do not know MIME type for " + filepath.Ext(file))
+ }
+ },
}
diff --git a/main.go b/main.go
index 847a329..60b2f16 100644
--- a/main.go
+++ b/main.go
@@ -36,7 +36,7 @@ func fail(msg string, args ...interface{}) {
if len(args) > 0 {
msg = fmt.Sprintf(msg, args...)
}
- fmt.Fprintln(os.Stderr, "ERROR: ", msg)
+ fmt.Fprintln(os.Stderr, "ERROR: "+msg)
os.Exit(1)
}
diff --git a/news.xml.gotpl b/news.xml.gotpl
new file mode 100644
index 0000000..653ac81
--- /dev/null
+++ b/news.xml.gotpl
@@ -0,0 +1,23 @@
+
+ {{ .Description }} Shownotes:
+
+