Skip to content

Commit

Permalink
Fix rtf backslashes
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Apr 10, 2024
1 parent 9ec77fc commit fbd3858
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/Code/Converters/RtfReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
20 changes: 20 additions & 0 deletions tests/files/rtf2.rtf
Original file line number Diff line number Diff line change
@@ -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 \
}
12 changes: 10 additions & 2 deletions tests/formats/RtfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

class RtfTest extends TestCase
{

use AdditionalAssertionsTrait;

public function testParsesRtfFile()
{
$content = file_get_contents('./tests/files/rtf.rtf');
Expand All @@ -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);
}
}

0 comments on commit fbd3858

Please sign in to comment.