2024-02-09 16:04:40 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-09 20:38:16 +01:00
|
|
|
"bufio"
|
2024-02-09 20:55:36 +01:00
|
|
|
"bytes"
|
2024-02-09 20:38:16 +01:00
|
|
|
"errors"
|
2024-02-09 16:04:40 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2024-02-09 17:50:52 +01:00
|
|
|
"io/fs"
|
2024-02-09 16:04:40 +01:00
|
|
|
"os"
|
2024-02-09 17:50:52 +01:00
|
|
|
"path/filepath"
|
2024-02-09 20:38:16 +01:00
|
|
|
"strings"
|
|
|
|
|
2024-02-09 22:02:59 +01:00
|
|
|
"github.com/niklasfasching/go-org/org"
|
2024-02-09 20:38:16 +01:00
|
|
|
"gopkg.in/yaml.v3"
|
2024-02-09 16:04:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-09 17:50:52 +01:00
|
|
|
// TODO consider using cobra or something else to make cli more declarative
|
|
|
|
// and get a better ux out of the box
|
2024-02-09 16:04:40 +01:00
|
|
|
initCmd := flag.NewFlagSet("init", flag.ExitOnError)
|
|
|
|
newCmd := flag.NewFlagSet("new", flag.ExitOnError)
|
|
|
|
serveCmd := flag.NewFlagSet("serve", flag.ExitOnError)
|
|
|
|
|
|
|
|
if len(os.Args) < 2 {
|
2024-02-09 17:50:52 +01:00
|
|
|
// TODO print usage
|
|
|
|
exit("expected subcommand")
|
2024-02-09 16:04:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch os.Args[1] {
|
|
|
|
case "init":
|
|
|
|
initCmd.Parse(os.Args[2:])
|
|
|
|
// get working directory
|
|
|
|
// default to .
|
|
|
|
// if not exist, create directory
|
|
|
|
// copy over default files
|
|
|
|
fmt.Println("not implemented yet")
|
|
|
|
case "build":
|
2024-02-09 17:50:52 +01:00
|
|
|
build()
|
2024-02-09 16:04:40 +01:00
|
|
|
case "new":
|
|
|
|
newCmd.Parse(os.Args[2:])
|
|
|
|
// prompt for title
|
|
|
|
// slugify
|
|
|
|
// fail if file already exist
|
|
|
|
// create a new .org file with the slug
|
|
|
|
// add front matter and org options
|
|
|
|
fmt.Println("not implemented yet")
|
|
|
|
case "serve":
|
|
|
|
// build
|
|
|
|
// serve target with file server
|
|
|
|
// (later watch and live reload)
|
|
|
|
serveCmd.Parse(os.Args[2:])
|
|
|
|
fmt.Println("not implemented yet")
|
|
|
|
default:
|
2024-02-09 17:50:52 +01:00
|
|
|
// TODO print usage
|
|
|
|
exit("unknown subcommand")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the files in src/ render them and copy the result to target/
|
2024-02-09 20:38:16 +01:00
|
|
|
// TODO move elsewhere ?
|
2024-02-09 17:50:52 +01:00
|
|
|
func build() {
|
2024-02-09 20:38:16 +01:00
|
|
|
const FILE_MODE = 0777
|
|
|
|
|
2024-02-09 17:50:52 +01:00
|
|
|
// fail if no src dir
|
|
|
|
_, err := os.ReadDir("src")
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
exit("missing src/ directory")
|
|
|
|
} else if err != nil {
|
|
|
|
panic("couldn't read src")
|
2024-02-09 16:04:40 +01:00
|
|
|
}
|
2024-02-09 17:50:52 +01:00
|
|
|
|
|
|
|
// clear previous target contents
|
|
|
|
os.RemoveAll("target")
|
|
|
|
os.Mkdir("target", FILE_MODE)
|
|
|
|
|
|
|
|
// render each source file and copy it over to target
|
|
|
|
filepath.WalkDir("src", func(path string, entry fs.DirEntry, err error) error {
|
|
|
|
if entry.IsDir() {
|
2024-02-09 22:09:42 +01:00
|
|
|
subpath, _ := filepath.Rel("src", path)
|
|
|
|
targetSubpath := filepath.Join("target", subpath)
|
2024-02-09 17:50:52 +01:00
|
|
|
os.MkdirAll(targetSubpath, FILE_MODE)
|
|
|
|
} else {
|
2024-02-09 20:38:16 +01:00
|
|
|
|
2024-02-09 22:02:59 +01:00
|
|
|
// FIXME what if non text file?
|
2024-02-09 22:09:42 +01:00
|
|
|
data, targetPath, err := render(path)
|
2024-02-09 20:38:16 +01:00
|
|
|
|
2024-02-09 17:50:52 +01:00
|
|
|
if err != nil {
|
2024-02-09 22:09:42 +01:00
|
|
|
panic(fmt.Sprintf("failed to load %s", targetPath))
|
2024-02-09 17:50:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// write the file contents over to target at the same location
|
2024-02-09 22:09:42 +01:00
|
|
|
err = os.WriteFile(targetPath, []byte(data), FILE_MODE)
|
2024-02-09 17:50:52 +01:00
|
|
|
if err != nil {
|
2024-02-09 22:09:42 +01:00
|
|
|
panic(fmt.Sprintf("failed to load %s", targetPath))
|
2024-02-09 17:50:52 +01:00
|
|
|
}
|
2024-02-09 22:09:42 +01:00
|
|
|
fmt.Printf("wrote %v\n", targetPath)
|
2024-02-09 17:50:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2024-02-09 20:38:16 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO move elsewhere?
|
2024-02-09 22:09:42 +01:00
|
|
|
func render(sourcePath string) (string, string, error) {
|
|
|
|
subpath, _ := filepath.Rel("src", sourcePath)
|
|
|
|
targetPath := filepath.Join("target", subpath)
|
|
|
|
isOrgFile := filepath.Ext(sourcePath) == ".org"
|
|
|
|
if isOrgFile {
|
|
|
|
targetPath = strings.TrimSuffix(targetPath, "org") + "html"
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(sourcePath)
|
2024-02-09 20:38:16 +01:00
|
|
|
if err != nil {
|
2024-02-09 22:09:42 +01:00
|
|
|
return "", "", err
|
2024-02-09 20:38:16 +01:00
|
|
|
}
|
2024-02-09 22:02:59 +01:00
|
|
|
defer file.Close()
|
2024-02-09 20:38:16 +01:00
|
|
|
|
2024-02-09 22:02:59 +01:00
|
|
|
fileContent, _, err := extractFrontMatter(file)
|
|
|
|
if err != nil {
|
2024-02-09 22:09:42 +01:00
|
|
|
return "", "", (fmt.Errorf("error in %s: %s", sourcePath, err))
|
2024-02-09 20:38:16 +01:00
|
|
|
}
|
|
|
|
|
2024-02-09 22:02:59 +01:00
|
|
|
var html string
|
|
|
|
// FIXME this should be renamed to .html
|
|
|
|
// (in general, the render process should be able to instruct a differnt target path)
|
2024-02-09 22:09:42 +01:00
|
|
|
if isOrgFile {
|
|
|
|
doc := org.New().Parse(bytes.NewReader(fileContent), sourcePath)
|
2024-02-09 22:02:59 +01:00
|
|
|
html, err = doc.Write(org.NewHTMLWriter())
|
|
|
|
if err != nil {
|
2024-02-09 22:09:42 +01:00
|
|
|
return "", "", err
|
2024-02-09 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
2024-02-09 20:38:16 +01:00
|
|
|
} else {
|
|
|
|
// TODO render liquid template
|
|
|
|
}
|
|
|
|
|
2024-02-09 22:02:59 +01:00
|
|
|
// TODO if yaml contains layout, pass to parent
|
|
|
|
|
2024-02-09 20:38:16 +01:00
|
|
|
// TODO minify
|
|
|
|
|
2024-02-09 22:09:42 +01:00
|
|
|
return html, targetPath, nil
|
2024-02-09 20:38:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func extractFrontMatter(file *os.File) ([]byte, map[string]interface{}, error) {
|
|
|
|
const FM_SEPARATOR = "---"
|
|
|
|
|
|
|
|
var outContent, yamlContent []byte
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
// if line starts front matter, write lines to yaml until front matter is closed
|
|
|
|
if strings.TrimSpace(line) == FM_SEPARATOR {
|
|
|
|
closed := false
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.TrimSpace(line) == FM_SEPARATOR {
|
|
|
|
closed = true
|
|
|
|
break
|
|
|
|
}
|
2024-02-09 20:55:36 +01:00
|
|
|
yamlContent = append(yamlContent, []byte(line+"\n")...)
|
2024-02-09 20:38:16 +01:00
|
|
|
}
|
|
|
|
if !closed {
|
|
|
|
return nil, nil, errors.New("front matter not closed")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// non front matter/yaml content goes to the output slice
|
2024-02-09 20:55:36 +01:00
|
|
|
outContent = append(outContent, []byte(line+"\n")...)
|
2024-02-09 20:38:16 +01:00
|
|
|
}
|
|
|
|
}
|
2024-02-09 20:55:36 +01:00
|
|
|
// drop the extraneous last line break
|
|
|
|
outContent = bytes.TrimRight(outContent, "\n")
|
2024-02-09 20:38:16 +01:00
|
|
|
|
|
|
|
var frontMatter map[string]interface{}
|
|
|
|
if len(yamlContent) != 0 {
|
|
|
|
err := yaml.Unmarshal([]byte(yamlContent), &frontMatter)
|
|
|
|
if err != nil {
|
2024-02-09 22:09:42 +01:00
|
|
|
return nil, nil, fmt.Errorf("invalid yaml: ", err)
|
2024-02-09 20:38:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outContent, frontMatter, nil
|
2024-02-09 16:04:40 +01:00
|
|
|
}
|
|
|
|
|
2024-02-09 17:50:52 +01:00
|
|
|
func exit(message string) {
|
|
|
|
fmt.Println(message)
|
2024-02-09 16:04:40 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|