From f79060f32f872aec7e6b5177dae065526cd80ef7 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 25 Mar 2023 20:11:10 +0800 Subject: [PATCH] add support for md front matter & open page unlisted - support page config define in front matter - support `title`, which can set HTML title. - if `title` is not set, then HTML title will try find page title defined in book.xml - if `title` is set but

title not set, it will generated a

title using the value of `title` - support `configurations` which can set page level configs here. - now can open page that has .md file but not defined in book.xml (must in a book defined) - old.title.gen is not supported yet. --- assets/xsd/configurations.xsd | 2 - composer.json | 8 +- composer.lock | 237 ++++------------------------ constant.php | 4 +- index.php | 59 ++----- src/Data/PageData.php | 28 ++++ src/Data/PageMeta.php | 66 ++++++-- src/Data/SiteConfig/ConfigName.php | 2 - src/Element/BookContent/Chapter.php | 2 +- src/Element/BookContent/Page.php | 22 +-- src/Utils/LaTeX/LaTeX.php | 24 --- src/Utils/Markdown/Markdown.php | 33 +++- src/Utils/PageContentType.php | 4 +- src/Utils/ReST/ReST.php | 23 --- src/Web/Main.php | 34 ++++ template/footer.php | 2 +- template/header.php | 2 +- template/main.php | 14 -- template/raw-article.php | 49 ------ template/raw-book-contents.php | 5 - 20 files changed, 207 insertions(+), 413 deletions(-) create mode 100644 src/Data/PageData.php delete mode 100644 src/Utils/LaTeX/LaTeX.php delete mode 100644 src/Utils/ReST/ReST.php create mode 100644 src/Web/Main.php delete mode 100644 template/main.php delete mode 100644 template/raw-article.php delete mode 100644 template/raw-book-contents.php diff --git a/assets/xsd/configurations.xsd b/assets/xsd/configurations.xsd index 9b4597e..7644fcf 100644 --- a/assets/xsd/configurations.xsd +++ b/assets/xsd/configurations.xsd @@ -8,7 +8,6 @@ - @@ -24,7 +23,6 @@ - diff --git a/composer.json b/composer.json index 461d3f8..e92deed 100644 --- a/composer.json +++ b/composer.json @@ -17,14 +17,12 @@ } }, "require": { - "php": ">=8.1", + "php": ">=8.2", "ext-xml": "*", "ext-dom": "*", "ext-mbstring": "*", "ext-fileinfo": "*", - "league/commonmark": ">=2.3.8", - "symfony/yaml": ">=4.0", - "gregwar/rst": "^1.0", - "xemlock/php-latex": "dev-master" + "league/commonmark": ">=2.4", + "symfony/yaml": ">=4.0" } } diff --git a/composer.lock b/composer.lock index 3b53504..12910ad 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "992b2bf6550d780081ce38b8a3010d02", + "content-hash": "070684f2b1149b2154d297b64b637ed3", "packages": [ { "name": "dflydev/dot-access-data", @@ -81,70 +81,18 @@ }, "time": "2022-10-27T11:44:00+00:00" }, - { - "name": "gregwar/rst", - "version": "v1.0.6", - "target-dir": "Gregwar/RST", - "source": { - "type": "git", - "url": "https://github.com/Gregwar/RST.git", - "reference": "93c630ae18c47d8f7503230fa6ca39a79ad3c598" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/Gregwar/RST/zipball/93c630ae18c47d8f7503230fa6ca39a79ad3c598", - "reference": "93c630ae18c47d8f7503230fa6ca39a79ad3c598", - "shasum": "" - }, - "require": { - "php": ">=5.3.0", - "symfony/polyfill-mbstring": "^1.12" - }, - "require-dev": { - "phpunit/phpunit": "^6.4" - }, - "type": "library", - "autoload": { - "psr-0": { - "Gregwar\\RST": "" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Grégoire Passault", - "email": "g.passault@gmail.com", - "homepage": "http://www.gregwar.com/" - } - ], - "description": "PHP library to parse reStructuredText documents", - "homepage": "https://github.com/Gregwar/RST", - "keywords": [ - "markup", - "parser", - "rst" - ], - "support": { - "issues": "https://github.com/Gregwar/RST/issues", - "source": "https://github.com/Gregwar/RST/tree/v1.0.6" - }, - "time": "2020-04-09T08:09:05+00:00" - }, { "name": "league/commonmark", - "version": "2.3.8", + "version": "2.4.0", "source": { "type": "git", "url": "https://github.com/thephpleague/commonmark.git", - "reference": "c493585c130544c4e91d2e0e131e6d35cb0cbc47" + "reference": "d44a24690f16b8c1808bf13b1bd54ae4c63ea048" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/commonmark/zipball/c493585c130544c4e91d2e0e131e6d35cb0cbc47", - "reference": "c493585c130544c4e91d2e0e131e6d35cb0cbc47", + "url": "https://api.github.com/repos/thephpleague/commonmark/zipball/d44a24690f16b8c1808bf13b1bd54ae4c63ea048", + "reference": "d44a24690f16b8c1808bf13b1bd54ae4c63ea048", "shasum": "" }, "require": { @@ -180,7 +128,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "2.4-dev" + "dev-main": "2.5-dev" } }, "autoload": { @@ -237,7 +185,7 @@ "type": "tidelift" } ], - "time": "2022-12-10T16:02:17+00:00" + "time": "2023-03-24T15:16:10+00:00" }, { "name": "league/config", @@ -385,16 +333,16 @@ }, { "name": "nette/utils", - "version": "v4.0.0-RC3", + "version": "v4.0.0", "source": { "type": "git", "url": "https://github.com/nette/utils.git", - "reference": "d5e843cbb941999f3a52ea410baeb2b07580233a" + "reference": "cacdbf5a91a657ede665c541eda28941d4b09c1e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/utils/zipball/d5e843cbb941999f3a52ea410baeb2b07580233a", - "reference": "d5e843cbb941999f3a52ea410baeb2b07580233a", + "url": "https://api.github.com/repos/nette/utils/zipball/cacdbf5a91a657ede665c541eda28941d4b09c1e", + "reference": "cacdbf5a91a657ede665c541eda28941d4b09c1e", "shasum": "" }, "require": { @@ -466,9 +414,9 @@ ], "support": { "issues": "https://github.com/nette/utils/issues", - "source": "https://github.com/nette/utils/tree/v4.0.0-RC3" + "source": "https://github.com/nette/utils/tree/v4.0.0" }, - "time": "2023-01-19T21:45:44+00:00" + "time": "2023-02-02T10:41:53+00:00" }, { "name": "psr/event-dispatcher", @@ -522,16 +470,16 @@ }, { "name": "symfony/deprecation-contracts", - "version": "v3.2.0", + "version": "v3.2.1", "source": { "type": "git", "url": "https://github.com/symfony/deprecation-contracts.git", - "reference": "1ee04c65529dea5d8744774d474e7cbd2f1206d3" + "reference": "e2d1534420bd723d0ef5aec58a22c5fe60ce6f5e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/1ee04c65529dea5d8744774d474e7cbd2f1206d3", - "reference": "1ee04c65529dea5d8744774d474e7cbd2f1206d3", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/e2d1534420bd723d0ef5aec58a22c5fe60ce6f5e", + "reference": "e2d1534420bd723d0ef5aec58a22c5fe60ce6f5e", "shasum": "" }, "require": { @@ -569,7 +517,7 @@ "description": "A generic function and convention to trigger deprecation notices", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/deprecation-contracts/tree/v3.2.0" + "source": "https://github.com/symfony/deprecation-contracts/tree/v3.2.1" }, "funding": [ { @@ -585,7 +533,7 @@ "type": "tidelift" } ], - "time": "2022-11-25T10:21:52+00:00" + "time": "2023-03-01T10:25:55+00:00" }, { "name": "symfony/polyfill-ctype", @@ -669,89 +617,6 @@ ], "time": "2022-11-03T14:55:06+00:00" }, - { - "name": "symfony/polyfill-mbstring", - "version": "v1.27.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/8ad114f6b39e2c98a8b0e3bd907732c207c2b534", - "reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534", - "shasum": "" - }, - "require": { - "php": ">=7.1" - }, - "provide": { - "ext-mbstring": "*" - }, - "suggest": { - "ext-mbstring": "For best performance" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-main": "1.27-dev" - }, - "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" - } - }, - "autoload": { - "files": [ - "bootstrap.php" - ], - "psr-4": { - "Symfony\\Polyfill\\Mbstring\\": "" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for the Mbstring extension", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "mbstring", - "polyfill", - "portable", - "shim" - ], - "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.27.0" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2022-11-03T14:55:06+00:00" - }, { "name": "symfony/polyfill-php80", "version": "v1.27.0", @@ -837,16 +702,16 @@ }, { "name": "symfony/yaml", - "version": "v6.2.5", + "version": "v6.2.7", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "2bbfbdacc8a15574f8440c4838ce0d7bb6c86b19" + "reference": "e8e6a1d59e050525f27a1f530aa9703423cb7f57" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/2bbfbdacc8a15574f8440c4838ce0d7bb6c86b19", - "reference": "2bbfbdacc8a15574f8440c4838ce0d7bb6c86b19", + "url": "https://api.github.com/repos/symfony/yaml/zipball/e8e6a1d59e050525f27a1f530aa9703423cb7f57", + "reference": "e8e6a1d59e050525f27a1f530aa9703423cb7f57", "shasum": "" }, "require": { @@ -891,7 +756,7 @@ "description": "Loads and dumps YAML files", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/yaml/tree/v6.2.5" + "source": "https://github.com/symfony/yaml/tree/v6.2.7" }, "funding": [ { @@ -907,65 +772,17 @@ "type": "tidelift" } ], - "time": "2023-01-10T18:53:53+00:00" - }, - { - "name": "xemlock/php-latex", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/xemlock/php-latex.git", - "reference": "d60bf4c919426d4fa6978aa1f63208d64df5cc73" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/xemlock/php-latex/zipball/d60bf4c919426d4fa6978aa1f63208d64df5cc73", - "reference": "d60bf4c919426d4fa6978aa1f63208d64df5cc73", - "shasum": "" - }, - "require": { - "ext-dom": "*", - "ext-mbstring": "*", - "php": ">=5.3.0" - }, - "require-dev": { - "phpunit/phpunit": ">=5.7" - }, - "default-branch": true, - "type": "library", - "autoload": { - "psr-0": { - "PhpLatex_": "library" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "xemlock", - "email": "xemlock@gmail.com" - } - ], - "description": "LaTeX parser and renderer", - "support": { - "issues": "https://github.com/xemlock/php-latex/issues", - "source": "https://github.com/xemlock/php-latex/tree/master" - }, - "time": "2022-05-03T22:31:34+00:00" + "time": "2023-02-16T09:57:23+00:00" } ], "packages-dev": [], "aliases": [], "minimum-stability": "alpha", - "stability-flags": { - "xemlock/php-latex": 20 - }, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=8.1", + "php": ">=8.2", "ext-xml": "*", "ext-dom": "*", "ext-mbstring": "*", diff --git a/constant.php b/constant.php index d7eecce..2519cdd 100644 --- a/constant.php +++ b/constant.php @@ -2,6 +2,6 @@ const APP_NAME = "ph-Bookshelf"; -const VERSION = "0.5.0-alpha1"; +const VERSION = "0.5.0-alpha2"; const CHANNEL = "suk-ws"; -const BRANCH = "master"; +const BRANCH = "config-v2.0"; diff --git a/index.php b/index.php index ca49a00..3f3e3ee 100644 --- a/index.php +++ b/index.php @@ -6,8 +6,15 @@ require "./vendor/autoload.php"; use SukWs\Bookshelf\Data\PageMeta; use SukWs\Bookshelf\Data\SiteConfig\RobotsPolicy; use SukWs\Bookshelf\Data\SiteMeta; +use SukWs\Bookshelf\Utils\Markdown\Markdown; use SukWs\Bookshelf\Utils\PageParse; -use SukWs\Bookshelf\Utils\RequestNotExistException; +use SukWs\Bookshelf\Web\Main; + +//$parser = new Markdown(); +// +//$data = $parser->parse(file_get_contents("./data/test_page.md")); +// +//exit($data->page_html); try { @@ -35,41 +42,17 @@ try { exit(SiteMeta::getConfigurationLevelShelf("site.robots")); } - } - - try { + } else if (PageMeta::init($uri)) { - // 寻找页面 + require "./template/header.php"; - if (sizeof($uri) > 0 && $uri[0] != null) { - // 非主页面,判定当前定义的 book - if ($uri[0] == "%root") { - PageMeta::$book = SiteMeta::getBookshelf()->getRootBook(); - } else { - $tmp = SiteMeta::getBookshelf()->getBook($uri[0]); - if ($tmp == null) - throw new RequestNotExistException("Book required \"$uri[0]\" not found!"); - PageMeta::$bookId = $uri[0]; - PageMeta::$book = $tmp->getContentedNode(); - } - - // 判定当前页面 - if (sizeof($uri) > 1 && $uri[1] != null) { - $tmp = PageMeta::$book->getPage($uri[1]); - if ($tmp == null) throw new RequestNotExistException("Page required \"$uri[1]\" not found on book \"$uri[0]\"!"); - PageMeta::$page = $tmp; - } else { - PageMeta::$page = PageMeta::$book->getChildren()->getChildren()[0]; - } - } else { - // 主页面 - PageMeta::$bookId = "%root"; - PageMeta::$book = SiteMeta::getBookshelf()->getRootBook(); - PageMeta::$page = PageMeta::$book->getChildren()->getChildren()[0]; - PageMeta::$isMainPage = true; - } + require "./template/nav.php"; - } catch (RequestNotExistException $e) { + Main::main(PageMeta::$page_data); + + require "./template/footer.php"; + + } else { // 页面寻找失败,寻找资源文件 @@ -88,20 +71,12 @@ try { ) {} else if ( // 搜索root书中的书籍资源文件夹中的文件 file_exists($resLoc = "./data/%root/%assets/$req") ) {} else { - throw $e; // 找不到资源文件 + throw new Exception("cannot find file " . $req); // 找不到资源文件 } PageParse::outputBinaryFile($resLoc); } - require "./template/header.php"; - - require "./template/nav.php"; - - require "./template/main.php"; - - require "./template/footer.php"; - } catch (Exception $e) { echo "

ERROR

" . $e->getMessage() . "

"; diff --git a/src/Data/PageData.php b/src/Data/PageData.php new file mode 100644 index 0000000..576a32b --- /dev/null +++ b/src/Data/PageData.php @@ -0,0 +1,28 @@ +page_html = $page_html; + $this->configurations = $configurations; + $this->title = $title; + $this->gen_title = $gen_title; + } + + public function getConfiguration (string $key): ?string { + return @$this->configurations[$key]; + } + +} \ No newline at end of file diff --git a/src/Data/PageMeta.php b/src/Data/PageMeta.php index efbccdd..654497c 100644 --- a/src/Data/PageMeta.php +++ b/src/Data/PageMeta.php @@ -2,19 +2,69 @@ namespace SukWs\Bookshelf\Data; +use Exception; use SukWs\Bookshelf\Data\SiteConfig\ConfigName; use SukWs\Bookshelf\Element\BookContent\BookContented; use SukWs\Bookshelf\Element\BookContent\Page; +use SukWs\Bookshelf\Utils\Markdown\Markdown; +use SukWs\Bookshelf\Utils\RequestNotExistException; class PageMeta { public static string $bookId; public static BookContented $book; - public static Page $page; + public static string $page_id; + public static PageData $page_data; public static bool $isMainPage = false; + /** + * @throws RequestNotExistException + * @throws Exception + */ + public static function init (array $uri): bool { + + if (!isset($uri[0]) || $uri[0] == "%root" || $uri[0] == "/" || $uri[0] == "") { + self::$book = SiteMeta::getBookshelf()->getRootBook(); + self::$bookId = "%root"; + } else { + $tmp = SiteMeta::getBookshelf()->getBook($uri[0]); + if ($tmp == null) + throw new RequestNotExistException("Book required \"$uri[0]\" not found!"); + self::$bookId = $uri[0]; + self::$book = $tmp->getContentedNode(); + } + + if (isset($uri[1])) { + self::$page_id = $uri[1]; + } else { + $tmp = self::$book->getChildren()->getChildren()[0]; + if ($tmp instanceof Page) self::$page_id = $tmp->getId(); + else throw new RequestNotExistException("No main page in required book \"$uri[0]\""); + self::$isMainPage = true; + } + if ($content = @file_get_contents(self::getPagePath("md"))) { + self::$page_data = (new Markdown())->parse($content); + } else { + return false; + } + + return true; + + } + + public static function getHtmlTitle (): string { + return self::getPageTitle() . " - " . self::$book->getName(); + } + public static function getPageTitle (): string { - return self::$page->getName()." - ".self::$book->getName(); + if (self::$page_data->title === null) { + $page_def = self::$book->getPage(self::$page_id); + if ($page_def === null) { + return "..."; + } + return $page_def->getName(); + } + return self::$page_data->title; } public static function getDescription (): string { @@ -30,17 +80,11 @@ class PageMeta { public static function getConfigurationLevelPage (string $key): ?string { $value = self::getConfigurationLevelBook($key); - $valueAttr = self::$page->getConfiguration($key); + $valueAttr = self::$page_data->getConfiguration($key); if ($valueAttr != null) $value = $valueAttr; return $value; } - public static function compatibilityOldTitlePolicy (): bool { - if (self::getConfigurationLevelPage(ConfigName::old_title_gen) == "true") - return true; - return false; - } - public static function highlightJsTheme (): string { $theme = trim(self::getConfigurationLevelPage(ConfigName::highlightjs_theme)); if (empty($theme)) return "atom-one-dark"; @@ -68,4 +112,8 @@ class PageMeta { return $lang; } + public static function getPagePath (?string $extension = null): string { + return "./data/" . self::$bookId . "/" . self::$page_id . ($extension == null ? "" : ".".$extension); + } + } diff --git a/src/Data/SiteConfig/ConfigName.php b/src/Data/SiteConfig/ConfigName.php index 4c56112..d1b6354 100644 --- a/src/Data/SiteConfig/ConfigName.php +++ b/src/Data/SiteConfig/ConfigName.php @@ -20,6 +20,4 @@ class ConfigName { public const ext_title_permalink_flash = "title.permalink.flash"; public const ext_rolling_title = "web-title.rolling"; - public const old_title_gen = "old.title.gen"; - } \ No newline at end of file diff --git a/src/Element/BookContent/Chapter.php b/src/Element/BookContent/Chapter.php index ce0e81e..618da87 100644 --- a/src/Element/BookContent/Chapter.php +++ b/src/Element/BookContent/Chapter.php @@ -94,7 +94,7 @@ class Chapter { %s
EOL, - $this->getPage(PageMeta::$page->getId())==null?"":" active", + $this->getPage(PageMeta::$page_id)==null?"":" active", $this->name ); foreach ($this->children as $node) { diff --git a/src/Element/BookContent/Page.php b/src/Element/BookContent/Page.php index 2d37a5e..aec3634 100644 --- a/src/Element/BookContent/Page.php +++ b/src/Element/BookContent/Page.php @@ -14,8 +14,6 @@ class Page { private Chapter $parent; - private array $configurations = array(); - public function __construct (string $id, string $name, Chapter $parent) { $this->id = $id; $this->name = $name; @@ -50,10 +48,6 @@ class Page { return $this->parent; } - public function getConfiguration (string $key): ?string { - return @$this->configurations[$key]; - } - public function getSummaryHtml (): string { // $str = // "