Skip to content

Commit

Permalink
Add line ending detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijs Riezebeek committed Feb 28, 2016
1 parent 37a6856 commit 2a0baad
Show file tree
Hide file tree
Showing 9 changed files with 3,275 additions and 56 deletions.
23 changes: 22 additions & 1 deletion src/SubRip/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ class File
/**
* @var Subtitle[]
*/
private $subtitles = [];
protected $subtitles = [];

/**
* @var string
*/
protected $line_ending = PHP_EOL;

/**
* @return int
Expand All @@ -30,4 +35,20 @@ public function getSubtitles ()
{
return $this->subtitles;
}

/**
* @return string
*/
public function getLineEnding ()
{
return $this->line_ending;
}

/**
* @param string $line_ending
*/
public function setLineEnding ($line_ending)
{
$this->line_ending = $line_ending;
}
}
3 changes: 3 additions & 0 deletions src/SubRip/NoLineEndingsFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php namespace ThijsR\Subtitles\SubRip;

class NoLineEndingsFoundException extends \Exception {}
77 changes: 69 additions & 8 deletions src/SubRip/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,77 @@ protected static function getFileContents ($file_path)
*/
protected static function scanFileContents ($file_contents)
{
$blocks = explode("\n\n", $file_contents);
$srt_file = new File();

try
{
$srt_file->setLineEnding(self::getFileLineEnding($file_contents));
}
catch (NoLineEndingsFoundException $e)
{
// Probably a file with no subtitles at all (empty)
return $srt_file;
}

$block_separator = $srt_file->getLineEnding() . $srt_file->getLineEnding();
$blocks = explode($block_separator, $file_contents);
self::scanBlocksIntoFile($srt_file, $blocks);

return self::scanBlocks($blocks);
return $srt_file;
}

/**
* @param string $file_contents
* @return string
* @throws NoLineEndingsFoundException
*/
private static function getFileLineEnding($file_contents)
{
// Search for double newlines that break up the blocks
if (preg_match("/(\r\n\r\n)/s", $file_contents))
{
return "\r\n";
}
if (preg_match("/(\n\r\n\r)/s", $file_contents))
{
return "\n\r";
}
if (preg_match("/(\n\n)/s", $file_contents))
{
return "\n";
}
if (preg_match("/(\r\r)/s", $file_contents))
{
return "\r";
}

// No double newlines so far, meaning a file with one subtitle
if (preg_match("/(\r\n)/s", $file_contents))
{
return "\r\n";
}
if (preg_match("/(\n\r)/s", $file_contents))
{
return "\n\r";
}
if (preg_match("/(\n)/s", $file_contents))
{
return "\n";
}
if (preg_match("/(\r)/s", $file_contents))
{
return "\r";
}

throw new NoLineEndingsFoundException("");
}

/**
* @param File $srt_file
* @param array $blocks
* @return File
*/
protected static function scanBlocks(array $blocks)
protected static function scanBlocksIntoFile(File $srt_file, array $blocks)
{
$srt_file = new File();
foreach ($blocks as $block)
{
if (trim($block) === "")
Expand All @@ -47,8 +106,6 @@ protected static function scanBlocks(array $blocks)

$srt_file->addSubtitle(self::createSubtitleFromBlock($block));
}

return $srt_file;
}

/**
Expand All @@ -68,9 +125,13 @@ protected static function createSubtitleFromBlock($block)
return $subtitle;
}

/**
* @param string $block
* @return mixed
*/
protected static function matchBlockToRegex ($block)
{
preg_match("/(\d*)\r?\n(\d\d:\d\d:\d\d,\d\d\d) --> (\d\d:\d\d:\d\d,\d\d\d)\r?\n(.*)/s", $block, $matches);
preg_match('/(\d*)(?:\r\n|\n\r|\n|\r)(\d{1,3}:\d{1,2}:\d{1,2},\d{1,3}) --> (\d{1,3}:\d{1,2}:\d{1,2},\d{1,3})(?:\r\n|\n\r|\n|\r)(.*)/s', $block, $matches);

return $matches;
}
Expand Down
9 changes: 0 additions & 9 deletions src/SubRip/Subtitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,4 @@ public function getFormattedStopTime ()
{
return $this->stop_time->toString();
}

/**
* @return string
*/
public function toString ()
{
return sprintf("%d\n%s --> %s\n%s",
$this->number, $this->getFormattedStartTime(), $this->getFormattedStopTime(), $this->text);
}
}
22 changes: 21 additions & 1 deletion src/SubRip/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ public static function writeFile (File $srt_file, $destination)
file_put_contents($destination, $file_contents);
}

/**
* @param File $srt_file
* @return string
*/
public static function writeFileToString (File $srt_file)
{
return self::createFileContents($srt_file);
}

/**
* @param File $srt_file
* @return string
Expand All @@ -23,15 +32,26 @@ private static function createFileContents (File $srt_file)
$new_file = "";
$subtitles = $srt_file->getSubtitles();
$subtitle_count = $srt_file->subtitleCount();
$line_ending = $srt_file->getLineEnding();

for ($i = 0; $i < $subtitle_count; $i += 1)
{
$new_file .= $subtitles[$i]->toString() . (self::isLastSubtitle($i, $subtitle_count) ? "\n" : "\n\n");
$new_file .= self::subtitleToString($subtitles[$i], $line_ending) . $line_ending;
if (!self::isLastSubtitle($i, $subtitle_count))
{
$new_file .= $line_ending;
}
}

return $new_file;
}

protected static function subtitleToString (Subtitle $subtitle, $line_ending)
{
return sprintf("%d$line_ending%s --> %s$line_ending%s",
$subtitle->number, $subtitle->getFormattedStartTime(), $subtitle->getFormattedStopTime(), $subtitle->text);
}

/**
* @param int $i
* @param int $subtitle_count
Expand Down
Loading

0 comments on commit 2a0baad

Please sign in to comment.