Skip to content

Commit 9f2ecac

Browse files
authored
Solved #59 (#60)
* Solved #59 * correct the code
1 parent aa9412f commit 9f2ecac

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

ssa.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ var ssaRegexpEffect = regexp.MustCompile(`\{[^\{]+\}`)
134134

135135
// ReadFromSSA parses an .ssa content
136136
func ReadFromSSA(i io.Reader) (o *Subtitles, err error) {
137+
o, err = ReadFromSSAWithOptions(i, defaultSSAOptions())
138+
return o, err
139+
}
140+
141+
// ReadFromSSAWithOptions parses an .ssa content
142+
func ReadFromSSAWithOptions(i io.Reader, opts SSAOptions) (o *Subtitles, err error) {
137143
// Init
138144
o = NewSubtitles()
139145
var scanner = bufio.NewScanner(i)
@@ -175,7 +181,9 @@ func ReadFromSSA(i io.Reader) (o *Subtitles, err error) {
175181
format = make(map[int]string)
176182
continue
177183
default:
178-
log.Printf("astisub: unknown section: %s", line)
184+
if opts.OnUnknownSectionName != nil {
185+
opts.OnUnknownSectionName(line)
186+
}
179187
sectionName = ssaSectionNameUnknown
180188
continue
181189
}
@@ -195,7 +203,9 @@ func ReadFromSSA(i io.Reader) (o *Subtitles, err error) {
195203
// Split on ":"
196204
var split = strings.Split(line, ":")
197205
if len(split) < 2 || split[0] == "" {
198-
log.Printf("astisub: not understood: '%s', ignoring", line)
206+
if opts.OnInvalidLine != nil {
207+
opts.OnInvalidLine(line)
208+
}
199209
continue
200210
}
201211
var header = strings.TrimSpace(split[0])
@@ -1268,3 +1278,20 @@ func (s Subtitles) WriteToSSA(o io.Writer) (err error) {
12681278
}
12691279
return
12701280
}
1281+
1282+
// SSAOptions
1283+
type SSAOptions struct {
1284+
OnUnknownSectionName func(name string)
1285+
OnInvalidLine func(line string)
1286+
}
1287+
1288+
func defaultSSAOptions() SSAOptions {
1289+
return SSAOptions{
1290+
OnUnknownSectionName: func(name string) {
1291+
log.Printf("astisub: unknown section: %s", name)
1292+
},
1293+
OnInvalidLine: func(line string) {
1294+
log.Printf("astisub: not understood: '%s', ignoring", line)
1295+
},
1296+
}
1297+
}

0 commit comments

Comments
 (0)