Added --new command that creates new page

Author Mitja Felicijan <m@mitjafelicijan.com> 2023-07-10 03:46:30 +0200
Committer Mitja Felicijan <m@mitjafelicijan.com> 2023-07-10 03:46:30 +0200
Commit 0d3c5134b8698359320ad0456dc8180c1857b106 (patch)
-rw-r--r-- go.mod 2
-rw-r--r-- go.sum 4
-rw-r--r-- main.go 52
-rw-r--r-- shell.nix 6
4 files changed, 60 insertions, 4 deletions
diff --git a/go.mod b/go.mod
...
5
require (
5
require (
6
	github.com/DavidBelicza/TextRank/v2 v2.1.3
6
	github.com/DavidBelicza/TextRank/v2 v2.1.3
7
	github.com/alexflint/go-arg v1.4.3
7
	github.com/alexflint/go-arg v1.4.3
  
8
	github.com/gosimple/slug v1.13.1
8
	github.com/microcosm-cc/bluemonday v1.0.24
9
	github.com/microcosm-cc/bluemonday v1.0.24
9
	github.com/otiai10/copy v1.12.0
10
	github.com/otiai10/copy v1.12.0
10
	github.com/tdewolff/minify/v2 v2.12.7
11
	github.com/tdewolff/minify/v2 v2.12.7
...
20
	github.com/aymerick/douceur v0.2.0 // indirect
21
	github.com/aymerick/douceur v0.2.0 // indirect
21
	github.com/dlclark/regexp2 v1.7.0 // indirect
22
	github.com/dlclark/regexp2 v1.7.0 // indirect
22
	github.com/gorilla/css v1.0.0 // indirect
23
	github.com/gorilla/css v1.0.0 // indirect
  
24
	github.com/gosimple/unidecode v1.0.1 // indirect
23
	github.com/tdewolff/parse/v2 v2.6.6 // indirect
25
	github.com/tdewolff/parse/v2 v2.6.6 // indirect
24
	golang.org/x/net v0.10.0 // indirect
26
	golang.org/x/net v0.10.0 // indirect
25
	golang.org/x/sys v0.8.0 // indirect
27
	golang.org/x/sys v0.8.0 // indirect
...
diff --git a/go.sum b/go.sum
...
22
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
22
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
23
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
23
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
24
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
24
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
  
25
github.com/gosimple/slug v1.13.1 h1:bQ+kpX9Qa6tHRaK+fZR0A0M2Kd7Pa5eHPPsb1JpHD+Q=
  
26
github.com/gosimple/slug v1.13.1/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
  
27
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
  
28
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
25
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
29
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
26
github.com/microcosm-cc/bluemonday v1.0.24 h1:NGQoPtwGVcbGkKfvyYk1yRqknzBuoMiUrO6R7uFTPlw=
30
github.com/microcosm-cc/bluemonday v1.0.24 h1:NGQoPtwGVcbGkKfvyYk1yRqknzBuoMiUrO6R7uFTPlw=
27
github.com/microcosm-cc/bluemonday v1.0.24/go.mod h1:ArQySAMps0790cHSkdPEJ7bGkF2VePWH773hsJNSHf8=
31
github.com/microcosm-cc/bluemonday v1.0.24/go.mod h1:ArQySAMps0790cHSkdPEJ7bGkF2VePWH773hsJNSHf8=
...
diff --git a/main.go b/main.go
...
23
  
23
  
24
	"github.com/DavidBelicza/TextRank/v2"
24
	"github.com/DavidBelicza/TextRank/v2"
25
	"github.com/alexflint/go-arg"
25
	"github.com/alexflint/go-arg"
  
26
	"github.com/gosimple/slug"
26
	"github.com/microcosm-cc/bluemonday"
27
	"github.com/microcosm-cc/bluemonday"
27
  
28
  
28
	"github.com/tdewolff/minify/v2"
29
	"github.com/tdewolff/minify/v2"
...
354
	log.Println("Done & done...")
355
	log.Println("Done & done...")
355
}
356
}
356
  
357
  
  
358
func newPage(projectRoot string, title string) {
  
359
	slug := slug.Make(title)
  
360
	t := time.Now()
  
361
	filename := fmt.Sprintf("%s-%s.md", t.Format("2006-01-02"), slug)
  
362
  
  
363
	var lines = []string{
  
364
		"---",
  
365
		fmt.Sprintf("title: \"%s\"", title),
  
366
		fmt.Sprintf("url: %s.html", slug),
  
367
		fmt.Sprintf("date: %s", t.Format("2006-01-02T15:04:05-07:00")),
  
368
		"type: post",
  
369
		"draft: true",
  
370
		"---",
  
371
		"",
  
372
		"Content...",
  
373
	}
  
374
  
  
375
	f, err := os.Create(path.Join(projectRoot, "content", filename))
  
376
	if err != nil {
  
377
		log.Fatal(err)
  
378
	}
  
379
	defer f.Close()
  
380
  
  
381
	for _, line := range lines {
  
382
		_, err := f.WriteString(line + "\n")
  
383
		if err != nil {
  
384
			log.Fatal(err)
  
385
		}
  
386
	}
  
387
  
  
388
	log.Printf("Page `%s` created\n", filename)
  
389
}
  
390
  
357
func main() {
391
func main() {
358
	projectRoot := os.Getenv("PROJECT_ROOT")
392
	projectRoot := os.Getenv("PROJECT_ROOT")
359
	if projectRoot == "" {
393
	if projectRoot == "" {
...
361
	}
395
	}
362
  
396
  
363
	var args struct {
397
	var args struct {
364
		Init   bool `arg:"-i,--init" help:"initialize new project"`
398
		Init   bool   `arg:"-i,--init" help:"initialize new project"`
365
		Build  bool `arg:"-b,--build" help:"build the website"`
399
		Build  bool   `arg:"-b,--build" help:"build the website"`
366
		Server bool `arg:"-s,--server" help:"simple embedded HTTP server"`
400
		Server bool   `arg:"-s,--server" help:"simple embedded HTTP server"`
  
401
		New    bool   `arg:"-n,--new" help:"create new page"`
  
402
		Title  string `arg:"positional"`
367
	}
403
	}
368
  
404
  
369
	arg.MustParse(&args)
405
	arg.MustParse(&args)
370
  
406
  
371
	if !args.Init && !args.Build && !args.Server {
407
	if !args.Init && !args.Build && !args.Server && !args.New {
372
		fmt.Println("No arguments provided. Try using `jbmafp --help`")
408
		fmt.Println("No arguments provided. Try using `jbmafp --help`")
373
		os.Exit(0)
409
		os.Exit(0)
374
	}
410
	}
...
383
  
419
  
384
	if args.Server {
420
	if args.Server {
385
		simpleServer(projectRoot)
421
		simpleServer(projectRoot)
  
422
	}
  
423
  
  
424
	if args.New {
  
425
		if len(args.Title) == 0 {
  
426
			fmt.Println("You must provide a title for the new page")
  
427
			os.Exit(1)
  
428
		}
  
429
		newPage(projectRoot, args.Title)
386
	}
430
	}
387
}
431
}
diff --git a/shell.nix b/shell.nix
  
1
{ pkgs ? import <nixpkgs> {} }:
  
2
  pkgs.mkShell {
  
3
    nativeBuildInputs = with pkgs.buildPackages; [
  
4
      go
  
5
    ];
  
6
}