1
0
mirror of https://github.com/suk-ws/ph-Bookshelf.git synced 2024-12-05 01:16:53 +08:00

使子元素维护其父元素的引用,修复资源文件位置书写的错误

This commit is contained in:
A.C.Sukazyo Eyre 2021-03-23 01:57:53 +08:00
parent fa218dc2b0
commit 0d34dfb04e
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
10 changed files with 140 additions and 45 deletions

View File

@ -23,23 +23,26 @@ class SiteMeta {
public static function getGitbookStylesheetsList (): array {
return array(
"gitbook/style.css",
"gitbook/gitbook-plugin-highlight/website.css",
"gitbook/gitbook-plugin-search/search.css",
"gitbook/gitbook-plugin-fontsettings/website.css"
"/assets/gitbook/style.css",
"/assets/gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css",
"/assets/gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css",
"/assets/gitbook/gitbook-plugin-highlight/website.css",
"/assets/gitbook/gitbook-plugin-search/search.css",
"/assets/gitbook/gitbook-plugin-fontsettings/website.css"
);
}
public static function getGitbookJavascriptList (): array {
return array(
"gitbook/gitbook.js",
"gitbook/theme.js",
"gitbook/gitbook-plugin-search/search-engine.js",
"gitbook/gitbook-plugin-search/search.js",
"gitbook/gitbook-plugin-lunr/lunr.min.js",
"gitbook/gitbook-plugin-lunr/search-lunr.js",
"gitbook/gitbook-plugin-sharing/buttons.js",
"gitbook/gitbook-plugin-fontsettings/fontsettings.js"
"/assets/gitbook/gitbook.js",
"/assets/gitbook/theme.js",
"/assets/gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js",
"/assets/gitbook/gitbook-plugin-search/search-engine.js",
"/assets/gitbook/gitbook-plugin-search/search.js",
"/assets/gitbook/gitbook-plugin-lunr/lunr.min.js",
"/assets/gitbook/gitbook-plugin-lunr/search-lunr.js",
"/assets/gitbook/gitbook-plugin-sharing/buttons.js",
"/assets/gitbook/gitbook-plugin-fontsettings/fontsettings.js"
);
}

View File

@ -1,21 +1,26 @@
<?php
require_once "./src/Element/BookCollection.php";
class Book {
private string $id;
private string $name;
private BookCollection $parent;
public function __construct (string $id, string $name) {
public function __construct (string $id, string $name, BookCollection $parent) {
$this->id = $id;
$this->name = $name;
$this->parent = $parent;
}
/**
* @param DOMNode $xmlData
* @param BookCollection $parent
* @return Book
* @throws Exception
*/
public static function parse (DOMNode $xmlData): Book {
public static function parse (DOMNode $xmlData, BookCollection $parent): Book {
if ($xmlData->hasAttributes()) {
$attrName = $xmlData->attributes->getNamedItem("name");
$attrId = $xmlData->attributes->getNamedItem("id");
@ -29,7 +34,7 @@ class Book {
throw new Exception("Book xml data missing attributes");
if ($xmlData->hasChildNodes())
throw new Exception("Book xml with id \"$id\" have some children which are not supported");
return new Book($id, $name);
return new Book($id, $name, $parent);
}
public function getId (): string {
@ -40,4 +45,11 @@ class Book {
return $this->name;
}
/**
* @return BookCollection|null
*/
public function getParent (): BookCollection {
return $this->parent;
}
}

View File

@ -10,19 +10,22 @@ class BookCollection {
/** @var Book[]|BookCollection[] */
private array $array;
private BookCollection $parent;
private function __construct (string $name, array $a) {
private function __construct (string $name, array $a, BookCollection $parent) {
$this->name = $name;
$this->array = $a;
$this->parent = $parent;
}
/**
* @param DOMNode $root
* @param BookCollection $parent
* @param bool $isRoot
* @return BookCollection
* @throws Exception
*/
public static function parse (DOMNode $root, bool $isRoot = false): BookCollection {
public static function parse (DOMNode $root, BookCollection $parent, bool $isRoot = false): BookCollection {
$name = BookCollection::ROOT;
if (!$isRoot) {
if ($root->hasAttributes()) {
@ -31,14 +34,14 @@ class BookCollection {
else $name = $attrName->nodeValue;
} else throw new Exception("BookCollection (not root) xml data missing attributes");
}
$node = new BookCollection($name, array());
$node = new BookCollection($name, array(), $parent);
for ($child = $root->firstChild; $child != null; $child = $child->nextSibling) {
switch ($child->nodeName) {
case "Book":
array_push($node->array, Book::parse($child));
array_push($node->array, Book::parse($child, $node));
break;
case "Collection":
array_push($node->array, BookCollection::parse($child));
array_push($node->array, BookCollection::parse($child, $node));
break;
case "#text":
break;
@ -60,4 +63,11 @@ class BookCollection {
return $this->array;
}
/**
* @return BookCollection|null
*/
public function getParent (): BookCollection {
return $this->parent;
}
}

View File

@ -3,10 +3,23 @@
require_once "./src/Element/Book.php";
require_once "./src/Element/BookContent/Chapter.php";
class BookContented extends Book {
class BookContented {
private string $id;
private string $name;
public function __construct (string $id, string $name) {
$this->id = $id;
$this->name = $name;
}
private Chapter $childs;
/**
* @param DOMNode $xmlData
* @return BookContented
* @throws Exception
*/
public static function parse (DOMNode $xmlData): BookContented {
if ($xmlData->hasAttributes() && $xmlData->hasChildNodes()) {
$attrName = $xmlData->attributes->getNamedItem("name");
@ -18,7 +31,7 @@ class BookContented extends Book {
if ($attrId == null) throw new Exception("BookWithContent xml data named \"$name\" missing attribute \"id\"");
else $id = $attrId->nodeValue;
$node = new BookContented($id, $name);
$node->childs = Chapter::parse($xmlData);
$node->childs = Chapter::parse($xmlData, null);
} else
throw new Exception("No child or attribute found on BookWithContent");
return $node;
@ -38,6 +51,14 @@ class BookContented extends Book {
}
public function getId (): string {
return $this->id;
}
public function getName (): string {
return $this->name;
}
public function getChilds (): Chapter {
return $this->childs;
}

View File

@ -9,29 +9,33 @@ class Chapter {
/** @var Chapter[]|Page[] */
private array $childs;
private function __construct (string $name, array $array) {
private Chapter $parent;
private function __construct (string $name, array $array, Chapter $parent) {
$this->name = $name;
$this->childs = $array;
$this->parent = $parent;
}
/**
* @param DOMNode $xmlData
* @param Chapter $parent
* @return Chapter
* @throws Exception
*/
public static function parse (DOMNode $xmlData): Chapter {
public static function parse (DOMNode $xmlData, Chapter $parent): Chapter {
if ($xmlData->hasAttributes()) {
$attrName = $xmlData->attributes->getNamedItem("name");
if ($attrName == null) throw new Exception("Chapter xml data missing attribute \"name\"");
else $node = new Chapter($xmlData->attributes->getNamedItem("name")->nodeValue, array());
else $node = new Chapter($xmlData->attributes->getNamedItem("name")->nodeValue, array(), $parent);
} else throw new Exception("Chapter xml data missing attributes");
for ($child = $xmlData->firstChild;$child != null ; $child = $child->nextSibling) {
switch ($child->nodeName) {
case "Page":
array_push($node->childs, Page::parse($child));
array_push($node->childs, Page::parse($child, $node));
break;
case "Chapter":
array_push($node->childs, self::parse($child));
array_push($node->childs, self::parse($child, $node));
break;
case "#text":
break;
@ -53,4 +57,11 @@ class Chapter {
return $this->childs;
}
/**
* @return Chapter|null
*/
public function getParent (): Chapter {
return $this->parent;
}
}

View File

@ -1,5 +1,6 @@
<?php
require_once "./src/Element/BookContent/Chapter.php";
require_once "./src/Element/BookContent/Segment.php";
class Page {
@ -9,19 +10,22 @@ class Page {
/** @var Segment[] */
private array $segues;
private Chapter $parent;
public function __construct (string $id, string $name, array $childs = array()) {
public function __construct (string $id, string $name, Chapter $parent, array $childs = array()) {
$this->id = $id;
$this->name = $name;
$this->parent = $parent;
$this->segues = $childs;
}
/**
* @param DOMNode $xmlData
* @param Chapter $parent
* @return Page
* @throws Exception
*/
public static function parse (DOMNode $xmlData): Page {
public static function parse (DOMNode $xmlData, Chapter $parent): Page {
if ($xmlData->hasAttributes()) {
$attrName = $xmlData->attributes->getNamedItem("name");
$attrId = $xmlData->attributes->getNamedItem("id");
@ -31,13 +35,13 @@ class Page {
else $name = $attrName->nodeValue;
if ($attrId == null) throw new Exception("Page xml data named \"$name\" missing attribute \"id\"");
else $id = $attrId->nodeValue;
$node = new Page($id, $name);
$node = new Page($id, $name, $parent);
} else
throw new Exception("Book xml data missing attributes");
for ($child = $xmlData->firstChild;$child != null ; $child = $child->nextSibling) {
switch ($child->nodeName) {
case "Segment":
array_push($node->segues, Segment::parse($child));
array_push($node->segues, Segment::parse($child, $node));
break;
case "#text":
break;
@ -63,4 +67,8 @@ class Page {
return $this->segues;
}
public function getParent (): Chapter {
return $this->parent;
}
}

View File

@ -1,21 +1,26 @@
<?php
require_once "./src/Element/BookContent/Page.php";
class Segment {
private string $id;
private string $name;
private Page $parent;
public function __construct ($id, $name) {
public function __construct (string $id, string $name, Page $parent) {
$this->id = $id;
$this->name = $name;
$this->parent = $parent;
}
/**
* @param DOMNode $xmlData
* @param Page $parent
* @return Segment
* @throws Exception
*/
public static function parse (DOMNode $xmlData): Segment {
public static function parse (DOMNode $xmlData, Page $parent): Segment {
if ($xmlData->hasAttributes()) {
$attrName = $xmlData->attributes->getNamedItem("name");
$attrId = $xmlData->attributes->getNamedItem("id");
@ -29,7 +34,7 @@ class Segment {
throw new Exception("Segment xml data missing attributes");
if ($xmlData->hasChildNodes())
throw new Exception("Segment xml named \"$name\" have some children which are not supported");
return new Segment($id, $name);
return new Segment($id, $name, $parent);
}
public function getId (): string {
@ -40,4 +45,8 @@ class Segment {
return $this->name;
}
public function getParent (): Page {
return $this->parent;
}
}

View File

@ -34,10 +34,10 @@ class Bookshelf {
for ($rc = $dom->firstChild; $rc != null; $rc = $rc->nextSibling) {
switch ($rc->nodeName) {
case "links":
$return->links = LinkCollection::parse($rc, true);
$return->links = LinkCollection::parse($rc, null, true);
break;
case "books":
$return->books = BookCollection::parse($rc, true);
$return->books = BookCollection::parse($rc, null, true);
break;
case "rootBook":
$return->rootBook = BookContented::parse($rc);

View File

@ -1,22 +1,26 @@
<?php
require_once "./src/Element/LinkCollection.php";
class Link {
private string $name;
private string $href;
private LinkCollection $parent;
public function __construct (string $name, string $href) {
public function __construct (string $name, string $href, LinkCollection $parent) {
$this->name = $name;
$this->href = $href;
$this->parent = $parent;
}
/**
* @param DOMNode $xmlData
* @param LinkCollection $parent
* @return Link
* @throws Exception
*/
public static function parse (DOMNode $xmlData): Link {
public static function parse (DOMNode $xmlData, LinkCollection $parent): Link {
if ($xmlData->hasAttributes()) {
$attrName = $xmlData->attributes->getNamedItem("name");
$attrHref = $xmlData->attributes->getNamedItem("href");
@ -30,7 +34,7 @@ class Link {
throw new Exception("Link xml data missing attributes");
if ($xmlData->hasChildNodes())
throw new Exception("Link xml named \"$name\" have some children which are not supported");
return new Link($name, $href);
return new Link($name, $href, $parent);
}
public function getName (): string {
@ -41,4 +45,11 @@ class Link {
return $this->href;
}
/**
* @return LinkCollection|null
*/
public function getParent (): LinkCollection {
return $this->parent;
}
}

View File

@ -10,19 +10,22 @@ class LinkCollection {
/** @var Link[]|LinkCollection[] */
private array $array;
private LinkCollection $parent;
private function __construct (string $name, array $a) {
private function __construct (string $name, array $a, LinkCollection $parent) {
$this->name = $name;
$this->array = $a;
$this->parent = $parent;
}
/**
* @param DOMNode $root
* @param LinkCollection $parent
* @param bool $isRoot
* @return LinkCollection
* @throws Exception
*/
public static function parse (DOMNode $root, bool $isRoot = false): LinkCollection {
public static function parse (DOMNode $root, LinkCollection $parent, bool $isRoot = false): LinkCollection {
$name = LinkCollection::ROOT;
if (!$isRoot) {
if ($root->hasAttributes()) {
@ -31,14 +34,14 @@ class LinkCollection {
else $name = $attrName->nodeValue;
} else throw new Exception("LinkCollection (not root) xml data missing attributes");
}
$node = new LinkCollection($name, array());
$node = new LinkCollection($name, array(), $parent);
for ($child = $root->firstChild; $child != null; $child = $child->nextSibling) {
switch ($child->nodeName) {
case "Link":
array_push($node->array, Link::parse($child));
array_push($node->array, Link::parse($child, $node));
break;
case "Collection":
array_push($node->array, LinkCollection::parse($child));
array_push($node->array, LinkCollection::parse($child, $node));
break;
case "#text":
break;
@ -60,4 +63,11 @@ class LinkCollection {
return $this->array;
}
/**
* @return LinkCollection|null
*/
public function getParent (): LinkCollection {
return $this->parent;
}
}