From 2a69d61aa322e810c6c14d3f8655498298ef975a Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Mon, 7 May 2018 08:58:42 -0700 Subject: [PATCH] build out process interface, migrate summary, add migrate --- mdbj-migrate/migrate.go | 74 ++++++++++++++++++++++++++++++++++++++++- mdbj-summary/summary.go | 52 +++++++++++++++++++++++++++-- process/process.go | 47 +++++++++----------------- 3 files changed, 138 insertions(+), 35 deletions(-) diff --git a/mdbj-migrate/migrate.go b/mdbj-migrate/migrate.go index 8f0cea1..db2683f 100644 --- a/mdbj-migrate/migrate.go +++ b/mdbj-migrate/migrate.go @@ -1 +1,73 @@ -package mdbj_migrate +package main + +import ( + "os" + "time" + "log" + "github.com/dballard/markdown-bullet-journal/process" + "fmt" +) + +const template = `# Work +- [ ] read emails + +# Home +- [ ] do laundry +- [ ] Weekly shopping + - [ ] apples + - [ ] bread + +# Daily Workout + +## Upper Body +- [ ] 0x10 pushups + +## Core +- [ ] 0x10 crunches +` + +type processHandler struct { + File *os.File +} + +func (ph *processHandler) Writeln(line string) { + ph.File.WriteString(line + "\n") +} + +// NOP +func (ph *processHandler) Eof() {} +func (ph *processHandler) NewFile() {} + +func (ph *processHandler) ProcessLine(line string, stack []string, todo bool, done bool) { + // TODO: handle [x] numXnum + if !done { + ph.Writeln(line) + } +} + +func main() { + ph := new(processHandler) + files := process.GetFiles() + + fileName := time.Now().Format("2006-01-02") + ".md" + + if _, err := os.Stat(fileName); os.IsNotExist(err) { + ph.File, err = os.Create(fileName) + if err != nil { + log.Fatal("Cannot open: ", fileName, " > ", err) + } + defer ph.File.Close() + } else { + log.Fatalf("File " + fileName + " already exists!") + } + + if len(files) == 0 { + // create first from template + fmt.Println("Generating " + fileName + " from template") + ph.File.WriteString(template) + } else { + lastFile := files[len(files)-1] + fmt.Println ("Migrating " + lastFile + " to " + fileName) + process.ProcessFile(ph, lastFile) + } +} diff --git a/mdbj-summary/summary.go b/mdbj-summary/summary.go index 7b67c09..af57fc1 100644 --- a/mdbj-summary/summary.go +++ b/mdbj-summary/summary.go @@ -4,21 +4,69 @@ import ( "runtime" "os" "github.com/dballard/markdown-bullet-journal/process" + "log" + "strconv" + "strings" ) type processHandler struct { - File *os.File + File *os.File + totalCount, doneCount int + header string + headerPrinted bool } func (ph *processHandler) Writeln(line string) { ph.File.WriteString(line + "\n") } +func (ph *processHandler) NewFile() { + ph.totalCount = 0 + ph.doneCount = 0 + ph.header = "" + ph.headerPrinted = false + +} + +func (ph *processHandler) Eof() { + ph.Writeln(strconv.Itoa(ph.doneCount) + " / " + strconv.Itoa(ph.totalCount)) +} + +func (ph *processHandler) ProcessLine(line string, stack []string, todo bool, done bool) { + if strings.Trim(line, " \t\n\r") == "" { + return + } + if line[0] == '#' { + ph.header = line[2:] + ph.headerPrinted = false; + return + } + + if todo { + ph.totalCount += 1 + } + + if done { + if !ph.headerPrinted { + ph.Writeln(" # " + ph.header) + ph.headerPrinted = true + } + ph.doneCount += 1 + // TODO: Math for [x] numXnum + ph.Writeln(" " + strings.Join(stack, " / ")) + } +} + func main() { ph := new(processHandler) if runtime.GOOS == "windows" { - + var err error + ph.File, err = os.Open("summary.md") + if err != nil { + log.Fatal("Cannot open summary.md: ", err) + } + defer ph.File.Close() } else { ph.File = os.Stdout } diff --git a/process/process.go b/process/process.go index 7745261..10a7d60 100644 --- a/process/process.go +++ b/process/process.go @@ -4,14 +4,15 @@ import ( "os" "log" "bufio" - "strings" "regexp" "io/ioutil" - "strconv" ) type ProcessHandler interface { Writeln(line string) + ProcessLine(line string, stack []string, todo bool, done bool) + Eof() + NewFile() } func GetFiles() (filteredFiles []string) { @@ -38,58 +39,40 @@ func ProcessFile(ph ProcessHandler, fileName string) { log.Fatal(err) } defer file.Close() + ph.NewFile() - header := "" - headerPrinted := false stack := make([]string, 0) - total := 0 - doneCount := 0 - scanner := bufio.NewScanner(file) for scanner.Scan() { - if scanner.Text() == "# Daily Health" { - break - } - if strings.Trim(scanner.Text(), " \t\n\r") == "" { + line := scanner.Text() + + /*if strings.Trim(line, " \t\n\r") == "" { continue } if scanner.Text()[0] == '#' { - header = scanner.Text()[2:] - headerPrinted = false; continue - } + }*/ startSpaces := regexp.MustCompile("^ *") - indentLevel := len(startSpaces.Find([]byte(scanner.Text())))/4 + indentLevel := len(startSpaces.Find([]byte(line)))/4 todo := false done := false if indentLevel < len(stack)-1 { stack = stack[: indentLevel+1] } if indentLevel == len(stack)-1 { - stack[len(stack)-1], todo, done = getText(scanner.Text(), indentLevel) + stack[len(stack)-1], todo, done = getText(line, indentLevel) } if indentLevel >= len(stack) { - line := "" - line, todo, done = getText(scanner.Text(), indentLevel) - stack = append(stack, line) + row := "" + row, todo, done = getText(line, indentLevel) + stack = append(stack, row) } - if todo { - total += 1 - } - - if done { - if !headerPrinted { - ph.Writeln(" # " + header) - headerPrinted = true - } - doneCount += 1 - ph.Writeln(" " + strings.Join(stack, " / ")) - } + ph.ProcessLine(line, stack, todo, done) } - ph.Writeln(strconv.Itoa(doneCount) + " / " + strconv.Itoa(total)) + ph.Eof() } func getText(str string, indentLevel int) (text string, todo bool, done bool) {