switch to embedding blog template
parent
355af071eb
commit
8be261647c
62
genblog.go
62
genblog.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
@ -9,10 +10,11 @@ import (
|
|||
"text/template"
|
||||
)
|
||||
|
||||
//const statsPath = "/home/vilmibm/bin/townstats"
|
||||
|
||||
const statsPath = "/town/bin/stats"
|
||||
|
||||
//go:embed blog.tmpl.html
|
||||
var blogTmpl string
|
||||
|
||||
type newsEntry struct {
|
||||
Title string `json:"title"` // Title of entry
|
||||
Pubdate string `json:"pubdate"` // Human readable date
|
||||
|
@ -29,63 +31,19 @@ func _main() error {
|
|||
return err
|
||||
}
|
||||
|
||||
hypertextDocument := getHeader()
|
||||
|
||||
for _, entry := range data.News {
|
||||
entryHTML, err := renderEntry(entry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hypertextDocument += entryHTML
|
||||
}
|
||||
|
||||
hypertextDocument += getFooter()
|
||||
|
||||
fmt.Println(hypertextDocument)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func renderEntry(entry newsEntry) (string, error) {
|
||||
t, err := template.New("news").Parse(`
|
||||
<h2>{{.Title}}</h2>
|
||||
<em>{{.Pubdate}}</em>
|
||||
{{.Content}}
|
||||
`)
|
||||
t, err := template.New("blog").Parse(blogTmpl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return fmt.Errorf("failed to parse the blog template: %w", err)
|
||||
}
|
||||
|
||||
out := bytes.Buffer{}
|
||||
err = t.Execute(&out, entry)
|
||||
if err != nil {
|
||||
return "", err
|
||||
if err = t.Execute(&out, data); err != nil {
|
||||
return fmt.Errorf("failed to render blog template: %w", err)
|
||||
}
|
||||
|
||||
return out.String(), nil
|
||||
}
|
||||
fmt.Println(out.String())
|
||||
|
||||
func getHeader() string {
|
||||
return `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>web log of tilde town</title>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="icon" href="/favicon.ico">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>a web log for tilde town or at least what passes for one</h1>
|
||||
`
|
||||
}
|
||||
|
||||
func getFooter() string {
|
||||
return `
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
return nil
|
||||
}
|
||||
|
||||
func stats() (*tildeData, error) {
|
||||
|
|
|
@ -89,7 +89,7 @@
|
|||
|
||||
<code>ssh join@tilde.town</code><br><br>
|
||||
-->
|
||||
if you think you might enjoy being a part of this community you can apply for a good time <a href="https://cgi.tilde.town/users/signup">using this form</a>.
|
||||
if you think you might enjoy being a part of this community you can apply for a homestead <a href="https://cgi.tilde.town/users/signup">using this form</a>.
|
||||
</p>
|
||||
<p>
|
||||
it can take up to 30 days to be approved and not all applications are approved.
|
||||
|
@ -111,7 +111,7 @@
|
|||
</section>
|
||||
<section id="donate">
|
||||
<p>
|
||||
the town is a non-commercial thing. <a href="https://patreon.com/nathanielksmith">donations</a> are appreciated
|
||||
the town is a non-commercial thing. <a href="https://patreon.com/vilmibm">donations</a> are appreciated
|
||||
</p>
|
||||
</section>
|
||||
<section id="lostkey">
|
||||
|
|
Loading…
Reference in New Issue