2020-11-20 17:07:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-05-01 17:56:41 +00:00
|
|
|
"fmt"
|
2020-11-20 17:07:38 +00:00
|
|
|
"os"
|
2021-05-10 15:06:55 +00:00
|
|
|
"text/template"
|
2021-10-02 20:25:01 +00:00
|
|
|
"time"
|
2020-11-20 17:07:38 +00:00
|
|
|
|
2022-02-09 05:25:48 +00:00
|
|
|
"github.com/google/shlex"
|
2021-05-10 16:15:58 +00:00
|
|
|
"github.com/pelletier/go-toml"
|
2020-11-20 17:07:38 +00:00
|
|
|
)
|
|
|
|
|
2021-05-14 04:17:10 +00:00
|
|
|
// Site represents a site.
|
|
|
|
type Site struct {
|
2021-05-10 04:44:25 +00:00
|
|
|
Title string `toml:"title"`
|
2021-05-10 14:26:12 +00:00
|
|
|
Tasks []*Task `toml:"tasks"`
|
2021-06-26 06:09:55 +00:00
|
|
|
Params map[string]string `toml:"params"`
|
2021-05-10 04:44:25 +00:00
|
|
|
Permalinks map[string]string `toml:"permalinks"`
|
2021-10-02 20:25:01 +00:00
|
|
|
Generated time.Time `toml:"-"`
|
2022-02-09 17:25:39 +00:00
|
|
|
Root *Page `toml:"-"`
|
2021-05-10 15:06:55 +00:00
|
|
|
permalinks map[string]*template.Template
|
2021-05-14 04:17:10 +00:00
|
|
|
templates Templates
|
2020-11-20 17:07:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 03:17:58 +00:00
|
|
|
// Task represents a site build task.
|
|
|
|
type Task struct {
|
2021-10-02 23:20:47 +00:00
|
|
|
Input []string `toml:"input"`
|
|
|
|
OutputExt string `toml:"output"`
|
|
|
|
TemplateExt string `toml:"template"`
|
|
|
|
Preprocess map[string]string `toml:"preprocess"`
|
|
|
|
Postprocess string `toml:"postprocess"`
|
|
|
|
StaticDir string `toml:"static_dir"`
|
|
|
|
OutputDir string `toml:"output_dir"`
|
|
|
|
URL string `toml:"url"`
|
|
|
|
UglyURLs bool `toml:"ugly_urls"`
|
2021-09-02 09:59:08 +00:00
|
|
|
Feeds []Feed `toml:"feeds"`
|
2022-02-09 05:25:48 +00:00
|
|
|
preprocess map[string][]string
|
|
|
|
postprocess []string
|
2021-09-03 03:41:52 +00:00
|
|
|
feeds map[string][]Feed
|
2021-09-02 09:59:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Feed struct {
|
|
|
|
InputDir string `toml:"input_dir"`
|
|
|
|
Title string `toml:"title"`
|
|
|
|
Template string `toml:"template"`
|
|
|
|
Output string `toml:"output"`
|
2021-05-10 16:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Task) Match(ext string) bool {
|
|
|
|
for i := range t.Input {
|
|
|
|
if t.Input[i] == ext {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2021-03-21 03:17:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 04:17:10 +00:00
|
|
|
// LoadSite loads the site with the given configuration file.
|
|
|
|
func LoadSite(config string) (*Site, error) {
|
|
|
|
f, err := os.Open(config)
|
2020-11-20 17:07:38 +00:00
|
|
|
if err != nil {
|
2021-03-20 06:02:36 +00:00
|
|
|
return nil, err
|
2020-11-20 17:07:38 +00:00
|
|
|
}
|
2021-03-20 06:02:36 +00:00
|
|
|
defer f.Close()
|
2020-11-20 17:07:38 +00:00
|
|
|
|
2021-10-02 20:25:01 +00:00
|
|
|
site := &Site{
|
|
|
|
Generated: time.Now(),
|
|
|
|
}
|
2021-05-14 04:17:10 +00:00
|
|
|
if err := toml.NewDecoder(f).Decode(site); err != nil {
|
2021-03-20 06:02:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-14 04:17:10 +00:00
|
|
|
funcs := site.funcs()
|
|
|
|
|
2021-05-10 15:06:55 +00:00
|
|
|
// Parse permalinks
|
2021-05-14 04:17:10 +00:00
|
|
|
site.permalinks = map[string]*template.Template{}
|
|
|
|
for path := range site.Permalinks {
|
|
|
|
t := template.New(fmt.Sprintf("permalink %q", path)).Funcs(funcs)
|
|
|
|
_, err := t.Parse(site.Permalinks[path])
|
2021-05-10 15:06:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-14 04:17:10 +00:00
|
|
|
site.permalinks[path] = t
|
2020-11-20 17:07:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 04:17:10 +00:00
|
|
|
// Load templates
|
|
|
|
templateExts := []string{}
|
|
|
|
for _, task := range site.Tasks {
|
|
|
|
if task.TemplateExt != "" {
|
|
|
|
templateExts = append(templateExts, task.TemplateExt)
|
2021-05-10 15:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-14 04:17:10 +00:00
|
|
|
site.templates.Funcs(funcs)
|
|
|
|
if err := site.templates.Load("templates", templateExts); err != nil {
|
|
|
|
return nil, err
|
2021-05-10 15:12:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 03:41:52 +00:00
|
|
|
for _, task := range site.Tasks {
|
2022-02-09 05:25:48 +00:00
|
|
|
// Populate feeds
|
2021-09-03 03:41:52 +00:00
|
|
|
task.feeds = map[string][]Feed{}
|
|
|
|
for _, feed := range task.Feeds {
|
2021-09-03 04:22:07 +00:00
|
|
|
dir := feed.InputDir
|
|
|
|
task.feeds[dir] = append(task.feeds[dir], feed)
|
|
|
|
}
|
2022-02-09 05:25:48 +00:00
|
|
|
|
|
|
|
// Parse commands
|
|
|
|
task.preprocess = map[string][]string{}
|
|
|
|
for path := range task.Preprocess {
|
|
|
|
preprocess, err := shlex.Split(task.Preprocess[path])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
task.preprocess[path] = preprocess
|
|
|
|
}
|
|
|
|
if task.Postprocess != "" {
|
|
|
|
postprocess, err := shlex.Split(task.Postprocess)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
task.postprocess = postprocess
|
|
|
|
}
|
2021-09-03 04:22:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 04:17:10 +00:00
|
|
|
return site, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) page(path string) *Page {
|
2022-02-09 17:25:39 +00:00
|
|
|
return s.Root.getPage(path)
|
2021-05-09 23:28:09 +00:00
|
|
|
}
|