mirror of
https://github.com/YGGverse/gemtext-php.git
synced 2026-03-31 17:55:38 +00:00
implement Listing support
This commit is contained in:
parent
ed5b33c708
commit
7f830f41bc
3 changed files with 99 additions and 0 deletions
|
|
@ -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
44
src/Entity/Listing.php
Normal 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
44
src/Parser/Listing.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue