diff --git a/src/Entity/Code.php b/src/Entity/Code.php new file mode 100644 index 0000000..80a25e3 --- /dev/null +++ b/src/Entity/Code.php @@ -0,0 +1,69 @@ +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; + } +} \ No newline at end of file diff --git a/src/Entity/Line.php b/src/Entity/Line.php deleted file mode 100644 index 03f5848..0000000 --- a/src/Entity/Line.php +++ /dev/null @@ -1,67 +0,0 @@ -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; - } -} diff --git a/src/Parser/Code.php b/src/Parser/Code.php new file mode 100644 index 0000000..39bde6b --- /dev/null +++ b/src/Parser/Code.php @@ -0,0 +1,76 @@ +[`]{3})(?[^`]+)(?[`]{3})$/m', + $line, + $matches + ): + + return true; + + // Multiline with optional alt support + case preg_match( + '/^(?[`]{3})(?[^`]+)$/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; + } +} \ No newline at end of file diff --git a/src/Parser/Line/Code.php b/src/Parser/Line/Code.php deleted file mode 100644 index 82baa21..0000000 --- a/src/Parser/Line/Code.php +++ /dev/null @@ -1,39 +0,0 @@ -[^`]+))?[`]{3}$/m', - $line->getData(), - $matches - ): - // Toggle escaped status - return $inline = true; - - // Multiline with optional alt support - case preg_match( - '/^[`]{3}\s*(?[^`]+)$/m', - $line->getData(), - $matches - ): - return true; - } - - return false; - } -} \ No newline at end of file diff --git a/src/Parser/Line/Link.php b/src/Parser/Link.php similarity index 82% rename from src/Parser/Line/Link.php rename to src/Parser/Link.php index 8d1ae7c..b0b20c4 100644 --- a/src/Parser/Line/Link.php +++ b/src/Parser/Link.php @@ -2,29 +2,24 @@ declare(strict_types=1); -namespace Yggverse\Gemtext\Parser\Line; +namespace Yggverse\Gemtext\Parser; class Link { public static function match( - \Yggverse\Gemtext\Entity\Line $line, + string $line, array &$matches = [] ): bool { - if ($line->isEscaped()) - { - return false; - } - return (bool) preg_match( '/^=>\s*(?
[^\s]+)(\s(?\d{4}-\d{2}-\d{2}))?(\s(?.+))?$/m', - $line->getData(), + $line, $matches ); } public static function getAddress( - \Yggverse\Gemtext\Entity\Line $line + string $line ): ?string { $matches = []; @@ -48,7 +43,7 @@ class Link } public static function getDate( - \Yggverse\Gemtext\Entity\Line $line + string $line ): ?string { $matches = []; @@ -72,7 +67,7 @@ class Link } public static function getAlt( - \Yggverse\Gemtext\Entity\Line $line + string $line ): ?string { $matches = [];