get rid of html inside generator once and for all

This commit is contained in:
gutmet 2020-05-28 21:52:50 +02:00
parent adbf2cad0e
commit 6cc17fd4e8
5 changed files with 108 additions and 83 deletions

View File

@ -23,26 +23,7 @@ var folder string
var style string var style string
var templateString string var templateString string
var t *template.Template var t *template.Template
var categories map[string][]post var categories map[string][]Post
type metadata struct {
Title string
Date time.Time
Categories []string
}
type post struct {
Content string
Meta metadata
Filename string
Link string
}
type categoryListing struct {
Name string
ASCIIName string
Posts []post
}
func linkToPost(filename string) string { func linkToPost(filename string) string {
filename = filepath.Base(filename) filename = filepath.Base(filename)
@ -62,14 +43,14 @@ func linkToCategory(name string) string {
return fmt.Sprintf("/%s/categories.html#%s", folder, ConvASCII(strings.ToLower(name))) return fmt.Sprintf("/%s/categories.html#%s", folder, ConvASCII(strings.ToLower(name)))
} }
func addToCategory(category string, post post) { func addToCategory(category string, post Post) {
c := strings.ToLower(category) c := strings.ToLower(category)
if len(c) > 0 { if len(c) > 0 {
categories[c] = append(categories[c], post) categories[c] = append(categories[c], post)
} }
} }
func keys(m map[string][]post) []string { func keys(m map[string][]Post) []string {
keys := make([]string, 0) keys := make([]string, 0)
for k := range m { for k := range m {
keys = append(keys, k) keys = append(keys, k)
@ -77,7 +58,7 @@ func keys(m map[string][]post) []string {
return keys return keys
} }
func emitPostPage(post post) string { func emitPostPage(post Post) string {
filename := strings.TrimPrefix(strings.TrimPrefix(post.Filename, "stage1/"), "stage1\\") filename := strings.TrimPrefix(strings.TrimPrefix(post.Filename, "stage1/"), "stage1\\")
link := post.Link link := post.Link
os.Remove(filepath.Join("stage2", filename)) os.Remove(filepath.Join("stage2", filename))
@ -110,13 +91,13 @@ func indexPageExists(i int, total int) bool {
return (i >= 0) && (i <= max) return (i >= 0) && (i <= max)
} }
type byDateDesc []post type byDateDesc []Post
func (p byDateDesc) Len() int { return len(p) } func (p byDateDesc) Len() int { return len(p) }
func (p byDateDesc) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p byDateDesc) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p byDateDesc) Less(i, j int) bool { return p[i].Meta.Date.After(p[j].Meta.Date) } func (p byDateDesc) Less(i, j int) bool { return p[i].Meta.Date.After(p[j].Meta.Date) }
func emitIndexPage(i int, posts []post, total int) { func emitIndexPage(i int, posts []Post, total int) {
file := indexFilename(i) file := indexFilename(i)
postCollection := make([]Blogpost, 0) postCollection := make([]Blogpost, 0)
sort.Sort(byDateDesc(posts)) sort.Sort(byDateDesc(posts))
@ -145,9 +126,9 @@ func emitIndexPage(i int, posts []post, total int) {
goutil.WriteFile(file, buf.String()) goutil.WriteFile(file, buf.String())
} }
func getMetadata(file string) (metadata, error) { func getMetadata(file string) (Metadata, error) {
metaAll, err := goutil.ReadFile(goutil.TrimExt(file) + ".desc") metaAll, err := goutil.ReadFile(goutil.TrimExt(file) + ".desc")
ret := metadata{} ret := Metadata{}
if err == nil { if err == nil {
lines := strings.Split(metaAll, "\n") lines := strings.Split(metaAll, "\n")
for _, line := range lines { for _, line := range lines {
@ -177,12 +158,12 @@ func getMetadata(file string) (metadata, error) {
} }
} }
func getPost(file string) (post, error) { func getPost(file string) (Post, error) {
tmp, err := goutil.ReadFile(file) tmp, err := goutil.ReadFile(file)
if err == nil { if err == nil {
meta, err2 := getMetadata(file) meta, err2 := getMetadata(file)
if err2 == nil { if err2 == nil {
p := post{} p := Post{}
p.Content = tmp p.Content = tmp
p.Meta = meta p.Meta = meta
p.Filename = file p.Filename = file
@ -190,71 +171,52 @@ func getPost(file string) (post, error) {
p.Link = linkToPost(filename) p.Link = linkToPost(filename)
return p, nil return p, nil
} else { } else {
return post{}, err2 return Post{}, err2
} }
} else { } else {
return post{}, err return Post{}, err
} }
} }
func makeCategories() { func makeCategories() {
ct := template.Must(template.New("Categories").Parse(categoryTemplate))
page := Page{} page := Page{}
page.Title = blogname + ": Categories" page.Title = blogname + ": Categories"
page.Style = style page.Style = style
keySet := keys(categories) keySet := keys(categories)
sort.Strings(keySet) sort.Strings(keySet)
page.Content += "<dl style=\"padding-bottom:150em\">"
for _, k := range keySet { for _, k := range keySet {
listing := categoryListing{} listing := CategoryListing{}
listing.Name = k listing.Name = k
listing.ASCIIName = ConvASCII(k) listing.ASCIIName = ConvASCII(k)
sort.Sort(byDate(categories[k])) sort.Sort(byDate(categories[k]))
listing.Posts = categories[k] listing.Posts = categories[k]
buf := new(bytes.Buffer) page.CategoryListings = append(page.CategoryListings, listing)
ct.Execute(buf, listing)
page.Content += buf.String()
} }
page.Content += "</dl>"
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
t.Execute(buf, page) t.Execute(buf, page)
goutil.WriteFile(filepath.Join("stage2", folder, "categories.html"), buf.String()) goutil.WriteFile(filepath.Join("stage2", folder, "categories.html"), buf.String())
} }
func makeTimeline(posts []post) { func makeTimeline(posts []Post) {
sort.Sort(byDateDesc(posts)) sort.Sort(byDateDesc(posts))
page := Page{} page := Page{}
page.Title = blogname + ": Timeline" page.Title = blogname + ": Timeline"
page.Style = style page.Style = style
page.Content += "<dl>" page.TimelineListing = posts
year := -1
month := -1
for _, p := range posts {
if year != p.Meta.Date.Year() {
year = p.Meta.Date.Year()
page.Content += "</dl>\n\n" + "<h3>" + strconv.Itoa(year) + "</h3>\n<dl>\n"
}
if month != int(p.Meta.Date.Month()) {
month = int(p.Meta.Date.Month())
page.Content += fmt.Sprintf("<dt>%02d</dt>", month)
}
page.Content += fmt.Sprintf("<dd><a href=\"%s\">%s</a></dd>\n", p.Link, p.Meta.Title)
}
page.Content += "</dl>"
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
t.Execute(buf, page) t.Execute(buf, page)
goutil.WriteFile(filepath.Join("stage2", folder, "timeline.html"), buf.String()) goutil.WriteFile(filepath.Join("stage2", folder, "timeline.html"), buf.String())
} }
type byDate []post type byDate []Post
func (p byDate) Len() int { return len(p) } func (p byDate) Len() int { return len(p) }
func (p byDate) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p byDate) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p byDate) Less(i, j int) bool { return p[i].Meta.Date.Before(p[j].Meta.Date) } func (p byDate) Less(i, j int) bool { return p[i].Meta.Date.Before(p[j].Meta.Date) }
func getPosts() []post { func getPosts() []Post {
files := goutil.ListFilesExt(filepath.Join("stage1", folder), ".html") files := goutil.ListFilesExt(filepath.Join("stage1", folder), ".html")
posts := make([]post, 0) posts := make([]Post, 0)
for _, file := range files { for _, file := range files {
tmp, err := getPost(file) tmp, err := getPost(file)
if err == nil { if err == nil {
@ -268,13 +230,13 @@ func makeBlog() {
posts := getPosts() posts := getPosts()
sort.Sort(byDate(posts)) sort.Sort(byDate(posts))
total := len(posts) total := len(posts)
s := make([]post, 0) s := make([]Post, 0)
n := 0 n := 0
for i, tmp := range posts { for i, tmp := range posts {
if (i / postsPerPage) != n { if (i / postsPerPage) != n {
emitIndexPage(n, s, total) emitIndexPage(n, s, total)
n = i / postsPerPage n = i / postsPerPage
s = make([]post, 0) s = make([]Post, 0)
} }
s = append(s, tmp) s = append(s, tmp)
} }
@ -293,7 +255,7 @@ func Generate(dir string) {
blogname = "blog" blogname = "blog"
} }
blogname = strings.TrimSpace(blogname) blogname = strings.TrimSpace(blogname)
categories = make(map[string][]post) categories = make(map[string][]Post)
var err error var err error
var err2 error var err2 error
style, err = goutil.ReadFile("style.css") style, err = goutil.ReadFile("style.css")
@ -306,9 +268,3 @@ func Generate(dir string) {
fmt.Println(err2) fmt.Println(err2)
} }
} }
const categoryTemplate string = `
<dt><a name="{{.ASCIIName}}" class="categoryListing">{{.Name}}</a></dt>
{{range $index, $post := .Posts}}<dd><a href="{{$post.Link}}">{{$post.Meta.Title}}</a></dd>
{{end}}
`

View File

@ -77,6 +77,10 @@ func initialize(style Style) error {
if err != nil { if err != nil {
return err return err
} }
err = os.Mkdir("stage0/blog", 0755)
if err != nil {
return err
}
err = dumpInitFiles(style) err = dumpInitFiles(style)
} }
return err return err

View File

@ -26,9 +26,19 @@ a.linktopost {
a.categoryListing { a.categoryListing {
color: #E8E8E8; color: #E8E8E8;
} }
a.timelineListing {
color: #E8E8E8;
text-decoration : none;
}
a.timelineListing:hover {
color : red;
}
div.timelineListing {
font-family: Monospace;
}
img { img {
display: block; display:block;
max-width: 80%; max-width: 100%;
margin-left: auto; margin-left: auto;
margin-right: auto; margin-right: auto;
} }

View File

@ -16,7 +16,7 @@ var defaultTemplate string = `<!DOCTYPE html>
<h1>{{if .Blogpost}}{{.Blogname}}: {{end}}{{.Title}}</h1> <h1>{{if .Blogpost}}{{.Blogname}}: {{end}}{{.Title}}</h1>
<div class="navigation"> <div class="navigation">
<a href="/">home</a> <a href="/">home</a>
<a href="/blog">blog</a> <a href="/blog/">blog</a>
<a href="/photos/pages/">photos</a> <a href="/photos/pages/">photos</a>
</div> </div>
@ -44,9 +44,35 @@ var defaultTemplate string = `<!DOCTYPE html>
{{end}} {{end}}
{{end}} {{end}}
<div class="content"> <div class="content">
{{.Content}} {{.Content}}
{{if .CategoryListings}}
<div class="categoryListing">
<dl style="padding-bottom:150em;">
{{range $i, $listing := .CategoryListings -}}
<dt><a name="{{$listing.ASCIIName}}" class="categoryListing">{{$listing.Name}}</a></dt>
{{range $j, $post := $listing.Posts -}}
<dd><a href="{{$post.Link}}">{{$post.Meta.Title}}</a></dd>
{{end}}
{{end}}
</dl>
</div> </div>
{{end}}
{{if .TimelineListing}}
<div class="timelineListing">
{{range $i, $post := .TimelineListing -}}
<a href="{{$post.Link}}" class="timelineListing">{{$post.Meta.FormattedDate}} {{$post.Meta.Title}}</a><br>
{{end}}
</div>
{{end}}
</div>
{{if .Categories}} {{if .Categories}}
<div class="categories"> <div class="categories">

View File

@ -1,5 +1,9 @@
package templatestructures package templatestructures
import (
"time"
)
type Page struct { type Page struct {
Title string Title string
Style string Style string
@ -11,6 +15,8 @@ type Page struct {
Blogpost bool Blogpost bool
PostCollection []Blogpost PostCollection []Blogpost
Categories []Category Categories []Category
CategoryListings []CategoryListing
TimelineListing []Post
Previous string Previous string
Later string Later string
Time string Time string
@ -32,3 +38,26 @@ type Category struct {
Name string Name string
Path string Path string
} }
type CategoryListing struct {
Name string
ASCIIName string
Posts []Post
}
type Post struct {
Content string
Meta Metadata
Filename string
Link string
}
type Metadata struct {
Title string
Date time.Time
Categories []string
}
func (m Metadata) FormattedDate() string {
return m.Date.Format("2006-01-02")
}