Files
go_blog/themes/manager.go

238 lines
5.2 KiB
Go
Raw Normal View History

2025-05-21 20:38:25 +08:00
package themes
2025-04-01 17:59:10 +08:00
import (
2025-05-21 20:38:25 +08:00
"errors"
2025-04-01 17:59:10 +08:00
"fmt"
"html/template"
2025-05-21 20:38:25 +08:00
"io/fs"
2025-04-01 17:59:10 +08:00
"os"
"path/filepath"
"strings"
2025-05-21 20:38:25 +08:00
"sync"
2025-05-22 13:56:57 +08:00
"time"
2025-05-21 20:38:25 +08:00
"github.com/gin-gonic/gin"
2025-05-22 13:56:57 +08:00
"golang.org/x/exp/slog"
"gopkg.in/yaml.v3"
2025-04-01 17:59:10 +08:00
)
2025-05-22 13:56:57 +08:00
const (
defaultLayout = "base.html"
themeConfigFile = "theme.yaml"
requiredTemplates = "home.html,post.html"
)
// ThemeMeta 主题元数据
2025-05-21 20:38:25 +08:00
type ThemeMeta struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
2025-05-22 13:56:57 +08:00
Author string `yaml:"author"`
2025-05-21 20:38:25 +08:00
Description string `yaml:"description"`
2025-05-22 13:56:57 +08:00
License string `yaml:"license"`
2025-05-21 20:38:25 +08:00
}
2025-05-22 13:56:57 +08:00
// ThemeManager 主题管理器
2025-04-01 17:59:10 +08:00
type ThemeManager struct {
2025-05-22 13:56:57 +08:00
mu sync.RWMutex
themesDir string
currentTheme string
templates *template.Template
staticFS gin.StaticFS
meta ThemeMeta
funcMap template.FuncMap
2025-05-21 20:38:25 +08:00
}
2025-05-22 13:56:57 +08:00
// NewManager 创建主题管理器实例
func NewManager(themesDir, defaultTheme string) (*ThemeManager, error) {
m := &ThemeManager{
2025-05-21 20:38:25 +08:00
themesDir: themesDir,
2025-05-22 13:56:57 +08:00
funcMap: defaultFuncMap(),
2025-05-21 20:38:25 +08:00
}
2025-05-22 13:56:57 +08:00
if err := m.LoadTheme(defaultTheme); err != nil {
return nil, fmt.Errorf("初始化默认主题失败: %w", err)
}
return m, nil
2025-05-21 20:38:25 +08:00
}
2025-05-22 13:56:57 +08:00
// 默认模板函数
func defaultFuncMap() template.FuncMap {
return template.FuncMap{
"safeHTML": func(s string) template.HTML { return template.HTML(s) },
"dateFmt": func(t time.Time) string { return t.Format("2006-01-02") },
"assetPath": func(name string) string {
return fmt.Sprintf("/theme/static/%s?t=%d", name, time.Now().Unix())
},
}
}
2025-05-21 20:38:25 +08:00
2025-05-22 13:56:57 +08:00
// LoadTheme 加载指定主题
func (m *ThemeManager) LoadTheme(name string) error {
start := time.Now()
themePath := filepath.Join(m.themesDir, name)
// 验证主题完整性
if err := validateTheme(themePath); err != nil {
return fmt.Errorf("主题验证失败: %w", err)
2025-05-21 20:38:25 +08:00
}
2025-05-22 13:56:57 +08:00
// 加载元数据
meta, err := loadThemeMeta(themePath)
2025-05-21 20:38:25 +08:00
if err != nil {
2025-05-22 13:56:57 +08:00
return fmt.Errorf("元数据加载失败: %w", err)
2025-05-21 20:38:25 +08:00
}
2025-05-22 13:56:57 +08:00
// 解析模板
tpls, err := parseTemplates(themePath, m.funcMap)
if err != nil {
return fmt.Errorf("模板解析失败: %w", err)
}
2025-05-21 20:38:25 +08:00
2025-05-22 13:56:57 +08:00
// 准备静态文件系统
staticPath := filepath.Join(themePath, "static")
staticFS := gin.Dir(staticPath, false)
2025-05-21 20:38:25 +08:00
2025-05-22 13:56:57 +08:00
// 原子化更新状态
m.mu.Lock()
defer m.mu.Unlock()
2025-05-21 20:38:25 +08:00
2025-05-22 13:56:57 +08:00
m.currentTheme = name
m.templates = tpls
m.staticFS = staticFS
m.meta = meta
slog.Info("主题加载成功",
"theme", name,
"templates", len(tpls.Templates()),
"duration", time.Since(start),
)
return nil
2025-04-01 17:59:10 +08:00
}
2025-05-22 13:56:57 +08:00
// 验证主题完整性
func validateTheme(themePath string) error {
// 检查必须文件
2025-05-21 20:38:25 +08:00
requiredFiles := []string{
2025-05-22 13:56:57 +08:00
themeConfigFile,
filepath.Join("templates", "base.html"),
}
for _, f := range strings.Split(requiredTemplates, ",") {
requiredFiles = append(requiredFiles,
filepath.Join("templates", f))
2025-05-21 20:38:25 +08:00
}
2025-04-01 17:59:10 +08:00
2025-05-22 13:56:57 +08:00
var missing []string
2025-05-21 20:38:25 +08:00
for _, f := range requiredFiles {
2025-05-22 13:56:57 +08:00
if _, err := os.Stat(filepath.Join(themePath, f)); err != nil {
if os.IsNotExist(err) {
missing = append(missing, f)
}
2025-05-21 20:38:25 +08:00
}
2025-04-01 17:59:10 +08:00
}
2025-05-22 13:56:57 +08:00
if len(missing) > 0 {
return fmt.Errorf("缺失必要文件: %v", missing)
}
return nil
2025-05-21 20:38:25 +08:00
}
2025-05-22 13:56:57 +08:00
// 加载主题元数据
func loadThemeMeta(themePath string) (ThemeMeta, error) {
configPath := filepath.Join(themePath, themeConfigFile)
data, err := os.ReadFile(configPath)
if err != nil {
return ThemeMeta{}, err
}
2025-04-01 17:59:10 +08:00
2025-05-22 13:56:57 +08:00
var meta ThemeMeta
if err := yaml.Unmarshal(data, &meta); err != nil {
return ThemeMeta{}, err
}
2025-05-21 20:38:25 +08:00
2025-05-22 13:56:57 +08:00
if meta.Name == "" {
return ThemeMeta{}, errors.New("主题名称不能为空")
}
return meta, nil
}
// 解析模板文件
func parseTemplates(themePath string, funcMap template.FuncMap) (*template.Template, error) {
2025-05-21 20:38:25 +08:00
tplDir := filepath.Join(themePath, "templates")
2025-05-22 13:56:57 +08:00
baseFile := filepath.Join(tplDir, defaultLayout)
// 先解析基础模板
baseTpl, err := template.New("base").
Funcs(funcMap).
ParseFiles(baseFile)
if err != nil {
return nil, fmt.Errorf("基础模板解析失败: %w", err)
}
// 遍历模板目录
return filepath.WalkDir(tplDir, func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
2025-05-21 20:38:25 +08:00
return nil
}
2025-05-22 13:56:57 +08:00
if path == baseFile { // 已处理基础模板
return nil
2025-04-01 17:59:10 +08:00
}
2025-05-21 20:38:25 +08:00
2025-05-22 13:56:57 +08:00
if filepath.Ext(path) != ".html" {
return nil
}
2025-05-21 20:38:25 +08:00
// 克隆基础模板并解析
2025-05-22 13:56:57 +08:00
_, err = baseTpl.Clone().
ParseFiles(path)
2025-05-21 20:38:25 +08:00
if err != nil {
2025-05-22 13:56:57 +08:00
return fmt.Errorf("解析 %s 失败: %w", filepath.Base(path), err)
2025-04-01 17:59:10 +08:00
}
2025-05-21 20:38:25 +08:00
2025-04-01 17:59:10 +08:00
return nil
})
2025-05-22 13:56:57 +08:00
}
2025-05-21 20:38:25 +08:00
2025-05-22 13:56:57 +08:00
// GetTemplate 获取指定模板
func (m *ThemeManager) GetTemplate(name string) *template.Template {
m.mu.RLock()
defer m.mu.RUnlock()
2025-04-01 17:59:10 +08:00
2025-05-22 13:56:57 +08:00
return m.templates.Lookup(name + ".html")
}
// RegisterStatic 注册静态资源路由
func (m *ThemeManager) RegisterStatic(r *gin.Engine) {
m.mu.RLock()
defer m.mu.RUnlock()
r.StaticFS("/theme/static", m.staticFS)
}
// CurrentThemeInfo 返回当前主题信息
func (m *ThemeManager) CurrentThemeInfo() ThemeMeta {
m.mu.RLock()
defer m.mu.RUnlock()
2025-04-01 17:59:10 +08:00
2025-05-22 13:56:57 +08:00
return m.meta
2025-04-01 17:59:10 +08:00
}
2025-05-21 20:38:25 +08:00
2025-05-22 13:56:57 +08:00
// ListThemes 获取可用主题列表
2025-05-21 20:38:25 +08:00
func (m *ThemeManager) ListThemes() ([]string, error) {
2025-05-22 13:56:57 +08:00
entries, err := os.ReadDir(m.themesDir)
2025-05-21 20:38:25 +08:00
if err != nil {
return nil, err
}
var themes []string
2025-05-22 13:56:57 +08:00
for _, entry := range entries {
if entry.IsDir() {
if validateTheme(filepath.Join(m.themesDir, entry.Name())) == nil {
themes = append(themes, entry.Name())
}
2025-05-21 20:38:25 +08:00
}
}
return themes, nil
2025-04-09 16:56:05 +08:00
}