Skip to content

Commit

Permalink
(Update) comparison inside code block
Browse files Browse the repository at this point in the history
  • Loading branch information
LostRager committed May 22, 2024
1 parent 68ef67e commit d3f18d7
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions app/Helpers/Bbcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,25 +324,29 @@ public function parse(?string $source, bool $replaceLineBreaks = true): string
// Common comparison syntax used in other torrent management systems is quite specific,
// so it must be done here instead
$source = preg_replace_callback(
'/\[comparison=(.*?)]\s*(.*?)\s*\[\/comparison]/is',
'/\[code](.*?)\[\/code]|(\[comparison=(.*?)]\s*(.*?)\s*\[\/comparison])/is',
function ($matches) {
$comparates = preg_split('/\s*,\s*/', $matches[1]);
$urls = preg_split('/\s*(?:,|\s)\s*/', $matches[2]);

if ($comparates === false || $urls === false) {
return 'Broken comparison';
}

$validatedUrls = collect($urls)->map(fn ($url) => $this->sanitizeUrl($url, isImage: true));
$chunkedUrls = $validatedUrls->chunk(\count($comparates));
$html = view('partials.comparison', ['comparates' => $comparates, 'urls' => $chunkedUrls])->render();
$html = preg_replace('/\s+/', ' ', $html);

if (!\is_string($html)) {
return 'Broken html';
if (!empty($matches[1])) {
return $matches[0];
} else {
$comparates = preg_split('/\s*,\s*/', $matches[3]);
$urls = preg_split('/\s*(?:,|\s)\s*/', $matches[4]);

if ($comparates === false || $urls === false) {
return 'Broken comparison';
}

$validatedUrls = collect($urls)->map(fn ($url) => $this->sanitizeUrl($url, isImage: true));
$chunkedUrls = $validatedUrls->chunk(\count($comparates));
$html = view('partials.comparison', ['comparates' => $comparates, 'urls' => $chunkedUrls])->render();
$html = preg_replace('/\s+/', ' ', $html);

if (!\is_string($html)) {
return 'Broken html';
}

return $html;
}

return $html;
},
$source
);
Expand Down

0 comments on commit d3f18d7

Please sign in to comment.