summaryrefslogtreecommitdiff
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
parentf537b8194d0cbc6197fb77c7f20663922c836a28 (diff)
downloadjbmafp-0d3c5134b8698359320ad0456dc8180c1857b106.tar.gz
Added --new command that creates new page
-rw-r--r--go.mod2
-rw-r--r--go.sum4
-rw-r--r--main.go52
-rw-r--r--shell.nix6
4 files changed, 60 insertions, 4 deletions
diff --git a/go.mod b/go.mod
index e5b9f5d..c5dba15 100644
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,7 @@ go 1.20
require (
github.com/DavidBelicza/TextRank/v2 v2.1.3
github.com/alexflint/go-arg v1.4.3
+ github.com/gosimple/slug v1.13.1
github.com/microcosm-cc/bluemonday v1.0.24
github.com/otiai10/copy v1.12.0
github.com/tdewolff/minify/v2 v2.12.7
@@ -20,6 +21,7 @@ require (
github.com/aymerick/douceur v0.2.0 // indirect
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
+ github.com/gosimple/unidecode v1.0.1 // indirect
github.com/tdewolff/parse/v2 v2.6.6 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
diff --git a/go.sum b/go.sum
index 0fec4c2..8756439 100644
--- a/go.sum
+++ b/go.sum
@@ -22,6 +22,10 @@ github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+m
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
+github.com/gosimple/slug v1.13.1 h1:bQ+kpX9Qa6tHRaK+fZR0A0M2Kd7Pa5eHPPsb1JpHD+Q=
+github.com/gosimple/slug v1.13.1/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
+github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
+github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
github.com/microcosm-cc/bluemonday v1.0.24 h1:NGQoPtwGVcbGkKfvyYk1yRqknzBuoMiUrO6R7uFTPlw=
github.com/microcosm-cc/bluemonday v1.0.24/go.mod h1:ArQySAMps0790cHSkdPEJ7bGkF2VePWH773hsJNSHf8=
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)
+ }
}
diff --git a/shell.nix b/shell.nix
new file mode 100644
index 0000000..18d6c87
--- /dev/null
+++ b/shell.nix
@@ -0,0 +1,6 @@
+{ pkgs ? import <nixpkgs> {} }:
+ pkgs.mkShell {
+ nativeBuildInputs = with pkgs.buildPackages; [
+ go
+ ];
+}