mirror of
https://github.com/YGGverse/gemtext-php.git
synced 2026-03-31 09:45:33 +00:00
implement Header entity support
This commit is contained in:
parent
a68ec839e4
commit
889cca12b5
3 changed files with 150 additions and 0 deletions
|
|
@ -32,6 +32,20 @@ class Document
|
|||
|
||||
break;
|
||||
|
||||
// Header
|
||||
case Parser\Header::match($line):
|
||||
|
||||
$this->_entities[] = new Entity\Header(
|
||||
Parser\Header::getLevel(
|
||||
$line
|
||||
),
|
||||
Parser\Header::getText(
|
||||
$line
|
||||
)
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
// Link
|
||||
case Parser\Link::match($line):
|
||||
|
||||
|
|
|
|||
73
src/Entity/Header.php
Normal file
73
src/Entity/Header.php
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yggverse\Gemtext\Entity;
|
||||
|
||||
class Header
|
||||
{
|
||||
public const TAG = '#';
|
||||
|
||||
private int $_level;
|
||||
|
||||
private ?string $_text;
|
||||
|
||||
public function __construct(
|
||||
int $level,
|
||||
?string $text = null
|
||||
) {
|
||||
$this->setLevel(
|
||||
$level
|
||||
);
|
||||
|
||||
$this->setText(
|
||||
$text
|
||||
);
|
||||
}
|
||||
|
||||
public function setLevel(
|
||||
int $level
|
||||
): void
|
||||
{
|
||||
if (!in_array($level, [1, 2, 3]))
|
||||
{
|
||||
throw new \Exception(
|
||||
_('Incorrect header level')
|
||||
);
|
||||
}
|
||||
|
||||
$this->_level = $level;
|
||||
}
|
||||
|
||||
public function getLevel(): int
|
||||
{
|
||||
return $this->_level;
|
||||
}
|
||||
|
||||
public function setText(
|
||||
?string $text
|
||||
): void
|
||||
{
|
||||
if ($text)
|
||||
{
|
||||
$text = trim(
|
||||
$text
|
||||
);
|
||||
}
|
||||
|
||||
$this->_text = $text;
|
||||
}
|
||||
|
||||
public function getText(): ?string
|
||||
{
|
||||
return $this->_text;
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return str_repeat(
|
||||
self::TAG,
|
||||
$this->_level
|
||||
) . $this->_text;
|
||||
}
|
||||
}
|
||||
63
src/Parser/Header.php
Normal file
63
src/Parser/Header.php
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yggverse\Gemtext\Parser;
|
||||
|
||||
class Header
|
||||
{
|
||||
public static function match(
|
||||
string $line,
|
||||
array &$matches = []
|
||||
): bool
|
||||
{
|
||||
return (bool) preg_match(
|
||||
'/^(?<level>#{1,3})(?<text>.*)$/m',
|
||||
$line,
|
||||
$matches
|
||||
);
|
||||
}
|
||||
|
||||
public static function getLevel(
|
||||
string $line
|
||||
): ?int
|
||||
{
|
||||
$matches = [];
|
||||
|
||||
if (self::match($line, $matches))
|
||||
{
|
||||
if (isset($matches['level']))
|
||||
{
|
||||
return (int) strlen(
|
||||
$matches['level']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
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