mirror of
https://github.com/YGGverse/gemini-php.git
synced 2025-01-14 00:58:03 +00:00
add gemtext features
This commit is contained in:
parent
edf0234056
commit
00dddea554
25
README.md
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
|
||||
|
||||
Toolkit provides DokuWiki API for Gemini.
|
||||
|
86
src/Gemtext/Body.php
Normal file
86
src/Gemtext/Body.php
Normal file
@ -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…
Reference in New Issue
Block a user