Browse Source

draft index menu

main
ghost 11 months ago
parent
commit
05f9929e37
  1. 4
      example/config.json
  2. 205
      src/server.php

4
example/config.json

@ -29,10 +29,14 @@
}, },
"string": "string":
{ {
"resources":"Resources",
"links":"Links", "links":"Links",
"sections":"Sections",
"pages":"Pages",
"actions":"Actions", "actions":"Actions",
"main":"Main page", "main":"Main page",
"source":"Source", "source":"Source",
"welcome":"About",
"about":"=> https://github.com/YGGverse/dokuwiki-gemini-server GitHub" "about":"=> https://github.com/YGGverse/dokuwiki-gemini-server GitHub"
} }
} }

205
src/server.php

@ -107,33 +107,177 @@ $server->setHandler(
// Route begin // Route begin
switch ($request->getPath()) switch ($request->getPath())
{ {
// Home request // Static route here
case null: case null:
case '/': case false:
case '':
if ($path = $filesystem->getPagePathByUri($config->dokuwiki->uri->home)) // @TODO redirect to document root (/)
{
$reader = new \Yggverse\Gemini\Dokuwiki\Reader(); break;
case '/search':
// @TODO implement search feature
break;
default:
// Parse request
preg_match('/^\/([^\/]*)$/', $request->getPath(), $matches);
$uri = isset($matches[1]) ? $matches[1] : '';
// Directory request, build index links as no side menu in gemini version
if ($directory = $filesystem->getDirectoryPathByUri($uri))
{
// Check for cached results
/*
if ($content = $memory->get('/'))
{
$response->setContent( $response->setContent(
$reader->toGemini( $content
file_get_contents( );
return $response;
}
*/
// Build home page
$lines = [
PHP_EOL
];
// Append header
$lines[] = sprintf(
'# %s',
$config->string->welcome
);
// Get children sections
$sections = [];
foreach ($filesystem->getTree() as $path => $files)
{
if (str_starts_with($path, $directory) && $path != $directory)
{
$sections[] = sprintf(
'=> gemini://%s%s/%s',
$config->gemini->server->host,
$config->gemini->server->port == 1965 ? null : ':' . $config->gemini->server->port,
$filesystem->getDirectoryUriByPath(
$path $path
) )
) );
}
}
if ($sections)
{
// Keep unique
$sections = array_unique(
$sections
); );
return $response; // Sort asc
sort(
$sections
);
// Append header
$lines[] = sprintf(
'## %s',
$config->string->sections
);
// Append sections
foreach ($sections as $section)
{
$lines[] = $section;
}
} }
// Internal page request // Get children pages
default: $pages = [];
if (preg_match('/^\/([^\/]*)$/', $request->getPath(), $matches)) foreach ($filesystem->getPagePathsByPath($directory) as $file)
{ {
if (!empty($matches[1])) $pages[] = sprintf(
'=> gemini://%s%s/%s',
$config->gemini->server->host,
$config->gemini->server->port == 1965 ? null : ':' . $config->gemini->server->port,
$filesystem->getPageUriByPath(
$file
)
);
}
if ($pages)
{ {
if ($path = $filesystem->getPagePathByUri($matches[1])) // Keep unique
$pages = array_unique(
$pages
);
// Sort asc
sort(
$pages
);
// Append header
$lines[] = sprintf(
'## %s',
$config->string->pages
);
// Append pages
foreach ($pages as $page)
{
$lines[] = $page;
}
}
// Append about info
$lines[] = sprintf(
'## %s',
$config->string->resources
);
// Append source link
$lines[] = sprintf(
'=> %s %s',
$config->dokuwiki->url->source,
$config->string->source
);
// Append about info
$lines[] = $config->string->about;
// Merge lines
$content = implode(
PHP_EOL,
$lines
);
// @TODO '~index:menu~'
// Cache results
$memory->set(
'/',
$content
);
// Response
$response->setContent(
$content
);
return $response;
}
// File request, get page content
if ($path = $filesystem->getPagePathByUri($uri))
{ {
// Check for cached results // Check for cached results
if ($content = $memory->get($path)) if ($content = $memory->get($path))
@ -159,6 +303,37 @@ $server->setHandler(
) )
); );
// Define index menu
/* @TODO
$pages = [];
if ($directory = $filesystem->getDirectoryPathByUri($uri))
{
foreach ($filesystem->getPagePathsByPath($directory) as $file)
{
$pages[] = sprintf(
'=> gemini://%s%s/%s',
$config->gemini->server->host,
$config->gemini->server->port == 1965 ? null : ':' . $config->gemini->server->port,
$filesystem->getPageUriByPath(
$file
)
);
}
}
if ($pages)
{
$reader->setRule(
'/\{\{indexmenu>:([^\}]+)\}\}/i',
implode(
PHP_EOL,
$pages
)
);
}
*/
// Convert // Convert
$gemini = $reader->toGemini( $gemini = $reader->toGemini(
file_get_contents( file_get_contents(
@ -232,8 +407,6 @@ $server->setHandler(
return $response; return $response;
} }
} }
}
}
// Route not found // Route not found
$response->setCode( $response->setCode(

Loading…
Cancel
Save