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 @@ -13,46 +13,46 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup
): string
{
return sprintf(
'<tt>%s</tt>',
htmlspecialchars(
self::TAG_CODE,
self::_escape(
$value
)
);
}
public static function h1(
string $value
string $value,
int $width = self::WRAP_WIDTH
): string
{
return sprintf(
'<span size="xx-large">%s</span>',
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(
'<span size="x-large">%s</span>',
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(
'<span size="large">%s</span>',
htmlspecialchars(
$value
)
return self::_wrap(
self::TAG_H3,
$value,
$width
);
}
@ -64,13 +64,13 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup @@ -64,13 +64,13 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup
{
return sprintf(
'<a href="%s" title="%s"><span underline="none">%s</span></a>',
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 @@ -81,18 +81,13 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup
int $width = self::WRAP_WIDTH
): string
{
return sprintf(
'<span>%s</span>', // @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 @@ -101,15 +96,10 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup
int $width = self::WRAP_WIDTH
): string
{
return sprintf(
'<i>%s</i>',
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 @@ -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 @@ -131,37 +119,28 @@ class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup
string $value
): string
{
return htmlspecialchars(
return self::_escape(
$value
);
}
public static function tag(
string $const,
bool $close
protected static function _escape(
string $value
): string
{
if (in_array($const, [
self::TAG_CODE,
self::TAG_LIST,
self::TAG_QUOTE,
self::TAG_TEXT
])) {
return sprintf(
$close ? '</%s>' : '<%s>',
$const
);
}
throw new Exception;
// @TODO PR #135
// https://docs.gtk.org/glib/func.markup_escape_text.html
return htmlspecialchars(
$value
);
}
// @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 @@ -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 @@ -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
)
)
);
}
}

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

@ -10,11 +10,19 @@ namespace Yggverse\Yoda\Interface\Model\Gtk\Pango; @@ -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 = '<tt>%s</tt>';
public const TAG_CODE_CLOSE = '</tt>';
public const TAG_CODE_OPEN = '<tt>';
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 static function code(
@ -54,9 +62,4 @@ interface Markup @@ -54,9 +62,4 @@ interface Markup
public static function pre(
string $value
): 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 @@ -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 @@ -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 @@ -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 @@ -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;

Loading…
Cancel
Save