Improve template

This commit is contained in:
Andrea Fazzi 2022-04-21 11:29:16 +02:00
parent 190da856fc
commit cb8a546156
2 changed files with 47 additions and 15 deletions

View file

@ -1,6 +1,17 @@
# Buongiorno Aldo!
Oggi è il {{day}}.{{month}}.{{year}}.
Oggi è il {{date}}. Il tempo a Trieste è **soleggiato**.
# Situazione meteo
{{range $item := weather}}
* {{$item.Title}}
{{end}}
# Ultime notizie
{{range $item := news}}
* {{$item.Title}}
{{end}}
# Prossimo bus per il centro
@ -13,7 +24,3 @@ Oggi è il {{date}}. Il tempo a Trieste è **soleggiato**.
2. Fare la spesa
3. Ritirare la pensione
# Ultime notizie
* {{.Item.Title}}

View file

@ -15,10 +15,41 @@ import (
)
var funcmap = template.FuncMap{
"date": func() string {
now := time.Now()
"day": func() int {
_, _, day := time.Now().Date()
return day
},
"month": func() int {
_, month, _ := time.Now().Date()
return int(month)
},
"year": func() int {
year, _, _ := time.Now().Date()
return year
},
"news": func() []*gofeed.Item {
result := make([]*gofeed.Item, 0)
return now.Format("2006-02-01")
fp := gofeed.NewParser()
feed, _ := fp.ParseURL("https://www.agi.it/innovazione/rss")
for _, it := range feed.Items {
result = append(result, it)
}
return result[:2]
},
"weather": func() []*gofeed.Item {
result := make([]*gofeed.Item, 0)
fp := gofeed.NewParser()
feed, _ := fp.ParseURL("https://www.meteowebcam.eu/rss.xml")
for _, it := range feed.Items {
result = append(result, it)
}
return result[:1]
},
}
@ -34,9 +65,6 @@ func convertPDFToPNG(filename string) error {
}
func serveScreen(res http.ResponseWriter, req *http.Request) {
fp := gofeed.NewParser()
feed, _ := fp.ParseURL("https://www.agi.it/innovazione/rss")
tpl, err := tplutil.LoadTextTemplate("./data/screen.tpl.md", funcmap)
if err != nil {
panic(err)
@ -53,16 +81,13 @@ func serveScreen(res http.ResponseWriter, req *http.Request) {
Item *gofeed.Item
}
data.Title = feed.Title
data.Item = feed.Items[0]
log.Println("Generating markdown file from template.")
if err := tpl.Execute(f, data); err != nil {
panic(err)
}
log.Println("Converting markdown to pdf.")
err = pandoc.Convert("./data/screen.md", "./data/screen.pdf", "-V", "geometry:papersize={3.8in,6.4in}", "-V", "pagestyle:empty")
err = pandoc.Convert("./data/screen.md", "./data/screen.pdf", "-V", "geometry:papersize={3.8in,6in}", "-V", "pagestyle:empty")
if err != nil {
panic(err)
}