mirror of
https://github.com/suk-ws/ph-Bookshelf.git
synced 2024-12-05 09:26:52 +08:00
支持了复数文本解析器,添加对 ReST 解析的支持
This commit is contained in:
parent
0c83a11a23
commit
c02929c7b1
@ -22,6 +22,7 @@
|
|||||||
"ext-dom": "*",
|
"ext-dom": "*",
|
||||||
"ext-mbstring": "*",
|
"ext-mbstring": "*",
|
||||||
"ext-fileinfo": "*",
|
"ext-fileinfo": "*",
|
||||||
"league/commonmark": ">=2.3.8"
|
"league/commonmark": ">=2.3.8",
|
||||||
|
"gregwar/rst": "^1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,6 @@
|
|||||||
|
|
||||||
const APP_NAME = "ph-Bookshelf";
|
const APP_NAME = "ph-Bookshelf";
|
||||||
|
|
||||||
const VERSION = "0.3.1";
|
const VERSION = "0.3.2";
|
||||||
const CHANNEL = "suk-ws";
|
const CHANNEL = "suk-ws";
|
||||||
const BRANCH = "master";
|
const BRANCH = "master";
|
||||||
|
@ -107,8 +107,16 @@ class Page {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMarkdownContent (): string {
|
public function getContentFilename (string $type): string {
|
||||||
return file_get_contents(sprintf("./data/%s/%s.md", PageMeta::$book->getId(), $this->id));
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,10 +13,19 @@ use League\CommonMark\Extension\Strikethrough\StrikethroughExtension;
|
|||||||
use League\CommonMark\Extension\Table\TableExtension;
|
use League\CommonMark\Extension\Table\TableExtension;
|
||||||
use League\CommonMark\Extension\TaskList\TaskListExtension;
|
use League\CommonMark\Extension\TaskList\TaskListExtension;
|
||||||
use League\CommonMark\MarkdownConverter;
|
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 {
|
public static function getDefaultParser (): ConverterInterface {
|
||||||
|
|
||||||
@ -49,14 +58,14 @@ class Parser {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function getParser (): ConverterInterface {
|
private function getParser (): ConverterInterface {
|
||||||
if (Parser::$converter === null)
|
if ($this->converter === null)
|
||||||
Parser::$converter = self::getDefaultParser();
|
$this->converter = self::getDefaultParser();
|
||||||
return Parser::$converter;
|
return $this->converter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function parse (string $article): string {
|
public function parse (string $raw): string {
|
||||||
return self::getParser()->convert($article);
|
return $this->getParser()->convert($raw);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
14
src/Utils/PageContentType.php
Normal file
14
src/Utils/PageContentType.php
Normal 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
23
src/Utils/ReST/ReST.php
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,16 +1,16 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use SukWs\Bookshelf\Data\PageMeta;
|
use SukWs\Bookshelf\Data\PageMeta;
|
||||||
use SukWs\Bookshelf\Utils\Markdown\Parser;
|
use SukWs\Bookshelf\Utils\Markdown\Markdown;
|
||||||
|
use SukWs\Bookshelf\Utils\ReST\ReST;
|
||||||
$pageMarkdownContent = PageMeta::$page->getMarkdownContent();
|
|
||||||
|
|
||||||
// if the `compatibility.article.title.oldversion` is enabled
|
// if the `compatibility.article.title.oldversion` is enabled
|
||||||
// that means the title should be auto-generated from book.xml
|
// that means the title should be auto-generated from book.xml
|
||||||
// but not written on page.md.
|
// but not written on page.md.
|
||||||
// this code will generate a title from book.xml if the start
|
// this code will generate a title from book.xml if the start
|
||||||
// of the page.md is not `# Title` formatting page title.
|
// 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);
|
$length = strlen($pageMarkdownContent);
|
||||||
for ($i=0; $i<$length; $i++) {
|
for ($i=0; $i<$length; $i++) {
|
||||||
if (empty(trim($pageMarkdownContent[$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;
|
||||||
|
Loading…
Reference in New Issue
Block a user