Browse Source

Implement Gemtext/Link class

main 0.3.0
yggverse 7 months ago
parent
commit
7916351299
  1. 44
      README.md
  2. 63
      src/Gemtext/Link.php

44
README.md

@ -79,10 +79,52 @@ $body = new \Yggverse\Gemini\Gemtext\Body( @@ -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

@ -0,0 +1,63 @@ @@ -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;
}
}
Loading…
Cancel
Save