Browse Source

add gemtext features

main
yggverse 8 months ago
parent
commit
00dddea554
  1. 25
      README.md
  2. 86
      src/Gemtext/Body.php

25
README.md

@ -58,6 +58,31 @@ var_dump(
); );
``` ```
## Gemtext
Object-oriented API for Gemtext
### Body
Basic methods to work with `text/gemini` documents
```
$body = new \Yggverse\Gemini\Gemtext\Body(
$response->getBody() // gemtext body from client response or .gmi file
);
```
#### Body::getH1
#### Body::getH2
#### Body::getH3
#### Body::getLinks
```
var_dump(
$body->getLinks() // returns array of clickable links
);
```
## DokuWiki ## DokuWiki
Toolkit provides DokuWiki API for Gemini. Toolkit provides DokuWiki API for Gemini.

86
src/Gemtext/Body.php

@ -0,0 +1,86 @@
<?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 $line)
{
$this->_lines[] = $line;
}
}
public function getH1(): array
{
$matches = [];
foreach ($this->_lines as $line)
{
if (preg_match('/^#([^#]+)/', trim($line), $match))
{
$matches[] = trim(
$match[1]
);
}
}
return $matches;
}
public function getH2(): array
{
$matches = [];
foreach ($this->_lines as $line)
{
if (preg_match('/^##([^#]+)/', trim($line), $match))
{
$matches[] = trim(
$match[1]
);
}
}
return $matches;
}
public function getH3(): array
{
$matches = [];
foreach ($this->_lines as $line)
{
if (preg_match('/^###([^#]+)/', trim($line), $match))
{
$matches[] = trim(
$match[1]
);
}
}
return $matches;
}
public function getLinks(): array
{
$matches = [];
foreach ($this->_lines as $i => $line)
{
if (preg_match('/^=>(.*)/', trim($line), $match))
{
$matches[] = trim(
$match[1]
);
}
}
return $matches;
}
}
Loading…
Cancel
Save