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-13 17:07:54 +00:00
|
|
|
"strings"
|
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)
|
2018-05-13 17:07:54 +00:00
|
|
|
indentPattern := ""
|
|
|
|
startSpaces := regexp.MustCompile("^[\t ]*")
|
|
|
|
indentLevel := 0
|
2018-05-07 15:14:59 +00:00
|
|
|
for scanner.Scan() {
|
2018-05-07 15:58:42 +00:00
|
|
|
line := scanner.Text()
|
2018-05-13 17:07:54 +00:00
|
|
|
// 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)
|
2018-05-07 15:14:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 17:07:54 +00:00
|
|
|
// number of times indent pattern repeats at front of line
|
|
|
|
if indentPattern == "" {
|
|
|
|
indentLevel = 0
|
|
|
|
} else {
|
|
|
|
indentLevel = strings.Count(startSpaces.FindString(line), indentPattern)
|
|
|
|
}
|
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-13 17:07:54 +00:00
|
|
|
stack[len(stack)-1], todo, done, repTask = getText(line, indentLevel, indentPattern)
|
2018-05-07 15:14:59 +00:00
|
|
|
}
|
|
|
|
if indentLevel >= len(stack) {
|
2018-05-07 15:58:42 +00:00
|
|
|
row := ""
|
2018-05-13 17:07:54 +00:00
|
|
|
row, todo, done, repTask = getText(line, indentLevel, indentPattern)
|
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-13 17:07:54 +00:00
|
|
|
func getText(str string, indentLevel int, indentPattern string) (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
|
|
|
}
|
2018-05-13 17:07:54 +00:00
|
|
|
str = strings.TrimLeft(str, strings.Repeat(indentPattern, indentLevel))
|
|
|
|
text = str[2:]
|
2018-05-07 15:14:59 +00:00
|
|
|
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
|
|
|
|
}
|