1
0
mirror of https://github.com/suk-ws/ph-Bookshelf.git synced 2025-01-18 23:12:23 +08:00

add html title support and extra css/js obj.

This commit is contained in:
A.C.Sukazyo Eyre 2023-03-13 12:04:55 +08:00
parent 0d6672f07f
commit 9cae04526c
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
10 changed files with 193 additions and 86 deletions

View File

@ -38,12 +38,9 @@ class Body {
$this->__self->appendChild($this->_sidebar->build());
$this->__self->appendChild($this->_main->build());
foreach ($this->root->res_manager->getJavascriptLazyloadsDOM($this->root->document) as $script)
foreach ($this->root->res_manager->build_javascriptLazyloadsDOM($this->root->document) as $script)
$this->__self->appendChild($script);
// output the warnings message at the end.
$this->__self->appendChild(WebLog::getWarningsAsJsLog()->build($this->root->document));
return $this->__self;
}

View File

@ -6,6 +6,7 @@ use DOMElement;
use SukWs\Bookshelf\Data\Bookshelf\NodeBookshelf;
use SukWs\Bookshelf\Utils\DOMHtml;
use SukWs\Bookshelf\Web\HtmlPage;
use SukWs\Bookshelf\Web\WebLog;
class Head {
@ -15,6 +16,10 @@ class Head {
public readonly array $standard_headers;
public ?string $site_title = null;
public ?string $page_title = null;
public string $separator = " - ";
public function __construct (HtmlPage $root) {
$this->root = $root;
@ -34,7 +39,20 @@ class Head {
}
public function _parseBookshelf (NodeBookshelf $_data_shelf): void {
// todo use shelf config
$this->site_title = $_data_shelf->_site_name;
WebLog::info("set html <title> site name as \"".$this->site_title."\"");
}
public function build_html_title (): string {
if ($this->site_title == null) {
WebLog::error("Header Title cannot be set for site-title is not set.");
return "...";
}
$title = "";
if ($this->page_title != null)
$title .= $this->page_title . $this->separator;
$title .= $this->site_title;
return $title;
}
public function build (): DOMElement {
@ -42,12 +60,15 @@ class Head {
foreach ($this->standard_headers as $meta)
$this->__self->appendChild($meta);
// todo maybe css js manager?
foreach ($this->root->res_manager->getStylesheetsDOM($this->root->document) as $style)
foreach ($this->root->res_manager->build_stylesheetsDOM($this->root->document) as $style)
$this->__self->appendChild($style);
foreach ($this->root->res_manager->getJavascriptPreloadsDOM($this->root->document) as $script)
foreach ($this->root->res_manager->build_javascriptPreloadsDOM($this->root->document) as $script)
$this->__self->appendChild($script);
$title_dom = $this->root->document->createElement('title');
$title_dom->appendChild($this->root->document->createTextNode($this->build_html_title()));
$this->__self->appendChild($title_dom);
return $this->__self;
}

View File

@ -8,6 +8,7 @@ use SukWs\Bookshelf\Data\Bookshelf\NodeBookshelf;
use SukWs\Bookshelf\Utils\DOMHtml;
use SukWs\Bookshelf\Web\Html\Body;
use SukWs\Bookshelf\Web\Html\Head;
use SukWs\Bookshelf\Web\WebResource\WebResManager;
class HtmlPage {
@ -45,6 +46,10 @@ class HtmlPage {
public function build(): self {
$this->_html_html->appendChild($this->_html_head->build());
$this->_html_html->appendChild($this->_html_body->build());
// output the warnings message at the end.
$this->_html_html->appendChild(WebLog::getWarningsAsJsLog()->build($this->document));
return $this;
}

View File

@ -1,73 +0,0 @@
<?php
namespace SukWs\Bookshelf\Web;
use DOMDocument;
use DOMElement;
use SukWs\Bookshelf\Utils\Resources;
use SukWs\Bookshelf\Web\WebResource\JavascriptRaw;
use SukWs\Bookshelf\Web\WebResource\JavascriptRef;
use SukWs\Bookshelf\Web\WebResource\StylesheetsRef;
class WebResManager {
public HtmlPage $root;
/* @var StylesheetsRef[] */ public array $stylesheets;
/* @var JavascriptRef|JavascriptRaw[] */ public array $javascript_preload;
/* @var JavascriptRef|JavascriptRaw[] */ public array $javascript_lazyload;
public function __construct() {
$this->stylesheets = self::getBasicCssList();
$this->javascript_preload = array();
$this->javascript_lazyload = self::getBasicJsLazyloadList();
}
/** @return DOMElement[] */
public function getStylesheetsDOM (DOMDocument $root): array {
$return = array();
foreach ($this->stylesheets as $item) {
$return[] = $item->build($root);
}
return $return;
}
/** @return DOMElement[] */
public function getJavascriptPreloadsDOM (DOMDocument $root): array {
$return = array();
foreach ($this->javascript_preload as $item) {
$return[] = $item->build($root);
}
return $return;
}
/** @return DOMElement[] */
public function getJavascriptLazyloadsDOM (DOMDocument $root): array {
$return = array();
foreach ($this->javascript_lazyload as $item) {
$return[] = $item->build($root);
}
return $return;
}
/** @return StylesheetsRef[] */
public static function getBasicCssList (): array {
return array(
new StylesheetsRef(Resources::webResPath('css', "main", 1)),
new StylesheetsRef(Resources::webResPath('css', "bread-card-markdown", 1)),
new StylesheetsRef(Resources::webResPath('css', "bread-card-markdown-footnote", 1)),
new StylesheetsRef(Resources::webResPath('css', "bread-card-markdown-task-list", 1)),
new StylesheetsRef(Resources::webResPath('css', "bread-card-markdown-heading-permalink", 1)),
new StylesheetsRef(Resources::webResPath('css', "bread-card-markdown-compat-highlight-js", 1)),
);
}
/** @return JavascriptRef[] */
public static function getBasicJsLazyloadList (): array {
return array(
new JavascriptRef(Resources::webResPath('js', "lib/utils-touchscreen-event", 1)),
new JavascriptRef(Resources::webResPath('js', "main", 1)),
);
}
}

View File

@ -5,8 +5,21 @@ namespace SukWs\Bookshelf\Web\WebResource;
use DOMDocument;
use DOMElement;
interface IWebResource {
abstract class IWebResource {
public function build (DOMDocument $root): DOMElement;
public bool $enabled = true;
abstract public function build (DOMDocument $root): DOMElement;
public function toggleEnable (?bool $is = null): IWebResource {
if ($is === null)
$is = !$this->enabled;
$this->enabled = $is;
return $this;
}
public function isEnabled (): bool {
return $this->enabled;
}
}

View File

@ -5,7 +5,7 @@ namespace SukWs\Bookshelf\Web\WebResource;
use DOMDocument;
use DOMElement;
class JavascriptRaw implements IWebResource {
class JavascriptRaw extends IWebResource {
public string $javascript_raw_code;

View File

@ -6,7 +6,7 @@ use DOMElement;
use DOMDocument;
use SukWs\Bookshelf\Utils\DOMHtml;
class JavascriptRef implements IWebResource {
class JavascriptRef extends IWebResource {
public string $uri;

View File

@ -0,0 +1,26 @@
<?php
namespace SukWs\Bookshelf\Web\WebResource;
class StylesheetHighlightjsTheme extends StylesheetsRef {
public const PATH_BASE = "//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/{{theme}}.min.js";
public string $theme;
public function __construct (string $theme) {
parent::__construct(self::themePath($theme));
$this->theme = $theme;
}
public function setTheme (string $theme): StylesheetHighlightjsTheme {
$this->uri = self::themePath($theme);
$this->theme = $theme;
return $this;
}
public static function themePath (string $theme): string {
return str_replace("{{theme}}", $theme, self::PATH_BASE);
}
}

View File

@ -6,7 +6,7 @@ use DOMDocument;
use DOMElement;
use SukWs\Bookshelf\Utils\DOMHtml;
class StylesheetsRef implements IWebResource {
class StylesheetsRef extends IWebResource {
public string $uri;

View File

@ -0,0 +1,118 @@
<?php
namespace SukWs\Bookshelf\Web\WebResource;
use DOMDocument;
use DOMElement;
use SukWs\Bookshelf\Utils\Resources;
use SukWs\Bookshelf\Web\HtmlPage;
class WebResManager {
public HtmlPage $root;
/* @var StylesheetsRef[] */ public array $stylesheets;
/* @var JavascriptRef|JavascriptRaw[] */ public array $javascript_preload;
/* @var JavascriptRef|JavascriptRaw[] */ public array $javascript_lazyload;
public readonly StylesheetHighlightjsTheme $_css_highlightjs;
public readonly StylesheetsRef $_css_regex_highlight;
public readonly StylesheetsRef $_css_ext_listing_rainbow;
public readonly JavascriptRef $_js_highlightjs;
public readonly JavascriptRef $_js_regex_highlight;
public readonly JavascriptRef $_js_ext_rolling_title;
public readonly JavascriptRef $_js_ext_title_permalink_flash;
public function __construct() {
$stylesheets = self::getBasicCssList();
$javascript_preload = array();
$javascript_lazyload = self::getBasicJsLazyloadList();
$this->_css_highlightjs =
(new StylesheetHighlightjsTheme("atom-one-dark"))
->toggleEnable(true);
$this->_js_highlightjs =
(new JavascriptRef("//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"))
->toggleEnable(true);
$stylesheets[] = $this->_css_highlightjs;
$javascript_lazyload[] = $this->_js_highlightjs;
$this->_css_regex_highlight =
(new StylesheetsRef("//cdn.jsdelivr.net/gh/suk-ws/regex-colorizer@master/regex-colorizer-default.min.css"))
->toggleEnable(true);
$this->_js_regex_highlight =
(new JavascriptRef("//cdn.jsdelivr.net/gh/suk-ws/regex-colorizer@master/regex-colorizer.min.js"))
->toggleEnable(true);
$stylesheets[] = $this->_css_highlightjs;
$javascript_lazyload[] = $this->_js_regex_highlight;
$this->_css_ext_listing_rainbow =
(new StylesheetsRef(Resources::webResPath('css', 'bread-card-markdown-enhanced-listing-rainbow', 1)))
->toggleEnable(false);
$stylesheets[] = $this->_css_ext_listing_rainbow;
$this->_js_ext_rolling_title =
(new JavascriptRef(Resources::webResPath('js', "enhanced-rolling-title", 1)))
->toggleEnable(false);
$javascript_lazyload[] = $this->_js_ext_rolling_title;
$this->_js_ext_title_permalink_flash =
(new JavascriptRef(Resources::webResPath('js', 'bread-card-markdown-heading-permalink-highlight', 1)))
->toggleEnable(false);
$this->javascript_lazyload[] = $this->_js_ext_title_permalink_flash;
$this->stylesheets = $stylesheets;
$this->javascript_preload = $javascript_preload;
$this->javascript_lazyload = $javascript_lazyload;
}
/** @return DOMElement[] */
public function build_stylesheetsDOM (DOMDocument $root): array {
$return = array();
foreach ($this->stylesheets as $item) {
if ($item->enabled) $return[] = $item->build($root);
}
return $return;
}
/** @return DOMElement[] */
public function build_javascriptPreloadsDOM (DOMDocument $root): array {
$return = array();
foreach ($this->javascript_preload as $item) {
if ($item->enabled) $return[] = $item->build($root);
}
return $return;
}
/** @return DOMElement[] */
public function build_javascriptLazyloadsDOM (DOMDocument $root): array {
$return = array();
foreach ($this->javascript_lazyload as $item) {
if ($item->enabled) $return[] = $item->build($root);
}
return $return;
}
/** @return StylesheetsRef[] */
public static function getBasicCssList (): array {
return array(
new StylesheetsRef(Resources::webResPath('css', "main", 1)),
new StylesheetsRef(Resources::webResPath('css', "bread-card-markdown", 1)),
new StylesheetsRef(Resources::webResPath('css', "bread-card-markdown-footnote", 1)),
new StylesheetsRef(Resources::webResPath('css', "bread-card-markdown-task-list", 1)),
new StylesheetsRef(Resources::webResPath('css', "bread-card-markdown-heading-permalink", 1)),
new StylesheetsRef(Resources::webResPath('css', "bread-card-markdown-compat-highlight-js", 1)),
);
}
/** @return JavascriptRef[] */
public static function getBasicJsLazyloadList (): array {
return array(
new JavascriptRef(Resources::webResPath('js', "lib/utils-touchscreen-event", 1)),
new JavascriptRef(Resources::webResPath('js', "main", 1)),
);
}
}