diff --git a/index.tmpl b/index.tmpl new file mode 100644 index 0000000..a466348 --- /dev/null +++ b/index.tmpl @@ -0,0 +1,19 @@ + + + + life reports. + + + +
+

life reports

+ +
+
+ + + diff --git a/lifereport.go b/lifereport.go index cc5ee70..d6fd6e6 100644 --- a/lifereport.go +++ b/lifereport.go @@ -16,6 +16,7 @@ type ( // various areas of a given person's life for the given time period -- // in this case, a month. Report struct { + File string Num int Month string Year int `json:"year"` @@ -50,8 +51,8 @@ func ParseReport(f *os.File) (*Report, error) { // Determine report number and month from filename in the format // NNN-month.json - baseName := path.Base(f.Name()) - fd := strings.Split(baseName[:strings.Index(baseName, ".json")], "-") + r.File = path.Base(f.Name()) + fd := strings.Split(r.File[:strings.Index(r.File, ".json")], "-") if len(fd) >= 2 { r.Num, err = strconv.Atoi(fd[0]) if err != nil { @@ -66,3 +67,10 @@ func ParseReport(f *os.File) (*Report, error) { return &r, nil } + +// ByNum implements sort.Interface for []Report based on the Num field. +type ByNum []Report + +func (n ByNum) Len() int { return len(n) } +func (n ByNum) Swap(i, j int) { n[i], n[j] = n[j], n[i] } +func (n ByNum) Less(i, j int) bool { return n[i].Num < n[j].Num } diff --git a/lifereport/report.go b/lifereport/report.go index 1cd9f9e..6e2bfe7 100644 --- a/lifereport/report.go +++ b/lifereport/report.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "path" + "sort" "strings" report "github.com/thebaer/life-report" @@ -32,6 +33,8 @@ func main() { return } + var reports []report.Report + for _, file := range files { if file.Name() == "template.json" || !strings.HasSuffix(file.Name(), ".json") { continue @@ -64,5 +67,23 @@ func main() { t.Execute(o, r) o.Close() + + r.File = outName + reports = append(reports, *r) } + + // Create index file + i, err := os.Create(path.Join(dir, "index.html")) + if err != nil { + fmt.Fprintf(os.Stdout, "Unable to write index: %v\n", err) + return + } + it, err := template.ParseFiles("../index.tmpl") + if err != nil { + fmt.Printf("Unable to parse index template: %v\n", err) + return + } + sort.Sort(sort.Reverse(report.ByNum(reports))) + it.Execute(i, reports) + i.Close() }