package main import ( "bytes" "log" "os" "os/exec" "path" "strings" "text/template" "github.com/BurntSushi/toml" ) // Config contains site configuration. type Config struct { Title string `toml:"title"` // site title URLs []string `toml:"urls"` // site URLs Feeds map[string]string `toml:"feeds"` // site feeds Tasks map[string]*Task `toml:"tasks"` // site tasks Templates *Templates `toml:"-"` // site templates } // Task represents a site build task. type Task struct { InputExt string `toml:"input_ext"` // input file extension OutputExt string `toml:"output_ext"` // output file extension TemplateExt string `toml:"template_ext"` // template file extension PostProcess string `toml:"postprocess"` // postprocess command StaticDir string `toml:"static_dir"` // static file directory OutputDir string `toml:"output_dir"` // output directory } func (t Task) Format(p *Page) (string, []byte) { path := path.Join(p.Path, "index"+t.OutputExt) // Run a custom command. if t.PostProcess != "" { split := strings.Split(t.PostProcess, " ") cmd := exec.Command(split[0], split[1:]...) buf := new(bytes.Buffer) cmd.Stdin = strings.NewReader(p.Content) cmd.Stdout = buf if err := cmd.Run(); err != nil { log.Fatal(err) } return path, buf.Bytes() } return path, []byte(p.Content) } // DefaultConfig returns the default configuration. func DefaultConfig() *Config { c := new(Config) c.Feeds = make(map[string]string) c.Tasks = make(map[string]*Task) c.Tasks["gemini"] = &Task{ InputExt: ".gmi", OutputExt: ".gmi", TemplateExt: ".gmi", StaticDir: "static", OutputDir: "public", } return c } // LoadConfig loads the configuration from the provided path. func LoadConfig(path string) (*Config, error) { f, err := os.Open(path) if err != nil { return nil, err } defer f.Close() c := DefaultConfig() if _, err := toml.DecodeReader(f, c); err != nil { return nil, err } return c, nil } // LoadTemplates loads templates from the provided path. func (c *Config) LoadTemplates(path string) error { // Site contains site metadata passed to templates type Site struct { Title string URLs []string } // Load templates c.Templates = NewTemplates() c.Templates.Funcs(template.FuncMap{ "site": func() Site { return Site{ Title: c.Title, URLs: c.URLs, } }, }) c.Templates.LoadDefault() return c.Templates.Load(path) }