2020-12-29 15:34:55 +01:00
|
|
|
package site
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"git.sr.ht/~ewintr/shitty-ssg/pkg/adoc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrUnknownSiteID = errors.New("unknown site id")
|
|
|
|
)
|
|
|
|
|
|
|
|
type SiteID string
|
|
|
|
|
|
|
|
const (
|
|
|
|
SITE_EWNL = SiteID("ewnl")
|
|
|
|
SITE_VKVNL = SiteID("vkvnl")
|
|
|
|
)
|
|
|
|
|
|
|
|
type TemplateConfig struct {
|
|
|
|
Name string
|
|
|
|
TemplateNames []string
|
|
|
|
TemplateExt string
|
|
|
|
Template *template.Template
|
|
|
|
Render func(targetPath string, tpl *template.Template, posts Posts, staticPages []*StaticPage) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type SiteConfig struct {
|
|
|
|
ID SiteID
|
|
|
|
BaseURL string
|
|
|
|
TemplateConfigs []*TemplateConfig
|
|
|
|
RSS bool
|
|
|
|
StaticPages []*StaticPage
|
|
|
|
KindMap map[adoc.Kind]Kind
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSiteConfig(id SiteID) (*SiteConfig, error) {
|
|
|
|
var config *SiteConfig
|
|
|
|
|
|
|
|
switch id {
|
|
|
|
case SITE_EWNL:
|
|
|
|
config = SITE_CONFIG_EWNL
|
|
|
|
case SITE_VKVNL:
|
|
|
|
config = SITE_CONFIG_VKVNL
|
|
|
|
default:
|
|
|
|
return &SiteConfig{}, ErrUnknownSiteID
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *SiteConfig) ParseTemplates(tplPath string) error {
|
|
|
|
for _, tplConf := range sc.TemplateConfigs {
|
2020-12-31 08:41:51 +01:00
|
|
|
var tFiles []string
|
2020-12-29 15:34:55 +01:00
|
|
|
for _, tName := range tplConf.TemplateNames {
|
|
|
|
tFiles = append(tFiles, filepath.Join(tplPath, fmt.Sprintf("%s.%s", tName, tplConf.TemplateExt)))
|
|
|
|
}
|
2020-12-31 08:41:51 +01:00
|
|
|
tpl, err := template.ParseFiles(tFiles...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tplConf.Template = tpl
|
2020-12-29 15:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *SiteConfig) MapKind(docKind adoc.Kind) Kind {
|
|
|
|
siteKind, ok := sc.KindMap[docKind]
|
|
|
|
if !ok {
|
|
|
|
return KIND_INVALID
|
|
|
|
}
|
|
|
|
|
|
|
|
return siteKind
|
|
|
|
}
|