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 @@ + + + ../pentaradio.png +

{{ .Description }}

+

Shownotes:

+ + + {{- range (exceptmp3 .AudioFileNames) }} + + {{- end }} + + {{- range .ChapterMarks }} + + {{- end }} + + +
diff --git a/scan.go b/scan.go index 7db4ea4..423a807 100644 --- a/scan.go +++ b/scan.go @@ -24,6 +24,7 @@ type ScanResult struct { AudioFileNames []string //parts extracted from the shownotes Title string + AuthorNames string Description string //parts extracted from the chaptermarks ChapterMarks []ChapterMark @@ -98,6 +99,7 @@ func (scan *ScanResult) ParseShownotes() { shownotesRx := regexp.MustCompile( fmt.Sprintf(`^# %s\n`, expectedTitle) + //header line `# Titel: "(.*)"\n\n` + //episode title + `Mit (.*)\.\n\n` + //author names `((?s:.*?))\n\n##\s`, //short description (everything up until the first

) ) match := shownotesRx.FindStringSubmatch(string(buf)) @@ -106,7 +108,8 @@ func (scan *ScanResult) ParseShownotes() { } scan.Title = match[1] - scan.Description = match[2] + scan.AuthorNames = match[2] + scan.Description = match[3] } func (scan ScanResult) ChapterMarksFile() string {