Repos / s4g / 8dc9004381
commit 8dc90043816329f6946162fc480524e46ab26ee8
Author: Nhân <hi@imnhan.com>
Date:   Wed Jul 12 20:41:54 2023 +0700

    prepend web root to Article.WebPath
    
    This exposed a bunch of path-related bugs too, which are now fixed.

diff --git a/docs/_theme/includes.tmpl b/docs/_theme/includes.tmpl
index 19b5122..cdd6ec2 100644
--- a/docs/_theme/includes.tmpl
+++ b/docs/_theme/includes.tmpl
@@ -3,7 +3,7 @@
 <nav>
   <a href="{{.Site.Root}}">Home</a>
   {{- range .ArticlesInNav}}
-  <a href="{{$.Site.Root}}{{.WebPath}}">{{.Title}}</a>
+  <a href="{{.WebPath}}">{{.Title}}</a>
   {{- end}}
 
   {{- if not .Post.PostedAt.IsZero}}
diff --git a/docs/feed.xml b/docs/feed.xml
index 4852045..4947aba 100644
--- a/docs/feed.xml
+++ b/docs/feed.xml
@@ -10,15 +10,15 @@
   </author>
   <entry>
     <title>This is a motherfucking website.</title>
-    <id>https://coolzone.example.com/mfws.html</id>
-    <link href="https://coolzone.example.com/mfws.html"></link>
+    <id>https://coolzone.example.com/webmaker2000/mfws.html</id>
+    <link href="https://coolzone.example.com/webmaker2000/mfws.html"></link>
     <published>2023-04-05T00:00:00+07:00</published>
     <updated>2023-04-05T00:00:00+07:00</updated>
   </entry>
   <entry>
     <title>I&#39;m Going To Scale My Foot Up Your Ass</title>
-    <id>https://coolzone.example.com/scale/</id>
-    <link href="https://coolzone.example.com/scale/"></link>
+    <id>https://coolzone.example.com/webmaker2000/scale/</id>
+    <link href="https://coolzone.example.com/webmaker2000/scale/"></link>
     <published>2008-04-24T00:00:00+07:00</published>
     <updated>2008-04-24T00:00:00+07:00</updated>
   </entry>
diff --git a/docs/index.html b/docs/index.html
index f7eb7b2..8144f02 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -20,7 +20,7 @@ <h1 class="site-title">CoolZone</h1>
 
 <div class="pages">
   <a href="/webmaker2000/">Home</a>
-  <a href="about/">About</a>
+  <a href="/webmaker2000/about/">About</a>
   <a class="feed-link" href="/webmaker2000/feed.xml">
     <img src="/webmaker2000/_theme/feed.svg" alt="Atom Feed" title="Atom Feed">
   </a>
@@ -35,11 +35,11 @@ <h1 class="site-title">CoolZone</h1>
 <ul>
   <li>
     2023-04-05 —
-    <a href="mfws.html">This is a motherfucking website.</a>
+    <a href="/webmaker2000/mfws.html">This is a motherfucking website.</a>
   </li>
   <li>
     2008-04-24 —
-    <a href="scale/">I&#39;m Going To Scale My Foot Up Your Ass</a>
+    <a href="/webmaker2000/scale/">I&#39;m Going To Scale My Foot Up Your Ass</a>
   </li>
 </ul>
 
diff --git a/feed.go b/feed.go
index 4cee63a..7ecd3dd 100644
--- a/feed.go
+++ b/feed.go
@@ -16,9 +16,11 @@ func generateFeed(site SiteMetadata, posts []Article, path string) []byte {
 	}
 	var entries []*atom.Entry
 	for _, p := range posts {
+		// trim WebPath's leading slash because siteAddr already has one
+		link := siteAddr + p.WebPath[1:]
 		entries = append(entries, &atom.Entry{
-			ID:        siteAddr + p.WebPath(),
-			Link:      []atom.Link{{Href: siteAddr + p.WebPath()}},
+			ID:        link,
+			Link:      []atom.Link{{Href: link}},
 			Title:     p.Title,
 			Published: atom.Time(p.PostedAt),
 			Updated:   atom.Time(p.PostedAt),
diff --git a/main.go b/main.go
index 4f4d081..1050c09 100644
--- a/main.go
+++ b/main.go
@@ -122,7 +122,7 @@ func regenerate(fsys writablefs.FS) (site SiteMetadata) {
 	defer timer("Took %s")()
 
 	site = ReadSiteMetadata(fsys)
-	articles := findArticles(fsys)
+	articles := findArticles(fsys, site)
 
 	if len(articles) == 0 {
 		fmt.Println("No articles found.")
@@ -183,27 +183,23 @@ type Article struct {
 	OutputPath string
 	DjotBody   []byte
 	ArticleMetadata
-	webPath       string
+	WebPath       string
 	templatePaths []string
 }
 
-func (a *Article) WebPath() string {
-	if a.webPath != "" {
-		return a.webPath
-	}
-	path := a.OutputPath
-	if strings.HasSuffix(path, "/index.html") {
-		path = strings.TrimSuffix(path, "index.html")
+func (a *Article) ComputeWebPath(root string) {
+	webPath := root + a.OutputPath
+	if strings.HasSuffix(webPath, "/index.html") {
+		webPath = strings.TrimSuffix(webPath, "index.html")
 	}
 
-	parts := strings.Split(path, "/")
+	parts := strings.Split(webPath, "/")
 	escaped := make([]string, len(parts))
 	for i := 0; i < len(parts); i++ {
 		escaped[i] = url.PathEscape(parts[i])
 	}
 
-	a.webPath = strings.Join(escaped, "/")
-	return a.webPath
+	a.WebPath = strings.Join(escaped, "/")
 }
 
 func (a *Article) TemplatePaths() []string {
@@ -310,7 +306,7 @@ func WriteHomePage(
 	fsys.WriteFile("index.html", buf.Bytes())
 }
 
-func findArticles(fsys writablefs.FS) (result []Article) {
+func findArticles(fsys writablefs.FS, site SiteMetadata) (result []Article) {
 
 	fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
 		if d.IsDir() || !strings.HasSuffix(d.Name(), DjotExt) {
@@ -351,6 +347,7 @@ func findArticles(fsys writablefs.FS) (result []Article) {
 			DjotBody:        bodyText,
 			ArticleMetadata: meta,
 		}
+		article.ComputeWebPath(site.Root)
 		result = append(result, article)
 		return nil
 	})
diff --git a/theme/includes.tmpl b/theme/includes.tmpl
index 19b5122..cdd6ec2 100644
--- a/theme/includes.tmpl
+++ b/theme/includes.tmpl
@@ -3,7 +3,7 @@
 <nav>
   <a href="{{.Site.Root}}">Home</a>
   {{- range .ArticlesInNav}}
-  <a href="{{$.Site.Root}}{{.WebPath}}">{{.Title}}</a>
+  <a href="{{.WebPath}}">{{.Title}}</a>
   {{- end}}
 
   {{- if not .Post.PostedAt.IsZero}}