mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
refactor page mime routing to init multimedia support
This commit is contained in:
parent
720f9ebbae
commit
81595f0b79
6 changed files with 223 additions and 195 deletions
300
src/Entity/Browser/Container/Page/Content/Gemtext.php
Normal file
300
src/Entity/Browser/Container/Page/Content/Gemtext.php
Normal file
|
|
@ -0,0 +1,300 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yggverse\Yoda\Entity\Browser\Container\Page\Content;
|
||||
|
||||
use \Yggverse\Gemtext\Document;
|
||||
use \Yggverse\Net\Address;
|
||||
|
||||
class Gemtext extends \Yggverse\Yoda\Abstract\Entity\Browser\Container\Page\Content\Markup
|
||||
{
|
||||
public function setSource(
|
||||
string $value,
|
||||
string | null &$title = null,
|
||||
bool $preformatted = false
|
||||
): void
|
||||
{
|
||||
$this->_source = $value;
|
||||
|
||||
$document = new Document(
|
||||
$value
|
||||
);
|
||||
|
||||
$line = [];
|
||||
|
||||
foreach ($document->getEntities() as $entity)
|
||||
{
|
||||
switch (true)
|
||||
{
|
||||
case $entity instanceof \Yggverse\Gemtext\Entity\Code:
|
||||
|
||||
if ($entity->isInline())
|
||||
{
|
||||
$line[] = sprintf(
|
||||
'<tt>%s</tt>',
|
||||
htmlspecialchars(
|
||||
$entity->getAlt()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
$line[] = $preformatted ? '</tt>' : '<tt>';
|
||||
|
||||
$preformatted = !($preformatted); // toggle
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case $entity instanceof \Yggverse\Gemtext\Entity\Header:
|
||||
|
||||
if ($preformatted)
|
||||
{
|
||||
$line[] = htmlspecialchars(
|
||||
$this->_wrap(
|
||||
$entity->toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
switch ($entity->getLevel())
|
||||
{
|
||||
case 1: // #
|
||||
|
||||
$line[] = sprintf(
|
||||
'<span size="xx-large">%s</span>',
|
||||
htmlspecialchars(
|
||||
$this->_wrap(
|
||||
$entity->getText()
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// Find and return document title by first # tag
|
||||
if (empty($title))
|
||||
{
|
||||
$title = $entity->getText();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 2: // ##
|
||||
|
||||
$line[] = sprintf(
|
||||
'<span size="x-large">%s</span>',
|
||||
htmlspecialchars(
|
||||
$this->_wrap(
|
||||
$entity->getText()
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
case 3: // ###
|
||||
|
||||
$line[] = sprintf(
|
||||
'<span size="large">%s</span>',
|
||||
htmlspecialchars(
|
||||
$this->_wrap(
|
||||
$entity->getText()
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
break;
|
||||
default:
|
||||
|
||||
throw new \Exception;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case $entity instanceof \Yggverse\Gemtext\Entity\Link:
|
||||
|
||||
if ($preformatted)
|
||||
{
|
||||
$line[] = htmlspecialchars(
|
||||
$this->_wrap(
|
||||
$entity->toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
$line[] = sprintf(
|
||||
'<a href="%s" title="%s">%s</a>',
|
||||
$this->_url(
|
||||
$entity->getAddress()
|
||||
),
|
||||
htmlspecialchars(
|
||||
$entity->getAddress()
|
||||
),
|
||||
htmlspecialchars(
|
||||
$this->_wrap(
|
||||
$entity->getAlt() ? $entity->getAlt()
|
||||
: $entity->getAddress() // @TODO date
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case $entity instanceof \Yggverse\Gemtext\Entity\Listing:
|
||||
|
||||
if ($preformatted)
|
||||
{
|
||||
$line[] = htmlspecialchars(
|
||||
$this->_wrap(
|
||||
$entity->toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
$line[] = sprintf(
|
||||
'* %s',
|
||||
htmlspecialchars(
|
||||
$this->_wrap(
|
||||
$entity->getItem()
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case $entity instanceof \Yggverse\Gemtext\Entity\Quote:
|
||||
|
||||
if ($preformatted)
|
||||
{
|
||||
$line[] = htmlspecialchars(
|
||||
$this->_wrap(
|
||||
$entity->toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
$line[] = sprintf(
|
||||
'<i>%s</i>',
|
||||
htmlspecialchars(
|
||||
$this->_wrap(
|
||||
$entity->getText()
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case $entity instanceof \Yggverse\Gemtext\Entity\Text:
|
||||
|
||||
if ($preformatted)
|
||||
{
|
||||
$line[] = htmlspecialchars(
|
||||
$this->_wrap(
|
||||
$entity->toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
$line[] = htmlspecialchars(
|
||||
$this->_wrap(
|
||||
$entity->getData()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
throw new \Exception;
|
||||
}
|
||||
}
|
||||
|
||||
$this->gtk->set_markup(
|
||||
implode(
|
||||
PHP_EOL,
|
||||
$line
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
protected function _onActivateLink(
|
||||
\GtkLabel $label,
|
||||
string $href
|
||||
) {
|
||||
// Format URL
|
||||
$url = $this->_url(
|
||||
$href
|
||||
);
|
||||
|
||||
// Update request entry
|
||||
$this->content->page->navbar->request->setValue(
|
||||
$this->_url(
|
||||
$href
|
||||
)
|
||||
);
|
||||
|
||||
// Update page
|
||||
$this->content->page->update();
|
||||
|
||||
// Prevent propagation for supported protocols
|
||||
if (in_array(
|
||||
parse_url(
|
||||
$url,
|
||||
PHP_URL_SCHEME
|
||||
),
|
||||
[
|
||||
'nex',
|
||||
'gemini',
|
||||
'file'
|
||||
])
|
||||
) return true;
|
||||
}
|
||||
|
||||
private function _wrap(
|
||||
string $value
|
||||
): string
|
||||
{
|
||||
return wordwrap(
|
||||
$value,
|
||||
$this->_wrap,
|
||||
PHP_EOL,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
private function _url(
|
||||
string $link
|
||||
): ?string
|
||||
{
|
||||
$address = new Address(
|
||||
$link
|
||||
);
|
||||
|
||||
if ($address->isRelative())
|
||||
{
|
||||
$address->toAbsolute(
|
||||
new Address(
|
||||
$this->content->page->navbar->request->getValue()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $address->get();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue