mirror of
https://github.com/YGGverse/gemtext-php.git
synced 2026-03-31 17:55:38 +00:00
implement code parser
This commit is contained in:
parent
dc581d6071
commit
921458f25c
2 changed files with 39 additions and 23 deletions
|
|
@ -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
39
src/Parser/Line/Code.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue