diff --git a/README.md b/README.md index 201dae7..4b41247 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Gemtext/Link.php b/src/Gemtext/Link.php new file mode 100644 index 0000000..f7ed077 --- /dev/null +++ b/src/Gemtext/Link.php @@ -0,0 +1,63 @@ +_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; + } +} \ No newline at end of file