implement pango markup api

This commit is contained in:
yggverse 2024-07-29 16:09:27 +03:00
parent 58bc99de06
commit f81cf9fdee
5 changed files with 237 additions and 108 deletions

View file

@ -0,0 +1,134 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Abstract\Model\Gtk\Pango;
class Markup implements \Yggverse\Yoda\Interface\Model\Gtk\Pango\Markup
{
public static function code(
string $value
): string
{
return sprintf(
'<tt>%s</tt>',
htmlspecialchars(
$value
)
);
}
public static function h1(
string $value
): string
{
return sprintf(
'<span size="xx-large">%s</span>',
htmlspecialchars(
$value
)
);
}
public static function h2(
string $value
): string
{
return sprintf(
'<span size="x-large">%s</span>',
htmlspecialchars(
$value
)
);
}
public static function h3(
string $value
): string
{
return sprintf(
'<span size="large">%s</span>',
htmlspecialchars(
$value
)
);
}
public static function link(
string $href,
string $title,
string $value
): string
{
return sprintf(
'<a href="%s" title="%s"><span underline="none">%s</span></a>',
htmlspecialchars(
$href
),
htmlspecialchars(
$title
),
htmlspecialchars(
$value
)
);
}
public static function list(
string $value
): string
{
return sprintf(
'* %s', // @TODO
htmlspecialchars(
$value
)
);
}
public static function quote(
string $value
): string
{
return sprintf(
'<i>%s</i>',
htmlspecialchars(
$value
)
);
}
public static function text(
string $value
): string
{
return htmlspecialchars(
$value
);
}
public static function pre(
string $value
): string
{
return htmlspecialchars(
$value
);
}
public static function tag(
string $const,
bool $close
): string
{
if (in_array($const, [self::TAG_CODE]))
{
return sprintf(
$close ? '</%s>' : '<%s>',
$const
);
}
throw new Exception;
}
}

View file

@ -10,7 +10,7 @@ use \GdkEvent;
use \GtkLabel; use \GtkLabel;
use \Pango; use \Pango;
use \Yggverse\Yoda\Abstract\Entity\Browser\Container\Page\Content\Markup; use \Yggverse\Yoda\Model\Gtk\Pango\Markup;
use \Yggverse\Gemtext\Document; use \Yggverse\Gemtext\Document;
use \Yggverse\Gemtext\Entity\Code; use \Yggverse\Gemtext\Entity\Code;
@ -20,10 +20,9 @@ use \Yggverse\Gemtext\Entity\Listing;
use \Yggverse\Gemtext\Entity\Quote; use \Yggverse\Gemtext\Entity\Quote;
use \Yggverse\Gemtext\Entity\Text; use \Yggverse\Gemtext\Entity\Text;
use \Yggverse\Gemtext\Parser\Link as LinkParser; # use \Yggverse\Gemtext\Parser\Link as LinkParser;
class Gemtext extends \Yggverse\Yoda\Abstract\Entity\Browser\Container\Page\Content\Markup
class Gemtext extends Markup
{ {
public function set( public function set(
string $source, string $source,
@ -45,17 +44,15 @@ class Gemtext extends Markup
if ($entity->isInline()) if ($entity->isInline())
{ {
$line[] = sprintf( $line[] = Markup::code(
'<tt>%s</tt>', $entity->getAlt()
htmlspecialchars(
$entity->getAlt()
)
); );
} }
else else
{ {
$line[] = $preformatted ? '</tt>' : '<tt>'; $line[] = $preformatted ? Markup::tag(Markup::TAG_CODE, true)
: Markup::tag(Markup::TAG_CODE, false);
$preformatted = !($preformatted); // toggle $preformatted = !($preformatted); // toggle
} }
@ -66,10 +63,8 @@ class Gemtext extends Markup
if ($preformatted) if ($preformatted)
{ {
$line[] = htmlspecialchars( $line[] = Markup::pre(
$this->_wrap( $entity->toString()
$entity->toString()
)
); );
} }
@ -79,13 +74,8 @@ class Gemtext extends Markup
{ {
case 1: // # case 1: // #
$line[] = sprintf( $line[] = Markup::h1(
'<span size="xx-large">%s</span>', $entity->getText()
htmlspecialchars(
$this->_wrap(
$entity->getText()
)
)
); );
// Find and return document title by first # tag // Find and return document title by first # tag
@ -98,26 +88,16 @@ class Gemtext extends Markup
case 2: // ## case 2: // ##
$line[] = sprintf( $line[] = Markup::h2(
'<span size="x-large">%s</span>', $entity->getText()
htmlspecialchars(
$this->_wrap(
$entity->getText()
)
)
); );
break; break;
case 3: // ### case 3: // ###
$line[] = sprintf( $line[] = Markup::h3(
'<span size="large">%s</span>', $entity->getText()
htmlspecialchars(
$this->_wrap(
$entity->getText()
)
)
); );
break; break;
@ -133,31 +113,20 @@ class Gemtext extends Markup
if ($preformatted) if ($preformatted)
{ {
$line[] = htmlspecialchars( $line[] = Markup::pre(
$this->_wrap( $entity->toString()
$entity->toString()
)
); );
} }
else else
{ {
$line[] = sprintf( $line[] = Markup::h3(
'<a href="%s" title="%s"><span underline="none">%s</span></a>', $this->_url(
htmlspecialchars(
$this->_url(
$entity->getAddress()
)
),
htmlspecialchars(
$entity->getAddress() $entity->getAddress()
), ),
htmlspecialchars( $entity->getAddress(),
$this->_wrap( $entity->getAlt() ? $entity->getAlt()
$entity->getAlt() ? $entity->getAlt() : $entity->getAddress() // @TODO date
: $entity->getAddress() // @TODO date
)
)
); );
} }
@ -167,22 +136,15 @@ class Gemtext extends Markup
if ($preformatted) if ($preformatted)
{ {
$line[] = htmlspecialchars( $line[] = Markup::pre(
$this->_wrap( $entity->toString()
$entity->toString()
)
); );
} }
else else
{ {
$line[] = sprintf( $line[] = Markup::list(
'* %s', $entity->getItem()
htmlspecialchars(
$this->_wrap(
$entity->getItem()
)
)
); );
} }
@ -192,22 +154,15 @@ class Gemtext extends Markup
if ($preformatted) if ($preformatted)
{ {
$line[] = htmlspecialchars( $line[] = Markup::pre(
$this->_wrap( $entity->toString()
$entity->toString()
)
); );
} }
else else
{ {
$line[] = sprintf( $line[] = Markup::quote(
'<i>%s</i>', $entity->getText()
htmlspecialchars(
$this->_wrap(
$entity->getText()
)
)
); );
} }
@ -217,19 +172,15 @@ class Gemtext extends Markup
if ($preformatted) if ($preformatted)
{ {
$line[] = htmlspecialchars( $line[] = Markup::pre(
$this->_wrap( $entity->toString()
$entity->toString()
)
); );
} }
else else
{ {
$line[] = htmlspecialchars( $line[] = Markup::text(
$this->_wrap( $entity->getData()
$entity->getData()
)
); );
} }
@ -327,13 +278,4 @@ class Gemtext extends Markup
return false; return false;
} }
protected function _onSizeAllocate(
GtkLabel $label,
GdkEvent $event
): bool
{
// @TODO
return false;
}
} }

View file

@ -7,30 +7,18 @@ namespace Yggverse\Yoda\Entity\Browser\Container\Page\Content;
use \GdkEvent; use \GdkEvent;
use \GtkLabel; use \GtkLabel;
use \Yggverse\Yoda\Abstract\Entity\Browser\Container\Page\Content\Markup; use \Yggverse\Yoda\Model\Gtk\Pango\Markup;
class Plain extends Markup class Plain extends \Yggverse\Yoda\Abstract\Entity\Browser\Container\Page\Content\Markup
{ {
public function set( public function set(
string $source string $source
): void ): void
{ {
$this->gtk->set_markup( $this->gtk->set_markup(
sprintf( Markup::code( // @TODO
'<tt>%s</tt>', $this->_source = $source
htmlspecialchars(
$this->_source = $source
)
) )
); );
} }
protected function _onSizeAllocate(
GtkLabel $label,
GdkEvent $event
): bool
{
// @TODO
return false;
}
} }

View file

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Interface\Model\Gtk\Pango;
/*
* Pango markup API
*
*/
interface Markup
{
public const TAG_CODE = 'tt';
public static function code(
string $value
): string;
public static function h1(
string $value
): string;
public static function h2(
string $value
): string;
public static function h3(
string $value
): string;
public static function link(
string $href,
string $title,
string $value
): string;
public static function list(
string $value
): string;
public static function quote(
string $value
): string;
public static function text(
string $value
): string;
public static function pre(
string $value
): string;
public static function tag(
string $const,
bool $close
): string;
}

View file

@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Model\Gtk\Pango;
class Markup extends \Yggverse\Yoda\Abstract\Model\Gtk\Pango\Markup
{}