From 6cc17fd4e8f3bd3a84e72533aff7bef40e8f32ae Mon Sep 17 00:00:00 2001 From: gutmet Date: Thu, 28 May 2020 21:52:50 +0200 Subject: [PATCH] get rid of html inside generator once and for all --- blog/blog.go | 88 ++++++------------------ initer/initer.go | 4 ++ initer/styles.go | 14 +++- initer/templates.go | 30 +++++++- templatestructures/templatestructures.go | 55 +++++++++++---- 5 files changed, 108 insertions(+), 83 deletions(-) diff --git a/blog/blog.go b/blog/blog.go index 3938a33..09d17a4 100644 --- a/blog/blog.go +++ b/blog/blog.go @@ -23,26 +23,7 @@ var folder string var style string var templateString string var t *template.Template -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 -} +var categories map[string][]Post func linkToPost(filename string) string { 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))) } -func addToCategory(category string, post post) { +func addToCategory(category string, post Post) { c := strings.ToLower(category) if len(c) > 0 { categories[c] = append(categories[c], post) } } -func keys(m map[string][]post) []string { +func keys(m map[string][]Post) []string { keys := make([]string, 0) for k := range m { keys = append(keys, k) @@ -77,7 +58,7 @@ func keys(m map[string][]post) []string { return keys } -func emitPostPage(post post) string { +func emitPostPage(post Post) string { filename := strings.TrimPrefix(strings.TrimPrefix(post.Filename, "stage1/"), "stage1\\") link := post.Link os.Remove(filepath.Join("stage2", filename)) @@ -110,13 +91,13 @@ func indexPageExists(i int, total int) bool { return (i >= 0) && (i <= max) } -type byDateDesc []post +type byDateDesc []Post 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) 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) postCollection := make([]Blogpost, 0) sort.Sort(byDateDesc(posts)) @@ -145,9 +126,9 @@ func emitIndexPage(i int, posts []post, total int) { 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") - ret := metadata{} + ret := Metadata{} if err == nil { lines := strings.Split(metaAll, "\n") 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) if err == nil { meta, err2 := getMetadata(file) if err2 == nil { - p := post{} + p := Post{} p.Content = tmp p.Meta = meta p.Filename = file @@ -190,71 +171,52 @@ func getPost(file string) (post, error) { p.Link = linkToPost(filename) return p, nil } else { - return post{}, err2 + return Post{}, err2 } } else { - return post{}, err + return Post{}, err } } func makeCategories() { - ct := template.Must(template.New("Categories").Parse(categoryTemplate)) page := Page{} page.Title = blogname + ": Categories" page.Style = style keySet := keys(categories) sort.Strings(keySet) - page.Content += "
" for _, k := range keySet { - listing := categoryListing{} + listing := CategoryListing{} listing.Name = k listing.ASCIIName = ConvASCII(k) sort.Sort(byDate(categories[k])) listing.Posts = categories[k] - buf := new(bytes.Buffer) - ct.Execute(buf, listing) - page.Content += buf.String() + page.CategoryListings = append(page.CategoryListings, listing) } - page.Content += "
" buf := new(bytes.Buffer) t.Execute(buf, page) goutil.WriteFile(filepath.Join("stage2", folder, "categories.html"), buf.String()) } -func makeTimeline(posts []post) { +func makeTimeline(posts []Post) { sort.Sort(byDateDesc(posts)) page := Page{} page.Title = blogname + ": Timeline" page.Style = style - page.Content += "
" - year := -1 - month := -1 - for _, p := range posts { - if year != p.Meta.Date.Year() { - year = p.Meta.Date.Year() - page.Content += "
\n\n" + "

" + strconv.Itoa(year) + "

\n
\n" - } - if month != int(p.Meta.Date.Month()) { - month = int(p.Meta.Date.Month()) - page.Content += fmt.Sprintf("
%02d
", month) - } - page.Content += fmt.Sprintf("
%s
\n", p.Link, p.Meta.Title) - } - page.Content += "
" + page.TimelineListing = posts buf := new(bytes.Buffer) t.Execute(buf, page) 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) 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 getPosts() []post { +func getPosts() []Post { files := goutil.ListFilesExt(filepath.Join("stage1", folder), ".html") - posts := make([]post, 0) + posts := make([]Post, 0) for _, file := range files { tmp, err := getPost(file) if err == nil { @@ -268,13 +230,13 @@ func makeBlog() { posts := getPosts() sort.Sort(byDate(posts)) total := len(posts) - s := make([]post, 0) + s := make([]Post, 0) n := 0 for i, tmp := range posts { if (i / postsPerPage) != n { emitIndexPage(n, s, total) n = i / postsPerPage - s = make([]post, 0) + s = make([]Post, 0) } s = append(s, tmp) } @@ -293,7 +255,7 @@ func Generate(dir string) { blogname = "blog" } blogname = strings.TrimSpace(blogname) - categories = make(map[string][]post) + categories = make(map[string][]Post) var err error var err2 error style, err = goutil.ReadFile("style.css") @@ -306,9 +268,3 @@ func Generate(dir string) { fmt.Println(err2) } } - -const categoryTemplate string = ` -
{{.Name}}
-{{range $index, $post := .Posts}}
{{$post.Meta.Title}}
-{{end}} -` diff --git a/initer/initer.go b/initer/initer.go index d6f39e1..3b64d1e 100644 --- a/initer/initer.go +++ b/initer/initer.go @@ -77,6 +77,10 @@ func initialize(style Style) error { if err != nil { return err } + err = os.Mkdir("stage0/blog", 0755) + if err != nil { + return err + } err = dumpInitFiles(style) } return err diff --git a/initer/styles.go b/initer/styles.go index 8fcf2b6..4436c00 100644 --- a/initer/styles.go +++ b/initer/styles.go @@ -26,9 +26,19 @@ a.linktopost { a.categoryListing { color: #E8E8E8; } +a.timelineListing { + color: #E8E8E8; + text-decoration : none; +} +a.timelineListing:hover { + color : red; +} +div.timelineListing { + font-family: Monospace; +} img { - display: block; - max-width: 80%; + display:block; + max-width: 100%; margin-left: auto; margin-right: auto; } diff --git a/initer/templates.go b/initer/templates.go index fb83b9a..f872b88 100644 --- a/initer/templates.go +++ b/initer/templates.go @@ -16,7 +16,7 @@ var defaultTemplate string = `

{{if .Blogpost}}{{.Blogname}}: {{end}}{{.Title}}

@@ -44,9 +44,35 @@ var defaultTemplate string = ` {{end}} {{end}} + +
- {{.Content}} +{{.Content}} + +{{if .CategoryListings}} +
+
+{{range $i, $listing := .CategoryListings -}} +
{{$listing.Name}}
+{{range $j, $post := $listing.Posts -}} +
{{$post.Meta.Title}}
+{{end}} +{{end}} +
+{{end}} + +{{if .TimelineListing}} +
+{{range $i, $post := .TimelineListing -}} +{{$post.Meta.FormattedDate}} {{$post.Meta.Title}}
+{{end}} +
+{{end}} + +
+ + {{if .Categories}}
diff --git a/templatestructures/templatestructures.go b/templatestructures/templatestructures.go index 1a0b285..6c5b41b 100644 --- a/templatestructures/templatestructures.go +++ b/templatestructures/templatestructures.go @@ -1,19 +1,25 @@ package templatestructures +import ( + "time" +) + type Page struct { - Title string - Style string - Subpages []Subpage - Description string - Content string - Blogname string - Blogdir string - Blogpost bool - PostCollection []Blogpost - Categories []Category - Previous string - Later string - Time string + Title string + Style string + Subpages []Subpage + Description string + Content string + Blogname string + Blogdir string + Blogpost bool + PostCollection []Blogpost + Categories []Category + CategoryListings []CategoryListing + TimelineListing []Post + Previous string + Later string + Time string } type Subpage struct { @@ -32,3 +38,26 @@ type Category struct { Name 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") +}