From 8f43bdc2c16e892a3b2e7044d1444a0a0f22d620 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 5 May 2021 14:57:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E7=AE=80=E5=8D=95=E7=9A=84=E8=B5=84=E6=BA=90=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E4=BA=86=E6=A0=87=E9=A2=98=E8=B7=B3=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 现在无法被解析为页面的请求会尝试从 /data/%assets/ 中寻找资源文件 - 给标题解析添加了和内容相同的 id 生成,从而可以使用跳转了 --- index.php | 65 +++++++++++++------ lib/Parsedown/Parsedown.php | 15 ++++- src/Utils/PageParse.php | 87 ++++++++++++++++++++++++++ src/Utils/RequestNotExistException.php | 10 +++ 4 files changed, 156 insertions(+), 21 deletions(-) create mode 100644 src/Utils/PageParse.php create mode 100644 src/Utils/RequestNotExistException.php diff --git a/index.php b/index.php index d808540..c81dd4a 100644 --- a/index.php +++ b/index.php @@ -4,6 +4,8 @@ require_once "./src/Data/SiteMeta.php"; require_once "./src/Data/PageMeta.php"; require_once "./lib/Parsedown/Parsedown.php"; +require_once "./src/Utils/PageParse.php"; +require_once "./src/Utils/RequestNotExistException.php"; $parser = new Parsedown(); @@ -15,29 +17,52 @@ try { SiteMeta::load(); // 格式化所给链接,并将链接转化为路径字符串数组 - $tmp = $_GET['p']; - if (strlen($tmp) > 0 && $tmp[strlen($tmp) - 1] === '/') - $tmp = substr($tmp, 0, -1); - $uri = explode("/", $tmp, 2); + $req = $_GET['p']; + if (strlen($req) > 0 && $req[strlen($req) - 1] === '/') + $tmp = substr($req, 0, -1); + $uri = explode("/", $req, 2); - if (sizeof($uri) > 0 && $uri[0] != null) { - // 非主页面,判定当前定义的 book - $tmp = SiteMeta::getBookshelf()->getBook($uri[0]); - if ($tmp == null) throw new Exception("Book required \"$uri[0]\" not found!"); - PageMeta::$book = $tmp->getContentedNode(); - // 判定当前页面 - if (sizeof($uri) > 1 && $uri[1] != null) { - $tmp = PageMeta::$book->getPage($uri[1]); - if ($tmp == null) throw new Exception("Page required \"$uri[1]\" not found on book \"$uri[0]\"!"); - PageMeta::$page = $tmp; + try { + + // 寻找页面 + + if (sizeof($uri) > 0 && $uri[0] != null) { + // 非主页面,判定当前定义的 book + $tmp = SiteMeta::getBookshelf()->getBook($uri[0]); + if ($tmp == null) throw new RequestNotExistException("Book required \"$uri[0]\" not found!"); + PageMeta::$book = $tmp->getContentedNode(); + // 判定当前页面 + if (sizeof($uri) > 1 && $uri[1] != null) { + $tmp = PageMeta::$book->getPage($uri[1]); + if ($tmp == null) throw new RequestNotExistException("Page required \"$uri[1]\" not found on book \"$uri[0]\"!"); + PageMeta::$page = $tmp; + } else { + PageMeta::$page = PageMeta::$book->getChilds()->getChilds()[0]; + } } else { + // 主页面 + PageMeta::$book = SiteMeta::getBookshelf()->getRootBook(); PageMeta::$page = PageMeta::$book->getChilds()->getChilds()[0]; + PageMeta::$isMainPage = true; } - } else { - // 主页面 - PageMeta::$book = SiteMeta::getBookshelf()->getRootBook(); - PageMeta::$page = PageMeta::$book->getChilds()->getChilds()[0]; - PageMeta::$isMainPage = true; + + } catch (RequestNotExistException $e) { + + // 页面寻找失败,寻找资源文件 + + if (!is_file($resLoc = "./data/%assets/$req")) { // 全局文件夹的资源文件 +// if (!is_file($resLoc = sprintf("./data/%s/%s", PageParse::genPathFromUriArray($uri), $req))) { // 以当前页面为基准位置的资源文件 +// if (!is_file($resLoc = sprintf("./data/%s/%s", PageParse::genPathFromUriArray(array_slice($uri, 0, -1)), $req))) { // 以当前页面为基准位置的资源文件(fallback版) +// if (!is_file($resLoc = sprintf("./data/%s/%s", PageMeta::$book->getId(), $req))) { // 以当前书籍为基准位置的资源文件 +// if (!is_file($resLoc = "./data/$req")) { // 全局的资源文件 + throw $e; +// } +// } +// } +// } + } + PageParse::outputBinaryFile($resLoc); + } require "./template/header.php"; @@ -90,7 +115,7 @@ try {
-

getName() ?>

+

getName() ?>

text(PageMeta::$page->getMarkdownContent()) ?>
diff --git a/lib/Parsedown/Parsedown.php b/lib/Parsedown/Parsedown.php index bd34560..0ffd0ac 100644 --- a/lib/Parsedown/Parsedown.php +++ b/lib/Parsedown/Parsedown.php @@ -561,7 +561,20 @@ class Parsedown 'function' => 'lineElements', 'argument' => $text, 'destination' => 'elements', - ) + ), + /** + * Custom Start + * + * 生成标题的跳转用id + * + * @author Sukazyo + */ + 'attributes' => array( + 'id' => $text, + ), + /** + * Custom End + */ ), ); diff --git a/src/Utils/PageParse.php b/src/Utils/PageParse.php new file mode 100644 index 0000000..03dbf3f --- /dev/null +++ b/src/Utils/PageParse.php @@ -0,0 +1,87 @@ + "a2d/WAE42WR6/39272833" + * + * @param string[] $uri + * @return string + */ + public static function genPathFromUriArray (array $uri): string { + $result = ""; + foreach ($uri as $n) { + $result .= $n . "/"; + } + return substr($result, 0, -1); + } + +} diff --git a/src/Utils/RequestNotExistException.php b/src/Utils/RequestNotExistException.php new file mode 100644 index 0000000..3f16d0d --- /dev/null +++ b/src/Utils/RequestNotExistException.php @@ -0,0 +1,10 @@ +