Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom split logic for scanner #125

Merged
merged 1 commit into from
Dec 19, 2024

Conversation

justin-taylor
Copy link
Contributor

@justin-taylor justin-taylor commented Dec 17, 2024

This PR provides a custom bufio.SplitFunc that works with carriage returns \r, allowing for parsing of files created by windows software.

Closes #118

Copy link
Owner

@asticode asticode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice PR!

Just a few minor changes needed 👍

subtitles.go Outdated
@@ -927,3 +930,32 @@ func escapeHTML(i string) string {
func unescapeHTML(i string) string {
return htmlUnescaper.Replace(i)
}

func NewScanner(i io.Reader) *bufio.Scanner {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, I don't see the point of exporting this method, therefore could you rename it to newScanner instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Updated.

subtitles.go Outdated
return scanner
}

func splitLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really see the point of creating an extra splitLines function, since newScanner is simple enough could you add the split function directly as an anonymous function:

scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
  ...
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

func TestNewScanner(t *testing.T) {
exts := []string{"vtt", "srt", "ssa"}
for _, ext := range exts {
s, err := astisub.OpenFile("./testdata/example-in-scan-line." + ext)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rename the testdata files to example-in-carriage-return.(srt|ssa|vtt) instead?

@justin-taylor
Copy link
Contributor Author

@asticode all comments have been addressed. Let me know if there is anything else. Otherwise, can I get another tag 🙏 ?

@asticode asticode merged commit 721d3fc into asticode:master Dec 19, 2024
1 check passed
@asticode
Copy link
Owner

Thanks again for the PR ❤️

I've created the v0.32.0 tag 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement custom bufio.SplitFunc for WebVTT
2 participants