mirror of
https://github.com/YGGverse/gemtext-php.git
synced 2026-03-31 17:55:38 +00:00
update namespace
This commit is contained in:
parent
921458f25c
commit
76a34e4ea8
5 changed files with 151 additions and 117 deletions
69
src/Entity/Code.php
Normal file
69
src/Entity/Code.php
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Yggverse\Gemtext\Entity;
|
||||||
|
|
||||||
|
class Code
|
||||||
|
{
|
||||||
|
public const TAG = '```';
|
||||||
|
|
||||||
|
private ?string $_alt;
|
||||||
|
|
||||||
|
private bool $_inline;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
?string $alt = null,
|
||||||
|
bool $inline = false
|
||||||
|
) {
|
||||||
|
$this->setAlt(
|
||||||
|
$alt
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->setInline(
|
||||||
|
$inline
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setAlt(
|
||||||
|
?string $alt
|
||||||
|
): void
|
||||||
|
{
|
||||||
|
if ($alt)
|
||||||
|
{
|
||||||
|
$alt = trim(
|
||||||
|
$alt
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->_alt = $alt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAlt(): ?string
|
||||||
|
{
|
||||||
|
return $this->_alt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setInline(
|
||||||
|
bool $inline
|
||||||
|
): void
|
||||||
|
{
|
||||||
|
$this->_inline = $inline;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isInline(): bool
|
||||||
|
{
|
||||||
|
return $this->_inline;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
if ($this->_inline)
|
||||||
|
{
|
||||||
|
return $this->_alt ? self::TAG . $this->_alt . self::TAG
|
||||||
|
: self::TAG;
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::TAG;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,67 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Yggverse\Gemtext\Entity;
|
|
||||||
|
|
||||||
class Line
|
|
||||||
{
|
|
||||||
private string $_data;
|
|
||||||
|
|
||||||
private bool $_escaped;
|
|
||||||
private ?int $_number;
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
string $data = '',
|
|
||||||
bool $escaped = false,
|
|
||||||
?int $number = null
|
|
||||||
) {
|
|
||||||
$this->setData(
|
|
||||||
$data
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->setEscaped(
|
|
||||||
$escaped
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->setNumber(
|
|
||||||
$number
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getData(): string
|
|
||||||
{
|
|
||||||
return $this->_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setData(
|
|
||||||
string $data
|
|
||||||
): void
|
|
||||||
{
|
|
||||||
$this->_data = $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isEscaped(): bool
|
|
||||||
{
|
|
||||||
return $this->_escaped;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setEscaped(
|
|
||||||
bool $escaped
|
|
||||||
): void
|
|
||||||
{
|
|
||||||
$this->_escaped = $escaped;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getNumber(): ?int
|
|
||||||
{
|
|
||||||
return $this->_number;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setNumber(
|
|
||||||
?int $number
|
|
||||||
): void
|
|
||||||
{
|
|
||||||
$this->_number = $number;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
76
src/Parser/Code.php
Normal file
76
src/Parser/Code.php
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Yggverse\Gemtext\Parser;
|
||||||
|
|
||||||
|
class Code
|
||||||
|
{
|
||||||
|
public static function match(
|
||||||
|
string $line,
|
||||||
|
array &$matches = []
|
||||||
|
): bool
|
||||||
|
{
|
||||||
|
// Multiple format resolver
|
||||||
|
// https://geminiprotocol.net/docs/gemtext.gmi#preformatted-text
|
||||||
|
switch (true)
|
||||||
|
{
|
||||||
|
// Inline ^```preformatted```
|
||||||
|
case preg_match(
|
||||||
|
'/^(?<tag>[`]{3})(?<inline>[^`]+)(?<close>[`]{3})$/m',
|
||||||
|
$line,
|
||||||
|
$matches
|
||||||
|
):
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// Multiline with optional alt support
|
||||||
|
case preg_match(
|
||||||
|
'/^(?<tag>[`]{3})(?<alt>[^`]+)$/m',
|
||||||
|
$line,
|
||||||
|
$matches
|
||||||
|
):
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getAlt(
|
||||||
|
string $line
|
||||||
|
): ?string
|
||||||
|
{
|
||||||
|
$matches = [];
|
||||||
|
|
||||||
|
if (self::match($line, $matches))
|
||||||
|
{
|
||||||
|
if (isset($matches['alt']))
|
||||||
|
{
|
||||||
|
$alt = trim(
|
||||||
|
$matches['alt']
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($alt)
|
||||||
|
{
|
||||||
|
return $alt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isInline(
|
||||||
|
string $line
|
||||||
|
): bool
|
||||||
|
{
|
||||||
|
$matches = [];
|
||||||
|
|
||||||
|
if (self::match($line, $matches))
|
||||||
|
{
|
||||||
|
return isset($matches['inline']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,39 +0,0 @@
|
||||||
<?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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,29 +2,24 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Yggverse\Gemtext\Parser\Line;
|
namespace Yggverse\Gemtext\Parser;
|
||||||
|
|
||||||
class Link
|
class Link
|
||||||
{
|
{
|
||||||
public static function match(
|
public static function match(
|
||||||
\Yggverse\Gemtext\Entity\Line $line,
|
string $line,
|
||||||
array &$matches = []
|
array &$matches = []
|
||||||
): bool
|
): bool
|
||||||
{
|
{
|
||||||
if ($line->isEscaped())
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (bool) preg_match(
|
return (bool) preg_match(
|
||||||
'/^=>\s*(?<address>[^\s]+)(\s(?<date>\d{4}-\d{2}-\d{2}))?(\s(?<alt>.+))?$/m',
|
'/^=>\s*(?<address>[^\s]+)(\s(?<date>\d{4}-\d{2}-\d{2}))?(\s(?<alt>.+))?$/m',
|
||||||
$line->getData(),
|
$line,
|
||||||
$matches
|
$matches
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getAddress(
|
public static function getAddress(
|
||||||
\Yggverse\Gemtext\Entity\Line $line
|
string $line
|
||||||
): ?string
|
): ?string
|
||||||
{
|
{
|
||||||
$matches = [];
|
$matches = [];
|
||||||
|
|
@ -48,7 +43,7 @@ class Link
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getDate(
|
public static function getDate(
|
||||||
\Yggverse\Gemtext\Entity\Line $line
|
string $line
|
||||||
): ?string
|
): ?string
|
||||||
{
|
{
|
||||||
$matches = [];
|
$matches = [];
|
||||||
|
|
@ -72,7 +67,7 @@ class Link
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getAlt(
|
public static function getAlt(
|
||||||
\Yggverse\Gemtext\Entity\Line $line
|
string $line
|
||||||
): ?string
|
): ?string
|
||||||
{
|
{
|
||||||
$matches = [];
|
$matches = [];
|
||||||
Loading…
Add table
Add a link
Reference in a new issue