From 590096afef6d0dc8b2bba5133077d14e8ee81f05 Mon Sep 17 00:00:00 2001 From: ghost Date: Thu, 1 Feb 2024 18:18:22 +0200 Subject: [PATCH] implement getLinks method --- README.md | 20 +++++++++++++++++++- src/Dokuwiki/Reader.php | 29 ++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index faa44ef..0c5d924 100644 --- a/README.md +++ b/README.md @@ -75,15 +75,33 @@ echo $reader->toGemini( Get document title ``` -echo $reader->getH1( +$gemini = $reader->toGemini( file_get_contents( '/host/data/pages/index.txt' ) ); + +echo $reader->getH1( + $gemini +); ``` #### Reader::getLinks +Get document links + +``` +$gemini = $reader->toGemini( + file_get_contents( + '/host/data/pages/index.txt' + ) +); + +echo $reader->getLinks( + $gemini +); +``` + ### Filesystem Provides methods for simple and secure interaction with DokuWiki file storage diff --git a/src/Dokuwiki/Reader.php b/src/Dokuwiki/Reader.php index ac32c40..ee18d94 100644 --- a/src/Dokuwiki/Reader.php +++ b/src/Dokuwiki/Reader.php @@ -246,12 +246,12 @@ class Reader ); } - public function getH1(string $data): ?string + public function getH1(string $gemini, ?string $regex = '/^[\s]?#([^#]+)/'): ?string { - foreach ((array) explode(PHP_EOL, $data) as $line) + foreach ((array) explode(PHP_EOL, $gemini) as $line) { preg_match_all( - '/^[\s]?#([^#]+)/', + $regex, $line, $matches ); @@ -266,4 +266,27 @@ class Reader } } } + + public function getLinks(string $gemini, ?string $regex = '/[A-z]+:\/\/\S+/'): array + { + $links = []; + + preg_match_all( + $regex, + $gemini, + $matches + ); + + if (!empty($matches[0])) + { + foreach ((array) $matches[0] as $link) + { + $links[] = trim( + $link + ); + } + } + + return $links; + } } \ No newline at end of file