summaryrefslogtreecommitdiff
path: root/render.go
blob: 16784f4263ca26fc4a95efc0679f93abb513e242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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">&copy; 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\">&gt;&gt;&gt; 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>")
}