2018-05-07 15:14:59 +00:00
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"log"
|
|
|
|
"bufio"
|
|
|
|
"regexp"
|
|
|
|
"io/ioutil"
|
2018-05-08 17:07:20 +00:00
|
|
|
"strconv"
|
2018-05-07 15:14:59 +00:00
|
|
|
)
|
|
|
|
|
2018-05-08 17:07:20 +00:00
|
|
|
type RepTask struct {
|
|
|
|
Is bool
|
|
|
|
A, B int
|
|
|
|
}
|
|
|
|
|
2018-05-07 15:14:59 +00:00
|
|
|
type ProcessHandler interface {
|
|
|
|
Writeln(line string)
|
2018-05-08 18:11:58 +00:00
|
|
|
ProcessLine(line string, indentLevel int, stack []string, todo bool, done bool, repTask RepTask)
|
2018-05-07 15:58:42 +00:00
|
|
|
Eof()
|
|
|
|
NewFile()
|
2018-05-07 15:14:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetFiles() (filteredFiles []string) {
|
|
|
|
// open current directory
|
|
|
|
files, err := ioutil.ReadDir("./")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredFiles = []string{}
|
|
|
|
|
|
|
|
// process files of '2*.md'
|
|
|
|
for _, file := range files {
|
|
|
|
if file.Name()[0] == '2' && file.Name()[len(file.Name())-3:] == ".md" {
|
|
|
|
filteredFiles = append(filteredFiles, file.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProcessFile(ph ProcessHandler, fileName string) {
|
|
|
|
file, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2018-05-07 15:58:42 +00:00
|
|
|
ph.NewFile()
|
2018-05-07 15:14:59 +00:00
|
|
|
|
|
|
|
stack := make([]string, 0)
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
2018-05-07 15:58:42 +00:00
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
/*if strings.Trim(line, " \t\n\r") == "" {
|
2018-05-07 15:14:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if scanner.Text()[0] == '#' {
|
|
|
|
continue
|
2018-05-07 15:58:42 +00:00
|
|
|
}*/
|
2018-05-07 15:14:59 +00:00
|
|
|
|
|
|
|
startSpaces := regexp.MustCompile("^ *")
|
2018-05-07 15:58:42 +00:00
|
|
|
indentLevel := len(startSpaces.Find([]byte(line)))/4
|
2018-05-07 15:14:59 +00:00
|
|
|
todo := false
|
|
|
|
done := false
|
2018-05-08 17:07:20 +00:00
|
|
|
var repTask RepTask
|
2018-05-07 15:14:59 +00:00
|
|
|
if indentLevel < len(stack)-1 {
|
|
|
|
stack = stack[: indentLevel+1]
|
|
|
|
}
|
|
|
|
if indentLevel == len(stack)-1 {
|
2018-05-08 17:07:20 +00:00
|
|
|
stack[len(stack)-1], todo, done, repTask = getText(line, indentLevel)
|
2018-05-07 15:14:59 +00:00
|
|
|
}
|
|
|
|
if indentLevel >= len(stack) {
|
2018-05-07 15:58:42 +00:00
|
|
|
row := ""
|
2018-05-08 17:07:20 +00:00
|
|
|
row, todo, done, repTask = getText(line, indentLevel)
|
2018-05-07 15:58:42 +00:00
|
|
|
stack = append(stack, row)
|
2018-05-07 15:14:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 18:11:58 +00:00
|
|
|
ph.ProcessLine(line, indentLevel, stack, todo, done, repTask)
|
2018-05-07 15:14:59 +00:00
|
|
|
}
|
2018-05-07 15:58:42 +00:00
|
|
|
ph.Eof()
|
2018-05-07 15:14:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 17:07:20 +00:00
|
|
|
func getText(str string, indentLevel int) (text string, todo bool, done bool, repTask RepTask) {
|
2018-05-07 15:14:59 +00:00
|
|
|
//fmt.Printf("indentLevel: %v str: '%s'\n", indentLevel, str )
|
|
|
|
if len(str) < (indentLevel*4 +2) {
|
2018-05-08 17:07:20 +00:00
|
|
|
return "", false, false, RepTask{false, 0, 0}
|
2018-05-07 15:14:59 +00:00
|
|
|
}
|
|
|
|
text = str[indentLevel*4 +2:]
|
|
|
|
done = false
|
|
|
|
todo = false
|
2018-05-08 17:07:20 +00:00
|
|
|
repTask.Is = false
|
2018-05-07 15:14:59 +00:00
|
|
|
if text[0] == '[' {
|
|
|
|
todo = true
|
|
|
|
if text[1] == 'x' || text[1] == 'X' {
|
|
|
|
done = true
|
|
|
|
}
|
|
|
|
if len(text) > 4 {
|
|
|
|
text = text[4:]
|
|
|
|
}
|
2018-05-08 17:07:20 +00:00
|
|
|
|
|
|
|
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]:]
|
|
|
|
}
|
2018-05-07 15:14:59 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|