mirror of
https://github.com/YGGverse/gemtext-php.git
synced 2026-03-31 17:55:38 +00:00
implement Quote support
This commit is contained in:
parent
11925c5044
commit
fd71028615
3 changed files with 99 additions and 0 deletions
|
|
@ -60,6 +60,17 @@ class Document
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// Quote
|
||||||
|
case Parser\Quote::match($line):
|
||||||
|
|
||||||
|
$this->_entities[] = new Entity\Quote(
|
||||||
|
Parser\Quote::getText(
|
||||||
|
$line
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
// Plain
|
// Plain
|
||||||
default:
|
default:
|
||||||
|
|
||||||
|
|
|
||||||
44
src/Entity/Quote.php
Normal file
44
src/Entity/Quote.php
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Yggverse\Gemtext\Entity;
|
||||||
|
|
||||||
|
class Quote
|
||||||
|
{
|
||||||
|
public const TAG = '>';
|
||||||
|
|
||||||
|
private ?string $_text;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
?string $text = null
|
||||||
|
) {
|
||||||
|
$this->setText(
|
||||||
|
$text
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setText(
|
||||||
|
?string $text
|
||||||
|
): void
|
||||||
|
{
|
||||||
|
if ($text)
|
||||||
|
{
|
||||||
|
$text = trim(
|
||||||
|
$text
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->_text = $text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function Text(): ?string
|
||||||
|
{
|
||||||
|
return $this->_text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return self::TAG . ' ' . $this->_text;
|
||||||
|
}
|
||||||
|
}
|
||||||
44
src/Parser/Quote.php
Normal file
44
src/Parser/Quote.php
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Yggverse\Gemtext\Parser;
|
||||||
|
|
||||||
|
class Quote
|
||||||
|
{
|
||||||
|
public static function match(
|
||||||
|
string $line,
|
||||||
|
array &$matches = []
|
||||||
|
): bool
|
||||||
|
{
|
||||||
|
return (bool) preg_match(
|
||||||
|
'/^>\s*(?<text>.*)$/m',
|
||||||
|
$line,
|
||||||
|
$matches
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getText(
|
||||||
|
string $line
|
||||||
|
): ?string
|
||||||
|
{
|
||||||
|
$matches = [];
|
||||||
|
|
||||||
|
if (self::match($line, $matches))
|
||||||
|
{
|
||||||
|
if (isset($matches['text']))
|
||||||
|
{
|
||||||
|
$text = trim(
|
||||||
|
$matches['text']
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($text)
|
||||||
|
{
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue