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

支持了复数文本解析器,添加对 ReST 解析的支持

This commit is contained in:
A.C.Sukazyo Eyre 2023-01-25 00:45:22 +08:00
parent 0c83a11a23
commit c02929c7b1
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
7 changed files with 91 additions and 18 deletions

View File

@ -22,6 +22,7 @@
"ext-dom": "*",
"ext-mbstring": "*",
"ext-fileinfo": "*",
"league/commonmark": ">=2.3.8"
"league/commonmark": ">=2.3.8",
"gregwar/rst": "^1.0"
}
}
}

View File

@ -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";

View File

@ -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));
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace SukWs\Bookshelf\Utils;
interface PageContentType {
/**
* @return string[]
*/
public function type (): array;
public function parse (string $raw): string;
}

23
src/Utils/ReST/ReST.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace SukWs\Bookshelf\Utils\ReST;
use Gregwar\RST\Parser;
use SukWs\Bookshelf\Utils\PageContentType;
class ReST implements PageContentType {
public const type = array("rst", "rest");
/**
* @return string[]
*/
public function type (): array {
return self::type;
}
public function parse (string $raw): string {
return (new Parser())->parse($raw);
}
}

View File

@ -1,16 +1,16 @@
<?php
use SukWs\Bookshelf\Data\PageMeta;
use SukWs\Bookshelf\Utils\Markdown\Parser;
$pageMarkdownContent = PageMeta::$page->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;