Browse Source

update markup api, implement header wrap support

PHP-GTK3
yggverse 4 months ago
parent
commit
fade94156b
  1. 143
      src/Abstract/Model/Gtk/Pango/Markup.php
  2. 23
      src/Interface/Model/Gtk/Pango/Markup.php
  3. 13
      src/Model/Gtk/Pango/Markup/Gemtext.php

143
src/Abstract/Model/Gtk/Pango/Markup.php

@ -13,46 +13,46 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup
): string ): string
{ {
return sprintf( return sprintf(
'<tt>%s</tt>', self::TAG_CODE,
htmlspecialchars( self::_escape(
$value $value
) )
); );
} }
public static function h1( public static function h1(
string $value string $value,
int $width = self::WRAP_WIDTH
): string ): string
{ {
return sprintf( return self::_wrap(
'<span size="xx-large">%s</span>', self::TAG_H1,
htmlspecialchars( $value,
$value $width
)
); );
} }
public static function h2( public static function h2(
string $value string $value,
int $width = self::WRAP_WIDTH
): string ): string
{ {
return sprintf( return self::_wrap(
'<span size="x-large">%s</span>', self::TAG_H2,
htmlspecialchars( $value,
$value $width
)
); );
} }
public static function h3( public static function h3(
string $value string $value,
int $width = self::WRAP_WIDTH
): string ): string
{ {
return sprintf( return self::_wrap(
'<span size="large">%s</span>', self::TAG_H3,
htmlspecialchars( $value,
$value $width
)
); );
} }
@ -64,13 +64,13 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup
{ {
return sprintf( return sprintf(
'<a href="%s" title="%s"><span underline="none">%s</span></a>', '<a href="%s" title="%s"><span underline="none">%s</span></a>',
htmlspecialchars( self::_escape(
$href $href
), ),
htmlspecialchars( self::_escape(
$title $title
), ),
htmlspecialchars( self::_escape(
$value $value
) )
); );
@ -81,18 +81,13 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup
int $width = self::WRAP_WIDTH int $width = self::WRAP_WIDTH
): string ): string
{ {
return sprintf( return self::_wrap(
'<span>%s</span>', // @TODO self::TAG_LIST,
self::_wrap( sprintf(
htmlspecialchars( '* %s',
sprintf( $value
'* %s', ),
$value $width
)
),
self::TAG_LIST,
$width
)
); );
} }
@ -101,15 +96,10 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup
int $width = self::WRAP_WIDTH int $width = self::WRAP_WIDTH
): string ): string
{ {
return sprintf( return self::_wrap(
'<i>%s</i>', self::TAG_QUOTE,
self::_wrap( $value,
htmlspecialchars( $width
$value
),
self::TAG_QUOTE,
$width
)
); );
} }
@ -119,10 +109,8 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup
): string ): string
{ {
return self::_wrap( return self::_wrap(
htmlspecialchars(
$value
),
self::TAG_TEXT, self::TAG_TEXT,
$value,
$width $width
); );
} }
@ -131,37 +119,28 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup
string $value string $value
): string ): string
{ {
return htmlspecialchars( return self::_escape(
$value $value
); );
} }
public static function tag( protected static function _escape(
string $const, string $value
bool $close
): string ): string
{ {
if (in_array($const, [ // @TODO PR #135
self::TAG_CODE, // https://docs.gtk.org/glib/func.markup_escape_text.html
self::TAG_LIST, return htmlspecialchars(
self::TAG_QUOTE, $value
self::TAG_TEXT );
])) {
return sprintf(
$close ? '</%s>' : '<%s>',
$const
);
}
throw new Exception;
} }
// @TODO optimization wanted, wordwrap / set_line_wrap not solution // @TODO optimization wanted, wordwrap / set_line_wrap not solution
protected static function _wrap( protected static function _wrap(
string $string,
string $tag, string $tag,
int $width, string $value,
string $break = PHP_EOL, int $width = self::WRAP_WIDTH,
string $break = self::WRAP_BREAK,
int $line = 1, int $line = 1,
array $words = [], array $words = [],
array $lines = [] array $lines = []
@ -173,26 +152,19 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup
true true
); );
foreach (explode(' ', $string) as $word) foreach (explode(' ', $value) as $word)
{ {
if (isset($words[$line])) if (isset($words[$line]))
{ {
$label->set_markup( $label->set_markup(
sprintf( sprintf(
'%s%s%s', $tag,
self::tag( self::_escape(
$tag, implode(
false ' ' , $words[$line]
), ) . ' ' . $word
implode(
' ' , $words[$line]
) . ' ' . $word,
self::tag(
$tag,
true
) )
) )
); );
if ($label->get_layout()->get_pixel_size()['width'] > $width) 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(); $label->destroy();
return implode( return sprintf(
$break, $tag,
$lines self::_escape(
implode(
$break,
$lines
)
)
); );
} }
} }

23
src/Interface/Model/Gtk/Pango/Markup.php

@ -10,11 +10,19 @@ namespace Yggverse\Yoda\Interface\Model\Gtk\Pango;
*/ */
interface Markup interface Markup
{ {
public const ENCODING = 'UTF-8'; public const TAG_CODE = '<tt>%s</tt>';
public const TAG_CODE = 'tt'; public const TAG_CODE_CLOSE = '</tt>';
public const TAG_QUOTE = 'i'; public const TAG_CODE_OPEN = '<tt>';
public const TAG_TEXT = 'span';
public const TAG_LIST = 'span'; public const TAG_H1 = '<span size="xx-large">%s</span>';
public const TAG_H2 = '<span size="x-large">%s</span>';
public const TAG_H3 = '<span size="large">%s</span>';
public const TAG_LINK = '<a href="%s" title="%s"><span underline="none">%s</span></a>';
public const TAG_LIST = '<span>%s</span>';
public const TAG_QUOTE = '<i>%s</i>';
public const TAG_TEXT = '<span>%s</span>';
public const WRAP_BREAK = PHP_EOL;
public const WRAP_WIDTH = 320; public const WRAP_WIDTH = 320;
public static function code( public static function code(
@ -54,9 +62,4 @@ interface Markup
public static function pre( public static function pre(
string $value string $value
): string; ): string;
public static function tag(
string $const,
bool $close
): string;
} }

13
src/Model/Gtk/Pango/Markup/Gemtext.php

@ -44,8 +44,8 @@ class Gemtext extends \Yggverse\Yoda\Abstract\Model\Gtk\Pango\Markup
else else
{ {
$line[] = $preformatted ? self::tag(self::TAG_CODE, true) $line[] = $preformatted ? self::TAG_CODE_CLOSE
: self::tag(self::TAG_CODE, false); : self::TAG_CODE_OPEN;
$preformatted = !($preformatted); // toggle $preformatted = !($preformatted); // toggle
} }
@ -68,7 +68,8 @@ class Gemtext extends \Yggverse\Yoda\Abstract\Model\Gtk\Pango\Markup
case 1: // # case 1: // #
$line[] = self::h1( $line[] = self::h1(
$entity->getText() $entity->getText(),
$width
); );
// Find title by first # tag // Find title by first # tag
@ -82,7 +83,8 @@ class Gemtext extends \Yggverse\Yoda\Abstract\Model\Gtk\Pango\Markup
case 2: // ## case 2: // ##
$line[] = self::h2( $line[] = self::h2(
$entity->getText() $entity->getText(),
$width
); );
break; break;
@ -90,7 +92,8 @@ class Gemtext extends \Yggverse\Yoda\Abstract\Model\Gtk\Pango\Markup
case 3: // ### case 3: // ###
$line[] = self::h3( $line[] = self::h3(
$entity->getText() $entity->getText(),
$width
); );
break; break;

Loading…
Cancel
Save