diff --git a/README.md b/README.md index 82545ae..50d96e4 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ description: This describes foobar Blog posts (located in stage0/blog/) have a similar header with values for title, date and categories. -Normal pages are created by hand, new blog posts via "wombat post". +Normal pages are created via "wombat newpage", new blog posts via "wombat newpost". build diff --git a/generate/generate.go b/generate/generate.go index e96d157..6d1d366 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -9,7 +9,6 @@ import ( "git.gutmet.org/wombat.git/blog" "git.gutmet.org/wombat.git/converter" "git.gutmet.org/wombat.git/gallery" - "git.gutmet.org/wombat.git/initer" "git.gutmet.org/wombat.git/site" "git.gutmet.org/wombat.git/splitter" "os" @@ -60,7 +59,6 @@ func stage2() { } func Generate(f GenerateFlags) error { - initer.InitializedOrDie() clean() gallery.Generate(gallery.Flags{}) stage1() diff --git a/page/page.go b/page/page.go new file mode 100644 index 0000000..865132d --- /dev/null +++ b/page/page.go @@ -0,0 +1,110 @@ +package page + +import ( + "bytes" + "errors" + "flag" + "fmt" + goutil "git.gutmet.org/goutil.git/misc" + gen "git.gutmet.org/wombat.git/generate" + "io/ioutil" + "os" + "path/filepath" + "text/template" +) + +var pageStub string = `--- +title: {{.Title}} +description: {{.Description}} +--- +` + +type page struct { + Title string + Description string +} + +type pageFlags struct { + title string + description string + path string + noserve bool +} + +func ensureDirectory(file string) { + dir := filepath.Dir(file) + err := os.MkdirAll(dir, 0755) + if err != nil { + panic(err) + } +} + +func makePageFile(path string, title string, description string, content string) error { + ensureDirectory(path) + page := page{} + page.Title = title + page.Description = description + t := template.Must(template.New("pageStub").Parse(pageStub)) + buf := new(bytes.Buffer) + t.Execute(buf, page) + err := goutil.WriteFile(path, buf.String()+content) + return err +} + +func newPage(fl pageFlags) error { + var err error + var content string + if fl.path == "" { + return errors.New("need path") + } + if filepath.Ext(fl.path) != ".md" { + fl.path += ".md" + } + if goutil.PathExists(fl.path) { + return errors.New(fl.path + " already exists...") + } + if fl.description != "" && fl.title != "" { + tmpcontent, _ := ioutil.ReadAll(os.Stdin) + content = string(tmpcontent) + } + if fl.title == "" { + fl.title, err = goutil.AskFor("Title") + if err != nil { + return err + } + } + if fl.description == "" { + fl.description, err = goutil.AskFor("Description") + if err != nil { + return err + } + } + err = makePageFile(fl.path, fl.title, fl.description, content) + if err != nil { + return err + } + if content == "" { + wait := true + err = goutil.OpenInEditor(fl.path, wait) + if err != nil { + fmt.Println(err) + fmt.Println("trying default application...") + err = goutil.OpenInDefaultApp(fl.path, wait) + } + } + if err != nil { + return err + } + return gen.Generate(gen.GenerateFlags{Noserve: fl.noserve}) +} + +func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) { + f := pageFlags{} + flagsInit := func(s *flag.FlagSet) { + s.StringVar(&f.path, "path", "", "file to post to") + s.StringVar(&f.title, "title", "", "if title and description are set: read content from stdin") + s.StringVar(&f.description, "description", "", "if title and description are set: read content from stdin") + s.BoolVar(&f.noserve, "noserve", false, "don't generate and serve afterwards") + } + return flagsInit, func([]string) error { return newPage(f) } +} diff --git a/post/post.go b/post/post.go index ee7e3d1..5113e05 100644 --- a/post/post.go +++ b/post/post.go @@ -7,7 +7,6 @@ import ( goutil "git.gutmet.org/goutil.git/misc" "git.gutmet.org/wombat.git/blog" gen "git.gutmet.org/wombat.git/generate" - "git.gutmet.org/wombat.git/initer" "io/ioutil" "os" "path/filepath" @@ -62,7 +61,6 @@ func makePostFile(dirsuffix string, title string, content string) string { func postNew(fl postFlags) error { var err error - initer.InitializedOrDie() if fl.title == "" { fl.title, err = goutil.AskFor("Title") if err != nil { @@ -88,14 +86,14 @@ func postNew(fl postFlags) error { if err != nil { return err } - noserve := (content != "") - return gen.Generate(gen.GenerateFlags{Noserve: noserve}) + return gen.Generate(gen.GenerateFlags{Noserve: fl.noserve}) } type postFlags struct { blog string title string category string + noserve bool } func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) { @@ -104,6 +102,7 @@ func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) { s.StringVar(&f.blog, "blog", "", "which blog folder to post to, e.g. -blog=foo for 'blogfoo' (default \"blog\")") s.StringVar(&f.title, "title", "", "if title and category are set: read content from stdin") s.StringVar(&f.category, "category", "", "if title and category are set: read content from stdin") + s.BoolVar(&f.noserve, "noserve", false, "don't generate and serve afterwards") } return flagsInit, func([]string) error { return postNew(f) } } diff --git a/wombat.go b/wombat.go index a5364ed..d23d475 100644 --- a/wombat.go +++ b/wombat.go @@ -7,6 +7,7 @@ import ( "git.gutmet.org/wombat.git/gallery" "git.gutmet.org/wombat.git/generate" "git.gutmet.org/wombat.git/initer" + "git.gutmet.org/wombat.git/page" "git.gutmet.org/wombat.git/post" "os" ) @@ -16,14 +17,24 @@ func serve(args []string) error { return nil } +func ifInited(cmd goutil.Command) goutil.Command { + oldExec := cmd.Exec + cmd.Exec = func(args []string) error { + initer.InitializedOrDie() + return oldExec(args) + } + return cmd +} + func main() { commands := []goutil.Command{ goutil.NewCommandWithFlags("init", initer.Command, "initialize wombat directory"), - goutil.NewCommandWithFlags("gen", generate.Command, "generate the website"), - goutil.NewCommandWithFlags("post", post.Command, "create a new blogpost"), - goutil.NewCommand("serve", serve, "just execute web server"), - goutil.NewCommandWithFlags("genpics", gallery.Command, "generate galleries"), - goutil.NewCommandWithFlags("newgallery", gallery.NewCommand, "init new gallery"), + ifInited(goutil.NewCommandWithFlags("gen", generate.Command, "generate the website")), + ifInited(goutil.NewCommand("serve", serve, "just execute web server")), + ifInited(goutil.NewCommandWithFlags("newpost", post.Command, "create a new blogpost")), + ifInited(goutil.NewCommandWithFlags("newpage", page.Command, "create a new page")), + ifInited(goutil.NewCommandWithFlags("newgallery", gallery.NewCommand, "init new gallery")), + ifInited(goutil.NewCommandWithFlags("genpics", gallery.Command, "generate galleries")), } err := goutil.Execute(commands) if err != nil {