commit e75f931290b8697099c84eb0d1277b738c6cc63b Author: Matt Baer Date: Tue Jan 27 21:35:07 2015 -0500 Simple squigglelog generator diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..74cae94 --- /dev/null +++ b/LICENSE @@ -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. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..ddfdba7 --- /dev/null +++ b/README.md @@ -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"}} + + + My ~log! + + +

~log

+

Welcome to my ~log.

+ {{template "log" .}} + + +{{end}} +``` diff --git a/entries/README.md b/entries/README.md new file mode 100644 index 0000000..9a5f994 --- /dev/null +++ b/entries/README.md @@ -0,0 +1,4 @@ +squigglelog entries +=================== + +Create each entry as a new file here. Naming scheme: **YYYYMMDD**. Add formatting with HTML. diff --git a/html/README.md b/html/README.md new file mode 100644 index 0000000..42b4389 --- /dev/null +++ b/html/README.md @@ -0,0 +1,4 @@ +squigglelog output +================== + +Generated HTML will end up in this folder. diff --git a/html/log.html b/html/log.html new file mode 100644 index 0000000..e69de29 diff --git a/squigglelog.go b/squigglelog.go new file mode 100644 index 0000000..87dc158 --- /dev/null +++ b/squigglelog.go @@ -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() +} + diff --git a/templates/log.html b/templates/log.html new file mode 100644 index 0000000..52dc0d3 --- /dev/null +++ b/templates/log.html @@ -0,0 +1,9 @@ +{{define "log"}} +
+ {{range .}} +
+

{{.Date}}

+

{{printf "%s" .Body}}

+ {{end}} +
+{{end}}