more flexible tabbing understanding: support tabs and spaces

This commit is contained in:
Dan Ballard 2018-05-13 10:07:54 -07:00
parent cbd04cef0a
commit c4668d3d6c
3 changed files with 28 additions and 13 deletions

View File

@ -16,6 +16,9 @@
- [x] not nested - [x] not nested
- -
asdasd asdasd
- tabbing
- [x] tabs handled
- [ ] tabs migrated
# Nothing done # Nothing done
@ -28,4 +31,4 @@
- [ ] 0x2 other things - [ ] 0x2 other things
- [ ] Group - [ ] Group
- [ ] 0x3 nesting rep - [ ] 0x3 nesting rep
- [x] 2x6 done nested rep - [x] 2x6 done nested rep

View File

@ -24,6 +24,8 @@ const EXPECTED = `# Work
- [ ] nesting 4 - [ ] nesting 4
- -
asdasd asdasd
- tabbing
- [ ] tabs migrated
# Nothing done # Nothing done

View File

@ -7,6 +7,7 @@ import (
"regexp" "regexp"
"io/ioutil" "io/ioutil"
"strconv" "strconv"
"strings"
) )
type RepTask struct { type RepTask struct {
@ -50,18 +51,26 @@ func ProcessFile(ph ProcessHandler, fileName string) {
stack := make([]string, 0) stack := make([]string, 0)
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
indentPattern := ""
startSpaces := regexp.MustCompile("^[\t ]*")
indentLevel := 0
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
// if current line has no spaces at front, reset indent pattern
/*if strings.Trim(line, " \t\n\r") == "" { if len(line) == 0 || (line[0] != ' ' && line[0] != '\t') {
continue indentPattern = ""
}
// if no indent pattern and opening of line is space, set indent pattern
if indentPattern == "" && len(line) > 0 && (line[0] != ' ' || line[0] != '\t') {
indentPattern = startSpaces.FindString(line)
} }
if scanner.Text()[0] == '#' {
continue
}*/
startSpaces := regexp.MustCompile("^ *") // number of times indent pattern repeats at front of line
indentLevel := len(startSpaces.Find([]byte(line)))/4 if indentPattern == "" {
indentLevel = 0
} else {
indentLevel = strings.Count(startSpaces.FindString(line), indentPattern)
}
todo := false todo := false
done := false done := false
var repTask RepTask var repTask RepTask
@ -69,11 +78,11 @@ func ProcessFile(ph ProcessHandler, fileName string) {
stack = stack[: indentLevel+1] stack = stack[: indentLevel+1]
} }
if indentLevel == len(stack)-1 { if indentLevel == len(stack)-1 {
stack[len(stack)-1], todo, done, repTask = getText(line, indentLevel) stack[len(stack)-1], todo, done, repTask = getText(line, indentLevel, indentPattern)
} }
if indentLevel >= len(stack) { if indentLevel >= len(stack) {
row := "" row := ""
row, todo, done, repTask = getText(line, indentLevel) row, todo, done, repTask = getText(line, indentLevel, indentPattern)
stack = append(stack, row) stack = append(stack, row)
} }
@ -82,12 +91,13 @@ func ProcessFile(ph ProcessHandler, fileName string) {
ph.Eof() ph.Eof()
} }
func getText(str string, indentLevel int) (text string, todo bool, done bool, repTask RepTask) { func getText(str string, indentLevel int, indentPattern string) (text string, todo bool, done bool, repTask RepTask) {
//fmt.Printf("indentLevel: %v str: '%s'\n", indentLevel, str ) //fmt.Printf("indentLevel: %v str: '%s'\n", indentLevel, str )
if len(str) < (indentLevel*4 +2) { if len(str) < (indentLevel*4 +2) {
return "", false, false, RepTask{false, 0, 0} return "", false, false, RepTask{false, 0, 0}
} }
text = str[indentLevel*4 +2:] str = strings.TrimLeft(str, strings.Repeat(indentPattern, indentLevel))
text = str[2:]
done = false done = false
todo = false todo = false
repTask.Is = false repTask.Is = false