wombat/generate/generate.go

84 lines
1.7 KiB
Go
Raw Permalink Normal View History

2019-01-01 19:41:03 +01:00
package generate
import (
"flag"
"fmt"
"os"
2024-06-18 23:01:52 +02:00
"git.fireandbrimst.one/aw/goutil/copy"
goutil "git.fireandbrimst.one/aw/goutil/misc"
"git.fireandbrimst.one/aw/simpleserver"
"git.fireandbrimst.one/aw/wombat/blog"
"git.fireandbrimst.one/aw/wombat/converter"
"git.fireandbrimst.one/aw/wombat/gallery"
"git.fireandbrimst.one/aw/wombat/site"
"git.fireandbrimst.one/aw/wombat/splitter"
2019-01-01 19:41:03 +01:00
)
func removeAll(path string) {
err := os.RemoveAll(path)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
func clean() {
removeAll("stage1")
removeAll("stage2")
}
func stage1() {
2020-10-11 13:35:13 +02:00
copy.CopyDirsLinkFiles("stage0", "stage1")
2019-01-01 19:41:03 +01:00
mdfiles := goutil.RecListFilesExt("stage1", ".md")
htmlfrags := goutil.RecListFilesExt("stage1", ".html")
splitter.SplitAll(append(mdfiles, htmlfrags...))
converter.ConvertAll(mdfiles)
}
func blogs() []string {
dirs, err := goutil.DirsWithPrefix("stage0", "blog")
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
return dirs
}
func stage2() {
2020-10-11 13:35:13 +02:00
copy.CopyDirsLinkFiles("stage1", "stage2")
2019-01-01 19:41:03 +01:00
site.Generate()
for _, blogdir := range blogs() {
blog.Generate(blogdir)
}
descfiles := goutil.RecListFilesExt("stage2", ".desc")
for _, f := range descfiles {
err := os.Remove(f)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
os.Remove("stage2/README")
}
func Generate(f GenerateFlags) error {
clean()
gallery.Generate(gallery.Flags{})
stage1()
stage2()
if !f.Noserve {
simpleserver.Serve("stage2")
}
return nil
}
type GenerateFlags struct {
Noserve bool
}
func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) {
f := GenerateFlags{}
flagsInit := func(s *flag.FlagSet) {
s.BoolVar(&f.Noserve, "noserve", false, "don't start webserver after generating")
}
return flagsInit, func([]string) error { return Generate(f) }
}