Skip to content

Commit

Permalink
Let another converter handle invalid LRC
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Aug 15, 2023
1 parent 63d593c commit 866b4d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Code/Converters/LrcConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class LrcConverter implements ConverterContract

public function canParseFileContent($file_content)
{
return preg_match(self::$regexp . 's', $file_content) === 1;
// only select when there is text after the timestamp
// do not select files that have timestamp and text somewhere on the other line
$regex = rtrim(self::$regexp, '/') . ' *[\p{L}]+' . '/s';
return preg_match($regex, $file_content) === 1;
}

public function fileContentToInternalFormat($file_content)
Expand Down
19 changes: 18 additions & 1 deletion tests/formats/LrcTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\formats;
namespace Tests\Formats;

use Done\Subtitles\Code\Converters\LrcConverter;
use Done\Subtitles\Code\Helpers;
Expand All @@ -19,6 +19,23 @@ public function testRecognizeLrc()
$this->assertTrue($converter::class === LrcConverter::class);
}

public function testNotLrc() // let other converter handle invalid lrc
{
$content = '[00:02:35]
Tere Vaaste Falak Se
Main Chaand Launga
Solah Satrah Sitaare
Sang Baandh Launga
[00:02:51]
Tere Vaaste Falak Se
Main Chaand Launga
Solah Satrah Sitaare
Sang Baandh Launga';
$converter = Helpers::getConverterByFileContent($content);
$this->assertTrue($converter::class !== LrcConverter::class);
}

public function testParsesLrc()
{
$expected = (new Subtitles())->loadFromFile('./tests/files/lrc.lrc')->getInternalFormat();
Expand Down

0 comments on commit 866b4d2

Please sign in to comment.