diff --git a/composer.json b/composer.json index 9645576..3079052 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "ext-dom": "*", "ext-mbstring": "*", "ext-fileinfo": "*", - "league/commonmark": ">=2.3.8" + "league/commonmark": ">=2.3.8", + "gregwar/rst": "^1.0" } -} \ No newline at end of file +} diff --git a/constant.php b/constant.php index d4f6824..7c0c39c 100644 --- a/constant.php +++ b/constant.php @@ -2,6 +2,6 @@ const APP_NAME = "ph-Bookshelf"; -const VERSION = "0.3.1"; +const VERSION = "0.3.2"; const CHANNEL = "suk-ws"; const BRANCH = "master"; diff --git a/src/Element/BookContent/Page.php b/src/Element/BookContent/Page.php index a900875..d2118e0 100644 --- a/src/Element/BookContent/Page.php +++ b/src/Element/BookContent/Page.php @@ -107,8 +107,16 @@ class Page { ); } - public function getMarkdownContent (): string { - return file_get_contents(sprintf("./data/%s/%s.md", PageMeta::$book->getId(), $this->id)); + public function getContentFilename (string $type): string { + return sprintf("./data/%s/%s.%s", PageMeta::$book->getId(), $this->id, $type); + } + + public function hasContent (string $type): string { + return file_exists($this->getContentFilename($type)); + } + + public function getContent (string $type): string { + return file_get_contents($this->getContentFilename($type)); } } diff --git a/src/Utils/Markdown/Parser.php b/src/Utils/Markdown/Markdown.php similarity index 76% rename from src/Utils/Markdown/Parser.php rename to src/Utils/Markdown/Markdown.php index 9e9361a..98981dd 100644 --- a/src/Utils/Markdown/Parser.php +++ b/src/Utils/Markdown/Markdown.php @@ -13,10 +13,19 @@ use League\CommonMark\Extension\Strikethrough\StrikethroughExtension; use League\CommonMark\Extension\Table\TableExtension; use League\CommonMark\Extension\TaskList\TaskListExtension; use League\CommonMark\MarkdownConverter; +use SukWs\Bookshelf\Utils\PageContentType; -class Parser { +class Markdown implements PageContentType { - private static ?ConverterInterface $converter = null; + private ?ConverterInterface $converter = null; + public const type = array("md"); + + /** + * @return string[] + */ + public function type (): array { + return self::type; + } public static function getDefaultParser (): ConverterInterface { @@ -49,14 +58,14 @@ class Parser { } - private static function getParser (): ConverterInterface { - if (Parser::$converter === null) - Parser::$converter = self::getDefaultParser(); - return Parser::$converter; + private function getParser (): ConverterInterface { + if ($this->converter === null) + $this->converter = self::getDefaultParser(); + return $this->converter; } - public static function parse (string $article): string { - return self::getParser()->convert($article); + public function parse (string $raw): string { + return $this->getParser()->convert($raw); } } \ No newline at end of file diff --git a/src/Utils/PageContentType.php b/src/Utils/PageContentType.php new file mode 100644 index 0000000..84d5d7e --- /dev/null +++ b/src/Utils/PageContentType.php @@ -0,0 +1,14 @@ +parse($raw); + } + +} diff --git a/template/raw-article.php b/template/raw-article.php index 0984edc..7cfd870 100644 --- a/template/raw-article.php +++ b/template/raw-article.php @@ -1,16 +1,16 @@ getMarkdownContent(); +use SukWs\Bookshelf\Utils\Markdown\Markdown; +use SukWs\Bookshelf\Utils\ReST\ReST; // if the `compatibility.article.title.oldversion` is enabled // that means the title should be auto-generated from book.xml // but not written on page.md. // this code will generate a title from book.xml if the start // of the page.md is not `# Title` formatting page title. -if (PageMeta::compatibilityOldTitlePolicy()) { +if (PageMeta::compatibilityOldTitlePolicy() && PageMeta::$page->hasContent(Markdown::type[0])) { + $pageMarkdownContent = PageMeta::$page->getContent(Markdown::type[0]); $length = strlen($pageMarkdownContent); for ($i=0; $i<$length; $i++) { if (empty(trim($pageMarkdownContent[$i]))) { @@ -24,4 +24,22 @@ if (PageMeta::compatibilityOldTitlePolicy()) { } } -echo Parser::parse($pageMarkdownContent); +$parsers = array( + new Markdown(), + new ReST() +); + +$htmlContent = null; + +foreach ($parsers as $parser) { + $ok = false; + foreach ($parser->type() as $suffix) { + if (PageMeta::$page->hasContent($suffix)) { + $htmlContent = $parser->parse(PageMeta::$page->getContent($suffix)); + $ok = true; + } + } + if ($ok) break; +} + +echo $htmlContent;