diff --git a/README.md b/README.md index 264841c..e482680 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,23 @@ # Blogo [![Go Report Card](https://goreportcard.com/badge/github.com/arnaucube/blogo)](https://goreportcard.com/report/github.com/arnaucube/blogo) Static blog generator, templating engine from markdown and html templates -![blogo](https://raw.githubusercontent.com/arnaucube/blogo/master/blogo.png "blogo") +![blogo](https://raw.githubusercontent.com/arnaucube/blogo/master/blogo-diagram.png "blogo-diagram") + +## Usage +``` +Usage of blogo: + -d dev mode + -p port (only for dev mode) (default "8080") +``` + +So, for local usage: +``` +blogo -d +``` +Which will re-generate the html files each time that a input file is modified, and will serve the html generated site at `http://127.0.0.1:8080`. + +For a single use, you can simply use `blogo` command, which will generate the html files without serving them. -## Use A complete usage example can be found in this repo: https://github.com/arnaucube/blogoExample ### Config example @@ -20,7 +34,7 @@ A complete usage example can be found in this repo: https://github.com/arnaucube "md": "article0.md", "metaimg": "img/article0-img.png", "metadescr": "description of the article 0" - }, + } ], "copyRaw": [ "css", diff --git a/bin/blogo-amd64-darwin b/bin/blogo-amd64-darwin new file mode 100755 index 0000000..21fb8ac Binary files /dev/null and b/bin/blogo-amd64-darwin differ diff --git a/bin/blogo-amd64-linux b/bin/blogo-amd64-linux new file mode 100755 index 0000000..e3b7ef5 Binary files /dev/null and b/bin/blogo-amd64-linux differ diff --git a/bin/blogo-amd64.exe b/bin/blogo-amd64.exe new file mode 100755 index 0000000..b408cca Binary files /dev/null and b/bin/blogo-amd64.exe differ diff --git a/blogo b/blogo deleted file mode 100755 index 894c430..0000000 Binary files a/blogo and /dev/null differ diff --git a/blogo-diagram.png b/blogo-diagram.png new file mode 100644 index 0000000..d6a64d9 Binary files /dev/null and b/blogo-diagram.png differ diff --git a/compile.sh b/compile.sh new file mode 100755 index 0000000..2d876ef --- /dev/null +++ b/compile.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +mkdir -p bin + +echo "building linux binaries" +GOOS=linux GOARCH=amd64 go build -o bin/blogo-amd64-linux *.go + +echo "building windows binaries" +GOOS=windows GOARCH=amd64 go build -o bin/blogo-amd64.exe *.go + +echo "building macOS binaries" +GOOS=darwin GOARCH=amd64 go build -o bin/blogo-amd64-darwin *.go diff --git a/files.go b/files.go index 23fd5b5..8e1617f 100644 --- a/files.go +++ b/files.go @@ -1,11 +1,16 @@ package main import ( + "fmt" "io/ioutil" + "log" + "os" "os/exec" + "path/filepath" "strings" "github.com/fatih/color" + "github.com/fsnotify/fsnotify" ) func readFile(path string) string { @@ -43,3 +48,65 @@ func copyRaw(original string, destination string) { _, err := exec.Command("cp", "-rf", original, destination).Output() check(err) } + +var watcherInputFiles, watcherPublic *fsnotify.Watcher + +func watch(dir string) { + var err error + var watcher *fsnotify.Watcher + if dir == "./blogo-input" { + watcherInputFiles, err = fsnotify.NewWatcher() + if err != nil { + log.Fatal(err) + } + watcher = watcherInputFiles + } else { + watcherPublic, err = fsnotify.NewWatcher() + if err != nil { + log.Fatal(err) + } + watcher = watcherPublic + } + + defer watcher.Close() + + if dir == "./blogo-input" { + if err := filepath.Walk(dir, watchInputFilesDir); err != nil { + log.Fatal(err) + } + } else { + if err := filepath.Walk(dir, watchPublicDir); err != nil { + log.Fatal(err) + } + } + + for { + select { + case event := <-watcher.Events: + fmt.Printf("file system event: %#v\n", event) + if dir == "./blogo-input" { + generateHTML() + } + case err := <-watcher.Errors: + log.Fatal("file system watcher error:", err) + } + } +} + +// watchInputFilesDir gets run as a walk func, searching for directories to add watchers to +func watchInputFilesDir(path string, fi os.FileInfo, err error) error { + if fi.Mode().IsDir() { + return watcherInputFiles.Add(path) + } + + return nil +} + +// watchPublicDir gets run as a walk func, searching for directories to add watchers to +func watchPublicDir(path string, fi os.FileInfo, err error) error { + if fi.Mode().IsDir() { + return watcherPublic.Add(path) + } + + return nil +} diff --git a/go.mod b/go.mod index 5a7611e..a5cce01 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.12 require ( github.com/fatih/color v1.9.0 + github.com/fsnotify/fsnotify v1.5.4 github.com/gomarkdown/markdown v0.0.0-20210514010506-3b9f47219fe7 ) diff --git a/go.sum b/go.sum index e42a69b..559fe83 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= +github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= +github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/gomarkdown/markdown v0.0.0-20210514010506-3b9f47219fe7 h1:oKYOfNR7Hp6XpZ4JqolL5u642Js5Z0n7psPVl+S5heo= github.com/gomarkdown/markdown v0.0.0-20210514010506-3b9f47219fe7/go.mod h1:aii0r/K0ZnHv7G0KF7xy1v0A7s2Ljrb5byB7MO5p6TU= github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= @@ -9,5 +11,6 @@ github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGe github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= golang.org/dl v0.0.0-20190829154251-82a15e2f2ead/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index 331b859..7794dba 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,10 @@ package main import ( + "flag" "fmt" + "log" + "net/http" "os" "strings" @@ -9,17 +12,39 @@ import ( "github.com/gomarkdown/markdown/parser" ) -const version = "v0_20210810" +const version = "v0_20220501" const directory = "blogo-input" const outputDir = "public" func main() { + devMode := flag.Bool("d", false, "dev mode") + port := flag.String("p", "8080", "port (only for dev mode)") + flag.Parse() + fmt.Println("Blogo version:", version) + _ = os.Mkdir(outputDir, os.ModePerm) + + fmt.Println("devMode:", *devMode) + generateHTML() + if !*devMode { + return + } + + go watch("./blogo-input") + // TODO public not watched, until a way to force browser refresh + // go watch("./public") + + // serve files + fs := http.FileServer(http.Dir("public")) + fmt.Printf("Blog being served in: \n http://127.0.0.1:%s\n http://localhost:%s\n", + *port, *port) + log.Fatal(http.ListenAndServe(":"+*port, fs)) +} + +func generateHTML() { readConfig(directory + "/blogo.json") fmt.Println(config) - _ = os.Mkdir(outputDir, os.ModePerm) - mdExtensions := parser.NoIntraEmphasis | parser.Tables | parser.FencedCode | parser.Autolink | parser.Strikethrough | parser.SpaceHeadings | parser.HeadingIDs | parser.BackslashLineBreak | parser.DefinitionLists