implement generate command

This commit is contained in:
Stefan Majewsky 2022-04-21 22:55:16 +02:00
parent 8cd5292b93
commit e3953dbc75
4 changed files with 96 additions and 3 deletions

View File

@ -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(`<?xml version="1.0" encoding="UTF-8"?>`)
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))
}
},
}

View File

@ -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)
}

23
news.xml.gotpl Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE item SYSTEM "https://www.c3d2.de/dtd/c3d2web.dtd">
<item title="pentaradio24: {{ .Title }}" date="{{ now }}" author="{{ .AuthorNames }}">
<image title="(((pentaradio))">../pentaradio.png</image>
<p>{{ .Description }}</p>
<p>Shownotes:</p>
<ul>
<li><link href="https://ftp.c3d2.de/pentaradio/shownotes/shownotes-pentaradio-{{ printf "%04d-%02d-%02d" .Year .Month .Day }}.txt">Shownotes als Textdatei</link></li>
<li><link href="https://codimd.c3d2.de/pentaradio-{{ printf "%04d-%02d" .Year .Month }}">Shownotes als Webseite</link></li>
</ul>
<resource size="{{ filesize (onlymp3 .AudioFileNames) }}"
type="{{ mimetype (onlymp3 .AudioFileNames) }}"
url="https://ftp.c3d2.de/pentaradio/{{ onlymp3 .AudioFileNames }}"
title="pentaradio24 vom 22. März 2022">
{{- range (exceptmp3 .AudioFileNames) }}
<alternative size="{{ filesize . }}" type="{{ mimetype . }}" url="https://ftp.c3d2.de/pentaradio/{{ . }}"/>
{{- end }}
<chapters xmlns="https://podlove.de/simple-chapters">
{{- range .ChapterMarks }}
<chapter start="{{ .StartTime }}" title="{{ .Title }}" {{- if .URL }} href="{{ .URL }}" {{- end }} />
{{- end }}
</chapters>
</resource>
</item>

View File

@ -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 <h2>)
)
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 {