-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdocument.go
244 lines (220 loc) · 4.31 KB
/
document.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"errors"
"io/ioutil"
"strings"
"time"
)
type TakeMark uint8
const (
Unmarked TakeMark = 0
// Denotes a good take.
Good TakeMark = 1
// Denotes a bad take.
Bad TakeMark = 2
// Used to denote a timespan where an audio sync peak, usually created with a clap or clapperboard, can be found.
// There can be multiple Sync takes, but only the first one will be used to determine the sync offset.
Sync TakeMark = 3
)
// Metadata prefixes used in scripts. They should be omited from selectable chunks.
func getMetaPrefixes() []string {
return []string{"TODO", "REF", "NOTE", "BIT"}
}
func (m TakeMark) String() string {
switch m {
case Unmarked:
return "unmarked"
case Good:
return "good"
case Bad:
return "bad"
case Sync:
return "sync"
}
return "unknown"
}
type Document struct {
headers []Header
syncTakes []Take
// Presice timestamp of the audio sync peak
SyncOffset time.Duration
}
func (doc *Document) CountChunks() int {
c := 0
for _, h := range doc.headers {
c += len(h.Chunks)
}
return c
}
func (doc *Document) GetChunk(index int) *Chunk {
for _, h := range doc.headers {
if index-len(h.Chunks) < 0 {
return &h.Chunks[index]
}
index -= len(h.Chunks)
}
return nil
}
type chunkOrder uint8
const (
chunk_normal chunkOrder = 0
chunk_meta chunkOrder = 1
)
type Header struct {
Text string
Chunks []Chunk
MetaChunks []MetaChunk
chunkOrder []chunkOrder
}
type Chunk struct {
Content string
Takes []Take
}
// A chunk that contains content that should not be selectable for takes.
type MetaChunk struct {
Content string
}
func (h *Header) AddChunk(chunk interface{}) error {
var ord chunkOrder
switch c := chunk.(type) {
case Chunk:
ord = chunk_normal
h.Chunks = append(h.Chunks, c)
case MetaChunk:
ord = chunk_meta
h.MetaChunks = append(h.MetaChunks, c)
default:
return errors.New("Invalid type for chunk")
}
h.chunkOrder = append(h.chunkOrder, ord)
return nil
}
func isMeta(line string) bool {
for _, prefix := range getMetaPrefixes() {
if strings.HasPrefix(line, prefix+":") {
return true
}
}
if strings.HasPrefix(line, "{") {
return true
}
if strings.HasPrefix(line, "![[") {
return true
}
return false
}
func parseDoc(md string) Document {
headers := []Header{}
lines := strings.Split(md, "\n")
h := Header{}
text := ""
doAddChunk := func() {
if strings.Contains(h.Text, "Intro bit") {
h.AddChunk(MetaChunk{
Content: text,
})
return
}
if isMeta(text) {
h.AddChunk(MetaChunk{
Content: text,
})
} else {
h.AddChunk(Chunk{
Content: text,
})
}
}
for _, line := range lines {
if line != "" && line[:1] == "#" {
if h.Text == "" {
h.Text = line
} else {
if text != "" {
text = strings.TrimSpace(text)
doAddChunk()
}
headers = append(headers, h)
h = Header{
Text: line,
}
text = ""
}
continue
}
if line == "" {
if text == "" {
continue
}
text = strings.TrimSpace(text)
doAddChunk()
text = ""
continue
}
trimmed := strings.TrimSpace(line)
if text != "" {
if strings.HasPrefix(line, "```") {
text += "\n"
} else if isMeta(line) {
text += "\n"
} else {
switch trimmed[0] {
case '-':
text += "\n"
default:
text += " "
}
}
}
text += line
if line == "```" {
text += "\n"
}
}
doAddChunk()
headers = append(headers, h)
return Document{
headers: headers,
}
}
func readScript(path string) error {
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
md := string(b)
currentSession.Doc = parseDoc(md)
return nil
}
func (d *Document) GetAllTakes() []Take {
var takes []Take
takes = append(takes, d.syncTakes...)
for _, h := range d.headers {
for _, c := range h.Chunks {
takes = append(takes, c.Takes...)
}
}
return takes
}
func (d *Document) GetRenderable() []interface{} {
var renderable []interface{}
for _, header := range d.headers {
renderable = append(renderable, header)
idxs := map[chunkOrder]int{
chunk_normal: 0,
chunk_meta: 0,
}
for _, ord := range header.chunkOrder {
var chunk interface{}
switch ord {
case chunk_normal:
chunk = header.Chunks[idxs[ord]]
case chunk_meta:
chunk = header.MetaChunks[idxs[ord]]
}
renderable = append(renderable, chunk)
idxs[ord]++
}
}
return renderable
}