From fade94156b121c49c45e95dc49f79d3cba906644 Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 1 Aug 2024 17:16:14 +0300 Subject: [PATCH] update markup api, implement header wrap support --- src/Abstract/Model/Gtk/Pango/Markup.php | 149 ++++++++++------------- src/Interface/Model/Gtk/Pango/Markup.php | 23 ++-- src/Model/Gtk/Pango/Markup/Gemtext.php | 13 +- 3 files changed, 84 insertions(+), 101 deletions(-) diff --git a/src/Abstract/Model/Gtk/Pango/Markup.php b/src/Abstract/Model/Gtk/Pango/Markup.php index 87bb718f..c23f241f 100644 --- a/src/Abstract/Model/Gtk/Pango/Markup.php +++ b/src/Abstract/Model/Gtk/Pango/Markup.php @@ -13,46 +13,46 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup ): string { return sprintf( - '%s', - htmlspecialchars( + self::TAG_CODE, + self::_escape( $value ) ); } public static function h1( - string $value + string $value, + int $width = self::WRAP_WIDTH ): string { - return sprintf( - '%s', - htmlspecialchars( - $value - ) + return self::_wrap( + self::TAG_H1, + $value, + $width ); } public static function h2( - string $value + string $value, + int $width = self::WRAP_WIDTH ): string { - return sprintf( - '%s', - htmlspecialchars( - $value - ) + return self::_wrap( + self::TAG_H2, + $value, + $width ); } public static function h3( - string $value + string $value, + int $width = self::WRAP_WIDTH ): string { - return sprintf( - '%s', - htmlspecialchars( - $value - ) + return self::_wrap( + self::TAG_H3, + $value, + $width ); } @@ -64,13 +64,13 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup { return sprintf( '%s', - htmlspecialchars( + self::_escape( $href ), - htmlspecialchars( + self::_escape( $title ), - htmlspecialchars( + self::_escape( $value ) ); @@ -81,18 +81,13 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup int $width = self::WRAP_WIDTH ): string { - return sprintf( - '%s', // @TODO - self::_wrap( - htmlspecialchars( - sprintf( - '* %s', - $value - ) - ), - self::TAG_LIST, - $width - ) + return self::_wrap( + self::TAG_LIST, + sprintf( + '* %s', + $value + ), + $width ); } @@ -101,15 +96,10 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup int $width = self::WRAP_WIDTH ): string { - return sprintf( - '%s', - self::_wrap( - htmlspecialchars( - $value - ), - self::TAG_QUOTE, - $width - ) + return self::_wrap( + self::TAG_QUOTE, + $value, + $width ); } @@ -119,10 +109,8 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup ): string { return self::_wrap( - htmlspecialchars( - $value - ), self::TAG_TEXT, + $value, $width ); } @@ -131,37 +119,28 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup string $value ): string { + return self::_escape( + $value + ); + } + + protected static function _escape( + string $value + ): string + { + // @TODO PR #135 + // https://docs.gtk.org/glib/func.markup_escape_text.html return htmlspecialchars( $value ); } - public static function tag( - string $const, - bool $close - ): string - { - if (in_array($const, [ - self::TAG_CODE, - self::TAG_LIST, - self::TAG_QUOTE, - self::TAG_TEXT - ])) { - return sprintf( - $close ? '' : '<%s>', - $const - ); - } - - throw new Exception; - } - // @TODO optimization wanted, wordwrap / set_line_wrap not solution protected static function _wrap( - string $string, string $tag, - int $width, - string $break = PHP_EOL, + string $value, + int $width = self::WRAP_WIDTH, + string $break = self::WRAP_BREAK, int $line = 1, array $words = [], array $lines = [] @@ -173,26 +152,19 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup true ); - foreach (explode(' ', $string) as $word) + foreach (explode(' ', $value) as $word) { if (isset($words[$line])) { $label->set_markup( sprintf( - '%s%s%s', - self::tag( - $tag, - false - ), - implode( - ' ' , $words[$line] - ) . ' ' . $word, - self::tag( - $tag, - true + $tag, + self::_escape( + implode( + ' ' , $words[$line] + ) . ' ' . $word ) ) - ); if ($label->get_layout()->get_pixel_size()['width'] > $width) @@ -214,9 +186,14 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup $label->destroy(); - return implode( - $break, - $lines + return sprintf( + $tag, + self::_escape( + implode( + $break, + $lines + ) + ) ); } } \ No newline at end of file diff --git a/src/Interface/Model/Gtk/Pango/Markup.php b/src/Interface/Model/Gtk/Pango/Markup.php index aa26f406..bb508a0d 100644 --- a/src/Interface/Model/Gtk/Pango/Markup.php +++ b/src/Interface/Model/Gtk/Pango/Markup.php @@ -10,11 +10,19 @@ namespace Yggverse\Yoda\Interface\Model\Gtk\Pango; */ interface Markup { - public const ENCODING = 'UTF-8'; - public const TAG_CODE = 'tt'; - public const TAG_QUOTE = 'i'; - public const TAG_TEXT = 'span'; - public const TAG_LIST = 'span'; + public const TAG_CODE = '%s'; + public const TAG_CODE_CLOSE = ''; + public const TAG_CODE_OPEN = ''; + + public const TAG_H1 = '%s'; + public const TAG_H2 = '%s'; + public const TAG_H3 = '%s'; + public const TAG_LINK = '%s'; + public const TAG_LIST = '%s'; + public const TAG_QUOTE = '%s'; + public const TAG_TEXT = '%s'; + + public const WRAP_BREAK = PHP_EOL; public const WRAP_WIDTH = 320; public static function code( @@ -54,9 +62,4 @@ interface Markup public static function pre( string $value ): string; - - public static function tag( - string $const, - bool $close - ): string; } \ No newline at end of file diff --git a/src/Model/Gtk/Pango/Markup/Gemtext.php b/src/Model/Gtk/Pango/Markup/Gemtext.php index eb3d2228..cbdf9103 100644 --- a/src/Model/Gtk/Pango/Markup/Gemtext.php +++ b/src/Model/Gtk/Pango/Markup/Gemtext.php @@ -44,8 +44,8 @@ class Gemtext extends \Yggverse\Yoda\Abstract\Model\Gtk\Pango\Markup else { - $line[] = $preformatted ? self::tag(self::TAG_CODE, true) - : self::tag(self::TAG_CODE, false); + $line[] = $preformatted ? self::TAG_CODE_CLOSE + : self::TAG_CODE_OPEN; $preformatted = !($preformatted); // toggle } @@ -68,7 +68,8 @@ class Gemtext extends \Yggverse\Yoda\Abstract\Model\Gtk\Pango\Markup case 1: // # $line[] = self::h1( - $entity->getText() + $entity->getText(), + $width ); // Find title by first # tag @@ -82,7 +83,8 @@ class Gemtext extends \Yggverse\Yoda\Abstract\Model\Gtk\Pango\Markup case 2: // ## $line[] = self::h2( - $entity->getText() + $entity->getText(), + $width ); break; @@ -90,7 +92,8 @@ class Gemtext extends \Yggverse\Yoda\Abstract\Model\Gtk\Pango\Markup case 3: // ### $line[] = self::h3( - $entity->getText() + $entity->getText(), + $width ); break;