@ -12,6 +12,26 @@ import (
"gitcitadel-online/internal/nostr"
"gitcitadel-online/internal/nostr"
)
)
// getTemplateFuncs returns the common template functions
func getTemplateFuncs ( ) template . FuncMap {
return template . FuncMap {
"year" : func ( ) int { return time . Now ( ) . Year ( ) } ,
"json" : func ( v interface { } ) ( string , error ) {
b , err := json . Marshal ( v )
if err != nil {
return "" , err
}
return string ( b ) , nil
} ,
"hasPrefix" : func ( s , prefix string ) bool {
return len ( s ) >= len ( prefix ) && s [ : len ( prefix ) ] == prefix
} ,
"shortenPubkey" : func ( pubkey string ) string {
return nostr . ShortenPubkey ( pubkey )
} ,
}
}
// HTMLGenerator generates HTML pages from wiki events
// HTMLGenerator generates HTML pages from wiki events
type HTMLGenerator struct {
type HTMLGenerator struct {
templates * template . Template
templates * template . Template
@ -86,21 +106,18 @@ type EBookInfo struct {
TimeISO string // ISO time
TimeISO string // ISO time
}
}
// UserBadgeInfo represents user badge data for display
type UserBadgeInfo struct {
Pubkey string
Picture string
DisplayName string
Name string
ShortNpub string
}
// NewHTMLGenerator creates a new HTML generator
// NewHTMLGenerator creates a new HTML generator
func NewHTMLGenerator ( templateDir string , linkBaseURL , siteName , siteURL , defaultImage string ) ( * HTMLGenerator , error ) {
func NewHTMLGenerator ( templateDir string , linkBaseURL , siteName , siteURL , defaultImage string ) ( * HTMLGenerator , error ) {
tmpl := template . New ( "base" ) . Funcs ( template . FuncMap {
tmpl := template . New ( "base" ) . Funcs ( getTemplateFuncs ( ) )
"year" : func ( ) int { return time . Now ( ) . Year ( ) } ,
"json" : func ( v interface { } ) ( string , error ) {
b , err := json . Marshal ( v )
if err != nil {
return "" , err
}
return string ( b ) , nil
} ,
"hasPrefix" : func ( s , prefix string ) bool {
return len ( s ) >= len ( prefix ) && s [ : len ( prefix ) ] == prefix
} ,
} )
// Load all templates
// Load all templates
templateFiles := [ ] string {
templateFiles := [ ] string {
@ -246,19 +263,7 @@ func (g *HTMLGenerator) GenerateBlogPage(blogIndex *nostr.IndexEvent, blogItems
}
}
// Use renderTemplate but with custom data
// Use renderTemplate but with custom data
renderTmpl := template . New ( "blog-render" ) . Funcs ( template . FuncMap {
renderTmpl := template . New ( "blog-render" ) . Funcs ( getTemplateFuncs ( ) )
"year" : func ( ) int { return time . Now ( ) . Year ( ) } ,
"json" : func ( v interface { } ) ( string , error ) {
b , err := json . Marshal ( v )
if err != nil {
return "" , err
}
return string ( b ) , nil
} ,
"hasPrefix" : func ( s , prefix string ) bool {
return len ( s ) >= len ( prefix ) && s [ : len ( prefix ) ] == prefix
} ,
} )
files := [ ] string {
files := [ ] string {
filepath . Join ( g . templateDir , "components.html" ) ,
filepath . Join ( g . templateDir , "components.html" ) ,
@ -392,19 +397,7 @@ func (g *HTMLGenerator) GenerateContactPage(success bool, errorMsg string, event
}
}
// Parse base.html together with contact.html to ensure correct blocks are used
// Parse base.html together with contact.html to ensure correct blocks are used
renderTmpl := template . New ( "render" ) . Funcs ( template . FuncMap {
renderTmpl := template . New ( "render" ) . Funcs ( getTemplateFuncs ( ) )
"year" : func ( ) int { return time . Now ( ) . Year ( ) } ,
"json" : func ( v interface { } ) ( string , error ) {
b , err := json . Marshal ( v )
if err != nil {
return "" , err
}
return string ( b ) , nil
} ,
"hasPrefix" : func ( s , prefix string ) bool {
return len ( s ) >= len ( prefix ) && s [ : len ( prefix ) ] == prefix
} ,
} )
// Parse base.html, components.html, and contact.html together
// Parse base.html, components.html, and contact.html together
files := [ ] string {
files := [ ] string {
@ -515,19 +508,7 @@ func (g *HTMLGenerator) GenerateErrorPage(statusCode int, feedItems []FeedItemIn
func ( g * HTMLGenerator ) renderTemplate ( templateName string , data PageData ) ( string , error ) {
func ( g * HTMLGenerator ) renderTemplate ( templateName string , data PageData ) ( string , error ) {
// Parse base.html together with the specific template to ensure correct blocks are used
// Parse base.html together with the specific template to ensure correct blocks are used
// This avoids the issue where all templates are parsed together and the last "content" block wins
// This avoids the issue where all templates are parsed together and the last "content" block wins
renderTmpl := template . New ( "render" ) . Funcs ( template . FuncMap {
renderTmpl := template . New ( "render" ) . Funcs ( getTemplateFuncs ( ) )
"year" : func ( ) int { return time . Now ( ) . Year ( ) } ,
"json" : func ( v interface { } ) ( string , error ) {
b , err := json . Marshal ( v )
if err != nil {
return "" , err
}
return string ( b ) , nil
} ,
"hasPrefix" : func ( s , prefix string ) bool {
return len ( s ) >= len ( prefix ) && s [ : len ( prefix ) ] == prefix
} ,
} )
// Parse base.html, components.html, and the specific template together
// Parse base.html, components.html, and the specific template together
// This ensures the correct "content" block and reusable components are available
// This ensures the correct "content" block and reusable components are available