From d12b52597fed0f905693a4f19ce53bdc7b81ebce Mon Sep 17 00:00:00 2001 From: ghost Date: Fri, 2 Feb 2024 21:24:10 +0200 Subject: [PATCH] implement Helper methods --- README.md | 35 ++++++++++ src/Dokuwiki/Helper.php | 141 ++++++++++++++++++++++++++++++++++++++++ src/Dokuwiki/Reader.php | 2 +- 3 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 src/Dokuwiki/Helper.php diff --git a/README.md b/README.md index dfff35d..49550f4 100644 --- a/README.md +++ b/README.md @@ -172,4 +172,39 @@ var_dump ( '/full/path/to/page.txt' ) ) +``` + +### Helper + +Useful methods to minify controller codebase + +``` +$helper = new \Yggverse\Gemini\Dokuwiki\Helper( + new \Yggverse\Gemini\Dokuwiki\Filesystem(), + new \Yggverse\Gemini\Dokuwiki\Reader() +); +``` + +#### Filesystem::getChildrenSectionLinksByUri + +Return simple array of children section links in Gemini format + +``` +var_dump ( + $helper->getChildrenSectionLinksByUri( + 'hello:world' + ) +) +``` + +#### Filesystem::getChildrenPageLinksByUri + +Return simple array of children page links in Gemini format + +``` +var_dump ( + $helper->getChildrenPageLinksByUri( + 'hello:world' + ) +) ``` \ No newline at end of file diff --git a/src/Dokuwiki/Helper.php b/src/Dokuwiki/Helper.php new file mode 100644 index 0000000..964fee6 --- /dev/null +++ b/src/Dokuwiki/Helper.php @@ -0,0 +1,141 @@ +_filesystem = $filesystem; + $this->_reader = $reader; + } + + public function getChildrenSectionLinksByUri(?string $uri = ''): array + { + $sections = []; + + if ($directory = $this->_filesystem->getDirectoryPathByUri($uri)) + { + foreach ((array) $this->_filesystem->getTree() as $path => $files) + { + if (str_starts_with($path, $directory) && $path != $directory) + { + // Init link name + $h1 = null; + + // Init this directory URI + $thisUri = $this->_filesystem->getDirectoryUriByPath( + $path + ); + + // Skip sections deeper this level + if (substr_count($thisUri, ':') > ($uri ? substr_count($uri, ':') + 1 : 0)) + { + continue; + } + + // Get section names + $segments = []; + + foreach ((array) explode(':', $thisUri) as $segment) + { + $segments[] = $segment; + + // Find section index if exists + if ($file = $this->_filesystem->getPagePathByUri(implode(':', $segments) . ':' . $segment)) + { + $h1 = $this->_reader->getH1( + $this->_reader->toGemini( + file_get_contents( + $file + ) + ) + ); + } + + // Find section page if exists + else if ($file = $this->_filesystem->getPagePathByUri(implode(':', $segments))) + { + $h1 = $this->_reader->getH1( + $this->_reader->toGemini( + file_get_contents( + $file + ) + ) + ); + } + + // Reset title of undefined segment + else + { + $h1 = null; + } + } + + // Register section link + $sections[] = sprintf( + '=> /%s %s', + $thisUri, + $h1 + ); + } + } + } + + // Keep unique + $sections = array_unique( + $sections + ); + + // Sort asc + sort( + $sections + ); + + return $sections; + } + + public function getChildrenPageLinksByUri(?string $uri = ''): array + { + $pages = []; + + if ($directory = $this->_filesystem->getDirectoryPathByUri($uri)) + { + foreach ((array) $this->_filesystem->getPagePathsByPath($directory) as $file) + { + $pages[] = sprintf( + '=> /%s %s', + $this->_filesystem->getPageUriByPath( + $file + ), + $this->_reader->getH1( + $this->_reader->toGemini( + file_get_contents( + $file + ) + ) + ) + ); + } + } + + // Keep unique + $pages = array_unique( + $pages + ); + + // Sort asc + sort( + $pages + ); + + return $pages; + } +} \ No newline at end of file diff --git a/src/Dokuwiki/Reader.php b/src/Dokuwiki/Reader.php index ffd0a47..29d3084 100644 --- a/src/Dokuwiki/Reader.php +++ b/src/Dokuwiki/Reader.php @@ -60,7 +60,7 @@ class Reader '/\[\[doku>([^\]]+)\]\]/i' => '$1( https://www.dokuwiki.org/$1 )', /// Index - /// Server-side implementation: https://github.com/YGGverse/dokuwiki-gemini-server + /// Useful with src/Dokuwiki/Helper.php '/\{\{indexmenu>:([^\}]+)\}\}/i' => '', '/\{\{indexmenu_n>[\d]+\}\}/i' => '',