From 7293103fc73179f2fde178bc4c26bd420d9dcf3f Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 18 Oct 2023 21:14:23 -0300 Subject: [PATCH] first step in the migration to templ. --- go.mod | 1 + go.sum | 2 ++ main.go | 25 ++++++++++---------- pages.go | 34 ++++++++++++++++++++++++++++ render.go | 23 ++++++++++++++++++- render_archive.go | 2 +- render_homepage.go | 2 +- render_relay.go | 2 +- render_robots.go | 2 +- templates/telegram_instant_view.html | 32 +++++++++++++------------- 10 files changed, 91 insertions(+), 34 deletions(-) create mode 100644 pages.go diff --git a/go.mod b/go.mod index 0af9404..85d0d66 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/pelletier/go-toml v1.9.5 github.com/rs/cors v1.10.0 github.com/rs/zerolog v1.29.1 + github.com/tylermmorton/tmpl v0.0.0-20230817025807-fd8b24ce5c3d golang.org/x/exp v0.0.0-20221106115401-f9659909a136 mvdan.cc/xurls/v2 v2.5.0 ) diff --git a/go.sum b/go.sum index 865b74f..214c0eb 100644 --- a/go.sum +++ b/go.sum @@ -165,6 +165,8 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tylermmorton/tmpl v0.0.0-20230817025807-fd8b24ce5c3d h1:WCBWmVpkAG1Ku1p87irpropCH3ioljo2IuFkTJ+cXJE= +github.com/tylermmorton/tmpl v0.0.0-20230817025807-fd8b24ce5c3d/go.mod h1:aFT85F39qRY7ZZT5pHU01s1Ru3o9EOmbd+UjbrxxHw4= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= diff --git a/main.go b/main.go index 37516e8..3186fb3 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,7 @@ var templates embed.FS var ( s Settings - tmpl *template.Template + tmpls *template.Template templateMapping map[string]string log = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: os.Stdout}).With().Timestamp().Logger() ) @@ -69,17 +69,16 @@ func main() { // initialize templates // use a mapping to expressly link the templates and share them between more kinds/types templateMapping = map[string]string{ - "homepage": "homepage.html", - "profile": "profile.html", - "profile_sitemap": "sitemap.xml", - "note": "note.html", - "telegram_instant_view": "telegram_instant_view.html", - "address": "other.html", - "relay": "relay.html", - "relay_sitemap": "sitemap.xml", - "archive": "archive.html", - "archive_sitemap": "sitemap.xml", - "robots": "robots.txt", + "homepage": "homepage.html", + "profile": "profile.html", + "profile_sitemap": "sitemap.xml", + "note": "note.html", + "address": "other.html", + "relay": "relay.html", + "relay_sitemap": "sitemap.xml", + "archive": "archive.html", + "archive_sitemap": "sitemap.xml", + "robots": "robots.txt", } funcMap := template.FuncMap{ @@ -92,7 +91,7 @@ func main() { "normalizeWebsiteURL": normalizeWebsiteURL, } - tmpl = template.Must( + tmpls = template.Must( template.New("tmpl"). Funcs(funcMap). ParseFS(templates, "templates/*"), diff --git a/pages.go b/pages.go new file mode 100644 index 0000000..530ff29 --- /dev/null +++ b/pages.go @@ -0,0 +1,34 @@ +//go:generate tmpl bind ./... + +package main + +import ( + _ "embed" + "html/template" + + "github.com/nbd-wtf/go-nostr" + "github.com/tylermmorton/tmpl" +) + +var ( + //go:embed templates/telegram_instant_view.html + tmplTelegramInstantView string + TelegramInstantViewTemplate = tmpl.MustCompile(&TelegramInstantViewPage{}) +) + +type TelegramInstantViewPage struct { + Video string + VideoType string + Image string + Summary template.HTML + Content template.HTML + Description string + Subject string + Metadata nostr.ProfileMetadata + AuthorLong string + CreatedAt string +} + +func (*TelegramInstantViewPage) TemplateText() string { + return tmplTelegramInstantView +} diff --git a/render.go b/render.go index 0b39898..4c2757c 100644 --- a/render.go +++ b/render.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "html" + "html/template" "net/http" "net/url" "strings" @@ -266,13 +267,33 @@ func render(w http.ResponseWriter, r *http.Request) { "oembed": oembed, } + // migrating to templ + if data.typ == "telegram_instant_view" { + err := TelegramInstantViewTemplate.Render(w, &TelegramInstantViewPage{ + Video: data.video, + VideoType: data.videoType, + Image: data.image, + Summary: template.HTML(summary), + Content: template.HTML(data.content), + Description: description, + Subject: subject, + Metadata: data.metadata, + AuthorLong: data.authorLong, + CreatedAt: data.createdAt, + }) + if err != nil { + log.Error().Err(err).Msg("error rendering tmpl") + } + return + } + // if a mapping is not found fallback to raw currentTemplate, ok := templateMapping[data.typ] if !ok { currentTemplate = "other.html" } - if err := tmpl.ExecuteTemplate(w, currentTemplate, params); err != nil { + if err := tmpls.ExecuteTemplate(w, currentTemplate, params); err != nil { log.Error().Err(err).Msg("error rendering") return } diff --git a/render_archive.go b/render_archive.go index b6a1e27..fe65e48 100644 --- a/render_archive.go +++ b/render_archive.go @@ -94,7 +94,7 @@ func renderArchive(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "max-age=60") } - if err := tmpl.ExecuteTemplate(w, templateMapping[typ], params); err != nil { + if err := tmpls.ExecuteTemplate(w, templateMapping[typ], params); err != nil { log.Error().Err(err).Msg("error rendering") return } diff --git a/render_homepage.go b/render_homepage.go index b202a7f..e9137ba 100644 --- a/render_homepage.go +++ b/render_homepage.go @@ -41,7 +41,7 @@ func renderHomepage(w http.ResponseWriter, r *http.Request) { "lastNotes": lastNotes, } - if err := tmpl.ExecuteTemplate(w, templateMapping[typ], params); err != nil { + if err := tmpls.ExecuteTemplate(w, templateMapping[typ], params); err != nil { log.Error().Err(err).Msg("error rendering") return } diff --git a/render_relay.go b/render_relay.go index a5ed04c..474f5ef 100644 --- a/render_relay.go +++ b/render_relay.go @@ -82,7 +82,7 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "max-age=60") } - if err := tmpl.ExecuteTemplate(w, templateMapping[typ], params); err != nil { + if err := tmpls.ExecuteTemplate(w, templateMapping[typ], params); err != nil { log.Error().Err(err).Msg("error rendering") return } diff --git a/render_robots.go b/render_robots.go index 987e97c..f98caaa 100644 --- a/render_robots.go +++ b/render_robots.go @@ -10,7 +10,7 @@ func renderRobots(w http.ResponseWriter, r *http.Request) { params := map[string]any{} - if err := tmpl.ExecuteTemplate(w, templateMapping[typ], params); err != nil { + if err := tmpls.ExecuteTemplate(w, templateMapping[typ], params); err != nil { log.Error().Err(err).Msg("error rendering") return } diff --git a/templates/telegram_instant_view.html b/templates/telegram_instant_view.html index fddcd01..dd73e7b 100644 --- a/templates/telegram_instant_view.html +++ b/templates/telegram_instant_view.html @@ -3,39 +3,39 @@ - + - -{{ if .description }} - + +{{ if not (eq "" .Description) }} + {{ end }} -{{ if .image }} - +{{ if not (eq "" .Image) }} + {{ end }} -{{ if .video }} - - - +{{ if not (eq "" .Video) }} + + + {{ end }} - +

- {{ if .subject }} {{.subject | escapeString}} {{ else }} {{.metadata.Name | - escapeString}} on Nostr: {{ end }} + {{ if not (eq "" .Subject) }} {{.Subject}} {{ else }} {{.Metadata.Name}} on + Nostr: {{ end }}

- {{ if .summary }} -

{{ .summary }}

+ {{ if not (eq "" .Summary) }} +

{{ .Summary }}

{{ end }} - {{.content}} + {{.Content}}