From 325ac1eb8ce1c6fbe85fc18382c76c756d7bd162 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 26 Jun 2024 20:01:35 +0300 Subject: [PATCH] drop gemtext features (use gemtext-php library) --- README.md | 102 ------------------- src/Gemtext/Body.php | 235 ------------------------------------------- src/Gemtext/Link.php | 69 ------------- 3 files changed, 406 deletions(-) delete mode 100644 src/Gemtext/Body.php delete mode 100644 src/Gemtext/Link.php diff --git a/README.md b/README.md index 6bfa299..d80d855 100644 --- a/README.md +++ b/README.md @@ -109,108 +109,6 @@ var_dump( ); ``` -## Gemtext - -Object-oriented API for Gemtext - -**Deprecated and will be removed in future releases! Use [gemtext-php](https://github.com/YGGverse/gemtext-php) instead.** - -### Body - -Basic methods to work with `text/gemini` documents - -``` php -$body = new \Yggverse\Gemini\Gemtext\Body( - $response->getBody() // gemtext body from client response or .gmi file content -); -``` - -#### Body::getLines -#### Body::getLine -#### Body::getH1 -#### Body::getH2 -#### Body::getH3 -#### Body::getQuote -#### Body::getCode -#### Body::getLinks - -``` php -var_dump( - $body->getLinks() // returns array of links (with line number in key) -); -``` - -#### Body::findLinks - -Find context links by protocol as argument, `gemini` by default - -``` php -var_dump( - $body->findLinks('http') // returns array of http links only (with line number in key) -); -``` - -#### Body::skipTags - -Strip gemini tags from Gemini document - -``` php -var_dump( - $body->skipTags() // strip all tags -); - -var_dump( - $body->skipTags( - [ // 1- and 2- level headers only - "##", - "###" - ] - ) -); -``` - -### Link - -Inline links parser. - -Allows to extract address, date with timestamp and alt text from link line given - -``` php -foreach ($body->getLinks() as $line) -{ - $link = new \Yggverse\Gemini\Gemtext\Link( - $line - ); - - var_dump( - $link->getAddress() - ); - - var_dump( - $link->getAlt() - ); -} -``` - -#### Link::getAddress -#### Link::getDate - -This method also validates time format and returns the unix timestamp as linked argument - -``` php -var_dump( - $link->getDate( - $timestamp // get unix time from this variable - ) -); - -var_dump( - $timestamp -); -``` - -#### Link::getAlt - ## GTK3 ### Pango diff --git a/src/Gemtext/Body.php b/src/Gemtext/Body.php deleted file mode 100644 index 3cfa3fb..0000000 --- a/src/Gemtext/Body.php +++ /dev/null @@ -1,235 +0,0 @@ - $line) - { - $this->_lines[$index] = $line; - } - } - - public function getLine(int $index): ?int - { - return isset($this->_lines[$index]) ? $this->_lines[$index] : null; - } - - public function getLines(): array - { - return $this->_lines; - } - - public function getH1(): array - { - $matches = []; - - foreach ($this->_lines as $index => $line) - { - if (preg_match('/^#([^#]+)/', trim($line), $match)) - { - $matches[$index] = trim( - $match[1] - ); - } - } - - return $matches; - } - - public function getH2(): array - { - $matches = []; - - foreach ($this->_lines as $index => $line) - { - if (preg_match('/^##([^#]+)/', trim($line), $match)) - { - $matches[$index] = trim( - $match[1] - ); - } - } - - return $matches; - } - - public function getH3(): array - { - $matches = []; - - foreach ($this->_lines as $index => $line) - { - if (preg_match('/^###([^#]+)/', trim($line), $match)) - { - $matches[$index] = trim( - $match[1] - ); - } - } - - return $matches; - } - - public function getLinks(): array - { - $matches = []; - - foreach ($this->_lines as $index => $line) - { - if (preg_match('/^=>(.*)/', trim($line), $match)) - { - $matches[$index] = trim( - $match[1] - ); - } - } - - return $matches; - } - - public function getQuote(): array - { - $matches = []; - - foreach ($this->_lines as $index => $line) - { - if (preg_match('/^>(.*)/', trim($line), $match)) - { - $matches[$index] = trim( - $match[1] - ); - } - } - - return $matches; - } - - public function getCode(): array - { - $matches = []; - - foreach ($this->_lines as $index => $line) - { - if (preg_match('/^```(.*)/', trim($line), $match)) - { - $matches[$index] = empty($match[1]) ? null : trim($match[1]); - } - } - - return $matches; - } - - public function findLinks(string $protocol = 'gemini'): array - { - $matches = []; - - foreach ($this->_lines as $index => $line) - { - if (preg_match('/' . $protocol . ':\/\/(.*)[\s\S\'"]*/', trim($line), $match)) - { - $matches[$index] = - sprintf( - '%s://%s', - $protocol, - trim( - $match[1] - ) - ); - } - } - - return $matches; - } - - public function skipTags(array $tags = []): string - { - $lines = []; - - foreach ($this->_lines as $line) - { - $line = trim( - $line - ); - - if ($tags) - { - foreach ($tags as $tag) - { - if(!in_array($tag, ['#', '##', '###', '=>', '*', '```'])) - { - continue; - } - - switch (true) - { - case str_starts_with($line, '#'): - - $line = preg_replace( - sprintf( - '/^%s([^#]+)/ui', - $tag - ), - '$1', - $line - ); - - break; - - case str_starts_with($line, '*'): - - $line = preg_replace( - '/^\*(.*)/ui', - '$1', - $line - ); - - break; - - default: - - $line = preg_replace( - sprintf( - '/^%s(.*)/ui', - $tag - ), - '$1', - $line - ); - } - } - } - - else - { - $line = preg_replace( - [ - '/^#([^#]+)/ui', - '/^##([^#]+)/ui', - '/^###([^#]+)/ui', - '/^=>(.*)/ui', - '/^\*(.*)/ui', - '/^```(.*)/ui', - ], - '$1', - $line - ); - } - - $lines[] = trim( - $line - ); - } - - return implode( - PHP_EOL, - $lines - ); - } -} \ No newline at end of file diff --git a/src/Gemtext/Link.php b/src/Gemtext/Link.php deleted file mode 100644 index 6557391..0000000 --- a/src/Gemtext/Link.php +++ /dev/null @@ -1,69 +0,0 @@ -_line = preg_replace( - '/^\s*=>(.*)/', - '$1', - trim( - $line - ) - ); - } - - public function getAddress(): ?string - { - if (preg_match('/^\s*([^\s]+)/', trim($this->_line), $match)) - { - return trim( - $match[1] - ); - } - - return null; - } - - public function getDate(?int &$timestamp = null): ?string - { - if (preg_match('/\s([\d]+-[\d+]+-[\d]+)\s/', trim($this->_line), $match)) - { - if ($result = strtotime($match[1])) - { - $timestamp = $result; - - return trim( - $match[1] - ); - } - } - - return null; - } - - public function getAlt(): ?string - { - if (preg_match('/\s[\d]+-[\d+]+-[\d]+\s(.*)$/', trim($this->_line), $match)) - { - return trim( - $match[1] - ); - } - - else if (preg_match('/\s(.*)$/', trim($this->_line), $match)) - { - return trim( - $match[1] - ); - } - - return null; - } -} \ No newline at end of file