Browse Source

drop gemtext features (use gemtext-php library)

main
yggverse 5 months ago
parent
commit
325ac1eb8c
  1. 102
      README.md
  2. 235
      src/Gemtext/Body.php
  3. 69
      src/Gemtext/Link.php

102
README.md

@ -109,108 +109,6 @@ var_dump( @@ -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

235
src/Gemtext/Body.php

@ -1,235 +0,0 @@ @@ -1,235 +0,0 @@
<?php
declare(strict_types=1);
namespace Yggverse\Gemini\Gemtext;
class Body
{
private array $_lines = [];
public function __construct(string $gemtext)
{
foreach ((array) explode(PHP_EOL, $gemtext) as $index => $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
);
}
}

69
src/Gemtext/Link.php

@ -1,69 +0,0 @@ @@ -1,69 +0,0 @@
<?php
declare(strict_types=1);
namespace Yggverse\Gemini\Gemtext;
class Link
{
private string $_line;
public function __construct(string $line)
{
$this->_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;
}
}
Loading…
Cancel
Save