2012-11-05

Go Site Generator

Here is the site generator I wrote in Go:

package main

import (
    "fmt"
    "io/ioutil"
    "os/exec"
    "os"
)

var template_master = `
<html>
<head>
<title>Actually</title>
<style>
    hr { border: 1px dashed #efefef; }
</style>
</head>

<body bgcolor= "#efefef">
    <center><h1>Actually</h1></center>
    <center><h3><i>It's Randolph Connor Berry</i></h3></center>
    <center>
    <i>~~~ A journal of daily thoughts, hopes, and explorations ~~~</i>
    </center>
    <center>
    <a href="toc.html"><i>Archives</i></a>
    </center>
%s
</body>
</html>
`

var template_post = `

    <div style="background-color:white;
                border:1px dashed;
                padding-left: 1%;
                padding-right: 1%;
                margin-left:auto;
                margin-right:auto;
                margin-bottom:10px;
                width:40%;">
        <h2><a id="%s">%s</a></h2>
        %s
    </div>

`

func main() {
    os.Chdir("entries")
    entries, _ := ioutil.ReadDir(".")

    // reverse entries
    for i, j := 0, len(entries)-1; i < j; i, j = i+1, j-1 {
        entries[i], entries[j] = entries[j], entries[i]
    }

    posts := ""
    for _, e := range entries {
        date := e.Name()
        cmd := exec.Command("markdown", date)
        post, _ := cmd.Output()

        post_html := fmt.Sprintf(template_post, date, date, post)
        posts += string(post_html)
    }

    main_page := fmt.Sprintf(template_master, posts)
    fmt.Print(main_page)
}

Table Of Contents In Go

Here is the code that generates the table of contents / archive listing:

package main

import (
    "fmt"
    "os"
    "io/ioutil"
    "strings"
    "regexp"
)

func main() {
    output := make([]string, 1)
    for _, arg := range os.Args[1:] {
        content, _ := ioutil.ReadFile(arg)
        data := string(content)

        lines := strings.Split(data, "\n")

        for _, line := range lines {
            found, _ := regexp.MatchString("^###", line)
            if found {
                date := arg
                line = strings.Replace(line, "### ", "", -1)
                line = strings.Replace(line, "\n", "", -1)
                output_line := fmt.Sprintf("<a href=\"index.html#%s\">%s</a><br>", date, line)
                output = append(output, output_line)
            }
        }
    }

    // reverse output lines
    for i, j := 0, len(output)-1; i < j; i, j = i+1, j-1 {
        output[i], output[j] = output[j], output[i]
    }

    for _, line := range output {
        fmt.Println(line)
    }

}
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s