Skip to content

Commit da526ac

Browse files
committed
export Caption
1 parent 01b0599 commit da526ac

14 files changed

+57
-74
lines changed

caps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
// filterCapitalization converts "ALL CAPS" text into "Initial letter capped"
10-
func filterCapitalization(captions []caption) []caption {
10+
func filterCapitalization(captions []Caption) []Caption {
1111

1212
for _, cap := range captions {
1313
for i, line := range cap.Text {

caps_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88

99
func TestFilterCapitalization(t *testing.T) {
1010

11-
var in = []caption{{
11+
in := []Caption{{
1212
Seq: 1,
1313
Start: makeTime(0, 0, 4, 630),
1414
End: makeTime(0, 0, 6, 18),
1515
Text: []string{"GO NINJA!", "NINJA GO!"},
1616
}}
1717

18-
var expected = []caption{{
18+
expected := []Caption{{
1919
1,
2020
makeTime(0, 0, 4, 630),
2121
makeTime(0, 0, 6, 18),

caption.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
package subtitles
22

3-
import "time"
3+
import (
4+
"fmt"
5+
"time"
6+
)
47

5-
// caption represents one subtitle block
6-
type caption struct {
8+
// Caption represents one subtitle block
9+
type Caption struct {
710
Seq int
811
Start time.Time
912
End time.Time
1013
Text []string
1114
}
15+
16+
// AsSrt renders the caption as srt
17+
func (cap Caption) AsSrt() string {
18+
res := fmt.Sprintf("%d", cap.Seq) + eol +
19+
SrtTime(cap.Start) + " --> " + SrtTime(cap.End) + eol
20+
for _, line := range cap.Text {
21+
res += line + eol
22+
}
23+
return res + eol
24+
}

caption_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import (
88

99
func TestRenderTime(t *testing.T) {
1010

11-
cap := caption{
11+
cap := Caption{
1212
1,
1313
makeTime(18, 40, 22, 110),
1414
makeTime(18, 41, 20, 123),
1515
[]string{"<i>Go ninja!</i>"},
1616
}
1717

18-
assert.Equal(t, "18:40:22,110 --> 18:41:20,123", cap.srtTime())
18+
assert.Equal(t, "1\n"+
19+
"18:40:22,110 --> 18:41:20,123\n"+
20+
"<i>Go ninja!</i>\n\n",
21+
cap.AsSrt())
1922
}

cleaner.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import (
1010

1111
// CleanupSub parses .srt or .ssa, performs cleanup and renders to a .srt, returning a string. caller is responsible for passing UTF8 string
1212
func CleanupSub(utf8 string, filterName string, keepAds bool, sync int) (string, error) {
13-
14-
var captions []caption
13+
var captions []Caption
1514

1615
if looksLikeSrt(utf8) {
1716
captions = parseSrt(utf8)
@@ -29,13 +28,12 @@ func CleanupSub(utf8 string, filterName string, keepAds bool, sync int) (string,
2928
}
3029

3130
captions = filterSubs(captions, filterName)
32-
3331
out := renderSrt(captions)
3432

3533
return out, nil
3634
}
3735

38-
func resyncSubs(subs []caption, sync int) []caption {
36+
func resyncSubs(subs []Caption, sync int) []Caption {
3937

4038
// var res []caption
4139
fmt.Printf("resyncing with %d\n", sync)
@@ -49,12 +47,8 @@ func resyncSubs(subs []caption, sync int) []caption {
4947
}
5048

5149
// RemoveAds removes advertisement from the subtitles
52-
func removeAds(subs []caption) []caption {
53-
54-
var res []caption
55-
50+
func removeAds(subs []Caption) (res []Caption) {
5651
ads := []string{
57-
5852
// english:
5953
"captions paid for by",
6054
"english subtitles",
@@ -136,6 +130,5 @@ func removeAds(subs []caption) []caption {
136130
seq++
137131
}
138132
}
139-
140-
return res
133+
return
141134
}

cleaner_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func TestRemoveAds(t *testing.T) {
1010

11-
var in = []caption{{
11+
in := []Caption{{
1212
1,
1313
makeTime(0, 0, 4, 630),
1414
makeTime(0, 0, 6, 18),
@@ -25,7 +25,7 @@ func TestRemoveAds(t *testing.T) {
2525
[]string{"No ninja!"},
2626
}}
2727

28-
var expected = []caption{{
28+
expected := []Caption{{
2929
1,
3030
makeTime(0, 0, 4, 630),
3131
makeTime(0, 0, 6, 18),

filter.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import (
55
)
66

77
// filterSubs pass the captions through a filter function
8-
func filterSubs(captions []caption, filter string) []caption {
9-
8+
func filterSubs(captions []Caption, filter string) []Caption {
109
if filter == "caps" {
1110
return filterCapitalization(captions)
1211
}
@@ -16,6 +15,5 @@ func filterSubs(captions []caption, filter string) []caption {
1615
if filter != "none" {
1716
fmt.Printf("Unrecognized filter name: %s\n", filter)
1817
}
19-
2018
return captions
2119
}

html.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@ import (
77
)
88

99
// filterHTML removes all html tags from captions
10-
func filterHTML(captions []caption) []caption {
11-
10+
func filterHTML(captions []Caption) []Caption {
1211
for _, cap := range captions {
1312
for i, line := range cap.Text {
1413
clean := sanitize.HTML(line)
15-
1614
if clean != cap.Text[i] {
1715
log.Printf("[html] %s -> %s\n", cap.Text[i], clean)
1816
cap.Text[i] = clean
1917
}
2018
}
2119
}
22-
2320
return captions
2421
}

html_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88

99
func TestFilterHTML(t *testing.T) {
1010

11-
var in = []caption{{
11+
in := []Caption{{
1212
1,
1313
makeTime(0, 0, 4, 630),
1414
makeTime(0, 0, 6, 18),
1515
[]string{"<b>GO NINJA!</b>", "NINJA&nbsp;GO!"},
1616
}}
1717

18-
var expected = []caption{{
18+
expected := []Caption{{
1919
1,
2020
makeTime(0, 0, 4, 630),
2121
makeTime(0, 0, 6, 18),

parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package subtitles
22

33
// parse tries to parse a subtitle from the data stream
4-
func parse(b []byte) []caption {
4+
func parse(b []byte) []Caption {
55

66
s := convertToUTF8(b)
77

0 commit comments

Comments
 (0)