Implement Gemtext/Link class

This commit is contained in:
yggverse 2024-04-03 04:29:22 +03:00
parent be8b86102f
commit 7916351299
2 changed files with 106 additions and 1 deletions

View File

@ -79,10 +79,52 @@ $body = new \Yggverse\Gemini\Gemtext\Body(
```
var_dump(
$body->getLinks() // returns array of clickable links
$body->getLinks() // returns array of inline links
);
```
### Link
Inline links parser.
Allows to extract address, date with timestamp and alt text from link line given
```
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
```
var_dump(
$link->getDate(
$timestamp // get unix time from this variable
)
);
var_dump(
$timestamp
);
```
#### Link::getAlt
## DokuWiki
Toolkit provides DokuWiki API for Gemini.

63
src/Gemtext/Link.php Normal file
View File

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