Reorganize; index rendering
Anirudh Oppiliappan x@icyphox.sh
Wed, 04 Aug 2021 10:22:43 +0530
4 files changed,
115 insertions(+),
87 deletions(-)
M
commands/build.go
→
commands/build.go
@@ -1,7 +1,6 @@
package commands import ( - "fmt" "os" "path/filepath" "sort"@@ -43,82 +42,76 @@
return nil } -// Core builder function. Converts markdown to html, -// copies over non .md files, etc. -func Build() error { - pages := Pages{} - err := pages.initPages() - if err != nil { - return err - } +func (pgs *Pages) processFiles() error { + for _, f := range pgs.Files { + if filepath.Ext(f) == ".md" { + // ex: pages/about.md + mdFile := filepath.Join(PAGES, f) + var htmlDir string + // ex: build/index.html (root index) + if f == "_index.md" { + htmlDir = BUILD + } else { + htmlDir = filepath.Join( + BUILD, + strings.TrimSuffix(f, ".md"), + ) + } + os.Mkdir(htmlDir, 0755) + // ex: build/about/index.html + htmlFile := filepath.Join(htmlDir, "index.html") - // Deal with files. - // ex: pages/{_index,about,etc}.md - err = func() error { - for _, f := range pages.Files { - if filepath.Ext(f) == ".md" { - // ex: pages/about.md - mdFile := filepath.Join(PAGES, f) - var htmlDir string - if f == "_index.md" { - htmlDir = BUILD - } else { - htmlDir = filepath.Join( - BUILD, - strings.TrimSuffix(f, ".md"), - ) - } - os.Mkdir(htmlDir, 0755) - // ex: build/about/index.html - htmlFile := filepath.Join(htmlDir, "index.html") + fb, err := os.ReadFile(mdFile) + if err != nil { + return err + } - fb, err := os.ReadFile(mdFile) - if err != nil { - return err - } - - out := markdown.Output{} - out.RenderMarkdown(fb) - if err = out.RenderHTML( - htmlFile, - TEMPLATES, - struct { - Cfg config.ConfigYaml - Fm markdown.Matter - Body string - }{config.Config, out.Meta, string(out.HTML)}, - ); err != nil { - return err - } - } else { - src := filepath.Join(PAGES, f) - util.CopyFile(src, BUILD) + out := markdown.Output{} + out.RenderMarkdown(fb) + if err = out.RenderHTML( + htmlFile, + TEMPLATES, + struct { + Cfg config.ConfigYaml + Fm markdown.Matter + Body string + }{config.Config, out.Meta, string(out.HTML)}, + ); err != nil { + return err } + } else { + src := filepath.Join(PAGES, f) + util.CopyFile(src, BUILD) } - return nil - }() - if err != nil { - return err } + return nil +} - // Deal with dirs -- i.e. of markdown files. - // ex: pages/{blog,travel}/*.md - err = func() error { - for _, d := range pages.Dirs { - // ex: build/blog - dstDir := filepath.Join(BUILD, d) - // ex: pages/blog - srcDir := filepath.Join(PAGES, d) - os.Mkdir(dstDir, 0755) +func (pgs *Pages) processDirs() error { + for _, d := range pgs.Dirs { + // ex: build/blog + dstDir := filepath.Join(BUILD, d) + // ex: pages/blog + srcDir := filepath.Join(PAGES, d) + os.Mkdir(dstDir, 0755) + + entries, err := os.ReadDir(srcDir) + if err != nil { + return err + } + + posts := []markdown.Output{} + // Collect all posts + for _, e := range entries { + // foo-bar.md -> foo-bar + slug := strings.TrimSuffix(e.Name(), filepath.Ext(e.Name())) - entries, err := os.ReadDir(srcDir) - if err != nil { - return err - } + // ex: build/blog/foo-bar/ + os.Mkdir(filepath.Join(dstDir, slug), 0755) + // ex: build/blog/foo-bar/index.html + htmlFile := filepath.Join(dstDir, slug, "index.html") - posts := []markdown.Output{} - // Collect all posts - for _, e := range entries { + if e.Name() != "_index.md" { ePath := filepath.Join(srcDir, e.Name()) fb, err := os.ReadFile(ePath) if err != nil {@@ -128,17 +121,17 @@
out := markdown.Output{} out.RenderMarkdown(fb) - slug := strings.TrimSuffix(filepath.Ext(e.Name()), ".md") - - htmlFile := filepath.Join(dstDir, slug) - out.RenderHTML( + if err = out.RenderHTML( htmlFile, TEMPLATES, struct { Cfg config.ConfigYaml Fm markdown.Matter Body string - }{config.Config, out.Meta, string(out.HTML)}) + }{config.Config, out.Meta, string(out.HTML)}, + ); err != nil { + return err + } posts = append(posts, out) }@@ -148,13 +141,48 @@ date1 := posts[j].Meta["date"].(time.Time)
date2 := posts[i].Meta["date"].(time.Time) return date1.Before(date2) }) + } - for _, p := range posts { - fmt.Println(p.Meta["date"]) - } + // Render index using posts slice. + // ex: build/blog/index.html + indexHTML := filepath.Join(dstDir, "index.html") + // ex: pages/blog/_index.md + indexMd, err := os.ReadFile(filepath.Join(srcDir, "_index.md")) + if err != nil { + return err } - return nil - }() + out := markdown.Output{} + out.RenderMarkdown(indexMd) + + out.RenderHTML(indexHTML, TEMPLATES, struct { + Cfg config.ConfigYaml + Fm markdown.Matter + Body string + Posts []markdown.Output + }{config.Config, out.Meta, string(out.HTML), posts}) + } + return nil +} + +// Core builder function. Converts markdown to html, +// copies over non .md files, etc. +func Build() error { + pages := Pages{} + err := pages.initPages() + if err != nil { + return err + } + + // Deal with files. + // ex: pages/{_index,about,etc}.md + err = pages.processFiles() + if err != nil { + return err + } + + // Deal with dirs -- i.e. dirs of markdown files. + // ex: pages/{blog,travel}/*.md + err = pages.processDirs() if err != nil { return err }
M
config/config.go
→
config/config.go
@@ -24,11 +24,11 @@
func init() { cf, err := os.ReadFile("config.yaml") if err != nil { - fmt.Errorf("error: %+v\n", err) + fmt.Fprintf(os.Stderr, "error: %+v\n", err) os.Exit(1) } if err = Config.ParseConfig(cf); err != nil { - fmt.Errorf("error: %+v\n", err) + fmt.Fprintf(os.Stderr, "error: %+v\n", err) } }
M
main.go
→
main.go
@@ -34,13 +34,13 @@ }
initPath := args[2] err := commands.Init(initPath) if err != nil { - fmt.Errorf("error: init: %+v\n", err) + fmt.Fprintf(os.Stderr, "error: init: %+v\n", err) } case "build": err := commands.Build() if err != nil { - fmt.Errorf("error: build: %+v\n", err) + fmt.Fprintf(os.Stderr, "error: build: %+v\n", err) } case "new":@@ -51,7 +51,7 @@ }
newPath := args[2] err := commands.New(newPath) if err != nil { - fmt.Errorf("error: new: %+v\n", err) + fmt.Fprintf(os.Stderr, "error: new: %+v\n", err) } }
M
markdown/markdown.go
→
markdown/markdown.go
@@ -45,8 +45,8 @@
// Renders out.HTML into dst html file, using the template specified // in the frontmatter. data is the template struct. func (out *Output) RenderHTML(dst, tmplDir string, data interface{}) error { - metaTemplate := out.Meta["template"].(string) - if metaTemplate == "" { + metaTemplate := out.Meta["template"] + if metaTemplate == nil { metaTemplate = "text.html" }@@ -60,7 +60,7 @@ if err != nil {
return err } - if err = t.ExecuteTemplate(w, metaTemplate, data); err != nil { + if err = t.ExecuteTemplate(w, metaTemplate.(string), data); err != nil { return err } return nil