From a11318072ff1a60e74c2ffa5a35496808b4bef95 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sun, 22 Dec 2019 12:57:14 +0100 Subject: [PATCH] Write new entries in the same file #3 --- main.go | 67 +++++++++++++++++++++++++++++++++++++++---- templates/full.html | 11 +++++++ templates/weekly.html | 33 +++++++-------------- 3 files changed, 84 insertions(+), 27 deletions(-) create mode 100644 templates/full.html 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 @@ + + + Weekly summaries + + +
+

Weekly

+ +
+ + diff --git a/templates/weekly.html b/templates/weekly.html index 468f1a2..dd1d14d 100644 --- a/templates/weekly.html +++ b/templates/weekly.html @@ -1,25 +1,14 @@ - - - Weekly summaries - - -
-

Weekly

- + +
{{ range . }} -
-

{{ .Name }}

- {{ if .Content }} - {{ if .Content.HTML }} -
{{ .Content.HTML }}
- {{ else }} -
{{ .Content.Text }}
- {{ end }} +

{{ .Name }}

+ {{ if .Content }} + {{ if .Content.HTML }} +
{{ .Content.HTML }}
+ {{ else }} +
{{ .Content.Text }}
{{ end }} - - -
+ {{ end }} + {{ end }} -
- - +
\ No newline at end of file