mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-03-31 16:45:27 +00:00
update markup api, implement header wrap support
This commit is contained in:
parent
66b94ee7d3
commit
fade94156b
3 changed files with 84 additions and 101 deletions
|
|
@ -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 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(
|
return htmlspecialchars(
|
||||||
$value
|
$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>' : '<%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
|
||||||
|
)
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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;
|
|
||||||
}
|
}
|
||||||
|
|
@ -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…
Add table
Add a link
Reference in a new issue