-
Notifications
You must be signed in to change notification settings - Fork 2
/
formatter.go
95 lines (83 loc) · 1.87 KB
/
formatter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package traindown
import (
"fmt"
"strings"
)
// Formatter formats Traindown documents
type Formatter struct {
l *Lexer
}
// NewFormatter returns a pointer to a Formatter
func NewFormatter() (*Formatter, error) {
lexer, err := NewLexer()
if err != nil {
return &Formatter{}, err
}
return &Formatter{&lexer}, nil
}
func spacer(inSession bool, inPerformance bool) string {
var spacer string
if inSession {
spacer = ""
} else if inPerformance {
spacer = " "
} else {
spacer = " "
}
return spacer
}
// Format takes a Traindown string and returns a prettier version of it.
func (f Formatter) Format(txt string) (string, error) {
tokens, err := f.l.Scan([]byte(txt))
if err != nil {
return "", fmt.Errorf("Failed to format: %q", err)
}
var s strings.Builder
inSession := true
inPerformance := false
for _, tok := range tokens {
switch tok.Name() {
case "DATE":
s.WriteString("@ ")
s.WriteString(tok.Value())
s.WriteString("\r\n")
case "FAILS":
s.WriteString(" ")
s.WriteString(tok.Value())
s.WriteString("f")
case "LOAD":
inPerformance = true
s.WriteString("\r\n")
s.WriteString(" ")
s.WriteString(tok.Value())
case "METADATA":
s.WriteString("\r\n")
s.WriteString(spacer(inSession, inPerformance))
s.WriteString("* ")
s.WriteString(tok.Value())
case "MOVEMENT", "MOVEMENT_SS":
inSession = false
inPerformance = false
s.WriteString("\r\n\r\n")
if tok.Value() == "MOVEMENT_SS" {
s.WriteString("+ ")
}
s.WriteString(tok.Value())
s.WriteString(":")
case "NOTE":
s.WriteString("\r\n")
s.WriteString(spacer(inSession, inPerformance))
s.WriteString("* ")
s.WriteString(tok.Value())
case "REPS":
s.WriteString(" ")
s.WriteString(tok.Value())
s.WriteString("r")
case "SETS":
s.WriteString(" ")
s.WriteString(tok.Value())
s.WriteString("s")
}
}
return s.String(), nil
}