2019-01-01 19:41:03 +01:00
|
|
|
package post
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-12-05 21:28:19 +01:00
|
|
|
"errors"
|
2019-01-01 19:41:03 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
2022-12-05 21:28:19 +01:00
|
|
|
|
|
|
|
goutil "git.gutmet.org/goutil.git/misc"
|
|
|
|
"git.gutmet.org/wombat.git/blog"
|
|
|
|
gen "git.gutmet.org/wombat.git/generate"
|
2019-01-01 19:41:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const dateformat string = "2006-01-02"
|
|
|
|
const stub string = `---
|
|
|
|
title: "{{.Title}}"
|
|
|
|
date: {{.Date}}
|
|
|
|
categories:
|
|
|
|
---
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
type post struct {
|
|
|
|
Title string
|
|
|
|
Date string
|
|
|
|
}
|
|
|
|
|
|
|
|
func filename(dirsuffix string, title string, now time.Time) string {
|
|
|
|
date := now.Format(dateformat)
|
|
|
|
asciiTitle := blog.ConvASCII(title)
|
|
|
|
return filepath.Join("stage0", "blog"+dirsuffix, fmt.Sprintf("%s-%s.md", date, asciiTitle))
|
|
|
|
}
|
|
|
|
|
|
|
|
func ensureDirectory(file string) {
|
|
|
|
dir := filepath.Dir(file)
|
|
|
|
err := os.MkdirAll(dir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 21:28:19 +01:00
|
|
|
func makePostFile(now time.Time, dirsuffix string, title string, content string) string {
|
2019-01-01 19:41:03 +01:00
|
|
|
file := filename(dirsuffix, title, now)
|
|
|
|
ensureDirectory(file)
|
|
|
|
post := post{}
|
|
|
|
post.Title = title
|
|
|
|
post.Date = now.Format(blog.TimeFormat)
|
|
|
|
t := template.Must(template.New("Poststub").Parse(stub))
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
t.Execute(buf, post)
|
|
|
|
err := goutil.WriteFile(file, buf.String()+content)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
|
|
|
func postNew(fl postFlags) error {
|
|
|
|
var err error
|
|
|
|
if fl.title == "" {
|
|
|
|
fl.title, err = goutil.AskFor("Title")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var content string
|
|
|
|
if fl.category != "" && fl.title != "" {
|
|
|
|
tmpcontent, _ := ioutil.ReadAll(os.Stdin)
|
|
|
|
content = string(tmpcontent)
|
|
|
|
}
|
2022-12-05 21:28:19 +01:00
|
|
|
var t time.Time
|
|
|
|
if fl.datetime.val != nil {
|
|
|
|
t = *fl.datetime.val
|
|
|
|
} else {
|
|
|
|
t = time.Now().UTC()
|
|
|
|
}
|
|
|
|
f := makePostFile(t, fl.blog, fl.title, content)
|
2019-01-01 19:41:03 +01:00
|
|
|
fmt.Println(f)
|
|
|
|
if content == "" {
|
|
|
|
wait := true
|
|
|
|
err = goutil.OpenInEditor(f, wait)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
fmt.Println("trying default application...")
|
|
|
|
err = goutil.OpenInDefaultApp(f, wait)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-26 17:09:41 +02:00
|
|
|
return gen.Generate(gen.GenerateFlags{Noserve: fl.noserve})
|
2019-01-01 19:41:03 +01:00
|
|
|
}
|
|
|
|
|
2022-12-05 21:28:19 +01:00
|
|
|
type DateFlag struct {
|
|
|
|
val *time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DateFlag) String() string {
|
|
|
|
if d.val == nil {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return time.Time(*d.val).Format(blog.TimeFormat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DateFlag) Set(s string) error {
|
|
|
|
if d == nil {
|
|
|
|
return errors.New("func (d *DateFlag) Set(s string): d is nil")
|
|
|
|
}
|
|
|
|
val, err := time.Parse(blog.TimeFormat, s)
|
|
|
|
if err == nil {
|
|
|
|
d.val = &val
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-01 19:41:03 +01:00
|
|
|
type postFlags struct {
|
|
|
|
blog string
|
|
|
|
title string
|
|
|
|
category string
|
2021-06-26 17:09:41 +02:00
|
|
|
noserve bool
|
2022-12-05 21:28:19 +01:00
|
|
|
datetime DateFlag
|
2019-01-01 19:41:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) {
|
|
|
|
f := postFlags{}
|
|
|
|
flagsInit := func(s *flag.FlagSet) {
|
|
|
|
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")
|
2021-06-26 17:09:41 +02:00
|
|
|
s.BoolVar(&f.noserve, "noserve", false, "don't generate and serve afterwards")
|
2022-12-05 21:28:19 +01:00
|
|
|
s.Var(&f.datetime, "date", "use date and time other than now (yyyy-mm-dd HH:MM)")
|
2019-01-01 19:41:03 +01:00
|
|
|
}
|
|
|
|
return flagsInit, func([]string) error { return postNew(f) }
|
|
|
|
}
|