feat: add TRMNL custom Liquid filters

This commit is contained in:
Benjamin Nussbaum 2025-07-15 15:05:08 +02:00
parent 227f0e51c2
commit 895d126ab7
12 changed files with 519 additions and 0 deletions

View file

@ -0,0 +1,22 @@
<?php
namespace App\Liquid\Filters;
use Keepsuit\Liquid\Filters\FiltersProvider;
/**
* Data filters for Liquid templates
*/
class Data extends FiltersProvider
{
/**
* Convert a variable to JSON
*
* @param mixed $value The variable to convert
* @return string JSON representation of the variable
*/
public function json(mixed $value): string
{
return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
}

View file

@ -0,0 +1,52 @@
<?php
namespace App\Liquid\Filters;
use DateTimeInterface;
use Illuminate\Support\Carbon;
use Keepsuit\Liquid\Filters\FiltersProvider;
/**
* Localization filters for Liquid templates
*
* Uses Laravel's translator for word translations. Translation files are located in the
* lang/{locale}/custom_plugins.php files.
*/
class Localization extends FiltersProvider
{
/**
* Localize a date with strftime syntax
*
* @param mixed $date The date to localize (string or DateTime)
* @param string $format The strftime format string
* @param string|null $locale The locale to use for localization
* @return string The localized date string
*/
public function l_date(mixed $date, string $format = 'Y-m-d', ?string $locale = null): string
{
$carbon = $date instanceof DateTimeInterface ? Carbon::instance($date) : Carbon::parse($date);
if ($locale) {
$carbon->locale($locale);
}
return $carbon->translatedFormat($format);
}
/**
* Translate a common word to another language
*
* @param string $word The word to translate
* @param string $locale The locale to translate to
* @return string The translated word
*/
public function l_word(string $word, string $locale): string
{
$translation = trans('custom_plugins.'.mb_strtolower($word), locale: $locale);
if ($translation === 'custom_plugins.'.mb_strtolower($word)) {
return $word;
}
return $translation;
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace App\Liquid\Filters;
use Illuminate\Support\Number;
use Keepsuit\Liquid\Filters\FiltersProvider;
class Numbers extends FiltersProvider
{
/**
* Format a number with delimiters (default: comma)
*
* @param mixed $value The number to format
* @param string $delimiter The delimiter to use (default: comma)
* @param string $separator The separator for decimal part (default: period)
*/
public function number_with_delimiter(mixed $value, string $delimiter = ',', string $separator = '.'): string
{
// 2 decimal places for floats, 0 for integers
$decimal = is_float($value + 0) ? 2 : 0;
return number_format($value, decimals: $decimal, decimal_separator: $separator, thousands_separator: $delimiter);
}
/**
* Format a number as currency
*
* @param mixed $value The number to format
* @param string $currency Currency symbol or locale code
* @param string $delimiter The delimiter to use (default: comma)
* @param string $separator The separator for decimal part (default: period)
*/
public function number_to_currency(mixed $value, string $currency = 'USD', string $delimiter = ',', string $separator = '.'): string
{
if ($currency === '$') {
$currency = 'USD';
} elseif ($currency === '€') {
$currency = 'EUR';
} elseif ($currency === '£') {
$currency = 'GBP';
}
if ($delimiter === '.' && $separator === ',') {
$locale = 'de';
} else {
$locale = 'en';
}
// 2 decimal places for floats, 0 for integers
$decimal = is_float($value + 0) ? 2 : 0;
return Number::currency($value, in: $currency, precision: $decimal, locale: $locale);
}
}

View file

@ -0,0 +1,61 @@
<?php
namespace App\Liquid\Filters;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Keepsuit\Liquid\Filters\FiltersProvider;
use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Exception\CommonMarkException;
/**
* String, Markup, and HTML filters for Liquid templates
*/
class StringMarkup extends FiltersProvider
{
/**
* Pluralize a word based on count
*
* @param string $word The word to pluralize
* @param int $count The count to determine pluralization
* @return string The pluralized word with count
*/
public function pluralize(string $word, int $count = 2): string
{
if ($count === 1) {
return "{$count} {$word}";
}
return "{$count} ".Str::plural($word, $count);
}
/**
* Convert markdown to HTML
*
* @param string $markdown The markdown text to convert
* @return string The HTML representation of the markdown
*/
public function markdown_to_html(string $markdown): ?string
{
$converter = new CommonMarkConverter();
try {
return $converter->convert($markdown);
} catch (CommonMarkException $e) {
Log::error('Markdown conversion error: '.$e->getMessage());
}
return null;
}
/**
* Strip HTML tags from a string
*
* @param string $html The HTML string to strip
* @return string The string without HTML tags
*/
public function strip_html(string $html): string
{
return strip_tags($html);
}
}

View file

@ -0,0 +1,43 @@
<?php
namespace App\Liquid\Filters;
use Keepsuit\Liquid\Concerns\ContextAware;
use Keepsuit\Liquid\Filters\FiltersProvider;
/**
* Uniqueness filters for Liquid templates
*/
class Uniqueness extends FiltersProvider
{
use ContextAware;
/**
* Append a random string to ensure uniqueness within a template
*
* @param string $prefix The prefix to append the random string to
* @return string The prefix with a random string appended
*/
public function append_random(string $prefix): string
{
return $prefix.$this->generateRandomString();
}
/**
* Generate a random string
*
* @param int $length The length of the random string
* @return string A random string
*/
private function generateRandomString(int $length = 4): string
{
$characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
$randomString = '';
for ($i = 0; $i < $length; ++$i) {
$randomString .= $characters[rand(0, mb_strlen($characters) - 1)];
}
return $randomString;
}
}

View file

@ -2,6 +2,11 @@
namespace App\Models;
use App\Liquid\Filters\Data;
use App\Liquid\Filters\Localization;
use App\Liquid\Filters\Numbers;
use App\Liquid\Filters\StringMarkup;
use App\Liquid\Filters\Uniqueness;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
@ -95,6 +100,14 @@ class Plugin extends Model
if ($this->markup_language === 'liquid') {
$environment = App::make('liquid.environment');
// Register all custom filters
$environment->filterRegistry->register(Numbers::class);
$environment->filterRegistry->register(Data::class);
$environment->filterRegistry->register(StringMarkup::class);
$environment->filterRegistry->register(Uniqueness::class);
$environment->filterRegistry->register(Localization::class);
$template = $environment->parseString($this->render_markup);
$context = $environment->newRenderContext(data: ['size' => $size, 'data' => $this->data_payload]);
$renderedContent = $template->render($context);