Repos / s4g / ca5cd18440
commit ca5cd1844033bd430d242d24a3028d5eace3b684
Author: Nhân <hi@imnhan.com>
Date:   Thu Jun 29 11:33:06 2023 +0700

    use front matter instead

diff --git a/main.go b/main.go
index 8abca0e..ebc5a52 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@
 	"net/http"
 	"os"
 	"path/filepath"
+	"strings"
 
 	"github.com/BurntSushi/toml"
 )
@@ -26,7 +27,11 @@ func main() {
 	meta := readSiteMetadata(fsys)
 	fmt.Println("Found site:", meta)
 
-	findPosts(fsys)
+	articles := findArticles(fsys)
+	fmt.Printf("Found %d articles:\n", len(articles))
+	for _, a := range articles {
+		fmt.Println(">", a.Path, "-", a.Meta.Title)
+	}
 
 	println("Serving local website at http://localhost:" + port)
 	http.Handle("/", http.FileServer(http.FS(fsys)))
@@ -51,19 +56,50 @@ func readSiteMetadata(fsys fs.FS) (sm SiteMetadata) {
 
 type Article struct {
 	Path     string
-	Title    string
 	DjotBody string
+	Meta     ArticleMetadata
+}
+
+type ArticleMetadata struct {
+	Title  string
+	IsPage bool
 }
-type Post Article
-type Page Article
 
-func findPosts(fsys fs.FS) (posts []Post) {
-	var paths []string
+func findArticles(fsys fs.FS) (articles []Article) {
+
 	fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
-		if d.Name() == "post.toml" {
-			paths = append(paths, filepath.Dir(path))
+		if d.IsDir() || !strings.HasSuffix(d.Name(), ".dj") {
+			return nil
+		}
+
+		fileContent, err := fs.ReadFile(fsys, path)
+		if err != nil {
+			panic(err)
+		}
+
+		parts := strings.SplitN(string(fileContent), "+++", 3)
+		if !(len(parts) == 3 && parts[0] == "") {
+			fmt.Printf("FIXME: Missing metadata in %s - Skipped.\n", path)
+			return nil
+		}
+		metaText := strings.TrimSpace(parts[1])
+		bodyText := strings.TrimSpace(parts[2])
+
+		var meta ArticleMetadata
+		_, err = toml.Decode(metaText, &meta)
+		if err != nil {
+			fmt.Printf("FIXME: Malformed article metadata in %s: %s", path, err)
+			return nil
+		}
+
+		article := Article{
+			Path:     path,
+			DjotBody: bodyText,
+			Meta:     meta,
 		}
+		articles = append(articles, article)
+		fmt.Printf("Found article %s - %s\n", article.Path, article.Meta.Title)
 		return nil
 	})
-	return posts
+	return articles
 }
diff --git a/www/about/index.dj b/www/about/index.dj
index 35ff8b8..e3f9cfe 100644
--- a/www/about/index.dj
+++ b/www/about/index.dj
@@ -1,3 +1,7 @@
++++
+Title = "About"
++++
+
 ## About this site
 
 It's a website.
diff --git a/www/about/page.toml b/www/about/page.toml
deleted file mode 100644
index 8b00bc3..0000000
--- a/www/about/page.toml
+++ /dev/null
@@ -1 +0,0 @@
-title = "About"
diff --git a/www/hello/index.dj b/www/hello/index.dj
index 18249f3..ff29966 100644
--- a/www/hello/index.dj
+++ b/www/hello/index.dj
@@ -1 +1,6 @@
++++
+Title = "Hello"
+IsPage = false
++++
+
 Hello world.
diff --git a/www/hello/post.toml b/www/hello/post.toml
deleted file mode 100644
index 5917a39..0000000
--- a/www/hello/post.toml
+++ /dev/null
@@ -1 +0,0 @@
-title = "Good morning"