diff options
| author | Sebastian Zack Tin Lahm-Lee <bctnry@sdf.org> | 2026-10-04 12:58:12 +0800 |
|---|---|---|
| committer | Sebastian Zack Tin Lahm-Lee <bctnry@sdf.org> | 2026-10-04 12:58:12 +0800 |
| commit | 78b410c668354c30323a259167beb7eddaa2d58f (patch) | |
| tree | b45dc58e106686deba0c71ac65572ccc9d29f0af /render.go | |
Diffstat (limited to 'render.go')
| -rw-r--r-- | render.go | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/render.go b/render.go new file mode 100644 index 0000000..16784f4 --- /dev/null +++ b/render.go @@ -0,0 +1,134 @@ +package main + +import ( + "bytes" + "fmt" + "html/template" + "net/http" + "os" + "path" + "path/filepath" + "strings" +) + +// The default layout replicates the original hand-written site shell +// (table-based two-column layout with sidebar and footer). A file named +// layout.html in the content root overrides it; it must keep using the +// {{.FullTitle}}, {{.Root}} and {{.Content}} actions. +const defaultLayout = `<!DOCTYPE html> +<html> + <head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>{{.FullTitle}}</title> + <link href="{{.Root}}style.css" rel="stylesheet" type="text/css" media="all"> + <link rel="alternate" type="application/rss+xml" title="明月樓閒話 Mingyuet Comments" href="{{.Root}}feed.xml"> + </head> + <body> + <table style="width: 960px"> + <tr valign="top"> + <td style="width: 400px"> + <div style="text-align: right"> + <div><img src="{{.Root}}mycomments.png" alt="Mingyuet Comments logo" /></div> + <div style="text-align: right; margin-top: 2em"> + <a href="{{.Root}}index.html">Home</a><br /><a href="{{.Root}}special.html">Special</a><br /><a href="{{.Root}}releases.html">Releases</a><br /><a href="{{.Root}}about.html">About</a><br /><a href="{{.Root}}feed.xml">RSS</a> + </div> + <div style="text-align: right"> + <b>Inquiries</b>:<br /> + zaaktin.lam {at} outlook.com + </div> + </div> + </td> + <td style="padding-left: 2em"> + {{.Content}} + </td> + </tr> + <tr> + <td colspan="2" style="text-align: center">© 2025-2026 Mingyuet Comments</td> + </tr> + </table> + </body> +</html> +` + +const siteName = "明月樓閒話 Mingyuet Comments" + +type layoutData struct { + FullTitle string + Root string + Content template.HTML +} + +func fullTitle(suffix string) string { + if suffix == "" { + return siteName + } + return suffix + " :: " + siteName +} + +// rootFor returns the relative prefix that points back at the site root +// from the given URL path ("./" at depth 0, "../" at depth 1, ...), the +// same convention the original hand-written pages used. +func rootFor(urlPath string) string { + trimmed := strings.Trim(path.Clean(urlPath), "/") + if trimmed == "" { + return "./" + } + n := strings.Count(trimmed, "/") + if n == 0 { + return "./" + } + return strings.Repeat("../", n) +} + +func loadLayout(siteRoot string) (*template.Template, error) { + if data, err := os.ReadFile(filepath.Join(siteRoot, "layout.html")); err == nil { + t, err := template.New("layout").Parse(string(data)) + if err != nil { + return nil, fmt.Errorf("parsing %s/layout.html: %w", siteRoot, err) + } + return t, nil + } + return template.New("layout").Parse(defaultLayout) +} + +// render wraps content in the layout and writes it out. +func (s *Server) render(w http.ResponseWriter, r *http.Request, status int, titleSuffix string, content template.HTML) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Header().Set("Cache-Control", "no-cache") + w.WriteHeader(status) + var b bytes.Buffer + s.layout.Execute(&b, layoutData{ + FullTitle: fullTitle(titleSuffix), + Root: rootFor(r.URL.Path), + Content: content, + }) + w.Write(b.Bytes()) +} + +// renderEntry renders one post as a listing-page entry, in the same shape +// as the original hand-written <div id="pNN" class="entry"> blocks. The +// date doubles as a link to the post's own page. +func renderEntry(p *Post, root string) string { + var b strings.Builder + fmt.Fprintf(&b, "\n\t\t <div id=\"%s\" class=\"entry\">\n", p.ID) + if p.Title != "" { + fmt.Fprintf(&b, "\t\t\t<h1>%s</h1>\n", p.Title) + } + fmt.Fprintf(&b, "\t\t\t<p><a href=\"%spost/%s.html\">%s</a></p>\n", root, p.Name, p.DateStr) + if p.HasMore { + b.WriteString(string(p.Excerpt)) + b.WriteString("\n") + fmt.Fprintf(&b, "\t\t\t<p><a href=\"%spost/%s.html\">>>> More</a></p>\n", root, p.Name) + } else { + b.WriteString(string(p.Excerpt)) + b.WriteString("\n") + } + b.WriteString("\t\t </div>") + return b.String() +} + +func (s *Server) renderNotFound(w http.ResponseWriter, r *http.Request) { + s.render(w, r, http.StatusNotFound, "Not Found", + "<h1>Page Not Found</h1>\n\t\t <p>The requested page was not found.</p>") +} |
