blog/templates/style.go

60 lines
1.2 KiB
Go

// templates/style.go
package templates
import (
"bytes"
"fmt"
)
// PageMetadata holds the frontmatter data
type PageMetadata struct {
Title string `toml:"title" yaml:"title"`
Stylesheet string `toml:"stylesheet" yaml:"stylesheet"`
Style string `toml:"style" yaml:"style"` // Inline CSS
}
// BuildFullPage wraps the content in the HTML shell, injecting metadata.
func BuildFullPage(content []byte, meta PageMetadata) []byte {
var buf bytes.Buffer
// Default title if missing
title := meta.Title
if title == "" {
title = "Blog"
}
// Default stylesheet if missing
cssLink := `<link rel="stylesheet" href="/default.css">`
if meta.Stylesheet != "" {
cssLink = fmt.Sprintf(`<link rel="stylesheet" href="%s">`, meta.Stylesheet)
}
buf.WriteString(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>`)
buf.WriteString(title)
buf.WriteString(`</title>
`)
buf.WriteString(cssLink)
if meta.Style != "" {
buf.WriteString(`<style>`)
buf.WriteString(meta.Style)
buf.WriteString(`</style>`)
}
buf.WriteString(`
</head>
<body>
`)
buf.Write(content)
buf.WriteString(`
</body>
</html>`)
return buf.Bytes()
}