implement code parser

This commit is contained in:
yggverse 2024-06-23 22:01:21 +03:00
parent dc581d6071
commit 921458f25c
2 changed files with 39 additions and 23 deletions

View file

@ -1,23 +0,0 @@
<?php
declare(strict_types=1);
namespace Yggverse\Gemtext\Entity;
class Code
{
/*
* Helper method
*
* Detect line given is escaped by previous iteration
*/
public static function escaped(string $line, bool &$status): bool
{
if (preg_match('/^```/m', $line))
{
$status = !($status); // toggle
}
return $status;
}
}

39
src/Parser/Line/Code.php Normal file
View file

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Yggverse\Gemtext\Parser\Line;
class Code
{
public static function match(
\Yggverse\Gemtext\Entity\Line $line,
array &$matches = [],
bool &$inline = false
): bool
{
// Multiple format resolver
// https://geminiprotocol.net/docs/gemtext.gmi#preformatted-text
switch (true)
{
// Inline ^```preformatted```
case preg_match(
'/^[`]{3}\s*((?<code>[^`]+))?[`]{3}$/m',
$line->getData(),
$matches
):
// Toggle escaped status
return $inline = true;
// Multiline with optional alt support
case preg_match(
'/^[`]{3}\s*(?<alt>[^`]+)$/m',
$line->getData(),
$matches
):
return true;
}
return false;
}
}