From 6a5b4937c97a832de269227541d461e2d0494bb0 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Mon, 26 Apr 2021 22:02:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E8=A7=A3=E6=9E=90=E5=92=8C=E5=BD=93=E5=89=8D?= =?UTF-8?q?=20book=20=E7=9A=84=E7=9B=AE=E5=BD=95=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 基础的请求链接解析实现 - 当前链接解析将第一个参数(首目录)视为 bookId,剩余视为 pageId - 即不支持 bookId中包含“/” - 同时实现了 books 的 active 判定 - 实现了通过 ID 进行查询容器中的 Book 或者 Page 的功能 - 实现了通过 Book对象 获取对应的 BookContented 对象 - 实现了 BookContented 的目录表生成 - 同时也有 active 判定 --- assets/gitbook-fix.js | 2 +- index.php | 46 +++++++++++++---------- src/Data/PageMeta.php | 2 +- src/Element/Book.php | 10 ++++- src/Element/BookCollection.php | 17 ++++++++- src/Element/BookContent/BookContented.php | 8 ++++ src/Element/BookContent/Chapter.php | 25 ++++++++++++ src/Element/BookContent/Page.php | 13 +++++++ src/Element/BookContent/Segment.php | 4 ++ src/Element/Bookshelf.php | 8 +++- 10 files changed, 111 insertions(+), 24 deletions(-) diff --git a/assets/gitbook-fix.js b/assets/gitbook-fix.js index d8f1f11..b64674b 100644 --- a/assets/gitbook-fix.js +++ b/assets/gitbook-fix.js @@ -1,6 +1,6 @@ const WITH_SUMMARY_CLASS = "with-summary" -var bookRoot; +let bookRoot; function summaryOnOrOff () { diff --git a/index.php b/index.php index 475b64b..46771b5 100644 --- a/index.php +++ b/index.php @@ -6,9 +6,32 @@ require_once "./src/Data/PageMeta.php"; try { SiteMeta::load(); - PageMeta::$book = SiteMeta::getBookshelf()->getRootBook(); - PageMeta::$page = PageMeta::$book->getChilds()->getChilds()[0]; - PageMeta::$isMainPage = true; + + // 格式化所给链接,并将链接转化为路径字符串数组 + $tmp = $_GET['p']; + if (strlen($tmp) > 0 && $tmp[strlen($tmp) - 1] === '/') + $tmp = substr($tmp, 0, -1); + $uri = explode("/", $tmp, 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; + } else { + PageMeta::$page = PageMeta::$book->getChilds()->getChilds()[0]; + } + } else { + // 主页面 + PageMeta::$book = SiteMeta::getBookshelf()->getRootBook(); + PageMeta::$page = PageMeta::$book->getChilds()->getChilds()[0]; + PageMeta::$isMainPage = true; + } require "./template/header.php"; @@ -21,22 +44,7 @@ try {
diff --git a/src/Data/PageMeta.php b/src/Data/PageMeta.php index 7df9bea..add6c7e 100644 --- a/src/Data/PageMeta.php +++ b/src/Data/PageMeta.php @@ -7,7 +7,7 @@ class PageMeta { public static BookContented $book; public static Page $page; - public static bool $isMainPage; + public static bool $isMainPage = false; public static function getPageTitle (): string { return self::$page->getName()." - ".self::$book->getName(); diff --git a/src/Element/Book.php b/src/Element/Book.php index f45687a..03e2f98 100644 --- a/src/Element/Book.php +++ b/src/Element/Book.php @@ -1,6 +1,7 @@ $this->name"; + return "
  • $this->name
  • "; + } + + /** + * @throws Exception + */ + public function getContentedNode (): BookContented { + return BookContented::parseString(file_get_contents("./data/$this->id/book.xml")); } } \ No newline at end of file diff --git a/src/Element/BookCollection.php b/src/Element/BookCollection.php index 23299ce..7c3a2be 100644 --- a/src/Element/BookCollection.php +++ b/src/Element/BookCollection.php @@ -72,7 +72,7 @@ class BookCollection { public function getHtml (): string { $str = ""; - if ($this->name != self::ROOT) $str .= "
  • $this->name
      "; + if ($this->name != self::ROOT) $str .= "
    • $this->name
        "; foreach ($this->array as $node) { $str .= $node->getHtml(); } @@ -80,4 +80,19 @@ class BookCollection { return $str; } + public function getBook (string $id): ?Book { + + foreach ($this->array as $node) { + if ($node instanceof Book && $node->getId() == $id) + return $node; + else if ($node instanceof BookCollection) { + $got = $node->getBook($id); + if ($got != null) return $got; + } + } + + return null; + + } + } diff --git a/src/Element/BookContent/BookContented.php b/src/Element/BookContent/BookContented.php index adb8c0f..9de6160 100644 --- a/src/Element/BookContent/BookContented.php +++ b/src/Element/BookContent/BookContented.php @@ -63,4 +63,12 @@ class BookContented { return $this->childs; } + public function getSummaryHtml (): string { + return $this->childs->getSummaryHtml(); + } + + public function getPage (string $id): Page { + return $this->childs->getPage($id); + } + } diff --git a/src/Element/BookContent/Chapter.php b/src/Element/BookContent/Chapter.php index 8354b97..b5ffb9a 100644 --- a/src/Element/BookContent/Chapter.php +++ b/src/Element/BookContent/Chapter.php @@ -64,4 +64,29 @@ class Chapter { return $this->parent; } + public function getSummaryHtml (): string { + $str = ""; + if ($this->parent != null) $str .= "
      • $this->name
          "; + foreach ($this->childs as $node) { + $str .= $node->getSummaryHtml(); + } + if ($this->parent != null) $str .= "
      • "; + return $str; + } + + public function getPage (string $id): ?Page { + + foreach ($this->childs as $node) { + if ($node instanceof Page && $node->getId() == $id) + return $node; + else if ($node instanceof Chapter) { + $got = $node->getPage($id); + if ($got != null) return $got; + } + } + + return null; + + } + } diff --git a/src/Element/BookContent/Page.php b/src/Element/BookContent/Page.php index 47b74c5..22abaec 100644 --- a/src/Element/BookContent/Page.php +++ b/src/Element/BookContent/Page.php @@ -71,4 +71,17 @@ class Page { return $this->parent; } + public function getSummaryHtml (): string { + $str = "
      • $this->name"; + if (sizeof($this->segues) > 0) { + $str .= "
          "; + foreach ($this->segues as $node) { + $str .= $node->getSummaryHtml(); + } + $str .= "
        "; + } + $str .= "
      • "; + return $str; + } + } diff --git a/src/Element/BookContent/Segment.php b/src/Element/BookContent/Segment.php index 6d4be6a..7423fc3 100644 --- a/src/Element/BookContent/Segment.php +++ b/src/Element/BookContent/Segment.php @@ -49,4 +49,8 @@ class Segment { return $this->parent; } + public function getSummaryHtml (): string { + return "
      • $this->name
      • "; + } + } \ No newline at end of file diff --git a/src/Element/Bookshelf.php b/src/Element/Bookshelf.php index 8c88e17..5c0cdbd 100644 --- a/src/Element/Bookshelf.php +++ b/src/Element/Bookshelf.php @@ -75,9 +75,15 @@ class Bookshelf { $str .= $this->links->getHtml(); $str .= "
      • "; $str .= $this->books->getHtml(); + $str .= "
      • "; + $str .= PageMeta::$book->getSummaryHtml(); + $str .= "
      • Generated with ph-Bookshelf
      • Front-End by GitBook
      • "; $str .= "
      "; - // TODO books list return $str; } + public function getBook (string $id): ?Book { + return $this->books->getBook($id); + } + }