From fa2bb2bd7712685bed08efb2f9e52e25a260c5d3 Mon Sep 17 00:00:00 2001 From: ghost Date: Mon, 4 Mar 2024 20:40:04 +0200 Subject: [PATCH] add gemtext mode --- example/config.json | 3 +- src/server.php | 109 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 95 insertions(+), 17 deletions(-) diff --git a/example/config.json b/example/config.json index 6457656..945f6b6 100644 --- a/example/config.json +++ b/example/config.json @@ -36,7 +36,8 @@ "view": { "raw":"Raw data", - "reader":"Reader" + "reader":"Reader", + "gemtext":"Gemtext" } } }, diff --git a/src/server.php b/src/server.php index b13041c..7a42cd7 100644 --- a/src/server.php +++ b/src/server.php @@ -496,6 +496,48 @@ $server->setHandler( break; + case 'gemtext': + + // Key + $result[] = null; + $result[] = sprintf( + '# %s', + trim( + preg_replace( // single-line + '/[\s]+/', + ' ', + $record['key'] + ) + ) + ); + + // Value + $result[] = null; + $result[] = trim( + preg_replace( + [ + '/[\n\r]{3,}/', // remove extra breaks + ], + [ + PHP_EOL . PHP_EOL, + ], + $record['value'] + ) + ); + + // Time + $result[] = null; + $result[] = sprintf( + '%s in %d', + date( + 'Y-m-d', + $record['time'] + ), + $record['block'] + ); + + break; + default: // Key @@ -546,24 +588,59 @@ $server->setHandler( $config->geminiapp->string->navigation ); - if ('raw' == $request->getQuery()) + switch ($request->getQuery()) { - $result[] = null; - $result[] = sprintf( - '=> /%s %s', - $record['transaction'], - $config->geminiapp->string->view->reader - ); - } + case 'raw': - else - { - $result[] = null; - $result[] = sprintf( - '=> /%s?raw %s', - $record['transaction'], - $config->geminiapp->string->view->raw - ); + $result[] = null; + $result[] = sprintf( + '=> /%s?gemtext %s', + $record['transaction'], + $config->geminiapp->string->view->gemtext + ); + + $result[] = null; + $result[] = sprintf( + '=> /%s %s', + $record['transaction'], + $config->geminiapp->string->view->reader + ); + + break; + + case 'gemtext': + + $result[] = null; + $result[] = sprintf( + '=> /%s %s', + $record['transaction'], + $config->geminiapp->string->view->reader + ); + + $result[] = null; + $result[] = sprintf( + '=> /%s?raw %s', + $record['transaction'], + $config->geminiapp->string->view->raw + ); + + break; + + default: + + $result[] = null; + $result[] = sprintf( + '=> /%s?gemtext %s', + $record['transaction'], + $config->geminiapp->string->view->gemtext + ); + + $result[] = null; + $result[] = sprintf( + '=> /%s?raw %s', + $record['transaction'], + $config->geminiapp->string->view->raw + ); } $result[] = sprintf(