Skip to content

Commit

Permalink
Add client file
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Feb 22, 2024
1 parent c14bbe4 commit 0544f53
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/Code/Converters/SmiConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,28 @@ public function fileContentToInternalFormat($file_content, $original_file_conten

$syncElements = $doc->getElementsByTagName('sync');

$data = [];
foreach ($syncElements as $syncElement) {
$time = $syncElement->getAttribute('start');

if(!$syncElement->childNodes->length) {
continue;
}

foreach ($syncElement->childNodes as $childNode) {
$lines = [];
$line = '';

$contentNode = null;

if ($childNode->nodeName === 'p' || $childNode->nodeName === '#text') {
$line = $doc->saveHTML($childNode);
$contentNode = $childNode;
} else if ($childNode->nodeName === 'font' && $childNode->childNodes->length) {
$contentNode = $childNode->childNodes->item(0);
}

if($contentNode) {
$line = $doc->saveHTML($contentNode);
$line = preg_replace('/<br\s*\/?>/', '<br>', $line); // normalize <br>
$line = str_replace("\u{00a0}", '', $line); // no brake space - &nbsp;
$line = str_replace("&amp;nbsp", '', $line); // somebody didn't have semicolon at the end of &nbsp
Expand All @@ -62,6 +76,10 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
];
}

if(empty($data)) {
return $internal_format;
}

$i = 0;
foreach ($data as $row) {
if (!isset($internal_format[$i - 1]['end']) && $i !== 0) {
Expand All @@ -76,7 +94,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
}
}
if (!isset($internal_format[$i - 1]['end'])) {
$internal_format[$i - 1]['end'] = $internal_format[$i - 1]['start'] + 2.067; // SubtitleEdit adds this time if there is no last nbsp block
$internal_format[$i - 1]['end'] = $internal_format[$i - 1]['start'] + 1;
}

return $internal_format;
Expand Down
24 changes: 22 additions & 2 deletions tests/formats/SmiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testFormatted()
'When we think',
'of E equals m c-squared,',
])
->add(17.35, 19.417, 'we have this vision of Einstein')
->add(17.35, 18.35, 'we have this vision of Einstein')
->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}
Expand Down Expand Up @@ -136,7 +136,27 @@ public function testClientFile2()
</SAMI>
')->getInternalFormat();
$expected = (new Subtitles())
->add(141.516, 143.583, 'test')
->add(141.516, 142.516, 'test')
->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}

public function testClientFile3()
{
$actual = Subtitles::loadFromString('<SAMI>
<HEAD>
<TITLE>금토드라마(열혈사제)-35회(19년04월13일(토))</TITLE>
<HEAD>
<BODY>
<SYNC START=144700>
<SYNC START=145308><FONT COLOR="FFFF00">-야, 처리해. </FONT>
<SYNC START=145994>-야, 이중권.
</BODY>
</SAMI>
')->getInternalFormat();
$expected = (new Subtitles())
->add(145.308, 145.994, '-야, 처리해.')
->add(145.994, 146.994, '-야, 이중권.')
->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}
Expand Down

0 comments on commit 0544f53

Please sign in to comment.