summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorMitja Felicijan <m@mitjafelicijan.com>2023-07-10 03:46:30 +0200
committerMitja Felicijan <m@mitjafelicijan.com>2023-07-10 03:46:30 +0200
commit0d3c5134b8698359320ad0456dc8180c1857b106 (patch)
treef3e7e2269296c713459d57dcfedbd04d4eafe7a5 /main.go
parentf537b8194d0cbc6197fb77c7f20663922c836a28 (diff)
downloadjbmafp-0d3c5134b8698359320ad0456dc8180c1857b106.tar.gz
Added --new command that creates new page
Diffstat (limited to 'main.go')
-rw-r--r--main.go52
1 files changed, 48 insertions, 4 deletions
diff --git a/main.go b/main.go
index 88d21c1..71f5f94 100644
--- a/main.go
+++ b/main.go
@@ -23,6 +23,7 @@ import (
"github.com/DavidBelicza/TextRank/v2"
"github.com/alexflint/go-arg"
+ "github.com/gosimple/slug"
"github.com/microcosm-cc/bluemonday"
"github.com/tdewolff/minify/v2"
@@ -354,6 +355,39 @@ func buildProject(projectRoot string) {
log.Println("Done & done...")
}
+func newPage(projectRoot string, title string) {
+ slug := slug.Make(title)
+ t := time.Now()
+ filename := fmt.Sprintf("%s-%s.md", t.Format("2006-01-02"), slug)
+
+ var lines = []string{
+ "---",
+ fmt.Sprintf("title: \"%s\"", title),
+ fmt.Sprintf("url: %s.html", slug),
+ fmt.Sprintf("date: %s", t.Format("2006-01-02T15:04:05-07:00")),
+ "type: post",
+ "draft: true",
+ "---",
+ "",
+ "Content...",
+ }
+
+ f, err := os.Create(path.Join(projectRoot, "content", filename))
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer f.Close()
+
+ for _, line := range lines {
+ _, err := f.WriteString(line + "\n")
+ if err != nil {
+ log.Fatal(err)
+ }
+ }
+
+ log.Printf("Page `%s` created\n", filename)
+}
+
func main() {
projectRoot := os.Getenv("PROJECT_ROOT")
if projectRoot == "" {
@@ -361,14 +395,16 @@ func main() {
}
var args struct {
- Init bool `arg:"-i,--init" help:"initialize new project"`
- Build bool `arg:"-b,--build" help:"build the website"`
- Server bool `arg:"-s,--server" help:"simple embedded HTTP server"`
+ Init bool `arg:"-i,--init" help:"initialize new project"`
+ Build bool `arg:"-b,--build" help:"build the website"`
+ Server bool `arg:"-s,--server" help:"simple embedded HTTP server"`
+ New bool `arg:"-n,--new" help:"create new page"`
+ Title string `arg:"positional"`
}
arg.MustParse(&args)
- if !args.Init && !args.Build && !args.Server {
+ if !args.Init && !args.Build && !args.Server && !args.New {
fmt.Println("No arguments provided. Try using `jbmafp --help`")
os.Exit(0)
}
@@ -384,4 +420,12 @@ func main() {
if args.Server {
simpleServer(projectRoot)
}
+
+ if args.New {
+ if len(args.Title) == 0 {
+ fmt.Println("You must provide a title for the new page")
+ os.Exit(1)
+ }
+ newPage(projectRoot, args.Title)
+ }
}