Skip to content

Commit

Permalink
[ttml] Change to more appropriate name and optimize ttml xml decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
NhanNguyen700 committed Jun 17, 2024
1 parent e403fea commit 2f60105
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 86 deletions.
24 changes: 24 additions & 0 deletions testdata/example-in-breaklines.ttml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<tt>
<head>
</head>
<body>
<div>
<p xml:id="1" begin="00:00:00.000" end="00:00:01.000">
<span>First line<br/>
Second line</span>
</p>
<p xml:id="2" begin="00:00:01.000" end="00:00:02.000">
<span>Third line<br></br>Fourth line</span>
</p>
<p xml:id="3" begin="00:00:02.000" end="00:00:03.000">
Fifth line
<br/>
Sixth <span>middle</span> line
</p>
<p xml:id="4" begin="00:00:03.000" end="00:00:04.000">
Seventh line
<br></br>Eighth <span>middle</span> line
</p>
</div>
</body>
</tt>
32 changes: 32 additions & 0 deletions testdata/example-out-breaklines.ttml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<tt xmlns="http://www.w3.org/ns/ttml" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" xmlns:tts="http://www.w3.org/ns/ttml#styling">
<head>
<styling></styling>
<layout></layout>
</head>
<body>
<div>
<p begin="00:00:00.000" end="00:00:01.000">
<span>First line</span>
<br></br>
<span>Second line</span>
</p>
<p begin="00:00:01.000" end="00:00:02.000">
<span>Third lineFourth line</span>
</p>
<p begin="00:00:02.000" end="00:00:03.000">
<span>Fifth line</span>
<br></br>
<span>Sixth </span>
<span>middle </span>
<span>line</span>
</p>
<p begin="00:00:03.000" end="00:00:04.000">
<span>Seventh line</span>
<br></br>
<span>Eighth </span>
<span>middle </span>
<span>line</span>
</p>
</div>
</body>
</tt>
34 changes: 0 additions & 34 deletions testdata/example-with-breaklines-in.ttml

This file was deleted.

42 changes: 0 additions & 42 deletions testdata/example-with-breaklines-out.ttml

This file was deleted.

13 changes: 6 additions & 7 deletions ttml.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,16 @@ func (i *TTMLInItems) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err
return nil
}

// handleBrTokenReader is used only for decoding TTMLInItems, do not use it anywhere else
type handleBrTokenReader struct {
// ttmlXmlDecoder is used only for decoding TTMLInItems, do not use it anywhere else
type ttmlXmlDecoder struct {
xml.Decoder
holdingToken xml.Token
}

// Token implements the TokenReader interface, when it meets the "br" tag, it will hold the token and return a newline
// instead. This is to work around the fact that the go xml unmarshaler will ignore the "br" tag if it's within a
// character data field.
func (r *handleBrTokenReader) Token() (xml.Token, error) {
func (r *ttmlXmlDecoder) Token() (xml.Token, error) {
if r.holdingToken != nil {
returnToken := r.holdingToken
r.holdingToken = nil
Expand All @@ -226,8 +226,8 @@ func (r *handleBrTokenReader) Token() (xml.Token, error) {
return t, nil
}

func newHandleBrTokenReader(r io.Reader) xml.TokenReader {
return &handleBrTokenReader{Decoder: *xml.NewDecoder(r), holdingToken: nil}
func newTTMLXmlDecoder(ts TTMLInSubtitle) *ttmlXmlDecoder {
return &ttmlXmlDecoder{Decoder: *xml.NewDecoder(strings.NewReader("<p>" + ts.Items + "</p>")), holdingToken: nil}
}

// TTMLInItem represents an input TTML item
Expand Down Expand Up @@ -413,8 +413,7 @@ func ReadFromTTML(i io.Reader) (o *Subtitles, err error) {

// Unmarshal items
var items = TTMLInItems{}
decoder := xml.NewTokenDecoder(newHandleBrTokenReader(strings.NewReader("<p>" + ts.Items + "</p>")))
if err = decoder.Decode(&items); err != nil {
if err = newTTMLXmlDecoder(ts).Decode(&items); err != nil {
err = fmt.Errorf("astisub: unmarshaling items failed: %w", err)
return
}
Expand Down
9 changes: 6 additions & 3 deletions ttml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package astisub_test

import (
"bytes"
"fmt"
"io/ioutil"
"strings"
"testing"
Expand Down Expand Up @@ -53,18 +54,20 @@ func TestTTML(t *testing.T) {
assert.Equal(t, string(c), w.String())
}

func TestBreakLineHandling(t *testing.T) {
func TestTTMLBreakLines(t *testing.T) {
// Open
s, err := astisub.OpenFile("./testdata/example-with-breaklines-in.ttml")
s, err := astisub.OpenFile("./testdata/example-in-breaklines.ttml")
assert.NoError(t, err)

// Write
w := &bytes.Buffer{}
err = s.WriteToTTML(w)
assert.NoError(t, err)

c, err := ioutil.ReadFile("./testdata/example-with-breaklines-out.ttml")
c, err := ioutil.ReadFile("./testdata/example-out-breaklines.ttml")
assert.NoError(t, err)

fmt.Println(w.String())

assert.Equal(t, strings.TrimSpace(string(c)), strings.TrimSpace(w.String()))
}

0 comments on commit 2f60105

Please sign in to comment.