From db64f6eeb50f17d90f0992e41ce5f5ee2c42e184 Mon Sep 17 00:00:00 2001 From: Min Date: Sat, 5 Oct 2024 18:32:03 +0800 Subject: [PATCH 1/3] feat: Issues-116 support options for WriteToTTML Issue: https://github.com/asticode/go-astisub/issues/116 --- testdata/example-in-no-indent.ttml | 1 + testdata/example-out-no-indent.ttml | 1 + ttml.go | 28 ++++++++++++++++++++++++++-- ttml_test.go | 17 +++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 testdata/example-in-no-indent.ttml create mode 100644 testdata/example-out-no-indent.ttml diff --git a/testdata/example-in-no-indent.ttml b/testdata/example-in-no-indent.ttml new file mode 100644 index 0000000..88d7947 --- /dev/null +++ b/testdata/example-in-no-indent.ttml @@ -0,0 +1 @@ +Copyright testTitle test

(deep rumbling)

MAN:

How did we end up here?

This place is horrible.

Smells like balls.

We don't belong

in this shithole.

(computer playing

electronic melody)

diff --git a/testdata/example-out-no-indent.ttml b/testdata/example-out-no-indent.ttml new file mode 100644 index 0000000..88d7947 --- /dev/null +++ b/testdata/example-out-no-indent.ttml @@ -0,0 +1 @@ +Copyright testTitle test

(deep rumbling)

MAN:

How did we end up here?

This place is horrible.

Smells like balls.

We don't belong

in this shithole.

(computer playing

electronic melody)

diff --git a/ttml.go b/ttml.go index 7f1ffd9..492caf7 100644 --- a/ttml.go +++ b/ttml.go @@ -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 @@ -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 diff --git a/ttml_test.go b/ttml_test.go index bba9e36..cc05071 100644 --- a/ttml_test.go +++ b/ttml_test.go @@ -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())) +} From 83ca26d9703cf904e3f6254cecd621c7cd9fbf1f Mon Sep 17 00:00:00 2001 From: Min Date: Mon, 7 Oct 2024 19:25:26 +0800 Subject: [PATCH 2/3] chore: rename TestTTMLWithOptionsNoIndent to TestWriteToTTMLWithIndentOption link: https://github.com/asticode/go-astisub/pull/117#discussion_r1789984044 --- ttml_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ttml_test.go b/ttml_test.go index cc05071..0f75d2e 100644 --- a/ttml_test.go +++ b/ttml_test.go @@ -69,7 +69,7 @@ func TestTTMLBreakLines(t *testing.T) { assert.Equal(t, strings.TrimSpace(string(c)), strings.TrimSpace(w.String())) } -func TestTTMLWithOptionsNoIndent(t *testing.T) { +func TestWriteToTTMLWithIndentOption(t *testing.T) { // Open s, err := astisub.OpenFile("./testdata/example-in-no-indent.ttml") assert.NoError(t, err) From afcbcf5cc93f5076bbd3063b2e6d22d40305916f Mon Sep 17 00:00:00 2001 From: Min Date: Mon, 7 Oct 2024 21:02:10 +0800 Subject: [PATCH 3/3] chore: use ./testdata/example-in.ttml link: https://github.com/asticode/go-astisub/pull/117#discussion_r1790088428 --- testdata/example-in-no-indent.ttml | 1 - ttml_test.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 testdata/example-in-no-indent.ttml diff --git a/testdata/example-in-no-indent.ttml b/testdata/example-in-no-indent.ttml deleted file mode 100644 index 88d7947..0000000 --- a/testdata/example-in-no-indent.ttml +++ /dev/null @@ -1 +0,0 @@ -Copyright testTitle test

(deep rumbling)

MAN:

How did we end up here?

This place is horrible.

Smells like balls.

We don't belong

in this shithole.

(computer playing

electronic melody)

diff --git a/ttml_test.go b/ttml_test.go index 0f75d2e..0f0ac77 100644 --- a/ttml_test.go +++ b/ttml_test.go @@ -71,7 +71,7 @@ func TestTTMLBreakLines(t *testing.T) { func TestWriteToTTMLWithIndentOption(t *testing.T) { // Open - s, err := astisub.OpenFile("./testdata/example-in-no-indent.ttml") + s, err := astisub.OpenFile("./testdata/example-in.ttml") assert.NoError(t, err) // Write