@@ 2,10 2,12 @@ package main
import (
"fmt"
+ "github.com/gomarkdown/markdown"
"html/template"
"io/ioutil"
"os"
"os/exec"
+ "strings"
"strconv"
"time"
)
@@ 28,11 30,20 @@ func compile() error {
return err
}
+ // About Generator
+ if err := generatePage(templates, "about.html", nil); err != nil {
+ return err
+ }
+
// VIP Generator
if err := generatePage(templates, "vip.html", nil); err != nil {
return err
}
+ if err := generatePosts(templates); err != nil {
+ return err
+ }
+
return nil
}
@@ 58,3 69,85 @@ func generatePage(templates *template.Template, page string, data interface{}) e
return nil
}
+// convertPost takes a filename and creates a compiled html page of
+// the markdown blog post, and prints it to a file with the same name
+// under build.
+func convertPost(templates *template.Template, filename string) error {
+ md, _ := ioutil.ReadFile("posts/" + filename)
+ html := markdown.ToHTML(md, nil, nil)
+
+ name := strings.TrimSuffix(filename, ".md")
+ if name == filename {
+ return fmt.Errorf(filename + " is not a markdown file.")
+ }
+
+ file, err := os.Create("build/" + name + ".html")
+ if err != nil {
+ return err
+ }
+
+ err = templates.ExecuteTemplate(file, "post.html", template.HTML(html))
+ if err != nil {
+ return err
+ }
+
+ if err := file.Close(); err != nil {
+ return err
+ }
+
+ fmt.Println("COMPILED POST: ", filename)
+ return nil
+}
+
+type Post struct {
+ Name string
+ Date string
+ URL string
+}
+
+// generatePosts is an umbrella function that converts all markdown files
+// in folder `posts/` to html. It also generates the `posts.html` page.
+// todo: add error checking, check for posts dir existence, dont panic
+func generatePosts(templates *template.Template) error {
+ posts, err := ioutil.ReadDir("posts")
+ if err != nil {
+ return err
+ }
+
+ if len(posts) == 0 {
+ fmt.Println("No posts were compiled.")
+ return nil
+ }
+
+ var metadata []Post
+
+ for _, p := range posts {
+ if err := convertPost(templates, p.Name()); err != nil {
+ return err
+ }
+
+ // metadata
+ URL := strings.Split(p.Name(), ".")[0]
+ name := strings.Split(URL, "_")
+ formalName := strings.ReplaceAll(name[1], "-", " ")
+
+ date, err := time.Parse("2006-01-02", name[0])
+ if err != nil {
+ return err
+ }
+
+ postInfo := Post {
+ formalName,
+ date.Format("January 2, 2006"),
+ URL,
+ }
+
+ metadata = append(metadata, postInfo)
+ }
+
+ if err := generatePage(templates, "posts.html", metadata); err != nil {
+ return err
+ }
+
+ return nil
+}
@@ 6,6 6,7 @@
<a href=""><s>anime</s></a> 
<a href="/about.html">about</a> 
<a href="/vip.html">music</a>
+ <a href="/posts.html">posts</a>
</nav>
<hr>
</header>
@@ 0,0 1,13 @@
+<html>
+ {{template "head"}}
+ <body>
+ {{template "nav"}}
+ <main>
+ <article>
+ {{ . }}
+ <i>by LordNature</i>
+ </article>
+ </main>
+ {{template "footer"}}
+ </body>
+</html>
@@ 0,0 1,17 @@
+<html>
+ {{template "head"}}
+ <body>
+ {{template "nav"}}
+ <main>
+ <ul>
+ {{ range $post := . }}
+ <li>
+ <a href="{{ $post.URL }}.html">{{ $post.Name }}</a><br>
+ Posted on {{ $post.Date }}
+ </li>
+ {{ end }}
+ </ul>
+ </main>
+ {{template "footer"}}
+ </body>
+</html>