1
0
mirror of https://github.com/suk-ws/ph-Bookshelf.git synced 2024-12-05 09:26:52 +08:00

完成了请求链接解析和当前 book 的目录生成

- 基础的请求链接解析实现
  - 当前链接解析将第一个参数(首目录)视为 bookId,剩余视为 pageId
    - 即不支持 bookId中包含“/”
  - 同时实现了 books 的 active 判定
- 实现了通过 ID 进行查询容器中的 Book 或者 Page 的功能
- 实现了通过 Book对象 获取对应的 BookContented 对象
- 实现了 BookContented 的目录表生成
  - 同时也有 active 判定
This commit is contained in:
A.C.Sukazyo Eyre 2021-04-26 22:02:22 +08:00
parent 9bb1dd1a75
commit 6a5b4937c9
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
10 changed files with 111 additions and 24 deletions

View File

@ -1,6 +1,6 @@
const WITH_SUMMARY_CLASS = "with-summary" const WITH_SUMMARY_CLASS = "with-summary"
var bookRoot; let bookRoot;
function summaryOnOrOff () { function summaryOnOrOff () {

View File

@ -6,9 +6,32 @@ require_once "./src/Data/PageMeta.php";
try { try {
SiteMeta::load(); 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"; require "./template/header.php";
@ -21,22 +44,7 @@ try {
</ul> </ul>
</div> </div>
<nav role="navigation"> <nav role="navigation">
<ul class="summary"> <?= SiteMeta::getBookshelf()->getHtml(); ?>
<?= SiteMeta::getBookshelf()->getHtml(); ?>
<li class="divider"></li>
<li class="chapter active" data-level="1.1" data-path="./"><a href="./">占位主页面</a></li>
<li class="chapter " data-level="1.2" data-path="api/">
<a href="#">占位章节</a>
<ul class="articles">
<li class="chapter " data-level="1.2.1" data-path="api/color.html"><a href="chp/page.html">占位页面</a></li>
<li class="chapter " data-level="1.2.2" data-path="api/suka-talk.html"><a href="chp/page-other.html">另一个占位页面</a></li>
</ul>
</li>
<li class="chapter " data-level="1.3" data-path="srv-dev.html"><a href="backnote.html">占位后记</a></li>
<li class="divider"></li>
<li><a href="https://github.com/Eyre-S/ph-Bookshelf" target="blank" class="gitbook-link">Generated with ph-Bookshelf</a></li>
<li><a href="https://www.gitbook.com" target="blank" class="gitbook-link">Front-End by GitBook</a></li>
</ul>
</nav> </nav>
</div> </div>
<div class="book-body"> <div class="book-body">

View File

@ -7,7 +7,7 @@ class PageMeta {
public static BookContented $book; public static BookContented $book;
public static Page $page; public static Page $page;
public static bool $isMainPage; public static bool $isMainPage = false;
public static function getPageTitle (): string { public static function getPageTitle (): string {
return self::$page->getName()." - ".self::$book->getName(); return self::$page->getName()." - ".self::$book->getName();

View File

@ -1,6 +1,7 @@
<?php <?php
require_once "./src/Element/BookCollection.php"; require_once "./src/Element/BookCollection.php";
require_once "./src/Element/BookContent/BookContented.php";
class Book { class Book {
@ -53,7 +54,14 @@ class Book {
} }
public function getHtml (): string { public function getHtml (): string {
return "<li class='link'><a class='link' href='$this->id'>$this->name</a></li>"; return "<li class='link" . (PageMeta::$book->getId()==$this->id?" active":"") . "'><a class='link' href='/$this->id'>$this->name</a></li>";
}
/**
* @throws Exception
*/
public function getContentedNode (): BookContented {
return BookContented::parseString(file_get_contents("./data/$this->id/book.xml"));
} }
} }

View File

@ -72,7 +72,7 @@ class BookCollection {
public function getHtml (): string { public function getHtml (): string {
$str = ""; $str = "";
if ($this->name != self::ROOT) $str .= "<li><a class='book-collection' href='#'>$this->name</a><ul class='book-collection'>"; if ($this->name != self::ROOT) $str .= "<li><a class='book-collection' href='#'>$this->name</a><ul class='book-collection summary'>";
foreach ($this->array as $node) { foreach ($this->array as $node) {
$str .= $node->getHtml(); $str .= $node->getHtml();
} }
@ -80,4 +80,19 @@ class BookCollection {
return $str; 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;
}
} }

View File

@ -63,4 +63,12 @@ class BookContented {
return $this->childs; return $this->childs;
} }
public function getSummaryHtml (): string {
return $this->childs->getSummaryHtml();
}
public function getPage (string $id): Page {
return $this->childs->getPage($id);
}
} }

View File

@ -64,4 +64,29 @@ class Chapter {
return $this->parent; return $this->parent;
} }
public function getSummaryHtml (): string {
$str = "";
if ($this->parent != null) $str .= "<li class='chapter'><a class='page-chapter' href='#'>$this->name</a><ul class='page-chapter summary'>";
foreach ($this->childs as $node) {
$str .= $node->getSummaryHtml();
}
if ($this->parent != null) $str .= "</ul></li>";
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;
}
} }

View File

@ -71,4 +71,17 @@ class Page {
return $this->parent; return $this->parent;
} }
public function getSummaryHtml (): string {
$str = "<li class='page-contented chapter" . (PageMeta::$page->getId()==$this->id?" active":"") . "'><a class='page-contented' href='/" . (PageMeta::$page->getId()==$this->id?"#":(PageMeta::$book->getId()."/".$this->id)) . "'>$this->name</a>";
if (sizeof($this->segues) > 0) {
$str .= "<ul class='page-contented summary'>";
foreach ($this->segues as $node) {
$str .= $node->getSummaryHtml();
}
$str .= "</ul>";
}
$str .= "</li>";
return $str;
}
} }

View File

@ -49,4 +49,8 @@ class Segment {
return $this->parent; return $this->parent;
} }
public function getSummaryHtml (): string {
return "<li class='page-segment chapter'><a href='/" . (PageMeta::$page->getId()==$this->parent->getId()?"":PageMeta::$book->getId()."/".$this->parent->getId()."") . "#$this->id'>$this->name</a></li>";
}
} }

View File

@ -75,9 +75,15 @@ class Bookshelf {
$str .= $this->links->getHtml(); $str .= $this->links->getHtml();
$str .= "<li class='divider'></li>"; $str .= "<li class='divider'></li>";
$str .= $this->books->getHtml(); $str .= $this->books->getHtml();
$str .= "<li class='divider'></li>";
$str .= PageMeta::$book->getSummaryHtml();
$str .= "<li class='divider'></li><li><a href='https://github.com/Eyre-S/ph-Bookshelf' target='blank' class='gitbook-link'>Generated with ph-Bookshelf</a></li><li><a href='https://www.gitbook.com' target='blank' class='gitbook-link'>Front-End by GitBook</a></li>";
$str .= "</ul>"; $str .= "</ul>";
// TODO books list
return $str; return $str;
} }
public function getBook (string $id): ?Book {
return $this->books->getBook($id);
}
} }