Switched to a .ssgod.json file to match other tools and to remove dependencies. Started using go:embed for the init.

master
Zed A. Shaw 3 days ago
parent 0a386e03cf
commit 5fab257230
  1. 1
      .gitignore
  2. 26
      README.md
  3. 31
      config/loader.go
  4. 7
      example/.ssgod.json
  5. 6
      example/ssgod.toml
  6. 1
      go.mod
  7. 2
      go.sum
  8. 20
      main.go

1
.gitignore vendored

@ -27,3 +27,4 @@ coverage/*
.venv
*.gz
public
ssgod

@ -8,6 +8,9 @@ SSG is a Static Site Generator that is only a Static Site Generator. No resumes
## Current Features
> __WARNING__: I changed the file format to .json to reduce dependencies and also made it a hidden
> `.ssgod.json` file to match other tools.
* Simply converts dirs to other dir.
* Default Go templates, and markdown.
* Watches the dir and reruns the build when there's a change.
@ -38,7 +41,7 @@ go tool ssgod
## Usage
These instructions assume you're running the tool using `go tool`. If not then, I mean, you're a
smart person right? Create a default `ssgod.toml` config:
smart person right? Create a default `.ssgod.json` config:
```shell
go tool ssgod init
@ -47,7 +50,7 @@ go tool ssgod init
You can change the config file name with the `--config` option:
```shell
go tool ssgod --config mysite.toml init
go tool ssgod --config mysite.json init
```
This option is available for all commands, so if you want to init a different config file then do as
@ -78,15 +81,16 @@ to prevent running the render too often.
> into `static/` so `ssgod` can faithfully recreate your `public/`.
If you want to have `ssgod` sync a `static` directory then you have to uncomment a line in the
`ssgod.toml` file to enable `sync_dir` as an option:
```toml
views = "pages"
layout = "pages/layouts/main.html"
target = "public"
watch_delay = "500ms"
# comment this out to sync static
sync_dir = "static"
`.ssgod.json` file to enable `sync_dir` as an option:
```json
{
"views": "pages",
"layout": "pages/layouts/main.html",
"target": "public",
"watch_delay": "500ms",
"sync_dir": "static"
}
```
In this example I've removed the comment. Once you do that `ssgod` will then __remove your

@ -2,29 +2,28 @@ package config
import (
"log"
"github.com/BurntSushi/toml"
"encoding/json"
"os"
)
type config struct {
Views string `toml:"views"`
Layout string `toml:"layout"`
Target string `toml:"target"`
WatchDelay string `toml:"watch_delay"`
SyncDir string `toml:"sync_dir"`
Views string `json:"views"`
Layout string `json:"layout"`
Target string `json:"target"`
WatchDelay string `json:"watch_delay"`
SyncDir string `json:"sync_dir"`
}
var Settings config
func Load(path string) {
metadata, err := toml.DecodeFile(path, &Settings)
config, err := os.ReadFile(path)
if err != nil {
log.Fatalf("invalid config path: %s", path)
}
if err != nil {
log.Fatalf("error loading config.toml: %v", err)
}
bad_keys := metadata.Undecoded()
if len(bad_keys) > 0 {
log.Fatalf("unknown configuration keys: %v", bad_keys);
}
err = json.Unmarshal(config, &Settings)
if err != nil {
log.Fatalf("json format error:", err)
}
}

@ -0,0 +1,7 @@
{
"views": "pages",
"layout": "pages/layouts/main.html",
"target": "public",
"watch_delay": "500ms",
"sync_dir": "static"
}

@ -1,6 +0,0 @@
views = "pages"
layout = "pages/layouts/main.html"
target = "public"
watch_delay = "500ms"
# comment this out, WARNING this deletes target!
# sync_dir = "static"

@ -3,7 +3,6 @@ module lcthw.dev/go/ssgod
go 1.24.2
require (
github.com/BurntSushi/toml v1.5.0
github.com/fsnotify/fsnotify v1.9.0
github.com/stretchr/testify v1.10.0
github.com/yuin/goldmark v1.7.13

@ -1,5 +1,3 @@
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=

@ -15,16 +15,11 @@ import (
"strings"
"text/template"
"time"
_ "embed"
)
var DEFAULT_CONFIG = `
views = "pages"
layout = "pages/layouts/main.html"
target = "public"
watch_delay = "500ms"
# comment this out, WARNING this deletes target!
# sync_dir = "static"
`
//go:embed example/.ssgod.json
var DEFAULT_CONFIG string
func Fatal(err error, format string, v ...any) {
err_format := fmt.Sprintf("ERROR: %v; %s", err, format)
@ -66,7 +61,7 @@ func RenderTemplate(out io.Writer, embed string, variables any) error {
func RenderMarkdown(path string, target_path string, page_id string) error {
log.Printf("MARKDOWN: %s -> %s", path, target_path)
out, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
out, err := os.Create(target_path)
defer out.Close()
if err != nil {
@ -94,7 +89,7 @@ func RenderMarkdown(path string, target_path string, page_id string) error {
func RenderHTML(source_path string, target_path string, page_id string) error {
log.Printf("RENDER: %s -> %s", source_path, target_path)
out, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
out, err := os.Create(target_path)
defer out.Close()
html_content, err := os.ReadFile(source_path)
@ -328,13 +323,14 @@ func InitConfig(config_file string) {
_, err := os.Stat(config_file)
if os.IsNotExist(err) {
out, err := os.OpenFile(config_file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
out, err := os.Create(config_file)
if err != nil {
log.Fatalf("error opening %s", config_file)
}
defer out.Close()
out.WriteString(DEFAULT_CONFIG)
log.Println("new config written to:", config_file)
} else {
log.Fatalf("there's already a %s file here", config_file)
}
@ -343,7 +339,7 @@ func InitConfig(config_file string) {
func main() {
var config_file string
flag.StringVar(&config_file, "config", "ssgod.toml", ".toml config file to use")
flag.StringVar(&config_file, "config", ".ssgod.json", ".json config file to use")
flag.Parse()
command := flag.Arg(0)

Loading…
Cancel
Save