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
-
asdasd
- tabbing
- [x] tabs handled
- [ ] tabs migrated
# Nothing done
@ -28,4 +31,4 @@
- [ ] 0x2 other things
- [ ] Group
- [ ] 0x3 nesting rep
- [x] 2x6 done nested rep
- [x] 2x6 done nested rep

View File

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

View File

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