Skip to content

Commit

Permalink
Remove empty lines in the block
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Aug 11, 2023
1 parent 936ab4b commit 2d0a573
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Subtitles.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ public static function loadFromString($string, $format = null)
}
$internal_format = $input_converter->fileContentToInternalFormat($converter->input);

// remove empty lines
foreach ($internal_format as $k => $row) {
$new_lines = [];
foreach ($row['lines'] as $line) {
if (trim($line) !== '') {
$new_lines[] = $line;
}
}
$internal_format[$k]['lines'] = $new_lines;
}

// trim lines
foreach ($internal_format as &$row) {
foreach ($row['lines'] as &$line) {
Expand All @@ -226,7 +237,7 @@ public static function loadFromString($string, $format = null)

// remove blocks without text
foreach ($internal_format as $k => $row) {
if (trim($row['lines'][0]) === '') {
if (!isset($row['lines'][0]) || trim($row['lines'][0]) === '') {
unset($internal_format[$k]);
}
}
Expand Down
1 change: 1 addition & 0 deletions test_helpers/AdditionalAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function assertInternalFormatsEqual($expected, $actual, $allowable_error
$this->assertTrue(abs(round($expected[$k]['start'], 2) - round($actual[$k]['start'], 2)) < $allowable_error, round($expected[$k]['start'], 3) . ' vs ' . round($actual[$k]['start'], 3));
$this->assertTrue(abs(round($expected[$k]['end'], 2) - round($actual[$k]['end'], 2)) < $allowable_error, round($expected[$k]['end'], 3) . ' vs ' . round($actual[$k]['end'], 3));

$this->assertEquals(count($expected[$k]['lines']), count($actual[$k]['lines']), 'Line count is different');
foreach ($expected[$k]['lines'] as $line_k => $line) {
$this->assertEquals($line, $actual[$k]['lines'][$line_k]);
}
Expand Down
14 changes: 14 additions & 0 deletions tests/OtherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,18 @@ public function testRemovesEmptySubtitles()
$expected = (new Subtitles())->add(1301.41, 1304.20, 'test')->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}

public function testRemoveEmptyLines()
{
$actual = Subtitles::loadFromString('
[Script Info]
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:01.00,0:00:02.00,Default,,0,0,0,,test\N
Dialogue: 0,0:00:03.00,0:00:04.00,Default,,0,0,0,,test
')->getInternalFormat();
$expected = (new Subtitles())->add(1, 2, 'test')->add(3, 4, 'test')->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}
}

0 comments on commit 2d0a573

Please sign in to comment.