Repos / s4g / a86d3219e6
commit a86d3219e649cbc6e5dee8c5c236a0508566dadf
Author: Nhân <hi@imnhan.com>
Date: Mon Jul 10 20:42:09 2023 +0700
simplify front matter separator
diff --git a/djot/djot.go b/djot/djot.go
index d412c2f..153b645 100644
--- a/djot/djot.go
+++ b/djot/djot.go
@@ -72,8 +72,8 @@ func splitAtDelimiter(data []byte, atEOF bool) (advance int, token []byte, err e
}
// Not thread-safe.
-func ToHtml(input string) (result string) {
- if _, err := service.writer.WriteString(input); err != nil {
+func ToHtml(input []byte) (result []byte) {
+ if _, err := service.writer.Write(input); err != nil {
panic(err)
}
if err := service.writer.WriteByte(delimiter); err != nil {
@@ -82,7 +82,10 @@ func ToHtml(input string) (result string) {
service.writer.Flush()
if !service.scanner.Scan() {
- panic("scanner unexpectedly stopped while converting djot to html: " + input[:50])
+ panic(fmt.Sprintf(
+ "scanner unexpectedly stopped while converting djot to html: %s\n",
+ input[:50],
+ ))
}
- return service.scanner.Text()
+ return service.scanner.Bytes()
}
diff --git a/main.go b/main.go
index 0c0aeb8..80b9632 100644
--- a/main.go
+++ b/main.go
@@ -142,7 +142,7 @@ type Article struct {
Path string
OutputPath string
webPath string
- DjotBody string
+ DjotBody []byte
ArticleMetadata
}
@@ -254,25 +254,24 @@ func findArticles(fsys writablefs.FS) (result []Article) {
return nil
}
- fileContent, err := fs.ReadFile(fsys, path)
+ file, err := fsys.Open(path)
if err != nil {
panic(err)
}
+ defer file.Close()
- parts := strings.SplitN(string(fileContent), "+++", 3)
- if !(len(parts) == 3 && parts[0] == "") {
- fmt.Printf("FIXME: Missing metadata in %s - Skipped.\n", path)
+ metaText, bodyText := SeparateMetadata(file)
+ if len(metaText) == 0 {
+ fmt.Printf("FIXME: Metadata not found in %s\n", path)
return nil
}
- metaText := strings.TrimSpace(parts[1])
- bodyText := strings.TrimSpace(parts[2])
meta := ArticleMetadata{
Templates: []string{"_theme/base.tmpl", "_theme/post.tmpl"},
ShowInFeed: true,
ShowInNav: false,
}
- err = UnmarshalMetadata([]byte(metaText), &meta)
+ err = UnmarshalMetadata(metaText, &meta)
if err != nil {
fmt.Printf("FIXME: Malformed article metadata in %s: %s\n", path, err)
return nil
diff --git a/metadata.go b/metadata.go
index 50f138f..a550ef8 100644
--- a/metadata.go
+++ b/metadata.go
@@ -1,7 +1,10 @@
package main
import (
+ "bufio"
+ "bytes"
"fmt"
+ "io"
"io/fs"
"reflect"
"strings"
@@ -127,3 +130,27 @@ func metaTextToMap(s []byte) map[string]string {
}
return result
}
+
+var frontMatterSep = []byte("---")
+
+func SeparateMetadata(r io.Reader) (metadata []byte, body []byte) {
+ s := bufio.NewScanner(r)
+ readingFrontMatter := true
+ var buffer []byte
+ for s.Scan() {
+ line := bytes.TrimSpace(s.Bytes())
+
+ if readingFrontMatter && bytes.Equal(line, frontMatterSep) {
+ metadata = buffer
+ buffer = body
+ readingFrontMatter = false
+ continue
+ }
+
+ buffer = append(buffer, line...)
+ buffer = append(buffer, '\n')
+ }
+
+ body = buffer
+ return metadata, body
+}
diff --git a/www/about/index.dj b/www/about/index.dj
index a8fe1cf..c040890 100644
--- a/www/about/index.dj
+++ b/www/about/index.dj
@@ -1,8 +1,7 @@
-+++
Title: About
ShowInFeed: false
ShowInNav: true
-+++
+---
## About this site
diff --git a/www/hello/index.dj b/www/hello/index.dj
index 03c4e26..7209c9c 100644
--- a/www/hello/index.dj
+++ b/www/hello/index.dj
@@ -1,6 +1,5 @@
-+++
Title: Hello
PostedAt: 2022-01-02
-+++
+---
Hello world.
diff --git a/www/mfws.dj b/www/mfws.dj
index 0b2dc40..f2aa3ac 100644
--- a/www/mfws.dj
+++ b/www/mfws.dj
@@ -1,7 +1,6 @@
-+++
Title: This is a motherfucking website.
PostedAt: 2023-04-05
-+++
+---
And it's fucking perfect.