Repos / s4g / 1417dbe7a8
commit 1417dbe7a8a9079c110cf7d256b73f1248622076
Author: Nhân <hi@imnhan.com>
Date:   Thu Jul 6 16:35:31 2023 +0700

    embed ArticleMetadata; add Templates field

diff --git a/feed.go b/feed.go
index 72a0069..ed45e21 100644
--- a/feed.go
+++ b/feed.go
@@ -8,7 +8,7 @@
 )
 
 // TODO: Use Article's updated date instead of PostedAt.
-// I need to implement Article.Meta.UpdatedAt first though.
+// I need to implement Article.UpdatedAt first though.
 func generateFeed(site SiteMetadata, posts []Article, path string) []byte {
 	siteAddr := site.Address
 	if !strings.HasSuffix(siteAddr, "/") {
@@ -19,16 +19,16 @@ func generateFeed(site SiteMetadata, posts []Article, path string) []byte {
 		entries = append(entries, &atom.Entry{
 			ID:        siteAddr + p.WebPath,
 			Link:      []atom.Link{{Href: siteAddr + p.WebPath}},
-			Title:     p.Meta.Title,
-			Published: atom.Time(p.Meta.PostedAt),
-			Updated:   atom.Time(p.Meta.PostedAt),
+			Title:     p.Title,
+			Published: atom.Time(p.PostedAt),
+			Updated:   atom.Time(p.PostedAt),
 		})
 	}
 
 	feed := atom.Feed{
 		ID:      siteAddr,
 		Title:   site.Name,
-		Updated: atom.Time(posts[0].Meta.PostedAt),
+		Updated: atom.Time(posts[0].PostedAt),
 		Entry:   entries,
 		Author: &atom.Person{
 			Name:  site.Author.Name,
diff --git a/main.go b/main.go
index 542fd48..b3001a3 100644
--- a/main.go
+++ b/main.go
@@ -37,18 +37,18 @@ func main() {
 
 	// Sort posts, newest first
 	sort.Slice(posts, func(i int, j int) bool {
-		return posts[i].Meta.PostedAt.Compare(posts[j].Meta.PostedAt) > 0
+		return posts[i].PostedAt.Compare(posts[j].PostedAt) > 0
 	})
 
-	startYear := posts[len(posts)-1].Meta.PostedAt.Year()
+	startYear := posts[len(posts)-1].PostedAt.Year()
 
 	fmt.Printf("Found %d posts, %d pages:\n", len(posts), len(pages))
 	for _, a := range posts {
-		fmt.Println(">", a.Path, "-", a.Meta.Title)
+		fmt.Println(">", a.Path, "-", a.Title)
 		a.WriteHtmlFile(&site, pages, startYear)
 	}
 	for _, a := range pages {
-		fmt.Println(">", a.Path, "-", a.Meta.Title)
+		fmt.Println(">", a.Path, "-", a.Title)
 		a.WriteHtmlFile(&site, pages, startYear)
 	}
 
@@ -98,14 +98,15 @@ type Article struct {
 	Path     string
 	WebPath  string
 	DjotBody string
-	Meta     ArticleMetadata
+	ArticleMetadata
 }
 
 type ArticleMetadata struct {
-	Title    string
-	IsPage   bool
-	IsDraft  bool
-	PostedAt time.Time
+	Title     string
+	IsPage    bool
+	IsDraft   bool
+	PostedAt  time.Time
+	Templates []string
 }
 
 func (a *Article) WriteHtmlFile(site *SiteMetadata, pages []Article, startYear int) {
@@ -114,13 +115,8 @@ func (a *Article) WriteHtmlFile(site *SiteMetadata, pages []Article, startYear i
 
 	// Then insert that content into the main template
 	var buf bytes.Buffer
-	tmpl := template.Must(
-		template.ParseFS(
-			a.Fs,
-			"_theme/base.tmpl",
-			"_theme/post.tmpl",
-		),
-	)
+	// TODO: should probably reuse the template object for common cases
+	tmpl := template.Must(template.ParseFS(a.Fs, a.Templates...))
 	err := tmpl.Execute(&buf, struct {
 		Site      *SiteMetadata
 		Content   template.HTML
@@ -133,7 +129,7 @@ func (a *Article) WriteHtmlFile(site *SiteMetadata, pages []Article, startYear i
 	}{
 		Site:      site,
 		Content:   template.HTML(contentHtml),
-		Title:     fmt.Sprintf("%s | %s", a.Meta.Title, site.Name),
+		Title:     fmt.Sprintf("%s | %s", a.Title, site.Name),
 		Post:      a,
 		Pages:     pages,
 		Feed:      site.HomePath + FEED_PATH,
@@ -211,7 +207,9 @@ func findArticles(fsys WritableFS) (posts, pages []Article) {
 		metaText := strings.TrimSpace(parts[1])
 		bodyText := strings.TrimSpace(parts[2])
 
-		var meta ArticleMetadata
+		meta := ArticleMetadata{
+			Templates: []string{"_theme/base.tmpl", "_theme/post.tmpl"},
+		}
 		_, err = toml.Decode(metaText, &meta)
 		if err != nil {
 			fmt.Printf("FIXME: Malformed article metadata in %s: %s", path, err)
@@ -219,13 +217,13 @@ func findArticles(fsys WritableFS) (posts, pages []Article) {
 		}
 
 		article := Article{
-			Fs:       fsys,
-			Path:     path,
-			WebPath:  strings.TrimSuffix(path, DJOT_EXT) + ".html",
-			DjotBody: bodyText,
-			Meta:     meta,
+			Fs:              fsys,
+			Path:            path,
+			WebPath:         strings.TrimSuffix(path, DJOT_EXT) + ".html",
+			DjotBody:        bodyText,
+			ArticleMetadata: meta,
 		}
-		if article.Meta.IsPage {
+		if article.IsPage {
 			pages = append(pages, article)
 		} else {
 			posts = append(posts, article)
diff --git a/www/_theme/home.tmpl b/www/_theme/home.tmpl
index 60e9a4a..aaa8ca8 100644
--- a/www/_theme/home.tmpl
+++ b/www/_theme/home.tmpl
@@ -11,7 +11,7 @@
 <div class="pages">
   <a href="{{.Site.HomePath}}">Home</a>
 {{- range .Pages}}
-  <a href="{{.WebPath}}">{{.Meta.Title}}</a>
+  <a href="{{.WebPath}}">{{.Title}}</a>
 {{- end}}
   <a class="feed-link" href="{{.Feed}}">
     <img src="{{.Site.HomePath}}_theme/feed.svg" alt="Atom Feed" title="Atom Feed">
@@ -24,10 +24,10 @@
 
 <ul>
   {{- range .Posts}}
-  {{- if not .Meta.IsDraft}}
+  {{- if not .IsDraft}}
   <li>
-    {{.Meta.PostedAt.Local.Format "2006-01-02"}} —
-    <a href="{{.WebPath}}">{{.Meta.Title}}</a>
+    {{.PostedAt.Local.Format "2006-01-02"}} —
+    <a href="{{.WebPath}}">{{.Title}}</a>
   </li>
   {{- end}}
   {{- end}}
diff --git a/www/_theme/post.tmpl b/www/_theme/post.tmpl
index 934c934..b6e7dc0 100644
--- a/www/_theme/post.tmpl
+++ b/www/_theme/post.tmpl
@@ -6,14 +6,14 @@
 <nav>
   <a href="{{.Site.HomePath}}">Home</a>
   {{- range .Pages}}
-  <a href="{{$.Site.HomePath}}{{.WebPath}}">{{.Meta.Title}}</a>
+  <a href="{{$.Site.HomePath}}{{.WebPath}}">{{.Title}}</a>
   {{- end}}
 
-  {{- if not .Post.Meta.PostedAt.IsZero}}
+  {{- if not .Post.PostedAt.IsZero}}
   <span class="posted-on">
     Posted on
-    <time datetime="{{.Post.Meta.PostedAt.Local.Format "2006-01-02"}}">
-        {{.Post.Meta.PostedAt.Local.Format "Monday, 02 Jan 2006"}}
+    <time datetime="{{.Post.PostedAt.Local.Format "2006-01-02"}}">
+        {{.Post.PostedAt.Local.Format "Monday, 02 Jan 2006"}}
     </time>
   </span>
   {{- end}}
@@ -22,7 +22,7 @@
 
 <hr>
 
-<h1 class="post-title">{{.Post.Meta.Title}}</h1>
+<h1 class="post-title">{{.Post.Title}}</h1>
 <content>
 {{.Content}}
 </content>