Browse Source

draft index menu

main
ghost 11 months ago
parent
commit
05f9929e37
  1. 4
      example/config.json
  2. 371
      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"
} }
} }

371
src/server.php

@ -107,131 +107,304 @@ $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();
$response->setContent( break;
$reader->toGemini(
file_get_contents(
$path
)
)
);
return $response; case '/search':
}
// @TODO implement search feature
break;
// Internal page request
default: default:
if (preg_match('/^\/([^\/]*)$/', $request->getPath(), $matches)) // 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))
{ {
if (!empty($matches[1])) // Check for cached results
/*
if ($content = $memory->get('/'))
{ {
if ($path = $filesystem->getPagePathByUri($matches[1])) $response->setContent(
{ $content
// Check for cached results );
if ($content = $memory->get($path))
{ return $response;
$response->setContent( }
$content */
);
// Build home page
return $response; $lines = [
} PHP_EOL
];
// Init reader
$reader = new \Yggverse\Gemini\Dokuwiki\Reader(); // Append header
$lines[] = sprintf(
// Define base URL '# %s',
$reader->setMacros( $config->string->welcome
'~URL:base~', );
sprintf(
'gemini://%s%s/%s', // Get children sections
$config->gemini->server->host, $sections = [];
$config->gemini->server->port == 1965 ? null : ':' . $config->gemini->server->port,
'' // @TODO append relative prefix (:)
)
);
// Convert foreach ($filesystem->getTree() as $path => $files)
$gemini = $reader->toGemini( {
file_get_contents( 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
) )
); );
}
}
$lines = [ if ($sections)
$gemini {
]; // Keep unique
$sections = array_unique(
// Get page links $sections
if ($links = $reader->getLinks($gemini)) );
{
$lines[] = sprintf( // Sort asc
'## %s', sort(
$config->string->links $sections
); );
foreach ($links as $link) // Append header
{ $lines[] = sprintf(
$lines[] = sprintf( '## %s',
'=> %s', $config->string->sections
$link );
);
} // Append sections
} foreach ($sections as $section)
{
// Append actions header $lines[] = $section;
$lines[] = sprintf( }
'## %s', }
$config->string->actions
);
// Append source and homepage link // Get children pages
$lines[] = sprintf( $pages = [];
'=> gemini://%s%s %s',
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)
{
// 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
if ($content = $memory->get($path))
{
$response->setContent(
$content
);
return $response;
}
// Init reader
$reader = new \Yggverse\Gemini\Dokuwiki\Reader();
// Define base URL
$reader->setMacros(
'~URL:base~',
sprintf(
'gemini://%s%s/%s',
$config->gemini->server->host,
$config->gemini->server->port == 1965 ? null : ':' . $config->gemini->server->port,
'' // @TODO append relative prefix (:)
)
);
// 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->host,
$config->gemini->server->port == 1965 ? null : ':' . $config->gemini->server->port, $config->gemini->server->port == 1965 ? null : ':' . $config->gemini->server->port,
$config->string->main $filesystem->getPageUriByPath(
$file
)
); );
}
}
// Append source link if ($pages)
$lines[] = sprintf( {
'=> %s/%s %s', $reader->setRule(
$config->dokuwiki->url->source, '/\{\{indexmenu>:([^\}]+)\}\}/i',
$matches[1], implode(
$config->string->source PHP_EOL,
); $pages
)
);
}
*/
// Append about info // Convert
$lines[] = $config->string->about; $gemini = $reader->toGemini(
file_get_contents(
$path
)
);
// Merge lines $lines = [
$content = implode( $gemini
PHP_EOL, ];
$lines
);
// Cache results // Get page links
$memory->set( if ($links = $reader->getLinks($gemini))
$path, {
$content $lines[] = sprintf(
); '## %s',
$config->string->links
);
// Response foreach ($links as $link)
$response->setContent( {
$content $lines[] = sprintf(
'=> %s',
$link
); );
return $response;
} }
} }
// Append actions header
$lines[] = sprintf(
'## %s',
$config->string->actions
);
// Append source and homepage link
$lines[] = sprintf(
'=> gemini://%s%s %s',
$config->gemini->server->host,
$config->gemini->server->port == 1965 ? null : ':' . $config->gemini->server->port,
$config->string->main
);
// Append source link
$lines[] = sprintf(
'=> %s/%s %s',
$config->dokuwiki->url->source,
$matches[1],
$config->string->source
);
// Append about info
$lines[] = $config->string->about;
// Merge lines
$content = implode(
PHP_EOL,
$lines
);
// Cache results
$memory->set(
$path,
$content
);
// Response
$response->setContent(
$content
);
return $response;
} }
} }

Loading…
Cancel
Save