diff --git a/src/Code/Converters/RtfReader.php b/src/Code/Converters/RtfReader.php index 4252626..6886184 100644 --- a/src/Code/Converters/RtfReader.php +++ b/src/Code/Converters/RtfReader.php @@ -18,7 +18,16 @@ public function fileContentToInternalFormat($file_content, $original_file_conten { // https://stackoverflow.com/a/63029792/4126621 $text = preg_replace("/(\{.*\})|}|(\\\\(?!')\S+)/m", '', $original_file_content); - $text = trim($text); + + // remove backslashes + $lines = mb_split("\n", $text); + foreach ($lines as &$line) { + $line = trim($line); + $line = rtrim($line, '\\'); + } + unset($line); + $text = implode("\n", $lines); + return Subtitles::loadFromString($text)->getInternalFormat(); } diff --git a/tests/files/rtf2.rtf b/tests/files/rtf2.rtf new file mode 100644 index 0000000..328815c --- /dev/null +++ b/tests/files/rtf2.rtf @@ -0,0 +1,20 @@ +{\rtf1\ansi\ansicpg1252\cocoartf2706 +\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;} +{\colortbl;\red255\green255\blue255;\red255\green255\blue255;\red0\green0\blue0;} +{\*\expandedcolortbl;;\cssrgb\c100000\c100000\c100000;\cssrgb\c0\c0\c0;} +\paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh18500\viewkind0 +\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0 + +\f0\fs24 \cf0 1\ +00:03:43:00 -> 00:03:49:00\ +Reflecting back on the nineteen forties, fifties and sixties\ +2\ +00:03:50:00 -> 00:04:00:00\ +\pard\pardeftab720\partightenfactor0 + +\fs29\fsmilli14667 \cf0 \cb2 \expnd0\expndtw0\kerning0 +\outl0\strokewidth0 \strokec3 During those times the elders back home were grieving, +\fs24 \cb1 \kerning1\expnd0\expndtw0 \outl0\strokewidth0 \ +\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0 +\cf0 \ +} \ No newline at end of file diff --git a/tests/formats/RtfTest.php b/tests/formats/RtfTest.php index bf50e3e..2de02ab 100644 --- a/tests/formats/RtfTest.php +++ b/tests/formats/RtfTest.php @@ -8,9 +8,7 @@ class RtfTest extends TestCase { - use AdditionalAssertionsTrait; - public function testParsesRtfFile() { $content = file_get_contents('./tests/files/rtf.rtf'); @@ -19,4 +17,14 @@ public function testParsesRtfFile() $this->assertInternalFormatsEqual($expected, $actual); } + public function testClientFileWithBackslashes() + { + $content = file_get_contents('./tests/files/rtf2.rtf'); + $actual = Subtitles::loadFromString($content)->getInternalFormat(); + $expected = (new Subtitles()) + ->add(223, 229, 'Reflecting back on the nineteen forties, fifties and sixties') + ->add(230, 240, 'During those times the elders back home were grieving,') + ->getInternalFormat(); + $this->assertInternalFormatsEqual($expected, $actual); + } } \ No newline at end of file