update Parser API, implement Document:getEntities(), Document:append() methods

This commit is contained in:
yggverse 2024-06-24 21:56:16 +03:00
parent f00f23aaf2
commit d5cd8c184d

52
src/Document.php Normal file → Executable file
View file

@ -15,71 +15,83 @@ class Document
) {
foreach ((array) explode(PHP_EOL, $data) as $line)
{
// Init match
$matches = [];
// Add entity
switch (true)
{
// Code
case Parser\Code::match($line):
case Parser\Code::match($line, $matches):
$this->_entity[] = new Entity\Code(
Parser\Code::getAlt(
$line
$line,
$matches
),
Parser\Code::isInline(
$line
$line,
$matches
)
);
break;
// Header
case Parser\Header::match($line):
case Parser\Header::match($line, $matches):
$this->_entity[] = new Entity\Header(
Parser\Header::getText(
$line
$line,
$matches
),
Parser\Header::getLevel(
$line
$line,
$matches
)
);
break;
// Link
case Parser\Link::match($line):
case Parser\Link::match($line, $matches):
$this->_entity[] = new Entity\Link(
Parser\Link::getAddress(
$line
$line,
$matches
),
Parser\Link::getAlt(
$line
$line,
$matches
),
Parser\Link::getDate(
$line
$line,
$matches
)
);
break;
// Listing
case Parser\Listing::match($line):
case Parser\Listing::match($line, $matches):
$this->_entity[] = new Entity\Listing(
Parser\Listing::getItem(
$line
$line,
$matches
)
);
break;
// Quote
case Parser\Quote::match($line):
case Parser\Quote::match($line, $matches):
$this->_entity[] = new Entity\Quote(
Parser\Quote::getText(
$line
$line,
$matches
)
);
@ -95,6 +107,11 @@ class Document
}
}
public function getEntities(): array
{
return $this->_entity;
}
public function getHeaders(): array
{
$headers = [];
@ -155,6 +172,13 @@ class Document
return $quotes;
}
public function append(
\Yggverse\Gemtext\Interface\Entity $entity
): void
{
$this->_entity[] = $entity;
}
public function toString(): string
{
$lines = [];