mirror of
https://github.com/aljazceru/njump.git
synced 2026-01-31 19:54:29 +01:00
first step in the migration to templ.
This commit is contained in:
1
go.mod
1
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
|
||||
)
|
||||
|
||||
2
go.sum
2
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=
|
||||
|
||||
25
main.go
25
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/*"),
|
||||
|
||||
34
pages.go
Normal file
34
pages.go
Normal file
@@ -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
|
||||
}
|
||||
23
render.go
23
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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -3,39 +3,39 @@
|
||||
|
||||
<!-- required stuff so telegram treats us like a medium.com article -->
|
||||
<meta property="al:android:app_name" content="Medium" />
|
||||
<meta property="article:published_time" content="{{.createdAt}}" />
|
||||
<meta property="article:published_time" content="{{.CreatedAt}}" />
|
||||
|
||||
<!-- stuff that goes in the actual telegram message preview -->
|
||||
<meta property="og:site_name" content="{{.authorLong | escapeString}}" />
|
||||
{{ if .description }}
|
||||
<meta property="og:description" content="{{.description | escapeString}}" />
|
||||
<meta property="og:site_name" content="{{.AuthorLong}}" />
|
||||
{{ if not (eq "" .Description) }}
|
||||
<meta property="og:description" content="{{.Description}}" />
|
||||
{{ end }}
|
||||
<!---->
|
||||
{{ if .image }}
|
||||
<meta property="og:image" content="{{.image | escapeString}}" />
|
||||
{{ if not (eq "" .Image) }}
|
||||
<meta property="og:image" content="{{.Image}}" />
|
||||
{{ end }}
|
||||
<!---->
|
||||
{{ if .video }}
|
||||
<meta property="og:video" content="{{.video | escapeString}}" />
|
||||
<meta property="og:video:secure_url" content="{{.video | escapeString}}" />
|
||||
<meta property="og:video:type" content="video/{{.videoType | escapeString}}" />
|
||||
{{ if not (eq "" .Video) }}
|
||||
<meta property="og:video" content="{{.Video}}" />
|
||||
<meta property="og:video:secure_url" content="{{.Video}}" />
|
||||
<meta property="og:video:type" content="video/{{.VideoType}}" />
|
||||
{{ end }}
|
||||
|
||||
<!-- stuff that affects the content inside the preview window -->
|
||||
<meta name="author" content="{{.metadata.Name}}" />
|
||||
<meta name="author" content="{{.Metadata.Name}}" />
|
||||
<meta name="telegram:channel" content="@nostr_protocol" />
|
||||
|
||||
<!-- basic content of the preview window -->
|
||||
<article>
|
||||
<h1>
|
||||
{{ if .subject }} {{.subject | escapeString}} {{ else }} {{.metadata.Name |
|
||||
escapeString}} on Nostr: {{ end }}
|
||||
{{ if not (eq "" .Subject) }} {{.Subject}} {{ else }} {{.Metadata.Name}} on
|
||||
Nostr: {{ end }}
|
||||
</h1>
|
||||
|
||||
{{ if .summary }}
|
||||
<h2>{{ .summary }}</h2>
|
||||
{{ if not (eq "" .Summary) }}
|
||||
<h2>{{ .Summary }}</h2>
|
||||
{{ end }}
|
||||
|
||||
<!---->
|
||||
{{.content}}
|
||||
{{.Content}}
|
||||
</article>
|
||||
|
||||
Reference in New Issue
Block a user