1
0
mirror of https://github.com/suk-ws/ph-Bookshelf.git synced 2025-03-15 15:57:29 +08:00

add 302 with additional '/' on a book main page.

This commit is contained in:
A.C.Sukazyo Eyre 2023-03-27 16:26:35 +08:00
parent f79060f32f
commit 867695b1a2
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
3 changed files with 19 additions and 19 deletions

View File

@ -6,16 +6,9 @@ require "./vendor/autoload.php";
use SukWs\Bookshelf\Data\PageMeta; use SukWs\Bookshelf\Data\PageMeta;
use SukWs\Bookshelf\Data\SiteConfig\RobotsPolicy; use SukWs\Bookshelf\Data\SiteConfig\RobotsPolicy;
use SukWs\Bookshelf\Data\SiteMeta; use SukWs\Bookshelf\Data\SiteMeta;
use SukWs\Bookshelf\Utils\Markdown\Markdown;
use SukWs\Bookshelf\Utils\PageParse; use SukWs\Bookshelf\Utils\PageParse;
use SukWs\Bookshelf\Web\Main; use SukWs\Bookshelf\Web\Main;
//$parser = new Markdown();
//
//$data = $parser->parse(file_get_contents("./data/test_page.md"));
//
//exit($data->page_html);
try { try {
SiteMeta::load(); SiteMeta::load();
@ -57,19 +50,19 @@ try {
// 页面寻找失败,寻找资源文件 // 页面寻找失败,寻找资源文件
if ( // 搜索全局资源文件夹的指定文件 if ( // 搜索全局资源文件夹的指定文件
file_exists($resLoc = "./data/%assets/$req") is_file($resLoc = "./data/%assets/$req")
) {} else if ( // 搜索原始路径上的文件 ) {} else if ( // 搜索原始路径上的文件
file_exists($resLoc = "./data/$req") is_file($resLoc = "./data/$req")
) {} else if ( // 搜索可能存在的书籍资源文件夹中的指定文件 ) {} else if ( // 搜索可能存在的书籍资源文件夹中的指定文件
sizeof($uri) > 1 && ($resBook = (SiteMeta::getBookshelf()->getBook($uri[0]))) != null && sizeof($uri) > 1 && ($resBook = (SiteMeta::getBookshelf()->getBook($uri[0]))) != null &&
file_exists($resLoc = "./data/{$resBook->getId()}/%assets/$uri[1]") is_file($resLoc = "./data/{$resBook->getId()}/%assets/$uri[1]")
) {} else if ( // 上面的 %root 兼容 ) {} else if ( // 上面的 %root 兼容
sizeof($uri) > 1 && ($resBook = $uri[0]) == "%root" && sizeof($uri) > 1 && ($resBook = $uri[0]) == "%root" &&
file_exists($resLoc = "./data/$resBook/%assets/$uri[1]") is_file($resLoc = "./data/$resBook/%assets/$uri[1]")
) {} else if ( // 搜索以root书为根目录的原始路径上的文件 ) {} else if ( // 搜索以root书为根目录的原始路径上的文件
file_exists($resLoc = "./data/%root/$req") is_file($resLoc = "./data/%root/$req")
) {} else if ( // 搜索root书中的书籍资源文件夹中的文件 ) {} else if ( // 搜索root书中的书籍资源文件夹中的文件
file_exists($resLoc = "./data/%root/%assets/$req") is_file($resLoc = "./data/%root/%assets/$req")
) {} else { ) {} else {
throw new Exception("cannot find file " . $req); // 找不到资源文件 throw new Exception("cannot find file " . $req); // 找不到资源文件
} }

View File

@ -7,6 +7,7 @@ use SukWs\Bookshelf\Data\SiteConfig\ConfigName;
use SukWs\Bookshelf\Element\BookContent\BookContented; use SukWs\Bookshelf\Element\BookContent\BookContented;
use SukWs\Bookshelf\Element\BookContent\Page; use SukWs\Bookshelf\Element\BookContent\Page;
use SukWs\Bookshelf\Utils\Markdown\Markdown; use SukWs\Bookshelf\Utils\Markdown\Markdown;
use SukWs\Bookshelf\Utils\PageParse;
use SukWs\Bookshelf\Utils\RequestNotExistException; use SukWs\Bookshelf\Utils\RequestNotExistException;
class PageMeta { class PageMeta {
@ -23,7 +24,7 @@ class PageMeta {
*/ */
public static function init (array $uri): bool { public static function init (array $uri): bool {
if (!isset($uri[0]) || $uri[0] == "%root" || $uri[0] == "/" || $uri[0] == "") { if (empty($uri[0]) || $uri[0] == "%root") {
self::$book = SiteMeta::getBookshelf()->getRootBook(); self::$book = SiteMeta::getBookshelf()->getRootBook();
self::$bookId = "%root"; self::$bookId = "%root";
} else { } else {
@ -34,13 +35,15 @@ class PageMeta {
self::$book = $tmp->getContentedNode(); self::$book = $tmp->getContentedNode();
} }
if (isset($uri[1])) { if (!isset($uri[1]) && !empty($uri[0])) {
self::$page_id = $uri[1]; PageParse::output302($uri[0]."/");
} else { } else if (empty($uri[1])) {
$tmp = self::$book->getChildren()->getChildren()[0]; $tmp = self::$book->getChildren()->getChildren()[0];
if ($tmp instanceof Page) self::$page_id = $tmp->getId(); if ($tmp instanceof Page) self::$page_id = $tmp->getId();
else throw new RequestNotExistException("No main page in required book \"$uri[0]\""); else throw new RequestNotExistException("No main page in required book \"$uri[0]\"");
self::$isMainPage = true; self::$isMainPage = true;
} else {
self::$page_id = $uri[1];
} }
if ($content = @file_get_contents(self::getPagePath("md"))) { if ($content = @file_get_contents(self::getPagePath("md"))) {
self::$page_data = (new Markdown())->parse($content); self::$page_data = (new Markdown())->parse($content);
@ -108,8 +111,7 @@ class PageMeta {
$i = trim($i); $i = trim($i);
if ($i != "") $lang[] =$i; if ($i != "") $lang[] =$i;
} }
$lang = array_unique($lang); return array_unique($lang);
return $lang;
} }
public static function getPagePath (?string $extension = null): string { public static function getPagePath (?string $extension = null): string {

View File

@ -67,6 +67,11 @@ class PageParse {
return true; return true;
} }
public static function output302 (string $url): void {
header("Location: $url", true, 302);
exit;
}
/** /**
* 根据链接数组生成对应的链接/路径 * 根据链接数组生成对应的链接/路径
* *