start process core. migrate summary to it

This commit is contained in:
Dan Ballard 2018-05-07 08:14:59 -07:00
parent 6417fe8d91
commit dd7fea3665
3 changed files with 134 additions and 95 deletions

1
mdbj-migrate/migrate.go Normal file
View File

@ -0,0 +1 @@
package mdbj_migrate

View File

@ -1,107 +1,32 @@
package main package main
import ( import (
"io/ioutil" "runtime"
"log"
"os" "os"
"bufio" "github.com/dballard/markdown-bullet-journal/process"
"strings"
"fmt"
"regexp"
) )
type processHandler struct {
File *os.File
}
func (ph *processHandler) Writeln(line string) {
ph.File.WriteString(line + "\n")
}
func main() { func main() {
files, err := ioutil.ReadDir("./") ph := new(processHandler)
if err != nil {
log.Fatal(err) if runtime.GOOS == "windows" {
} else {
ph.File = os.Stdout
} }
files := process.GetFiles()
for _, file := range files { for _, file := range files {
if file.Name()[len(file.Name())-3:] == ".md" { ph.Writeln("")
genReport(file) ph.Writeln(file)
} process.ProcessFile(ph, file)
} }
}
func genReport(fileInfo os.FileInfo) {
file, err := os.Open(fileInfo.Name())
if err != nil {
log.Fatal(err)
}
defer file.Close()
fmt.Println("")
fmt.Println(fileInfo.Name())
header := ""
headerPrinted := false
stack := make([]string, 0)
total := 0
doneCount := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if scanner.Text() == "# Daily Health" {
break
}
if strings.Trim(scanner.Text(), " \t\n\r") == "" {
continue
}
if scanner.Text()[0] == '#' {
header = scanner.Text()[2:]
headerPrinted = false;
continue
}
startSpaces := regexp.MustCompile("^ *")
indentLevel := len(startSpaces.Find([]byte(scanner.Text())))/4
todo := false
done := false
if indentLevel < len(stack)-1 {
stack = stack[: indentLevel+1]
}
if indentLevel == len(stack)-1 {
stack[len(stack)-1], todo, done = getText(scanner.Text(), indentLevel)
}
if indentLevel >= len(stack) {
line := ""
line, todo, done = getText(scanner.Text(), indentLevel)
stack = append(stack, line)
}
if todo {
total += 1
}
if done {
if !headerPrinted {
fmt.Println(" # " + header)
headerPrinted = true
}
doneCount += 1
fmt.Println(" " + strings.Join(stack, " / "))
}
}
fmt.Println(doneCount, "/", total)
}
func getText(str string, indentLevel int) (text string, todo bool, done bool) {
//fmt.Printf("indentLevel: %v str: '%s'\n", indentLevel, str )
if len(str) < (indentLevel*4 +2) {
return "", false, false
}
text = str[indentLevel*4 +2:]
done = false
todo = false
if text[0] == '[' {
todo = true
if text[1] == 'x' || text[1] == 'X' {
done = true
}
if len(text) > 4 {
text = text[4:]
}
}
return
} }

113
process/process.go Normal file
View File

@ -0,0 +1,113 @@
package process
import (
"os"
"log"
"bufio"
"strings"
"regexp"
"io/ioutil"
"strconv"
)
type ProcessHandler interface {
Writeln(line string)
}
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()
header := ""
headerPrinted := false
stack := make([]string, 0)
total := 0
doneCount := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if scanner.Text() == "# Daily Health" {
break
}
if strings.Trim(scanner.Text(), " \t\n\r") == "" {
continue
}
if scanner.Text()[0] == '#' {
header = scanner.Text()[2:]
headerPrinted = false;
continue
}
startSpaces := regexp.MustCompile("^ *")
indentLevel := len(startSpaces.Find([]byte(scanner.Text())))/4
todo := false
done := false
if indentLevel < len(stack)-1 {
stack = stack[: indentLevel+1]
}
if indentLevel == len(stack)-1 {
stack[len(stack)-1], todo, done = getText(scanner.Text(), indentLevel)
}
if indentLevel >= len(stack) {
line := ""
line, todo, done = getText(scanner.Text(), indentLevel)
stack = append(stack, line)
}
if todo {
total += 1
}
if done {
if !headerPrinted {
ph.Writeln(" # " + header)
headerPrinted = true
}
doneCount += 1
ph.Writeln(" " + strings.Join(stack, " / "))
}
}
ph.Writeln(strconv.Itoa(doneCount) + " / " + strconv.Itoa(total))
}
func getText(str string, indentLevel int) (text string, todo bool, done bool) {
//fmt.Printf("indentLevel: %v str: '%s'\n", indentLevel, str )
if len(str) < (indentLevel*4 +2) {
return "", false, false
}
text = str[indentLevel*4 +2:]
done = false
todo = false
if text[0] == '[' {
todo = true
if text[1] == 'x' || text[1] == 'X' {
done = true
}
if len(text) > 4 {
text = text[4:]
}
}
return
}