From 6f79ecbd2884ea310105b4aa239b33f3d552e9cf Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Tue, 8 May 2018 10:07:20 -0700 Subject: [PATCH] add numXnum suport to process and summary. add summary test --- mdbj-summary/2018-05-07-TEST.md | 28 +++++++++++++++ mdbj-summary/summary.go | 12 +++++-- mdbj-summary/summary_test.go | 60 +++++++++++++++++++++++++++++++++ process/process.go | 30 +++++++++++++---- 4 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 mdbj-summary/2018-05-07-TEST.md create mode 100644 mdbj-summary/summary_test.go diff --git a/mdbj-summary/2018-05-07-TEST.md b/mdbj-summary/2018-05-07-TEST.md new file mode 100644 index 0000000..437605f --- /dev/null +++ b/mdbj-summary/2018-05-07-TEST.md @@ -0,0 +1,28 @@ +# Work + +- [ ] Write tests + - [x] summary + - [ ] migrate + +# Test Data + +- note +- +- [ ] nesting1 + - [ ] nesting 2 + - [ ] nesting 3 + - [ ] nesting 4 + - [x] nesting 5 +- [x] not nested + - + asdasd + +# Nothing done + +- [ ] not done +- note + +# Repetition + +- [x] 5x5 things +- [ ] 0x2 other things \ No newline at end of file diff --git a/mdbj-summary/summary.go b/mdbj-summary/summary.go index af57fc1..4e3dd3f 100644 --- a/mdbj-summary/summary.go +++ b/mdbj-summary/summary.go @@ -32,7 +32,7 @@ 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) { +func (ph *processHandler) ProcessLine(line string, stack []string, todo bool, done bool, repTask process.RepTask) { if strings.Trim(line, " \t\n\r") == "" { return } @@ -48,12 +48,16 @@ func (ph *processHandler) ProcessLine(line string, stack []string, todo bool, do if done { if !ph.headerPrinted { - ph.Writeln(" # " + ph.header) + ph.Writeln("\t# " + ph.header) ph.headerPrinted = true } ph.doneCount += 1 // TODO: Math for [x] numXnum - ph.Writeln(" " + strings.Join(stack, " / ")) + repStr := "" + if repTask.Is { + repStr = strconv.Itoa( repTask.A * repTask.B) + } + ph.Writeln("\t\t" + repStr + strings.Join(stack, " / ")) } } @@ -77,4 +81,6 @@ func main() { ph.Writeln(file) process.ProcessFile(ph, file) } + + // If windows open summary.md } diff --git a/mdbj-summary/summary_test.go b/mdbj-summary/summary_test.go new file mode 100644 index 0000000..c40d76d --- /dev/null +++ b/mdbj-summary/summary_test.go @@ -0,0 +1,60 @@ +package main + +import ( + "testing" + "os" + "github.com/dballard/markdown-bullet-journal/process" + "strings" + "math" + "bytes" +) + +const EXPECTED = ` +2018-05-07-TEST.md + # Work + Write tests / summary + # Test Data + nesting1 / nesting 2 / nesting 3 / nesting 4 / nesting 5 + not nested + # Repetition + 25 things +4 / 12 +` + +func TestSummary(t *testing.T) { + ph := new(processHandler) + r, w, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + ph.File = w + + files := process.GetFiles() + for _, file := range files { + ph.Writeln("") + ph.Writeln(file) + process.ProcessFile(ph, file) + } + + w.Close() + var result = make([]byte, 1000) + n, _ := r.Read(result) + + //fmt.Printf("n:%v len(res):%v len(EXP):%v\n", n, len(result), len(EXPECTED)) + if ! bytes.Equal(result[:n], []byte(EXPECTED)) { + var diffLoc = 0 + for i, ch := range EXPECTED { + //fmt.Printf("%v/%v: %v %v\n", i, n, ch, result[i]) + if i > n-1 || result[i] != byte(ch) { + diffLoc = i + break + } + } + //fmt.Println(diffLoc) + line := strings.Count(string(result[:diffLoc]), "\n") + errorStr := string(result[int(math.Max(0, float64(diffLoc - 10))) : int(math.Min(float64(len(result)), float64(diffLoc + 10))) ]) + + t.Errorf("Summary results do not match expected:\nfirst difference at line %v: '%v'\n%v<---->\n%v\n", line, errorStr, string(result), EXPECTED) + + } +} diff --git a/process/process.go b/process/process.go index 10a7d60..71a070a 100644 --- a/process/process.go +++ b/process/process.go @@ -6,11 +6,17 @@ import ( "bufio" "regexp" "io/ioutil" + "strconv" ) +type RepTask struct { + Is bool + A, B int +} + type ProcessHandler interface { Writeln(line string) - ProcessLine(line string, stack []string, todo bool, done bool) + ProcessLine(line string, stack []string, todo bool, done bool, repTask RepTask) Eof() NewFile() } @@ -58,31 +64,33 @@ func ProcessFile(ph ProcessHandler, fileName string) { indentLevel := len(startSpaces.Find([]byte(line)))/4 todo := false done := false + var repTask RepTask if indentLevel < len(stack)-1 { stack = stack[: indentLevel+1] } if indentLevel == len(stack)-1 { - stack[len(stack)-1], todo, done = getText(line, indentLevel) + stack[len(stack)-1], todo, done, repTask = getText(line, indentLevel) } if indentLevel >= len(stack) { row := "" - row, todo, done = getText(line, indentLevel) + row, todo, done, repTask = getText(line, indentLevel) stack = append(stack, row) } - ph.ProcessLine(line, stack, todo, done) + ph.ProcessLine(line, stack, todo, done, repTask) } ph.Eof() } -func getText(str string, indentLevel int) (text string, todo bool, done bool) { +func getText(str string, indentLevel int) (text string, todo bool, done bool, repTask RepTask) { //fmt.Printf("indentLevel: %v str: '%s'\n", indentLevel, str ) if len(str) < (indentLevel*4 +2) { - return "", false, false + return "", false, false, RepTask{false, 0, 0} } text = str[indentLevel*4 +2:] done = false todo = false + repTask.Is = false if text[0] == '[' { todo = true if text[1] == 'x' || text[1] == 'X' { @@ -91,6 +99,16 @@ func getText(str string, indentLevel int) (text string, todo bool, done bool) { if len(text) > 4 { text = text[4:] } + + repTaskRegExp := regexp.MustCompile("^([0-9]*)[xX]([0-9]*)") + if repTaskRegExp.MatchString(text) { + repTask.Is = true + matches := repTaskRegExp.FindStringSubmatch(text) + repTask.A, _ = strconv.Atoi(matches[1]) + repTask.B, _ = strconv.Atoi(matches[2]) + loc := repTaskRegExp.FindIndex([]byte(text)) + text = text[loc[1]:] + } } return } \ No newline at end of file