arnaucube
arnaucube/blogoPublic Self-hosted repository
Add dev mode

Add dev mode: re-generate the html files each time that a input file is
modified, and serve them through an http server.
arnaucube root@arnaucube.com
Sun, 01 May 2022 20:02:32 +0200
commit

c95119d53756a9df33edacc8681b13a570bacaae

parent

8b015c43bd16612a16ef5daefcc332929ed8d002

M README.md → 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") -## Use +## 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. + A complete usage example can be found in this repo: https://github.com/arnaucube/blogoExample ### Config example

@@ -20,7 +34,7 @@ "thumb": "article0_thumb.md",

"md": "article0.md", "metaimg": "img/article0-img.png", "metadescr": "description of the article 0" - }, + } ], "copyRaw": [ "css",
D blogo

          
A 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
M files.go → 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 @@ color.Green(original + " --> to --> " + destination)

_, 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 +}
M go.mod → 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 )
M go.sum → 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/qkUbQO4ZiBa2brKq5r0l8TGeM=

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=
M main.go → main.go

@@ -1,7 +1,10 @@

package main import ( + "flag" "fmt" + "log" + "net/http" "os" "strings"

@@ -9,16 +12,38 @@ "github.com/gomarkdown/markdown"

"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 |