Add Makefile

Files are now only built when their source changed.
This commit is contained in:
ekzyis 2023-08-18 02:07:16 +02:00
parent e765141a82
commit 1d3d294b05
10 changed files with 96 additions and 70 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
html/pages/blog
public/**/*.html
hot-reload
renderer

27
Makefile Normal file
View File

@ -0,0 +1,27 @@
.PHONY: build run all
MARKDOWN=$(wildcard blog/*.md)
TARGETS= \
$(patsubst blog/%,public/blog/%,$(wildcard blog/*.html)) \
$(patsubst blog/%,public/blog/%,$(MARKDOWN:.md=.html)) \
$(patsubst html/pages/%,public/%,$(wildcard html/pages/*.html))
all: build run
build: renderer
run: $(TARGETS)
renderer: *.go
go build -o renderer .
public/blog/index.html: blog/index.html renderer
./renderer -src $< > html/pages/$<
./renderer -src html/pages/$< > $@
public/blog/%.html: blog/%.md renderer
./renderer -src $< > html/pages/$(<:.md=.html)
./renderer -src html/pages/$(<:.md=.html) > $@
public/%.html: html/pages/%.html renderer
./renderer -src $< > $@

View File

@ -1,33 +0,0 @@
package main
import (
"flag"
)
var (
dev bool
BlogSrcDir = "blog/"
BlogDstDir = "html/pages/blog/"
HtmlSrcDirs = []string{"html/pages/", "html/pages/blog/"}
HtmlTargetDirs = []string{"public/", "public/blog/"}
)
func init() {
flag.BoolVar(&dev, "dev", false, "Specify if files should be built for development mode")
flag.Parse()
}
func main() {
posts := GetPosts(BlogSrcDir)
for _, post := range *posts {
post.Render(BlogDstDir)
}
RenderBlogIndex(BlogSrcDir, BlogDstDir, posts)
// Go does not support ** globstar ...
// https://github.com/golang/go/issues/11862
for i, srcDir := range HtmlSrcDirs {
for _, source := range *GetHtmlSources(srcDir) {
source.Render(HtmlTargetDirs[i])
}
}
}

View File

@ -2,7 +2,7 @@
set -e
go run .
ENV=production make -B
rsync -avh public/ ekzyis.com:/var/www/ekzyis --delete --dry-run
echo

3
go.mod
View File

@ -1,4 +1,4 @@
module ekzyis.com
module renderer
go 1.20
@ -8,6 +8,7 @@ require (
github.com/PuerkitoBio/goquery v1.8.1 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12 // indirect
github.com/namsral/flag v1.7.4-pre // indirect
github.com/tdewolff/parse/v2 v2.6.7 // indirect
golang.org/x/net v0.14.0 // indirect
)

2
go.sum
View File

@ -5,6 +5,8 @@ github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsVi
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12 h1:uK3X/2mt4tbSGoHvbLBHUny7CKiuwUip3MArtukol4E=
github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/namsral/flag v1.7.4-pre h1:b2ScHhoCUkbsq0d2C15Mv+VU8bl8hAXV8arnWiOHNZs=
github.com/namsral/flag v1.7.4-pre/go.mod h1:OXldTctbM6SWH1K899kPZcf65KxJiD7MsceFUpB5yDo=
github.com/tdewolff/minify/v2 v2.12.8 h1:Q2BqOTmlMjoutkuD/OPCnJUpIqrzT3nRPkw+q+KpXS0=
github.com/tdewolff/minify/v2 v2.12.8/go.mod h1:YRgk7CC21LZnbuke2fmYnCTq+zhCgpb0yJACOTUNJ1E=
github.com/tdewolff/parse/v2 v2.6.7 h1:WrFllrqmzAcrKHzoYgMupqgUBIfBVOb0yscFzDf8bBg=

21
html.go
View File

@ -16,7 +16,6 @@ import (
var (
m *minify.M
BuildDate string
Env string
t = template.Must(template.ParseGlob("html/template/*.html"))
)
@ -24,10 +23,6 @@ func init() {
m = minify.New()
m.AddFunc("text/html", html.Minify)
BuildDate = time.Now().In(time.UTC).Format("2006-01-02 15:04:05.000000000 -0700")
Env = "production"
if dev {
Env = "development"
}
}
type HtmlSource struct {
@ -78,8 +73,7 @@ func GetVersion() string {
return out.String()
}
func (source *HtmlSource) Render(destDir string) {
destPath := destDir + filepath.Base(source.FsPath)
func (source *HtmlSource) Render() {
args := map[string]any{
"BuildDate": BuildDate,
"Env": Env,
@ -88,18 +82,13 @@ func (source *HtmlSource) Render(destDir string) {
"Content": string(source.Content),
"Version": GetVersion(),
}
ExecuteTemplate(destPath, args)
ExecuteTemplate(args)
}
func ExecuteTemplate(destPath string, args map[string]any) {
file, err := os.Create(destPath)
if err != nil {
panic(err)
}
defer file.Close()
mw := m.Writer("text/html", file)
func ExecuteTemplate(args map[string]any) {
mw := m.Writer("text/html", os.Stdout)
defer mw.Close()
err = t.ExecuteTemplate(mw, "layout.html", args)
err := t.ExecuteTemplate(mw, "layout.html", args)
if err != nil {
panic(err)
}

View File

@ -138,13 +138,7 @@ func GetPosts(srcDir string) *[]MarkdownPost {
return &posts
}
func (post *MarkdownPost) Render(destDir string) {
destPath := strings.ReplaceAll(destDir+filepath.Base(post.FsPath), ".md", ".html")
f, err := os.Create(destPath)
if err != nil {
panic(err)
}
defer f.Close()
func (post *MarkdownPost) Render() {
opts := html.RendererOptions{Flags: MarkdownToHtmlFlags}
renderer := html.NewRenderer(opts)
html := markdown.ToHTML(post.Content, nil, renderer)
@ -154,28 +148,23 @@ func (post *MarkdownPost) Render(destDir string) {
if err != nil {
panic(err)
}
t.Execute(f, *post)
t.Execute(os.Stdout, *post)
}
func RenderBlogIndex(srcDir string, destDir string, posts *[]MarkdownPost) {
func RenderBlogIndex(srcDir string) {
posts := GetPosts(srcDir)
srcPath := srcDir + "index.html"
destPath := destDir + "index.html"
f, err := os.Create(destPath)
if err != nil {
panic(err)
}
defer f.Close()
t := template.New(filepath.Base(srcPath))
t = t.Funcs(template.FuncMap{
"ToHref": func(fsPath string) string {
return "/" + strings.ReplaceAll(fsPath, ".md", ".html")
},
})
t, err = t.ParseFiles(srcPath)
t, err := t.ParseFiles(srcPath)
if err != nil {
panic(err)
}
err = t.Execute(f, map[string][]MarkdownPost{"Posts": *posts})
err = t.Execute(os.Stdout, map[string][]MarkdownPost{"Posts": *posts})
if err != nil {
panic(err)
}

50
renderer.go Normal file
View File

@ -0,0 +1,50 @@
package main
import (
"log"
"path"
"github.com/namsral/flag"
)
var (
Source string
Env string
)
func init() {
flag.StringVar(&Source, "src", "", "Source file")
flag.StringVar(&Env, "env", "development", "Specify for which environment files should be built")
flag.Parse()
}
func RenderExtension(path string, ext string) {
switch ext {
case ".md":
NewMarkdownPost(path).Render()
case ".html":
NewHtmlSource(path).Render()
default:
log.Fatalf("unknown extension: %s", ext)
}
}
func main() {
if Source == "" {
log.Fatal("no source given")
}
if Source == "blog/index.html" {
RenderBlogIndex("blog/")
return
}
if Source != "" {
ext := path.Ext(Source)
if ext == "" {
log.Fatal("file has no extension")
}
RenderExtension(Source, ext)
return
}
}

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash
function sync() {
go run . --dev
ENV=development make run $@
date +%s.%N > public/hot-reload
rsync -avh public/ dev.ekzyis.com:/var/www/dev.ekzyis --delete
}
@ -11,7 +11,7 @@ function cleanup() {
}
trap cleanup EXIT
sync
sync -B
while inotifywait -r -e modify html/ blog/ *.go; do
sync
done