~nature/galaxy.moe

e2498cbf148238ff92f5b8788e5433a2014c1c3d — LordNature 4 years ago aa9a44c
blog: Added ability to create blog posts.

generate.go:
	Added `about.html` to the generator.
	Added `generatePosts()` to call the umbrella function to generate blog posts.
	`convertPost()` converts a markdown file to the formatted html.
	`generatePosts()` is an umbrella function that generates each post and
		produces a `posts.html` file with all posts and URLs to them.
nav.html:
	Added `posts.html`.
post.html:
	Basic formatting for post. Just takes markdown from parsed data interface.
posts.html:
	Added with name, date, and URL.
4 files changed, 124 insertions(+), 0 deletions(-)

M generate.go
M templates/nav.html
A templates/post.html
A templates/posts.html
M generate.go => generate.go +93 -0
@@ 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
}

M templates/nav.html => templates/nav.html +1 -0
@@ 6,6 6,7 @@
		<a href=""><s>anime</s></a>&emsp;
		<a href="/about.html">about</a>&emsp;
    <a href="/vip.html">music</a>
    <a href="/posts.html">posts</a>
  </nav>
	<hr>
</header>

A templates/post.html => templates/post.html +13 -0
@@ 0,0 1,13 @@
<html>
	{{template "head"}}
	<body>
		{{template "nav"}}
		<main>
		  <article>
        {{ . }}
        <i>by LordNature</i>
      </article>
    </main>
		{{template "footer"}}
	</body>
</html>

A templates/posts.html => templates/posts.html +17 -0
@@ 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>