diff --git a/subtitles.go b/subtitles.go index 26b1ecb..ee7f6d2 100644 --- a/subtitles.go +++ b/subtitles.go @@ -236,7 +236,6 @@ type StyleAttributes struct { TTMLWritingMode *string TTMLZIndex *int WebVTTAlign string - WebVTTItalics bool WebVTTLine string WebVTTLines int WebVTTPosition string @@ -244,7 +243,7 @@ type StyleAttributes struct { WebVTTScroll string WebVTTSize string WebVTTStyles []string - WebVTTTags []*WebVTTTag + WebVTTTags []WebVTTTag WebVTTVertical string WebVTTViewportAnchor string WebVTTWidth string @@ -256,7 +255,11 @@ type WebVTTTag struct { Classes []string } -func (t *WebVTTTag) getStartTag() string { +func (t WebVTTTag) getStartTag() string { + if t.Name == "" { + return "" + } + s := t.Name if len(t.Classes) > 0 { s += "." + strings.Join(t.Classes, ".") @@ -269,7 +272,7 @@ func (t *WebVTTTag) getStartTag() string { return "<" + s + ">" } -func (t *WebVTTTag) getEndTag() string { +func (t WebVTTTag) getEndTag() string { if t.Name == "" { return "" } diff --git a/webvtt.go b/webvtt.go index f01dd84..6940ded 100644 --- a/webvtt.go +++ b/webvtt.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "log" "regexp" "sort" "strconv" @@ -305,15 +306,13 @@ func parseTextWebVTT(i string) (o Line) { // Create tokenizer tr := html.NewTokenizer(strings.NewReader(i)) - webVTTTagStack := make([]*WebVTTTag, 64) - webVTTTagStackIndex := -1 + webVTTTagStack := make([]WebVTTTag, 64) + webVTTTagStackIndex := -1 // This must be increased when pushing to stack and decreased when poping from stack // Loop - loopCount := 0 for { // Get next tag t := tr.Next() - loopCount++ // Process error if err := tr.Err(); err != nil { break @@ -339,15 +338,20 @@ func parseTextWebVTT(i string) (o Line) { annotation = strings.TrimSpace(matches[4]) } - // Only support voicename if appears in the begining of the line - // TODO: do something with in the middle of the line - if loopCount == 1 && tagName == "v" { - o.VoiceName = annotation + if tagName == "v" { + if o.VoiceName == "" { + // Only get voicename of the first appears in the line + o.VoiceName = annotation + } else { + // TODO: do something with other instead of ignoring + log.Printf("astisub: found another voice name %q in %q. Ignore", annotation, i) + } continue } + // Push the tag to stack webVTTTagStackIndex++ - webVTTTagStack[webVTTTagStackIndex] = &WebVTTTag{ + webVTTTagStack[webVTTTagStackIndex] = WebVTTTag{ Name: tagName, Classes: classes, Annotation: annotation, @@ -359,7 +363,7 @@ func parseTextWebVTT(i string) (o Line) { // Get style attribute var sa *StyleAttributes if webVTTTagStackIndex >= 0 { - tags := make([]*WebVTTTag, webVTTTagStackIndex+1) + tags := make([]WebVTTTag, webVTTTagStackIndex+1) copy(tags, webVTTTagStack[:webVTTTagStackIndex+1]) sa = &StyleAttributes{ WebVTTTags: tags,