Skip to content

Commit

Permalink
feat: Issues-116 support options for WriteToTTML
Browse files Browse the repository at this point in the history
Issue: #116
  • Loading branch information
Min authored and Min committed Oct 6, 2024
1 parent d27006f commit db64f6e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions testdata/example-in-no-indent.ttml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<tt xmlns="http://www.w3.org/ns/ttml" xml:lang="fr" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" xmlns:tts="http://www.w3.org/ns/ttml#styling"><head><metadata><ttm:copyright>Copyright test</ttm:copyright><ttm:title>Title test</ttm:title></metadata><styling><style xml:id="style_0" style="style_2" tts:color="white" tts:extent="100% 10%" tts:fontFamily="sansSerif" tts:fontStyle="normal" tts:origin="0% 90%" tts:textAlign="center"></style><style xml:id="style_1" tts:color="white" tts:extent="100% 13%" tts:fontFamily="sansSerif" tts:fontStyle="normal" tts:origin="0% 87%" tts:textAlign="center"></style><style xml:id="style_2" tts:color="white" tts:extent="100% 20%" tts:fontFamily="sansSerif" tts:fontStyle="normal" tts:origin="0% 80%" tts:textAlign="center"></style></styling><layout><region xml:id="region_0" style="style_0" tts:color="blue"></region><region xml:id="region_1" style="style_1"></region><region xml:id="region_2" style="style_2"></region></layout></head><body><div><p begin="00:01:39.000" end="00:01:41.040" region="region_1" style="style_1" tts:color="red"><span style="style_1" tts:color="black">(deep rumbling)</span></p><p begin="00:02:04.080" end="00:02:07.120" region="region_2"><span>MAN:</span><br></br><span>How did we </span><span style="style_1" tts:color="green">end up </span><span>here?</span></p><p begin="00:02:12.160" end="00:02:15.200" region="region_1"><span style="style_1">This place is horrible.</span></p><p begin="00:02:20.240" end="00:02:22.280" region="region_1"><span style="style_1">Smells like balls.</span></p><p begin="00:02:28.320" end="00:02:31.360" region="region_2"><span style="style_2">We don&#39;t belong</span><br></br><span style="style_1">in this shithole.</span></p><p begin="00:02:31.400" end="00:02:33.440" region="region_2"><span style="style_2">(computer playing</span><br></br><span style="style_1">electronic melody)</span></p></div></body></tt>
1 change: 1 addition & 0 deletions testdata/example-out-no-indent.ttml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<tt xmlns="http://www.w3.org/ns/ttml" xml:lang="fr" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" xmlns:tts="http://www.w3.org/ns/ttml#styling"><head><metadata><ttm:copyright>Copyright test</ttm:copyright><ttm:title>Title test</ttm:title></metadata><styling><style xml:id="style_0" style="style_2" tts:color="white" tts:extent="100% 10%" tts:fontFamily="sansSerif" tts:fontStyle="normal" tts:origin="0% 90%" tts:textAlign="center"></style><style xml:id="style_1" tts:color="white" tts:extent="100% 13%" tts:fontFamily="sansSerif" tts:fontStyle="normal" tts:origin="0% 87%" tts:textAlign="center"></style><style xml:id="style_2" tts:color="white" tts:extent="100% 20%" tts:fontFamily="sansSerif" tts:fontStyle="normal" tts:origin="0% 80%" tts:textAlign="center"></style></styling><layout><region xml:id="region_0" style="style_0" tts:color="blue"></region><region xml:id="region_1" style="style_1"></region><region xml:id="region_2" style="style_2"></region></layout></head><body><div><p begin="00:01:39.000" end="00:01:41.040" region="region_1" style="style_1" tts:color="red"><span style="style_1" tts:color="black">(deep rumbling)</span></p><p begin="00:02:04.080" end="00:02:07.120" region="region_2"><span>MAN:</span><br></br><span>How did we </span><span style="style_1" tts:color="green">end up </span><span>here?</span></p><p begin="00:02:12.160" end="00:02:15.200" region="region_1"><span style="style_1">This place is horrible.</span></p><p begin="00:02:20.240" end="00:02:22.280" region="region_1"><span style="style_1">Smells like balls.</span></p><p begin="00:02:28.320" end="00:02:31.360" region="region_2"><span style="style_2">We don&#39;t belong</span><br></br><span style="style_1">in this shithole.</span></p><p begin="00:02:31.400" end="00:02:33.440" region="region_2"><span style="style_2">(computer playing</span><br></br><span style="style_1">electronic melody)</span></p></div></body></tt>
28 changes: 26 additions & 2 deletions ttml.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,29 @@ func (t TTMLOutDuration) MarshalText() ([]byte, error) {
return []byte(formatDuration(time.Duration(t), ".", 3)), nil
}

// WriteToTTMLOptions represents TTML write options.
type WriteToTTMLOptions struct {
Indent string // Default is 4 spaces.
}

// WriteToTTMLOption represents a WriteToTTML option.
type WriteToTTMLOption func(o *WriteToTTMLOptions)

// WriteToTTMLWithIndentOption sets the indent option.
func WriteToTTMLWithIndentOption(indent string) WriteToTTMLOption {
return func(o *WriteToTTMLOptions) {
o.Indent = indent
}
}

// WriteToTTML writes subtitles in .ttml format
func (s Subtitles) WriteToTTML(o io.Writer) (err error) {
func (s Subtitles) WriteToTTML(o io.Writer, opts ...WriteToTTMLOption) (err error) {
// Create write options
wo := &WriteToTTMLOptions{Indent: " "}
for _, opt := range opts {
opt(wo)
}

// Do not write anything if no subtitles
if len(s.Items) == 0 {
return ErrNoSubtitlesToWrite
Expand Down Expand Up @@ -714,7 +735,10 @@ func (s Subtitles) WriteToTTML(o io.Writer) (err error) {

// Marshal XML
var e = xml.NewEncoder(o)
e.Indent("", " ")

// Set indent
e.Indent("", wo.Indent)

if err = e.Encode(ttml); err != nil {
err = fmt.Errorf("astisub: xml encoding failed: %w", err)
return
Expand Down
17 changes: 17 additions & 0 deletions ttml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,20 @@ func TestTTMLBreakLines(t *testing.T) {

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

func TestTTMLWithOptionsNoIndent(t *testing.T) {
// Open
s, err := astisub.OpenFile("./testdata/example-in-no-indent.ttml")
assert.NoError(t, err)

// Write
w := &bytes.Buffer{}

err = s.WriteToTTML(w, astisub.WriteToTTMLWithIndentOption(""))
assert.NoError(t, err)

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

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

0 comments on commit db64f6e

Please sign in to comment.