Browse Source

implement pango markup api

PHP-GTK3
yggverse 4 months ago
parent
commit
f81cf9fdee
  1. 134
      src/Abstract/Model/Gtk/Pango/Markup.php
  2. 126
      src/Entity/Browser/Container/Page/Content/Gemtext.php
  3. 20
      src/Entity/Browser/Container/Page/Content/Plain.php
  4. 57
      src/Interface/Model/Gtk/Pango/Markup.php
  5. 8
      src/Model/Gtk/Pango/Markup.php

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

@ -0,0 +1,134 @@ @@ -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;
}
}

126
src/Entity/Browser/Container/Page/Content/Gemtext.php

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

20
src/Entity/Browser/Container/Page/Content/Plain.php

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

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

@ -0,0 +1,57 @@ @@ -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;
}

8
src/Model/Gtk/Pango/Markup.php

@ -0,0 +1,8 @@ @@ -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
{}
Loading…
Cancel
Save