diff --git a/srt_test.go b/srt_test.go
index 611c3fc..80ed233 100644
--- a/srt_test.go
+++ b/srt_test.go
@@ -3,6 +3,7 @@ package astisub_test
import (
"bytes"
"io/ioutil"
+ "os"
"testing"
"github.com/asticode/go-astisub"
@@ -51,3 +52,40 @@ func TestNonUTF8SRT(t *testing.T) {
_, err := astisub.OpenFile("./testdata/example-in-non-utf8.srt")
assert.Error(t, err)
}
+
+func TestSRTStyled(t *testing.T) {
+ // Open
+ s, err := astisub.OpenFile("./testdata/example-styled-in.srt")
+ assert.NoError(t, err)
+ assertStyledSubtitleItems(t, s)
+ assertSRTSubtitleStyles(t, s)
+
+ // No subtitles to write
+ w := &bytes.Buffer{}
+ err = astisub.Subtitles{}.WriteToSRT(w)
+ assert.EqualError(t, err, astisub.ErrNoSubtitlesToWrite.Error())
+
+ // Write
+ c, err := os.ReadFile("./testdata/example-styled-out.srt")
+ assert.NoError(t, err)
+ err = s.WriteToSRT(w)
+ assert.NoError(t, err)
+ assert.Equal(t, string(c), w.String())
+}
+
+func TestReadSRTWriteWebVTTStyled(t *testing.T) {
+ // Open
+ s, err := astisub.OpenFile("./testdata/example-styled-in.srt")
+ assert.NoError(t, err)
+ assertStyledSubtitleItems(t, s)
+ assertSRTSubtitleStyles(t, s)
+
+ w := &bytes.Buffer{}
+
+ // Write
+ c, err := os.ReadFile("./testdata/example-styled-out.vtt")
+ assert.NoError(t, err)
+ err = s.WriteToWebVTT(w)
+ assert.NoError(t, err)
+ assert.Equal(t, string(c), w.String())
+}
diff --git a/subtitles_test.go b/subtitles_test.go
index a8012ba..e4ee9fd 100644
--- a/subtitles_test.go
+++ b/subtitles_test.go
@@ -40,6 +40,60 @@ func assertSubtitleItems(t *testing.T, i *astisub.Subtitles) {
assert.Equal(t, "electronic melody)", i.Items[5].Lines[1].String())
}
+func assertStyledSubtitleItems(t *testing.T, i *astisub.Subtitles) {
+ assert.Len(t, i.Items, 6)
+ assert.Equal(t, 17*time.Second+985*time.Millisecond, i.Items[0].StartAt)
+ assert.Equal(t, 20*time.Second+521*time.Millisecond, i.Items[0].EndAt)
+ assert.Equal(t, "[instrumental music]", i.Items[0].Lines[0].String())
+ assert.Equal(t, 47*time.Second+115*time.Millisecond, i.Items[1].StartAt)
+ assert.Equal(t, 48*time.Second+282*time.Millisecond, i.Items[1].EndAt)
+ assert.Equal(t, "[ticks]", i.Items[1].Lines[0].String())
+ assert.Equal(t, 58*time.Second+192*time.Millisecond, i.Items[2].StartAt)
+ assert.Equal(t, 59*time.Second+727*time.Millisecond, i.Items[2].EndAt)
+ assert.Equal(t, "[instrumental music]", i.Items[2].Lines[0].String())
+ assert.Equal(t, 1*time.Minute+1*time.Second+662*time.Millisecond, i.Items[3].StartAt)
+ assert.Equal(t, 1*time.Minute+3*time.Second+63*time.Millisecond, i.Items[3].EndAt)
+ assert.Equal(t, "[dog barking]", i.Items[3].Lines[0].String())
+ assert.Equal(t, 1*time.Minute+26*time.Second+787*time.Millisecond, i.Items[4].StartAt)
+ assert.Equal(t, 1*time.Minute+29*time.Second+523*time.Millisecond, i.Items[4].EndAt)
+ assert.Equal(t, "[beeping]", i.Items[4].Lines[0].String())
+ assert.Equal(t, 1*time.Minute+29*time.Second+590*time.Millisecond, i.Items[5].StartAt)
+ assert.Equal(t, 1*time.Minute+31*time.Second+992*time.Millisecond, i.Items[5].EndAt)
+ assert.Equal(t, "[automated]", i.Items[5].Lines[0].String())
+ assert.Equal(t, "'The time is 7:35.'", i.Items[5].Lines[1].String())
+}
+
+func assertSRTSubtitleStyles(t *testing.T, i *astisub.Subtitles) {
+ assert.Len(t, i.Items, 6)
+ assert.Equal(t, "#00ff00", *i.Items[0].Lines[0].Items[0].InlineStyle.SRTColor)
+ assert.Zero(t, i.Items[0].Lines[0].Items[0].InlineStyle.SRTPosition)
+ assert.True(t, i.Items[0].Lines[0].Items[0].InlineStyle.SRTBold)
+ assert.False(t, i.Items[0].Lines[0].Items[0].InlineStyle.SRTItalics)
+ assert.False(t, i.Items[0].Lines[0].Items[0].InlineStyle.SRTUnderline)
+ assert.Equal(t, "#ff00ff", *i.Items[1].Lines[0].Items[0].InlineStyle.SRTColor)
+ assert.Zero(t, i.Items[1].Lines[0].Items[0].InlineStyle.SRTPosition)
+ assert.False(t, i.Items[1].Lines[0].Items[0].InlineStyle.SRTBold)
+ assert.False(t, i.Items[1].Lines[0].Items[0].InlineStyle.SRTItalics)
+ assert.False(t, i.Items[1].Lines[0].Items[0].InlineStyle.SRTUnderline)
+ assert.Equal(t, "#00ff00", *i.Items[2].Lines[0].Items[0].InlineStyle.SRTColor)
+ assert.Zero(t, i.Items[2].Lines[0].Items[0].InlineStyle.SRTPosition)
+ assert.False(t, i.Items[2].Lines[0].Items[0].InlineStyle.SRTBold)
+ assert.False(t, i.Items[2].Lines[0].Items[0].InlineStyle.SRTItalics)
+ assert.False(t, i.Items[2].Lines[0].Items[0].InlineStyle.SRTUnderline)
+ assert.Nil(t, i.Items[3].Lines[0].Items[0].InlineStyle.SRTColor)
+ assert.Zero(t, i.Items[3].Lines[0].Items[0].InlineStyle.SRTPosition)
+ assert.True(t, i.Items[3].Lines[0].Items[0].InlineStyle.SRTBold)
+ assert.False(t, i.Items[3].Lines[0].Items[0].InlineStyle.SRTItalics)
+ assert.True(t, i.Items[3].Lines[0].Items[0].InlineStyle.SRTUnderline)
+ assert.Nil(t, i.Items[4].Lines[0].Items[0].InlineStyle)
+ assert.Nil(t, i.Items[5].Lines[0].Items[0].InlineStyle)
+ assert.Nil(t, i.Items[5].Lines[1].Items[0].InlineStyle.SRTColor)
+ assert.Zero(t, i.Items[5].Lines[1].Items[0].InlineStyle.SRTPosition)
+ assert.False(t, i.Items[5].Lines[1].Items[0].InlineStyle.SRTBold)
+ assert.True(t, i.Items[5].Lines[1].Items[0].InlineStyle.SRTItalics)
+ assert.False(t, i.Items[5].Lines[1].Items[0].InlineStyle.SRTUnderline)
+}
+
func mockSubtitles() *astisub.Subtitles {
return &astisub.Subtitles{Items: []*astisub.Item{{EndAt: 3 * time.Second, StartAt: time.Second, Lines: []astisub.Line{{Items: []astisub.LineItem{{Text: "subtitle-1"}}}}}, {EndAt: 7 * time.Second, StartAt: 3 * time.Second, Lines: []astisub.Line{{Items: []astisub.LineItem{{Text: "subtitle-2"}}}}}}}
}
diff --git a/testdata/example-styled-in.srt b/testdata/example-styled-in.srt
new file mode 100644
index 0000000..28d5c90
--- /dev/null
+++ b/testdata/example-styled-in.srt
@@ -0,0 +1,24 @@
+1
+00:00:17,985 --> 00:00:20,521
+[instrumental music]
+
+2
+00:00:47,115 --> 00:00:48,282
+[ticks]
+
+3
+00:00:58,192 --> 00:00:59,727
+[instrumental music]
+
+4
+00:01:01,662 --> 00:01:03,063
+[dog barking]
+
+5
+00:01:26,787 --> 00:01:29,523
+[beeping]
+
+6
+00:01:29,590 --> 00:01:31,992
+[automated]
+'The time is 7:35.'
diff --git a/testdata/example-styled-out.srt b/testdata/example-styled-out.srt
new file mode 100644
index 0000000..4f14974
--- /dev/null
+++ b/testdata/example-styled-out.srt
@@ -0,0 +1,24 @@
+1
+00:00:17,985 --> 00:00:20,521
+[instrumental music]
+
+2
+00:00:47,115 --> 00:00:48,282
+[ticks]
+
+3
+00:00:58,192 --> 00:00:59,727
+[instrumental music]
+
+4
+00:01:01,662 --> 00:01:03,063
+[dog barking]
+
+5
+00:01:26,787 --> 00:01:29,523
+[beeping]
+
+6
+00:01:29,590 --> 00:01:31,992
+[automated]
+'The time is 7:35.'
diff --git a/testdata/example-styled-out.vtt b/testdata/example-styled-out.vtt
new file mode 100644
index 0000000..7bd4bb0
--- /dev/null
+++ b/testdata/example-styled-out.vtt
@@ -0,0 +1,26 @@
+WEBVTT
+
+1
+00:00:17.985 --> 00:00:20.521
+[instrumental music]
+
+2
+00:00:47.115 --> 00:00:48.282
+[ticks]
+
+3
+00:00:58.192 --> 00:00:59.727
+[instrumental music]
+
+4
+00:01:01.662 --> 00:01:03.063
+[dog barking]
+
+5
+00:01:26.787 --> 00:01:29.523
+[beeping]
+
+6
+00:01:29.590 --> 00:01:31.992
+[automated]
+'The time is 7:35.'
diff --git a/webvtt.go b/webvtt.go
index 7808a05..8287090 100644
--- a/webvtt.go
+++ b/webvtt.go
@@ -31,11 +31,13 @@ const (
// Vars
var (
+ bytesWebVTTItalicEndTag = []byte("")
+ bytesWebVTTItalicStartTag = []byte("")
bytesWebVTTTimeBoundariesSeparator = []byte(webvttTimeBoundariesSeparator)
webVTTRegexpInlineTimestamp = regexp.MustCompile(`<((?:\d{2,}:)?\d{2}:\d{2}\.\d{3})>`)
webVTTRegexpTag = regexp.MustCompile(`(*\s*([^\.\s]+)(\.[^\s/]*)*\s*([^/]*)\s*/*>)`)
webVTTEscaper = strings.NewReplacer("&", "&", "<", "<")
- webVTTUnescaper = strings.NewReplacer(" ", " ", "&", "&", "<", "<")
+ webVTTUnescaper = strings.NewReplacer("&", "&", "<", "<")
)
// parseDurationWebVTT parses a .vtt duration