Skip to content

Commit

Permalink
Add primitive RTF parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Apr 2, 2024
1 parent 6f802fd commit abaf5aa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/Code/Converters/RtfReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ public function canParseFileContent($file_content)

public function fileContentToInternalFormat($file_content, $original_file_content)
{

// https://stackoverflow.com/a/63029792/4126621
$text = preg_replace("/(\{.*\})|}|(\\\\(?!')\S+)/m", '', $original_file_content);
$text = trim($text);
return Subtitles::loadFromString($text)->getInternalFormat();
}

public function internalFormatToFileContent(array $internal_format, array $options)
{
// not implemented
throw new \Exception('not implemented');
}


Expand Down
2 changes: 1 addition & 1 deletion src/Subtitles.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Subtitles
['extension' => 'txt', 'format' => 'txt_quicktime', 'name' => 'Quick Time Text', 'class' => TxtQuickTimeConverter::class],
['extension' => 'scc', 'format' => 'scc', 'name' => 'Scenarist', 'class' => SccConverter::class],
['extension' => 'lrc', 'format' => 'lrc', 'name' => 'LyRiCs', 'class' => LrcConverter::class],
// ['extension' => 'rtf', 'format' => 'rtf', 'name' => 'Rich text format', 'class' => RtfReader::class], // libraryies eather throws exception, not parses, or takes long to parse 2h file
['extension' => 'rtf', 'format' => 'rtf', 'name' => 'Rich text format', 'class' => RtfReader::class], // libraryies eather throws exception, not parses, or takes long to parse 2h file
['extension' => 'csv', 'format' => 'csv', 'name' => 'Coma Separated Values', 'class' => CsvConverter::class], // must be last from bottom
['extension' => 'bin', 'format' => 'bin', 'name' => 'Binary', 'class' => BinaryFinder::class],
['extension' => 'txt', 'format' => 'txt', 'name' => 'Plaintext', 'class' => TxtConverter::class], // must be the last one
Expand Down
Binary file added tests/files/rtf.rtf
Binary file not shown.
22 changes: 22 additions & 0 deletions tests/formats/RtfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Formats;

use Done\Subtitles\Subtitles;
use PHPUnit\Framework\TestCase;
use Helpers\AdditionalAssertionsTrait;

class RtfTest extends TestCase
{

use AdditionalAssertionsTrait;

public function testParsesRtfFile()
{
$content = file_get_contents('./tests/files/rtf.rtf');
$actual = Subtitles::loadFromString($content)->getInternalFormat();
$expected = (new Subtitles())->add(1, 2, 'word')->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}

}

0 comments on commit abaf5aa

Please sign in to comment.