diff --git a/src/Web/Html/Body.php b/src/Web/Html/Body.php index 8113c31..9e41ec4 100644 --- a/src/Web/Html/Body.php +++ b/src/Web/Html/Body.php @@ -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; } diff --git a/src/Web/Html/Head.php b/src/Web/Html/Head.php index af1611e..457ac05 100644 --- a/src/Web/Html/Head.php +++ b/src/Web/Html/Head.php @@ -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 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; } diff --git a/src/Web/HtmlPage.php b/src/Web/HtmlPage.php index 207c124..ee9c3f8 100644 --- a/src/Web/HtmlPage.php +++ b/src/Web/HtmlPage.php @@ -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; } diff --git a/src/Web/WebResManager.php b/src/Web/WebResManager.php deleted file mode 100644 index 8a61797..0000000 --- a/src/Web/WebResManager.php +++ /dev/null @@ -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)), - ); - } - -} diff --git a/src/Web/WebResource/IWebResource.php b/src/Web/WebResource/IWebResource.php index 2403347..4c12c2a 100644 --- a/src/Web/WebResource/IWebResource.php +++ b/src/Web/WebResource/IWebResource.php @@ -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; + } } \ No newline at end of file diff --git a/src/Web/WebResource/JavascriptRaw.php b/src/Web/WebResource/JavascriptRaw.php index 2c798f9..87af5e9 100644 --- a/src/Web/WebResource/JavascriptRaw.php +++ b/src/Web/WebResource/JavascriptRaw.php @@ -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; diff --git a/src/Web/WebResource/JavascriptRef.php b/src/Web/WebResource/JavascriptRef.php index 18c2040..dde85b2 100644 --- a/src/Web/WebResource/JavascriptRef.php +++ b/src/Web/WebResource/JavascriptRef.php @@ -6,7 +6,7 @@ use DOMElement; use DOMDocument; use SukWs\Bookshelf\Utils\DOMHtml; -class JavascriptRef implements IWebResource { +class JavascriptRef extends IWebResource { public string $uri; diff --git a/src/Web/WebResource/StylesheetHighlightjsTheme.php b/src/Web/WebResource/StylesheetHighlightjsTheme.php new file mode 100644 index 0000000..1380ef8 --- /dev/null +++ b/src/Web/WebResource/StylesheetHighlightjsTheme.php @@ -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); + } + +} \ No newline at end of file diff --git a/src/Web/WebResource/StylesheetsRef.php b/src/Web/WebResource/StylesheetsRef.php index 1b4ea2b..01e6727 100644 --- a/src/Web/WebResource/StylesheetsRef.php +++ b/src/Web/WebResource/StylesheetsRef.php @@ -6,7 +6,7 @@ use DOMDocument; use DOMElement; use SukWs\Bookshelf\Utils\DOMHtml; -class StylesheetsRef implements IWebResource { +class StylesheetsRef extends IWebResource { public string $uri; diff --git a/src/Web/WebResource/WebResManager.php b/src/Web/WebResource/WebResManager.php new file mode 100644 index 0000000..823447b --- /dev/null +++ b/src/Web/WebResource/WebResManager.php @@ -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)), + ); + } + +}