getEntities() as $entity)
{
switch (true)
{
case $entity instanceof \Yggverse\Gemtext\Entity\Code:
if ($entity->isInline())
{
$line[] = sprintf(
'%s',
htmlspecialchars(
$entity->getAlt()
)
);
}
else
{
$line[] = $preformatted ? '' : '';
$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(
'%s',
htmlspecialchars(
$this->_wrap(
$entity->getText()
)
)
);
// Find and return document title by first # tag
if (empty($title))
{
$title = $entity->getText();
}
break;
case 2: // ##
$line[] = sprintf(
'%s',
htmlspecialchars(
$this->_wrap(
$entity->getText()
)
)
);
break;
case 3: // ###
$line[] = sprintf(
'%s',
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(
'%s',
$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(
'%s',
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
): bool
{
// 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
return in_array(
parse_url(
$url,
PHP_URL_SCHEME
),
[
'nex',
'gemini',
'file'
]
);
}
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();
}
}