Browse Source

reduce controller by use \Yggverse\Gemini\Dokuwiki\Helper

main
ghost 10 months ago
parent
commit
03f0f767ef
  1. 177
      src/server.php

177
src/server.php

@ -76,6 +76,15 @@ $filesystem = new \Yggverse\Gemini\Dokuwiki\Filesystem(
) )
); );
// Init reader
$reader = new \Yggverse\Gemini\Dokuwiki\Reader();
// Init helper
$helper = new \Yggverse\Gemini\Dokuwiki\Helper(
$filesystem,
$reader
);
// Init server // Init server
$server = new \Yggverse\TitanII\Server(); $server = new \Yggverse\TitanII\Server();
@ -93,6 +102,8 @@ $server->setHandler(
global $config; global $config;
global $memory; global $memory;
global $filesystem; global $filesystem;
global $reader;
global $helper;
$response = new \Yggverse\TitanII\Response(); $response = new \Yggverse\TitanII\Response();
@ -142,9 +153,6 @@ $server->setHandler(
return $response; return $response;
} }
// Init reader
$reader = new \Yggverse\Gemini\Dokuwiki\Reader();
// Define base URL // Define base URL
$reader->setMacros( $reader->setMacros(
'~URL:base~', '~URL:base~',
@ -157,33 +165,59 @@ $server->setHandler(
); );
// Define index menu // Define index menu
/* @TODO $index = [];
$pages = [];
// Append index sections
if ($sections = $helper->getChildrenSectionLinksByUri($_uri))
{
// Append header
$index[] = sprintf(
'### %s',
$config->string->sections
);
if ($directory = $filesystem->getDirectoryPathByUri($_uri)) // Append sections
foreach ($sections as $section)
{ {
foreach ($filesystem->getPagePathsByPath($directory) as $file) $index[] = $section;
}
}
// Get children pages
if ($pages = $helper->getChildrenPageLinksByUri($_uri))
{ {
$pages[] = sprintf( // Append header
'=> /%s', $index[] = sprintf(
$filesystem->getPageUriByPath( '### %s',
$file $config->string->pages
)
); );
// Append pages
foreach ($pages as $page)
{
$index[] = $page;
} }
} }
if ($pages) // Set macros value
if ($index)
{ {
$reader->setRule( $reader->setRule(
'/\{\{indexmenu>:([^\}]+)\}\}/i', '/\{\{indexmenu>:([^\}]+)\}\}/i',
implode( implode(
PHP_EOL, PHP_EOL,
$pages $index
)
);
$reader->setRule(
'/\{\{indexmenu_n>[\d]+\}\}/i',
implode(
PHP_EOL,
$index
) )
); );
} }
*/
// Convert // Convert
$gemini = $reader->toGemini( $gemini = $reader->toGemini(
@ -326,86 +360,8 @@ $server->setHandler(
); );
// Get children sections // Get children sections
$sections = []; if ($sections = $helper->getChildrenSectionLinksByUri($_uri))
foreach ($filesystem->getTree() as $path => $files)
{
if (str_starts_with($path, $directory) && $path != $directory)
{
// Init link name
$alt = null;
// Init this directory URI
$uri = $filesystem->getDirectoryUriByPath(
$path
);
// Skip sections deeper this level
if (substr_count($uri, ':') > ($_uri ? substr_count($_uri, ':') + 1 : 0))
{
continue;
}
// Get section names
$segments = [];
foreach ((array) explode(':', $uri) as $segment)
{
$segments[] = $segment;
// Find section index if exists
if ($file = $filesystem->getPagePathByUri(implode(':', $segments) . ':' . $segment))
{
$alt = $reader->getH1(
$reader->toGemini(
file_get_contents(
$file
)
)
);
}
// Find section page if exists
else if ($file = $filesystem->getPagePathByUri(implode(':', $segments)))
{
$alt = $reader->getH1(
$reader->toGemini(
file_get_contents(
$file
)
)
);
}
// Reset title of undefined segment
else
{ {
$alt = null;
}
}
// Register section link
$sections[] = sprintf(
'=> /%s %s',
$uri,
$alt
);
}
}
// Append sections
if ($sections)
{
// Keep unique
$sections = array_unique(
$sections
);
// Sort asc
sort(
$sections
);
// Append header // Append header
$lines[] = sprintf( $lines[] = sprintf(
'## %s', '## %s',
@ -420,37 +376,8 @@ $server->setHandler(
} }
// Get children pages // Get children pages
$pages = []; if ($pages = $helper->getChildrenPageLinksByUri($_uri))
foreach ($filesystem->getPagePathsByPath($directory) as $file)
{ {
$pages[] = sprintf(
'=> /%s %s',
$filesystem->getPageUriByPath(
$file
),
$reader->getH1(
$reader->toGemini(
file_get_contents(
$file
)
)
)
);
}
if ($pages)
{
// Keep unique
$pages = array_unique(
$pages
);
// Sort asc
sort(
$pages
);
// Append header // Append header
$lines[] = sprintf( $lines[] = sprintf(
'## %s', '## %s',
@ -486,8 +413,6 @@ $server->setHandler(
$lines $lines
); );
// @TODO '~index:menu~'
// Cache results // Cache results
$memory->set( $memory->set(
'/', '/',

Loading…
Cancel
Save