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);
+ }
+
}