2020-12-04 12:50:20 +01:00
|
|
|
package site
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2020-12-21 07:36:45 +01:00
|
|
|
|
|
|
|
"git.sr.ht/~ewintr/shitty-ssg/pkg/adoc"
|
2020-12-04 12:50:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type StaticPage struct {
|
2020-12-04 16:52:25 +01:00
|
|
|
Name string
|
|
|
|
Path string
|
2020-12-04 12:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Site struct {
|
|
|
|
resourcesPath string
|
2020-12-29 15:34:55 +01:00
|
|
|
config *SiteConfig
|
2020-12-04 12:50:20 +01:00
|
|
|
posts Posts
|
|
|
|
staticPages []*StaticPage
|
|
|
|
}
|
|
|
|
|
2020-12-29 15:34:55 +01:00
|
|
|
func New(config *SiteConfig, resourcesPath string) (*Site, error) {
|
|
|
|
if err := config.ParseTemplates(filepath.Join(resourcesPath, "template")); err != nil {
|
2020-12-04 12:50:20 +01:00
|
|
|
return &Site{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Site{
|
|
|
|
resourcesPath: resourcesPath,
|
2020-12-29 15:34:55 +01:00
|
|
|
config: config,
|
2020-12-21 07:36:45 +01:00
|
|
|
posts: Posts{},
|
2020-12-04 16:52:25 +01:00
|
|
|
staticPages: []*StaticPage{},
|
2020-12-04 12:50:20 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-04 16:52:25 +01:00
|
|
|
func (s *Site) AddStaticPage(staticPath string) {
|
|
|
|
s.staticPages = append(s.staticPages, &StaticPage{
|
|
|
|
Name: filepath.Base(staticPath),
|
|
|
|
Path: staticPath,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-04 12:50:20 +01:00
|
|
|
func (s *Site) AddFilePost(fPath string) error {
|
|
|
|
content, err := ioutil.ReadFile(fPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-29 15:34:55 +01:00
|
|
|
post := NewPost(s.config, adoc.New(string(content)))
|
2020-12-21 07:36:45 +01:00
|
|
|
if post.Kind != KIND_INVALID {
|
|
|
|
s.posts = append(s.posts, post)
|
|
|
|
}
|
2020-12-04 12:50:20 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) RenderHTML(targetPath string) error {
|
|
|
|
posts := s.posts.Sort()
|
|
|
|
|
|
|
|
if err := resetTarget(targetPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := moveResources(targetPath, s.resourcesPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-29 15:34:55 +01:00
|
|
|
for _, tplConf := range s.config.TemplateConfigs {
|
|
|
|
if err := tplConf.Render(targetPath, tplConf.Template, posts, s.staticPages); err != nil {
|
|
|
|
return err
|
2020-12-04 12:50:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 15:34:55 +01:00
|
|
|
return nil
|
2020-12-04 12:50:20 +01:00
|
|
|
}
|