fix: convert ruby date format to php in Liquid

This commit is contained in:
Benjamin Nussbaum 2025-10-06 23:00:18 +02:00
parent c1786dfb6d
commit c8f6dd3bec
2 changed files with 37 additions and 5 deletions

View file

@ -168,6 +168,19 @@ class ExpressionUtils
public static function strftimeToPhpFormat(string $strftimeFormat): string public static function strftimeToPhpFormat(string $strftimeFormat): string
{ {
$conversions = [ $conversions = [
// Special Ruby format cases
'%N' => 'u', // Microseconds (Ruby) -> microseconds (PHP)
'%u' => 'u', // Microseconds (Ruby) -> microseconds (PHP)
'%-m' => 'n', // Month without leading zero (Ruby) -> month without leading zero (PHP)
'%-d' => 'j', // Day without leading zero (Ruby) -> day without leading zero (PHP)
'%-H' => 'G', // Hour without leading zero (Ruby) -> hour without leading zero (PHP)
'%-I' => 'g', // Hour 12h without leading zero (Ruby) -> hour 12h without leading zero (PHP)
'%-M' => 'i', // Minute without leading zero (Ruby) -> minute without leading zero (PHP)
'%-S' => 's', // Second without leading zero (Ruby) -> second without leading zero (PHP)
'%z' => 'O', // Timezone offset (Ruby) -> timezone offset (PHP)
'%Z' => 'T', // Timezone name (Ruby) -> timezone name (PHP)
// Standard strftime conversions
'%A' => 'l', // Full weekday name '%A' => 'l', // Full weekday name
'%a' => 'D', // Abbreviated weekday name '%a' => 'D', // Abbreviated weekday name
'%B' => 'F', // Full month name '%B' => 'F', // Full month name

View file

@ -216,15 +216,15 @@ class Plugin extends Model
*/ */
private function applyLiquidReplacements(string $template): string private function applyLiquidReplacements(string $template): string
{ {
$replacements = [
'date: "%N"' => 'date: "u"', $replacements = [];
'date: "%u"' => 'date: "u"',
'%-m/%-d/%Y' => 'm/d/Y',
];
// Apply basic replacements // Apply basic replacements
$template = str_replace(array_keys($replacements), array_values($replacements), $template); $template = str_replace(array_keys($replacements), array_values($replacements), $template);
// Convert Ruby/strftime date formats to PHP date formats
$template = $this->convertDateFormats($template);
// Convert {% render "template" with %} syntax to {% render "template", %} syntax // Convert {% render "template" with %} syntax to {% render "template", %} syntax
$template = preg_replace( $template = preg_replace(
'/{%\s*render\s+([^}]+?)\s+with\s+/i', '/{%\s*render\s+([^}]+?)\s+with\s+/i',
@ -251,6 +251,25 @@ class Plugin extends Model
return $template; return $template;
} }
/**
* Convert Ruby/strftime date formats to PHP date formats in Liquid templates
*/
private function convertDateFormats(string $template): string
{
// Handle date filter formats: date: "format"
$template = preg_replace_callback(
'/date:\s*"([^"]+)"/',
function ($matches): string {
$format = $matches[1];
$convertedFormat = \App\Liquid\Utils\ExpressionUtils::strftimeToPhpFormat($format);
return 'date: "'.$convertedFormat.'"';
},
$template
);
return $template;
}
/** /**
* Resolve Liquid variables in a template string using the Liquid template engine * Resolve Liquid variables in a template string using the Liquid template engine
* *