1
0
mirror of https://github.com/thebaer/squigglelog synced 2018-07-20 10:45:21 +00:00

Simple squigglelog generator

This commit is contained in:
Matt Baer 2015-01-27 21:35:07 -05:00
commit e75f931290
7 changed files with 210 additions and 0 deletions

22
LICENSE Normal file
View File

@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2015 Matt Baer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

30
README.md Normal file
View File

@ -0,0 +1,30 @@
~log
====
Use **squigglelog** to easily create a simple log for your tilde. [Create new posts](https://github.com/thebaer/squigglelog/tree/master/entries#squigglelog-entries) in the `entries/` folder, then run this:
```bash
go build squigglelog.go
./squigglelog -template mysquigglelog
```
This will use any template in `templates/` defined with _mysquigglelog_ (see below) to generate your full squigglelog page.
#### templates
Your template should look like this.
```html
{{define "mysquigglelog"}}
<html>
<head>
<title>My ~log!</title>
</head>
<body>
<h1>~log</h1>
<p>Welcome to my ~log.</p>
{{template "log" .}}
</body>
</html>
{{end}}
```

4
entries/README.md Normal file
View File

@ -0,0 +1,4 @@
squigglelog entries
===================
Create each entry as a new file here. Naming scheme: **YYYYMMDD**. Add formatting with HTML.

4
html/README.md Normal file
View File

@ -0,0 +1,4 @@
squigglelog output
==================
Generated HTML will end up in this folder.

0
html/log.html Normal file
View File

141
squigglelog.go Normal file
View File

@ -0,0 +1,141 @@
package main
import (
"fmt"
"io/ioutil"
"time"
"strconv"
"text/template"
"os"
"bufio"
"regexp"
"flag"
"encoding/json"
)
const entriesPath = "./entries/"
const templatesPath = "./templates/"
const outputPath = "./html/"
const defaultTemplateFile = "log"
type Entry struct {
Date string
Body []byte
}
func loadEntry(rawDate string) (*Entry, error) {
filename := entriesPath + rawDate
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
// Get raw date parts for formatting
year := rawDate[:4]
month, moErr := strconv.Atoi(rawDate[4:6])
if moErr != nil {
return nil, moErr
}
date, dateErr := strconv.Atoi(rawDate[6:8])
if dateErr != nil {
return nil, dateErr
}
formattedDate := fmt.Sprintf("%d %s %s", date, time.Month(month).String(), year)
return &Entry{Date: formattedDate, Body: body}, nil
}
func main() {
fmt.Println()
fmt.Println(" ~log generator v1.0")
fmt.Println()
// Get any arguments
templateFilePtr := flag.String("template", defaultTemplateFile, "Tildelog template file (defined name).")
flag.Parse()
c := configuration()
if c = nil {
c := configure()
}
if c != nil {
templateFilePtr = c.TemplateFile
}
entryFiles := getEntries()
entries := make([]Entry, len(*entryFiles))
i := 0
for _, file := range *entryFiles {
entry, err := loadEntry(file)
if err != nil {
fmt.Printf("Error, skipping entry %s: %s\n", file, err)
continue
}
fmt.Printf("Adding entry %s...\n", file)
entries[i] = *entry
i++
}
fmt.Printf("Using template %s...\n", *templateFilePtr)
generateLog(entries, *templateFilePtr)
fmt.Printf("Finished! Saved to %slog.html\n", outputPath)
}
type Config struct {
EntriesPath string
TemplateFile string
}
func configuration() *Config {
file, err := os.Open(os.Getenv("HOME") + "/.tildelog")
if err != nil {
return nil
}
d := json.NewDecoder(file)
c := Config{}
err = d.Decode(&c)
if err != nil {
fmt.Printf("Couldn't decode configuration: %s\n", err)
}
return &c
}
var validFileFormat = regexp.MustCompile("^[0-9]{8}$")
func getEntries() *[]string {
files, _ := ioutil.ReadDir(entriesPath)
fileList := make([]string, len(files))
fileCount := 0
// Traverse file list in reverse, i.e. newest to oldest
for i := len(files)-1; i >= 0; i-- {
file := files[i]
if validFileFormat.Match([]byte(file.Name())) {
fileList[fileCount] = file.Name()
fileCount++
}
}
fileList = fileList[:fileCount]
return &fileList
}
func generateLog(entries []Entry, templateFile string) {
file, err := os.Create(outputPath + "log.html")
if err != nil {
panic(err)
}
defer file.Close()
writer := bufio.NewWriter(file)
template, err := template.ParseGlob(templatesPath + "*.html")
if err != nil {
panic(err)
}
template.ExecuteTemplate(writer, templateFile, entries)
writer.Flush()
}

9
templates/log.html Normal file
View File

@ -0,0 +1,9 @@
{{define "log"}}
<div id="squigglelog">
{{range .}}
<hr />
<h3>{{.Date}}</h3>
<p>{{printf "%s" .Body}}</p>
{{end}}
</div>
{{end}}