Browse Source

implement Helper methods

main
ghost 8 months ago
parent
commit
d12b52597f
  1. 35
      README.md
  2. 141
      src/Dokuwiki/Helper.php
  3. 2
      src/Dokuwiki/Reader.php

35
README.md

@ -172,4 +172,39 @@ var_dump ( @@ -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'
)
)
```

141
src/Dokuwiki/Helper.php

@ -0,0 +1,141 @@ @@ -0,0 +1,141 @@
<?php
declare(strict_types=1);
namespace Yggverse\Gemini\Dokuwiki;
class Helper
{
private \Yggverse\Gemini\Dokuwiki\Filesystem $_filesystem;
private \Yggverse\Gemini\Dokuwiki\Reader $_reader;
public function __construct(
\Yggverse\Gemini\Dokuwiki\Filesystem $filesystem,
\Yggverse\Gemini\Dokuwiki\Reader $reader
) {
$this->_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;
}
}

2
src/Dokuwiki/Reader.php

@ -60,7 +60,7 @@ class Reader @@ -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' => '',

Loading…
Cancel
Save