-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubtitle.go
76 lines (62 loc) · 1.6 KB
/
subtitle.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
// Copyright 2013-2015, Homin Lee. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package subtitle
import (
"fmt"
"regexp"
"sort"
"time"
)
// Book is collection of scripts
type Book []Script
// Find a script on given timestamp.
// If not hit, it returns next script.
// So, caller should re-check the script is hit on the timestamp.
func (b Book) Find(ts time.Duration) *Script {
si := sort.Search(len(b), func(i int) bool {
return b[i].Start >= ts
})
if si >= len(b) {
return nil
}
return &b[si]
}
// Script represents a script with index and start/end time
type Script struct {
Idx int
Start, End time.Duration
Text string
}
// Duration returns how long the script should be shown
func (s *Script) Duration() time.Duration {
return s.End - s.Start
}
// TextWithoutMarkup strips HTML markup from script
func (s *Script) TextWithoutMarkup() string {
return reMakrup.ReplaceAllString(s.Text, "")
}
// CheckHit checks the script with given timestamp
func (s *Script) CheckHit(ts time.Duration) HitStatus {
switch {
case ts < s.Start:
return ScrEARLY
case ts >= s.Start && s.End >= s.End:
return ScrHIT
case s.End < ts:
return ScrLATE
}
return ScrINVALID
}
func (s *Script) String() string {
return fmt.Sprintf("%d:%s(%s-%s)", s.Idx, s.Text, s.Start, s.End)
}
// HitStatus is type for timestamp check
type HitStatus uint8
const (
ScrINVALID HitStatus = iota
ScrEARLY // Not yet
ScrHIT // Now
ScrLATE // Gone
)
var reMakrup = regexp.MustCompile("</?[^<>]+?>")