~nature/galaxy.moe

62a17d9d52b364cdc2dd3684898e5c6cf03b52d8 — LordNature 4 years ago aa768f7
generate.go: Moved generation functionality
2 files changed, 38 insertions(+), 31 deletions(-)

A generate.go
M main.go
A generate.go => generate.go +38 -0
@@ 0,0 1,38 @@
package main

import (
	"html/template"
	"os"
	"strconv"
	"time"
)


// generate takes a page in the format `{page}.html` and creates
// a compiled html file under the `build/` directory. If any failures
// happen, it alerts the user via stdout.
func generate(page string) error {
	var templates = template.Must(template.ParseFiles(
		"templates/head.html",
		"templates/footer.html",
		"templates/nav.html",
		"templates/" + page))

	templates.New("year").Parse(strconv.Itoa(time.Now().Year()))

	file, err := os.Create("build/" + page)
	if err != nil {
		return err
	}

	err = templates.ExecuteTemplate(file, page, nil)
	if err != nil {
		return err
	}

	if err := file.Close(); err != nil {
		return err
	}
	return nil
}


M main.go => main.go +0 -31
@@ 2,41 2,10 @@ package main

import (
	"fmt"
	"html/template"
	"io/ioutil"
	"net/http"
	"os"
	"time"
)

// generate takes a page in the format `{page}.html` and creates
// a compiled html file under the `build/` directory. If any failures
// happen, it alerts the user via stdout.
func generate(page string) error {
	var templates = template.Must(template.ParseFiles(
		"templates/head.html",
		"templates/footer.html",
		"templates/nav.html",
		"templates/" + page))

	templates.New("year").Parse(fmt.Sprint(time.Now().Year()))

	file, err := os.Create("build/" + page)
	if err != nil {
		return err
	}

	err = templates.ExecuteTemplate(file, page, nil)
	if err != nil {
		return err
	}

	if err := file.Close(); err != nil {
		return err
	}
	return nil
}

func main() {
	fmt.Println("COMPILING STATIC PAGES")
	pages, err := ioutil.ReadDir("templates")