implement Listing support

This commit is contained in:
yggverse 2024-06-24 03:28:54 +03:00
parent ed5b33c708
commit 7f830f41bc
3 changed files with 99 additions and 0 deletions

View file

@ -49,6 +49,17 @@ class Document
break;
// Listing
case Parser\Listing::match($line):
$this->_entities[] = new Entity\Listing(
Parser\Listing::getItem(
$line
)
);
break;
// Plain
default:

44
src/Entity/Listing.php Normal file
View file

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Yggverse\Gemtext\Entity;
class Listing
{
public const TAG = '*';
private ?string $_item;
public function __construct(
?string $item = null
) {
$this->setItem(
$item
);
}
public function setItem(
?string $item
): void
{
if ($item)
{
$item = trim(
$item
);
}
$this->_item = $item;
}
public function Item(): ?string
{
return $this->_item;
}
public function toString(): string
{
return self::TAG . ' ' . $this->_item;
}
}

44
src/Parser/Listing.php Normal file
View file

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Yggverse\Gemtext\Parser;
class Listing
{
public static function match(
string $line,
array &$matches = []
): bool
{
return (bool) preg_match(
'/^\*\s*(?<item>.*)$/m',
$line,
$matches
);
}
public static function getItem(
string $line
): ?string
{
$matches = [];
if (self::match($line, $matches))
{
if (isset($matches['item']))
{
$item = trim(
$matches['item']
);
if ($item)
{
return $item;
}
}
}
return null;
}
}