build out process interface, migrate summary, add migrate

This commit is contained in:
Dan Ballard 2018-05-07 08:58:42 -07:00
parent dd7fea3665
commit 2a69d61aa3
3 changed files with 138 additions and 35 deletions

View File

@ -1 +1,73 @@
package mdbj_migrate package main
import (
"os"
"time"
"log"
"github.com/dballard/markdown-bullet-journal/process"
"fmt"
)
const template = `# Work
- [ ] read emails
# Home
- [ ] do laundry
- [ ] Weekly shopping
- [ ] apples
- [ ] bread
# Daily Workout
## Upper Body
- [ ] 0x10 pushups
## Core
- [ ] 0x10 crunches
`
type processHandler struct {
File *os.File
}
func (ph *processHandler) Writeln(line string) {
ph.File.WriteString(line + "\n")
}
// NOP
func (ph *processHandler) Eof() {}
func (ph *processHandler) NewFile() {}
func (ph *processHandler) ProcessLine(line string, stack []string, todo bool, done bool) {
// TODO: handle [x] numXnum
if !done {
ph.Writeln(line)
}
}
func main() {
ph := new(processHandler)
files := process.GetFiles()
fileName := time.Now().Format("2006-01-02") + ".md"
if _, err := os.Stat(fileName); os.IsNotExist(err) {
ph.File, err = os.Create(fileName)
if err != nil {
log.Fatal("Cannot open: ", fileName, " > ", err)
}
defer ph.File.Close()
} else {
log.Fatalf("File " + fileName + " already exists!")
}
if len(files) == 0 {
// create first from template
fmt.Println("Generating " + fileName + " from template")
ph.File.WriteString(template)
} else {
lastFile := files[len(files)-1]
fmt.Println ("Migrating " + lastFile + " to " + fileName)
process.ProcessFile(ph, lastFile)
}
}

View File

@ -4,21 +4,69 @@ import (
"runtime" "runtime"
"os" "os"
"github.com/dballard/markdown-bullet-journal/process" "github.com/dballard/markdown-bullet-journal/process"
"log"
"strconv"
"strings"
) )
type processHandler struct { type processHandler struct {
File *os.File File *os.File
totalCount, doneCount int
header string
headerPrinted bool
} }
func (ph *processHandler) Writeln(line string) { func (ph *processHandler) Writeln(line string) {
ph.File.WriteString(line + "\n") ph.File.WriteString(line + "\n")
} }
func (ph *processHandler) NewFile() {
ph.totalCount = 0
ph.doneCount = 0
ph.header = ""
ph.headerPrinted = false
}
func (ph *processHandler) Eof() {
ph.Writeln(strconv.Itoa(ph.doneCount) + " / " + strconv.Itoa(ph.totalCount))
}
func (ph *processHandler) ProcessLine(line string, stack []string, todo bool, done bool) {
if strings.Trim(line, " \t\n\r") == "" {
return
}
if line[0] == '#' {
ph.header = line[2:]
ph.headerPrinted = false;
return
}
if todo {
ph.totalCount += 1
}
if done {
if !ph.headerPrinted {
ph.Writeln(" # " + ph.header)
ph.headerPrinted = true
}
ph.doneCount += 1
// TODO: Math for [x] numXnum
ph.Writeln(" " + strings.Join(stack, " / "))
}
}
func main() { func main() {
ph := new(processHandler) ph := new(processHandler)
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
var err error
ph.File, err = os.Open("summary.md")
if err != nil {
log.Fatal("Cannot open summary.md: ", err)
}
defer ph.File.Close()
} else { } else {
ph.File = os.Stdout ph.File = os.Stdout
} }

View File

@ -4,14 +4,15 @@ import (
"os" "os"
"log" "log"
"bufio" "bufio"
"strings"
"regexp" "regexp"
"io/ioutil" "io/ioutil"
"strconv"
) )
type ProcessHandler interface { type ProcessHandler interface {
Writeln(line string) Writeln(line string)
ProcessLine(line string, stack []string, todo bool, done bool)
Eof()
NewFile()
} }
func GetFiles() (filteredFiles []string) { func GetFiles() (filteredFiles []string) {
@ -38,58 +39,40 @@ func ProcessFile(ph ProcessHandler, fileName string) {
log.Fatal(err) log.Fatal(err)
} }
defer file.Close() defer file.Close()
ph.NewFile()
header := ""
headerPrinted := false
stack := make([]string, 0) stack := make([]string, 0)
total := 0
doneCount := 0
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
if scanner.Text() == "# Daily Health" { line := scanner.Text()
break
} /*if strings.Trim(line, " \t\n\r") == "" {
if strings.Trim(scanner.Text(), " \t\n\r") == "" {
continue continue
} }
if scanner.Text()[0] == '#' { if scanner.Text()[0] == '#' {
header = scanner.Text()[2:]
headerPrinted = false;
continue continue
} }*/
startSpaces := regexp.MustCompile("^ *") startSpaces := regexp.MustCompile("^ *")
indentLevel := len(startSpaces.Find([]byte(scanner.Text())))/4 indentLevel := len(startSpaces.Find([]byte(line)))/4
todo := false todo := false
done := false done := false
if indentLevel < len(stack)-1 { if indentLevel < len(stack)-1 {
stack = stack[: indentLevel+1] stack = stack[: indentLevel+1]
} }
if indentLevel == len(stack)-1 { if indentLevel == len(stack)-1 {
stack[len(stack)-1], todo, done = getText(scanner.Text(), indentLevel) stack[len(stack)-1], todo, done = getText(line, indentLevel)
} }
if indentLevel >= len(stack) { if indentLevel >= len(stack) {
line := "" row := ""
line, todo, done = getText(scanner.Text(), indentLevel) row, todo, done = getText(line, indentLevel)
stack = append(stack, line) stack = append(stack, row)
} }
if todo { ph.ProcessLine(line, stack, todo, done)
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)) ph.Eof()
} }
func getText(str string, indentLevel int) (text string, todo bool, done bool) { func getText(str string, indentLevel int) (text string, todo bool, done bool) {