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

General fixes related to new line handling, and adding tests for all formats #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/utils/regexes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class SrtRegex extends SubtitleRegexObject {
/// dart code.
class TtmlRegex extends SubtitleRegexObject {
static const String _regex =
r'<p[\w\d:="\s]+?begin="(\d{1,}:)?(\d{1,}:)?(\d{1,}).(\d{1,})s?"[\w\d:="\s]+end="(\d{1,}:)?(\d{1,}:)?(\d{1,}).(\d{1,})s?"[\w\d:="\s]*>(\D+)<\/p>';
r'<p[\w\d:="\s]+?begin="(\d{1,}):?(\d{1,}):?(\d{1,}).(\d{1,})s?"[\w\d:="\s]+end="(\d{1,}):?(\d{1,}):?(\d{1,}).(\d{1,})s?"[\w\d:="\s]*>(\D+)<\/p>';
const TtmlRegex()
: super(
pattern: _regex,
Expand Down
9 changes: 6 additions & 3 deletions lib/src/utils/subtitle_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ abstract class ISubtitleParser {
/// Normalize the text data of subtitle, remove unnecessary characters.
String normalize(String txt) {
return txt
.replaceAll(RegExp(r'<\/?[\w.]+\/?>|\n| {2,}'), ' ')
.replaceAll(RegExp(r'<\/?[\w.]+\/?>| {2,}'), ' ')
.replaceAll(RegExp(r' {2,}'), ' ')
// Remove multiple new lines
.replaceAll(RegExp(r'\n{2,}'), '\n')
.trim();
}
}
Expand Down Expand Up @@ -56,8 +58,9 @@ class SubtitleParser extends ISubtitleParser {
final pattern = regexObject.pattern;

var regExp = RegExp(pattern);
var matches = regExp
.allMatches(object.data.replaceAll('\r', '').replaceAll('\r\n', '\n'));
var cleanedData = object.data.replaceAll('\r', '').replaceAll('\r\n', '\n');

var matches = regExp.allMatches(cleanedData);

return _decodeSubtitleFormat(
matches,
Expand Down
13 changes: 13 additions & 0 deletions test/dfxp_subtitle
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<details> <summary>Sample DFXP/TTML content</summary>


<?xml version="1.0" encoding="UTF-8"?>
<tt xmlns="http://www.w3.org/ns/ttml">
<body>
<div>
<p begin="00:00:00.500" end="00:00:02.000">Hello from DFXP</p>
<p begin="00:00:02.000" end="00:00:04.000">Still the same TTML style</p>
</div>
</body>
</tt>
</details>
38 changes: 38 additions & 0 deletions test/dfxp_subtitle_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'dart:io';
import 'package:test/test.dart';
import 'package:subtitle/subtitle.dart';

void main() {
group('DFXP Subtitle Parsing', () {
test('Test parsing of DFXP format from file', () async {
// Read the DFXP subtitle content
final subtitleFile = File('test/dfxp_subtitle');
final data = await subtitleFile.readAsString();

// Parse the subtitle string with the correct type
var controller = SubtitleController(
provider: SubtitleProvider.fromString(
data: data,
type: SubtitleType.dfxp,
),
);
await controller.initial();
final subtitles = controller.subtitles;

// Basic checks
expect(subtitles.length, 2);

// First
expect(subtitles[0].index, 1);
expect(subtitles[0].start.toString(), '0:00:00.500000');
expect(subtitles[0].end.toString(), '0:00:02.000000');
expect(subtitles[0].data, 'Hello from DFXP');

// Second
expect(subtitles[1].index, 2);
expect(subtitles[1].start.toString(), '0:00:02.000000');
expect(subtitles[1].end.toString(), '0:00:04.000000');
expect(subtitles[1].data, 'Still the same TTML style');
});
});
}
11 changes: 11 additions & 0 deletions test/srt_subtitle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
1
00:00:00,000 --> 00:00:01,000
Hello world!

2
00:00:01,001 --> 00:00:03,500
This is a test for SRT subtitle format.

3
00:00:03,600 --> 00:00:05,000
Subtitle parsing is fun!
44 changes: 44 additions & 0 deletions test/srt_subtitle_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'dart:io';
import 'package:test/test.dart';
import 'package:subtitle/subtitle.dart';

void main() {
group('SRT Subtitle Parsing', () {
test('Test parsing of SRT format from file', () async {
// Read the SRT subtitle content from the file
final subtitleFile = File('test/srt_subtitle');
final data = await subtitleFile.readAsString();

// Parse the subtitle string with the correct type
var controller = SubtitleController(
provider: SubtitleProvider.fromString(
data: data,
type: SubtitleType.srt,
),
);
await controller.initial();
final subtitles = controller.subtitles;

// Basic checks
expect(subtitles.length, 3);

// First subtitle line
expect(subtitles[0].index, 1);
expect(subtitles[0].start.toString(), '0:00:00.000000');
expect(subtitles[0].end.toString(), '0:00:01.000000');
expect(subtitles[0].data, 'Hello world!');

// Second subtitle line
expect(subtitles[1].index, 2);
expect(subtitles[1].start.toString(), '0:00:01.001000');
expect(subtitles[1].end.toString(), '0:00:03.500000');
expect(subtitles[1].data, 'This is a test for SRT subtitle format.');

// Third subtitle line
expect(subtitles[2].index, 3);
expect(subtitles[2].start.toString(), '0:00:03.600000');
expect(subtitles[2].end.toString(), '0:00:05.000000');
expect(subtitles[2].data, 'Subtitle parsing is fun!');
});
});
}
3 changes: 2 additions & 1 deletion test/string_subtitle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1
00:00:02,250 --> 00:00:03,509
Why am I here?
Why am I
here?

2
00:00:03,960 --> 00:00:05,539
Expand Down
2 changes: 1 addition & 1 deletion test/string_subtitle_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void main() {
expect(subtitles[0].index, 1);
expect(subtitles[0].start.toString(), '0:00:02.250000');
expect(subtitles[0].end.toString(), '0:00:03.509000');
expect(subtitles[0].data, 'Why am I here?');
expect(subtitles[0].data, 'Why am I\nhere?');

expect(subtitles[9].index, 10);
expect(subtitles[9].start.toString(), '0:00:16.729000');
Expand Down
10 changes: 10 additions & 0 deletions test/ttml_subtitle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<tt xmlns="http://www.w3.org/ns/ttml">
<body>
<div>
<p begin="00:00:01.000" end="00:00:03.000">Hello from TTML</p>
<p begin="00:00:03.000" end="00:00:04.500">This is a TTML subtitle</p>
<p begin="00:00:05.000" end="00:00:07.000">Goodbye from TTML</p>
</div>
</body>
</tt>
44 changes: 44 additions & 0 deletions test/ttml_subtitle_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'dart:io';
import 'package:test/test.dart';
import 'package:subtitle/subtitle.dart';

void main() {
group('TTML Subtitle Parsing', () {
test('Test parsing of TTML format from file', () async {
// Read the TTML subtitle content from the file
final subtitleFile = File('test/ttml_subtitle');
final data = await subtitleFile.readAsString();

// Parse the subtitle string with the correct type (TTML)
var controller = SubtitleController(
provider: SubtitleProvider.fromString(
data: data,
type: SubtitleType.ttml,
),
);
await controller.initial();
final subtitles = controller.subtitles;

// Basic checks
expect(subtitles.length, 3);

// First TTML subtitle
expect(subtitles[0].index, 1);
expect(subtitles[0].start.toString(), '0:00:01.000000');
expect(subtitles[0].end.toString(), '0:00:03.000000');
expect(subtitles[0].data, 'Hello from TTML');

// Second TTML subtitle
expect(subtitles[1].index, 2);
expect(subtitles[1].start.toString(), '0:00:03.000000');
expect(subtitles[1].end.toString(), '0:00:04.500000');
expect(subtitles[1].data, 'This is a TTML subtitle');

// Third TTML subtitle
expect(subtitles[2].index, 3);
expect(subtitles[2].start.toString(), '0:00:05.000000');
expect(subtitles[2].end.toString(), '0:00:07.000000');
expect(subtitles[2].data, 'Goodbye from TTML');
});
});
}
14 changes: 14 additions & 0 deletions test/vtt_subtitle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
WEBVTT

1
00:00:00.000 --> 00:00:02.000
Hello from VTT

2
00:00:02.000 --> 00:00:03.500
This is a test
for VTT format

3
00:00:04.000 --> 00:00:05.000
Goodbye!
44 changes: 44 additions & 0 deletions test/vtt_subtitle_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'dart:io';
import 'package:test/test.dart';
import 'package:subtitle/subtitle.dart';

void main() {
group('VTT Subtitle Parsing', () {
test('Test parsing of VTT format from file', () async {
// Read the VTT subtitle content from the file
final subtitleFile = File('test/vtt_subtitle');
final data = await subtitleFile.readAsString();

// Parse the subtitle string with the correct type
var controller = SubtitleController(
provider: SubtitleProvider.fromString(
data: data,
type: SubtitleType.vtt,
),
);
await controller.initial();
final subtitles = controller.subtitles;

// Basic checks
expect(subtitles.length, 3);

// First
expect(subtitles[0].index, 1);
expect(subtitles[0].start.toString(), '0:00:00.000000');
expect(subtitles[0].end.toString(), '0:00:02.000000');
expect(subtitles[0].data, 'Hello from VTT');

// Second
expect(subtitles[1].index, 2);
expect(subtitles[1].start.toString(), '0:00:02.000000');
expect(subtitles[1].end.toString(), '0:00:03.500000');
expect(subtitles[1].data, 'This is a test \nfor VTT format');

// Third
expect(subtitles[2].index, 3);
expect(subtitles[2].start.toString(), '0:00:04.000000');
expect(subtitles[2].end.toString(), '0:00:05.000000');
expect(subtitles[2].data, 'Goodbye!');
});
});
}