1
0
mirror of https://github.com/suk-ws/ph-Bookshelf.git synced 2025-01-19 07:22:26 +08:00

add support for book.xml config version 2.0

This commit is contained in:
A.C.Sukazyo Eyre 2023-02-26 22:23:45 +08:00
parent 2fcdeafc72
commit 2a72004b7b
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
8 changed files with 56 additions and 45 deletions

View File

@ -49,6 +49,7 @@ try {
$tmp = SiteMeta::getBookshelf()->getBook($uri[0]); $tmp = SiteMeta::getBookshelf()->getBook($uri[0]);
if ($tmp == null) if ($tmp == null)
throw new RequestNotExistException("Book required \"$uri[0]\" not found!"); throw new RequestNotExistException("Book required \"$uri[0]\" not found!");
PageMeta::$bookId = $uri[0];
PageMeta::$book = $tmp->getContentedNode(); PageMeta::$book = $tmp->getContentedNode();
} }
@ -58,12 +59,13 @@ try {
if ($tmp == null) throw new RequestNotExistException("Page required \"$uri[1]\" not found on book \"$uri[0]\"!"); if ($tmp == null) throw new RequestNotExistException("Page required \"$uri[1]\" not found on book \"$uri[0]\"!");
PageMeta::$page = $tmp; PageMeta::$page = $tmp;
} else { } else {
PageMeta::$page = PageMeta::$book->getChilds()->getChildren()[0]; PageMeta::$page = PageMeta::$book->getChildren()->getChildren()[0];
} }
} else { } else {
// 主页面 // 主页面
PageMeta::$bookId = "%root";
PageMeta::$book = SiteMeta::getBookshelf()->getRootBook(); PageMeta::$book = SiteMeta::getBookshelf()->getRootBook();
PageMeta::$page = PageMeta::$book->getChilds()->getChildren()[0]; PageMeta::$page = PageMeta::$book->getChildren()->getChildren()[0];
PageMeta::$isMainPage = true; PageMeta::$isMainPage = true;
} }

View File

@ -8,6 +8,7 @@ use SukWs\Bookshelf\Element\BookContent\Page;
class PageMeta { class PageMeta {
public static string $bookId;
public static BookContented $book; public static BookContented $book;
public static Page $page; public static Page $page;
public static bool $isMainPage = false; public static bool $isMainPage = false;

View File

@ -66,8 +66,8 @@ class Book {
%s href='%s'" . ">%s</a> %s href='%s'" . ">%s</a>
EOF, EOF,
str_repeat("\t", $indent), $this->id, $this->id, str_repeat("\t", $indent), $this->id, $this->id,
str_repeat("\t", $indent), PageMeta::$book->getId()==$this->id ? " current" : "", str_repeat("\t", $indent), PageMeta::$bookId==$this->id ? " current" : "",
str_repeat("\t", $indent), PageMeta::$book->getId()==$this->id ? "javascript:void(0)" : $this->encodeUrl(), $this->name str_repeat("\t", $indent), PageMeta::$bookId==$this->id ? "javascript:void(0)" : $this->encodeUrl(), $this->name
); );
} }

View File

@ -4,44 +4,49 @@ namespace SukWs\Bookshelf\Element\BookContent;
use DOMDocument; use DOMDocument;
use DOMNode; use DOMNode;
use SukWs\Bookshelf\Element\Bookshelf;
use Exception; use Exception;
class BookContented { class BookContented {
private string $id; private string $configVersion;
private string $name; private string $name;
private array $configurations = array(); private array $configurations = array();
public function __construct (string $id, string $name) { public function __construct (string $configVersion) {
$this->id = $id; $this->configVersion = $configVersion;
$this->name = $name;
} }
private Chapter $childs; private Chapter $children;
/** /**
* @param DOMNode $xmlData * @param DOMNode $nodeBook
* @return BookContented * @return BookContented
* @throws Exception * @throws Exception
*/ */
public static function parse (DOMNode $xmlData): BookContented { public static function parse (DOMNode $nodeBook): BookContented {
if ($xmlData->hasAttributes() && $xmlData->hasChildNodes()) { $attrVersion = $nodeBook->attributes->getNamedItem("version");
$attrName = $xmlData->attributes->getNamedItem("name"); $bookConfigVersion = $attrVersion?->nodeValue;
$attrId = $xmlData->attributes->getNamedItem("id"); $return = new BookContented($bookConfigVersion);
if ($attrName == null) for ($child = $nodeBook->firstChild; $child != null; $child = $child->nextSibling) {
if ($attrId == null) throw new Exception("BookWithContent xml data missing attribute \"name\""); switch ($child->nodeName) {
else throw new Exception("BookWithContent xml data with id \"$attrId->nodeValue\" missing attribute \"name\""); case "book_name":
else $name = $attrName->nodeValue; if (!empty($return->name)) throw new Exception("Duplicated contents in Book.xml");
if ($attrId == null) throw new Exception("BookWithContent xml data named \"$name\" missing attribute \"id\""); $return->name = $child->nodeValue;
else $id = $attrId->nodeValue; break;
$node = new BookContented($id, $name); case "contents":
$node->childs = Chapter::parse($xmlData, null); if (!empty($return->children)) throw new Exception("Duplicated contents in Book.xml");
Bookshelf::parseConfigurationAttr($xmlData->attributes, $node->configurations, array("name", "id")); $return->children = Chapter::parse($child, null);
} else break;
throw new Exception("No child or attribute found on BookWithContent"); case "#comment":
return $node; case "#text":
if (empty(trim($child->nodeValue))) break;
default:
throw new Exception("Book.xml has sub-node \"$child->nodeName\" with is not supported.");
}
}
return $return;
} }
/** /**
@ -49,9 +54,10 @@ class BookContented {
* @return BookContented * @return BookContented
* @throws Exception * @throws Exception
*/ */
public static function parseRootBook (DOMNode $rootBookNode): BookContented { public static function parseRootBook (DOMNode $rootBookNode, string $bookName): BookContented {
$return = new BookContented("%root", ""); $return = new BookContented("2.0");
$return->childs = Chapter::parse($rootBookNode, null); $return->name = $bookName;
$return->children = Chapter::parse($rootBookNode, null);
return $return; return $return;
} }
@ -69,24 +75,24 @@ class BookContented {
} }
public function getId (): string { public function getConfigVersion (): string {
return $this->id; return $this->configVersion;
} }
public function getName (): string { public function getName (): string {
return $this->name; return $this->name;
} }
public function getChilds (): Chapter { public function getChildren (): Chapter {
return $this->childs; return $this->children;
} }
public function getSummaryHtml (): string { public function getSummaryHtml (): string {
return $this->childs->getSummaryHtml(); return $this->children->getSummaryHtml();
} }
public function getPage (string $id): ?Page { public function getPage (string $id): ?Page {
return $this->childs->getPage($id); return $this->children->getPage($id);
} }
public function getConfiguration (string $key): ?string { public function getConfiguration (string $key): ?string {

View File

@ -27,7 +27,7 @@ class Chapter {
* @return Chapter * @return Chapter
* @throws Exception * @throws Exception
*/ */
public static function parse (DOMNode $xmlData, ?Chapter $parent): Chapter { public static function parse (DOMNode $xmlData, ?Chapter $parent, ?string $idRoot = null): Chapter {
$child = $xmlData->firstChild; $child = $xmlData->firstChild;
if ($parent != null) { if ($parent != null) {
while ($child->nodeName != "caption") { while ($child->nodeName != "caption") {
@ -44,13 +44,15 @@ class Chapter {
$node = new Chapter($child->nodeValue, array(), $parent); $node = new Chapter($child->nodeValue, array(), $parent);
$child = $child->nextSibling; $child = $child->nextSibling;
} else $node = new Chapter("", array(), $parent); } else $node = new Chapter("", array(), $parent);
$attrRoot = $xmlData->attributes->getNamedItem("root");
$chapterIdRoot = $idRoot . $attrRoot?->nodeValue;
for (;$child != null ; $child = $child->nextSibling) { for (;$child != null ; $child = $child->nextSibling) {
switch ($child->nodeName) { switch ($child->nodeName) {
case "Page": case "Page":
$node->children[] = Page::parse($child, $node); $node->children[] = Page::parse($child, $node, $chapterIdRoot);
break; break;
case "Chapter": case "Chapter":
$node->children[] = self::parse($child, $node); $node->children[] = self::parse($child, $node, $chapterIdRoot);
break; break;
case "Separator": case "Separator":
$node->children[] = Separator::parse($child, $node); $node->children[] = Separator::parse($child, $node);

View File

@ -28,11 +28,11 @@ class Page {
* @return Page * @return Page
* @throws Exception * @throws Exception
*/ */
public static function parse (DOMNode $pageNode, Chapter $parent): Page { public static function parse (DOMNode $pageNode, Chapter $parent, ?string $idRoot): Page {
if ($pageNode->hasAttributes()) { if ($pageNode->hasAttributes()) {
$attrId = $pageNode->attributes->getNamedItem("id"); $attrId = $pageNode->attributes->getNamedItem("id");
if ($attrId == null) throw new Exception("an Page xml data missing attribute \"id\""); if ($attrId == null) throw new Exception("an Page xml data missing attribute \"id\"");
else $id = $attrId->nodeValue; else $id = $idRoot . $attrId->nodeValue;
} else } else
throw new Exception("Book xml data missing attributes"); throw new Exception("Book xml data missing attributes");
return new Page($id, $pageNode->nodeValue, $parent); return new Page($id, $pageNode->nodeValue, $parent);
@ -89,12 +89,12 @@ class Page {
public function encodeUrl (): string { public function encodeUrl (): string {
return str_replace( return str_replace(
"%2F", "/", "%2F", "/",
sprintf("/%s/%s", urlencode(PageMeta::$book->getId()), urlencode($this->id)) sprintf("/%s/%s", urlencode(PageMeta::$bookId), urlencode($this->id))
); );
} }
public function getContentFilename (string $type): string { public function getContentFilename (string $type): string {
return sprintf("./data/%s/%s.%s", PageMeta::$book->getId(), $this->id, $type); return sprintf("./data/%s/%s.%s", PageMeta::$bookId, $this->id, $type);
} }
public function hasContent (string $type): string { public function hasContent (string $type): string {

View File

@ -48,7 +48,7 @@ class Bookshelf {
$return->books = BookCollection::parse($rc, null, true); $return->books = BookCollection::parse($rc, null, true);
break; break;
case "root_book": case "root_book":
$return->rootBook = BookContented::parseRootBook($rc); $return->rootBook = BookContented::parseRootBook($rc, $return->siteName);
break; break;
case "configurations": case "configurations":
self::parseConfiguration($rc, $return->configurations); self::parseConfiguration($rc, $return->configurations);

View File

@ -12,7 +12,7 @@
?> ?>
<script><?= SiteMeta::getCustomScriptContent("custom") ?></script> <script><?= SiteMeta::getCustomScriptContent("custom") ?></script>
<script> <script>
bookCurrentId = "<?= PageMeta::$book->getId() ?>"; bookCurrentId = "<?= PageMeta::$bookId ?>";
pageCurrentId = "<?= PageMeta::$page->getId() ?>"; pageCurrentId = "<?= PageMeta::$page->getId() ?>";
<?php if (!(PageMeta::getConfigurationLevelPage(ConfigName::highlightjs)=="false")) : <?php if (!(PageMeta::getConfigurationLevelPage(ConfigName::highlightjs)=="false")) :
?>hljs.highlightAll();<?php endif; ?> ?>hljs.highlightAll();<?php endif; ?>