-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcaps.go
200 lines (165 loc) · 3.4 KB
/
caps.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
package caps
import (
"bufio"
"fmt"
"strconv"
"strings"
)
const DefaultLang = "en-US"
type CaptionReader interface {
Read([]byte) (*CaptionSet, error)
Detect([]byte) bool
}
type CaptionWriter interface {
Write(*CaptionSet) ([]byte, error)
}
type CaptionContent interface {
Text() bool
Style() bool
LineBreak() bool
Content() string
}
type isNot struct{}
func (isNot) Text() bool {
return false
}
func (isNot) LineBreak() bool {
return false
}
func (isNot) Style() bool {
return false
}
type CaptionText struct {
content string
isNot
}
func (CaptionText) Text() bool {
return true
}
func NewCaptionText(text string) CaptionContent {
return CaptionText{text, isNot{}}
}
func (c CaptionText) Content() string {
return c.content
}
type CaptionStyle struct {
Props StyleProps
Start bool
isNot
}
func (c CaptionStyle) Style() bool {
return true
}
func (c CaptionStyle) Content() string {
return c.Props.String()
}
func NewCaptionStyle(start bool, style StyleProps) CaptionContent {
return CaptionStyle{style, start, isNot{}}
}
type CaptionLineBreak struct{ isNot }
func (c CaptionLineBreak) LineBreak() bool {
return true
}
func (c CaptionLineBreak) Content() string {
return "\n"
}
func NewLineBreak() CaptionContent {
return CaptionLineBreak{isNot{}}
}
const defaultStyleID = "default"
// FIXME This is a simple placeholder for style types, this can be better represented
// but I need to implement more caption types first(this was written with just dfxp)
type StyleProps struct {
ID string
Class string
TextAlign string
FontFamily string
FontSize string
Color string
Italics bool
Bold bool
Underline bool
}
func (s StyleProps) String() string {
return fmt.Sprintf(`
class: %s\n
text-align: %s\n
font-family: %s\n
font-size: %s\n
color: %s\n
italics: %s\n
bold: %s\n
underline: %s\n
`,
s.Class,
s.TextAlign,
s.FontFamily,
s.FontSize,
s.Color,
strconv.FormatBool(s.Italics),
strconv.FormatBool(s.Bold),
strconv.FormatBool(s.Underline),
)
}
func DefaultStyleProps() StyleProps {
return StyleProps{Color: "white", FontFamily: "monospace", FontSize: "1c"}
}
type CaptionSet struct {
Styles map[string]StyleProps
Captions map[string][]*Caption
}
func NewCaptionSet() *CaptionSet {
return &CaptionSet{
Styles: map[string]StyleProps{},
Captions: map[string][]*Caption{},
}
}
func (c CaptionSet) SetCaptions(lang string, captions []*Caption) {
c.Captions[lang] = captions
}
func (c CaptionSet) Languages() []string {
keys := []string{}
for k := range c.Captions {
keys = append(keys, k)
}
return keys
}
func (c CaptionSet) IsEmpty() bool {
for _, captions := range c.Captions {
if len(captions) != 0 {
return false
}
}
return true
}
func (c CaptionSet) GetCaptions(lang string) []*Caption {
captions, ok := c.Captions[lang]
if !ok {
return []*Caption{}
}
return captions
}
func (c CaptionSet) AddStyle(style StyleProps) {
c.Styles[style.ID] = style
}
func (c CaptionSet) GetStyle(id string) StyleProps {
if style, ok := c.Styles[id]; ok {
return style
}
return DefaultStyleProps()
}
func (c CaptionSet) GetStyles() []StyleProps {
values := []StyleProps{}
for _, v := range c.Styles {
values = append(values, v)
}
return values
}
func SplitLines(s string) []string {
var lines []string
scanner := bufio.NewScanner(strings.NewReader(s))
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}