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 5, 2024
1 parent d27006f commit 84b59d7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
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>
19 changes: 18 additions & 1 deletion ttml.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,16 @@ func (t TTMLOutDuration) MarshalText() ([]byte, error) {

// WriteToTTML writes subtitles in .ttml format
func (s Subtitles) WriteToTTML(o io.Writer) (err error) {
return s.WriteToTTMLWithOptions(o, TTMLWriteOptions{})
}

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

// WriteToTTMLWithOptions writes subtitles in .ttml format with options.
func (s Subtitles) WriteToTTMLWithOptions(o io.Writer, opts TTMLWriteOptions) (err error) {
// Do not write anything if no subtitles
if len(s.Items) == 0 {
return ErrNoSubtitlesToWrite
Expand Down Expand Up @@ -714,7 +724,14 @@ func (s Subtitles) WriteToTTML(o io.Writer) (err error) {

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

// Indent
if opts.Indent != nil {
e.Indent("", *opts.Indent)
} else {
e.Indent("", " ")
}

if err = e.Encode(ttml); err != nil {
err = fmt.Errorf("astisub: xml encoding failed: %w", err)
return
Expand Down
20 changes: 20 additions & 0 deletions ttml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,23 @@ 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{}

// No indent
indent := ""

err = s.WriteToTTMLWithOptions(w, astisub.TTMLWriteOptions{Indent: &indent})
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 84b59d7

Please sign in to comment.