diff --git a/main.go b/main.go index 0119bd9..bcf00f5 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,16 @@ package main import ( + "bytes" "fmt" "html/template" + "io" + "io/ioutil" "log" "net/http" "net/url" "os" + "regexp" "time" "github.com/jinzhu/now" @@ -16,12 +20,31 @@ import ( "p83.nl/go/ekster/pkg/jf2" ) +func init() { + log.SetFlags(log.LstdFlags | log.Lshortfile) +} func main() { u, err := url.Parse(os.Args[1]) if err != nil { log.Fatal(err) } + filename := os.Args[2] + re, err := regexp.Compile(``) + if err != nil { + log.Fatal(err) + } + + f, err := openAndCreate(filename) + + data, err := ioutil.ReadAll(f) + if err != nil { + log.Fatal(err) + } + + f.Close() + + var buf bytes.Buffer items, err := getEntriesForFeed(u) if err != nil { log.Fatal(err) @@ -29,19 +52,53 @@ func main() { items = filterEntriesAfter(items, now.With(time.Now()).Monday()) - err = outputEntries(items) + err = outputEntries(items, &buf) + if err != nil { + log.Fatal(err) + } + + output := re.ReplaceAll(data, buf.Bytes()) + err = ioutil.WriteFile(filename, output, 0666) if err != nil { log.Fatal(err) } } -func outputEntries(items []microsub.Item) error { +func openAndCreate(filename string) (io.ReadCloser, error) { + f, err := os.Open(filename) + if err != nil && !os.IsExist(err) { + t, err := template.ParseFiles("templates/full.html") + if err != nil { + return nil, err + } + f, err = os.Create(filename) + if err != nil { + return nil, err + } + err = t.Execute(f, nil) + if err != nil { + return nil, err + } + err = f.Close() + if err != nil { + return nil, err + } + f, err = os.Open(filename) + if err != nil { + return nil, err + } + } else if err != nil { + return nil, err + } + return f, nil +} + +func outputEntries(items []microsub.Item, w io.Writer) error { t, err := template.ParseFiles("templates/weekly.html") if err != nil { - return err + log.Fatal(err) } - - err = t.Execute(os.Stdout, items) + err = t.Execute(w, items) if err != nil { return err } diff --git a/templates/full.html b/templates/full.html new file mode 100644 index 0000000..c9fc30f --- /dev/null +++ b/templates/full.html @@ -0,0 +1,11 @@ + +
+