Skip to content

Commit

Permalink
Implement Gemtext/Link class
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Apr 3, 2024
1 parent be8b861 commit 7916351
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
63 changes: 63 additions & 0 deletions src/Gemtext/Link.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 7916351

Please sign in to comment.