diff --git a/README.md b/README.md index 7979afb..201dae7 100644 --- a/README.md +++ b/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. diff --git a/src/Gemtext/Body.php b/src/Gemtext/Body.php new file mode 100644 index 0000000..2e85bd0 --- /dev/null +++ b/src/Gemtext/Body.php @@ -0,0 +1,86 @@ +_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; + } +} \ No newline at end of file