mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Expand public API of Components
Ref: https://github.com/erusev/parsedown/issues/694
This commit is contained in:
@@ -92,6 +92,14 @@ final class BlockQuote implements ContinuableBlock
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0: Block[], 1: State}
|
||||
*/
|
||||
public function contents(State $State)
|
||||
{
|
||||
return Parsedown::blocks($this->Lines, $State);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Handler<Element>
|
||||
*/
|
||||
@@ -100,10 +108,9 @@ final class BlockQuote implements ContinuableBlock
|
||||
return new Handler(
|
||||
/** @return Element */
|
||||
function (State $State) {
|
||||
list($StateRenderables, $State) = Parsedown::lines(
|
||||
$this->Lines,
|
||||
$State
|
||||
);
|
||||
list($Blocks, $State) = $this->contents($State);
|
||||
|
||||
$StateRenderables = Parsedown::stateRenderablesFrom($Blocks);
|
||||
|
||||
$Renderables = $State->applyTo($StateRenderables);
|
||||
$Renderables[] = new Text("\n");
|
||||
|
||||
@@ -101,15 +101,29 @@ final class FencedCode implements ContinuableBlock
|
||||
return new self($newCode, $this->infostring, $this->marker, $this->openerLength, false);
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
public function infostring()
|
||||
{
|
||||
return $this->infostring;
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
public function code()
|
||||
{
|
||||
return $this->code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Element
|
||||
*/
|
||||
public function stateRenderable()
|
||||
{
|
||||
$infostring = $this->infostring();
|
||||
|
||||
return new Element('pre', [], [new Element(
|
||||
'code',
|
||||
$this->infostring !== '' ? ['class' => "language-{$this->infostring}"] : [],
|
||||
[new Text($this->code)]
|
||||
$infostring !== '' ? ['class' => "language-{$infostring}"] : [],
|
||||
[new Text($this->code())]
|
||||
)]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,6 +76,18 @@ final class Header implements Block
|
||||
return new self($text, $level);
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
public function text()
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/** @return 1|2|3|4|5|6 */
|
||||
public function level()
|
||||
{
|
||||
return $this->level;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Handler<Element>
|
||||
*/
|
||||
@@ -85,9 +97,9 @@ final class Header implements Block
|
||||
/** @return Element */
|
||||
function (State $State) {
|
||||
return new Element(
|
||||
'h' . \strval($this->level),
|
||||
'h' . \strval($this->level()),
|
||||
[],
|
||||
$State->applyTo(Parsedown::line($this->text, $State))
|
||||
$State->applyTo(Parsedown::line($this->text(), $State))
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -74,6 +74,12 @@ final class IndentedCode implements ContinuableBlock
|
||||
return new self($newCode);
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
public function code()
|
||||
{
|
||||
return $this->code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Element
|
||||
*/
|
||||
@@ -82,7 +88,7 @@ final class IndentedCode implements ContinuableBlock
|
||||
return new Element(
|
||||
'pre',
|
||||
[],
|
||||
[new Element('code', [], [new Text($this->code)])]
|
||||
[new Element('code', [], [new Text($this->code())])]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,6 +149,12 @@ final class Markup implements ContinuableBlock
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
public function html()
|
||||
{
|
||||
return $this->html;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Handler<Element|RawHtml>
|
||||
*/
|
||||
@@ -158,9 +164,9 @@ final class Markup implements ContinuableBlock
|
||||
/** @return Element|RawHtml */
|
||||
function (State $State) {
|
||||
if ($State->get(SafeMode::class)->isEnabled()) {
|
||||
return new Element('p', [], [new Text($this->html)]);
|
||||
return new Element('p', [], [new Text($this->html())]);
|
||||
} else {
|
||||
return new RawHtml($this->html);
|
||||
return new RawHtml($this->html());
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -69,7 +69,7 @@ final class Paragraph implements ContinuableBlock
|
||||
return new Element(
|
||||
'p',
|
||||
[],
|
||||
$State->applyTo(Parsedown::line(\trim($this->text), $State))
|
||||
$State->applyTo(Parsedown::line(\trim($this->text()), $State))
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -68,6 +68,18 @@ final class SetextHeader implements AcquisitioningBlock
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
public function text()
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/** @return 1|2 */
|
||||
public function level()
|
||||
{
|
||||
return $this->level;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Handler<Element>
|
||||
*/
|
||||
@@ -77,9 +89,9 @@ final class SetextHeader implements AcquisitioningBlock
|
||||
/** @return Element */
|
||||
function (State $State) {
|
||||
return new Element(
|
||||
'h' . \strval($this->level),
|
||||
'h' . \strval($this->level()),
|
||||
[],
|
||||
$State->applyTo(Parsedown::line($this->text, $State))
|
||||
$State->applyTo(Parsedown::line($this->text(), $State))
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -18,7 +18,7 @@ final class TList implements ContinuableBlock
|
||||
/** @var Lines[] */
|
||||
private $Lis;
|
||||
|
||||
/** @var string|null */
|
||||
/** @var int|null */
|
||||
private $listStart;
|
||||
|
||||
/** @var bool */
|
||||
@@ -44,7 +44,7 @@ final class TList implements ContinuableBlock
|
||||
|
||||
/**
|
||||
* @param Lines[] $Lis
|
||||
* @param string|null $listStart
|
||||
* @param int|null $listStart
|
||||
* @param bool $isLoose
|
||||
* @param int $indent
|
||||
* @param 'ul'|'ol' $type
|
||||
@@ -123,15 +123,15 @@ final class TList implements ContinuableBlock
|
||||
|
||||
$markerTypeRegex = \preg_quote($markerType, '/');
|
||||
|
||||
/** @var string|null */
|
||||
/** @var int|null */
|
||||
$listStart = null;
|
||||
|
||||
if ($type === 'ol') {
|
||||
/** @psalm-suppress PossiblyFalseArgument */
|
||||
$listStart = \ltrim(\strstr($matches[1], $markerType, true), '0') ?: '0';
|
||||
$listStart = \intval(\strstr($matches[1], $markerType, true) ?: '0');
|
||||
|
||||
if (
|
||||
$listStart !== '1'
|
||||
$listStart !== 1
|
||||
&& isset($Block)
|
||||
&& $Block instanceof Paragraph
|
||||
&& ! $Context->previousEmptyLines() > 0
|
||||
@@ -272,6 +272,32 @@ final class TList implements ContinuableBlock
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0: Block[], 1: State}[]
|
||||
*/
|
||||
public function items(State $State)
|
||||
{
|
||||
return \array_map(
|
||||
/** @return array{0: Block[], 1: State} */
|
||||
function (Lines $Lines) use ($State) {
|
||||
return Parsedown::blocks($Lines, $State);
|
||||
},
|
||||
$this->Lis
|
||||
);
|
||||
}
|
||||
|
||||
/** @return 'ol'|'ul' */
|
||||
public function type()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/** @return int|null */
|
||||
public function listStart()
|
||||
{
|
||||
return $this->listStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Handler<Element>
|
||||
*/
|
||||
@@ -280,21 +306,24 @@ final class TList implements ContinuableBlock
|
||||
return new Handler(
|
||||
/** @return Element */
|
||||
function (State $State) {
|
||||
$listStart = $this->listStart();
|
||||
|
||||
return new Element(
|
||||
$this->type,
|
||||
$this->type(),
|
||||
(
|
||||
isset($this->listStart) && $this->listStart !== '1'
|
||||
? ['start' => $this->listStart]
|
||||
isset($listStart) && $listStart !== 1
|
||||
? ['start' => \strval($listStart)]
|
||||
: []
|
||||
),
|
||||
\array_map(
|
||||
/** @return Element */
|
||||
function (Lines $Lines) use ($State) {
|
||||
list($StateRenderables, $State) = Parsedown::lines(
|
||||
$Lines,
|
||||
$State
|
||||
);
|
||||
/**
|
||||
* @param array{0: Block[], 1: State} $Item
|
||||
* @return Element
|
||||
* */
|
||||
function ($Item) {
|
||||
list($Blocks, $State) = $Item;
|
||||
|
||||
$StateRenderables = Parsedown::stateRenderablesFrom($Blocks);
|
||||
$Renderables = $State->applyTo($StateRenderables);
|
||||
|
||||
if (! $this->isLoose
|
||||
@@ -309,7 +338,7 @@ final class TList implements ContinuableBlock
|
||||
|
||||
return new Element('li', [], $Renderables);
|
||||
},
|
||||
$this->Lis
|
||||
$this->items($State)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use Erusev\Parsedown\AST\StateRenderable;
|
||||
use Erusev\Parsedown\Components\AcquisitioningBlock;
|
||||
use Erusev\Parsedown\Components\Block;
|
||||
use Erusev\Parsedown\Components\ContinuableBlock;
|
||||
use Erusev\Parsedown\Components\Inline;
|
||||
use Erusev\Parsedown\Html\Renderables\Element;
|
||||
use Erusev\Parsedown\Parsedown;
|
||||
use Erusev\Parsedown\Parsing\Context;
|
||||
@@ -174,6 +175,51 @@ final class Table implements AcquisitioningBlock, ContinuableBlock
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return array<int, Inline[]> */
|
||||
public function headerRow(State $State)
|
||||
{
|
||||
return \array_map(
|
||||
/**
|
||||
* @param string $cell
|
||||
* @return Inline[]
|
||||
*/
|
||||
function ($cell) use ($State) {
|
||||
return Parsedown::inlines($cell, $State);
|
||||
},
|
||||
$this->headerCells
|
||||
);
|
||||
}
|
||||
|
||||
/** @return array<int, Inline[]>[] */
|
||||
public function rows(State $State)
|
||||
{
|
||||
return \array_map(
|
||||
/**
|
||||
* @param array<int, string> $cells
|
||||
* @return array<int, Inline[]>
|
||||
*/
|
||||
function ($cells) use ($State) {
|
||||
return \array_map(
|
||||
/**
|
||||
* @param string $cell
|
||||
* @return Inline[]
|
||||
*/
|
||||
function ($cell) use ($State) {
|
||||
return Parsedown::inlines($cell, $State);
|
||||
},
|
||||
$cells
|
||||
);
|
||||
},
|
||||
$this->rows
|
||||
);
|
||||
}
|
||||
|
||||
/** @return array<int, _Alignment|null> */
|
||||
public function alignments()
|
||||
{
|
||||
return $this->alignments;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Handler<Element>
|
||||
*/
|
||||
@@ -185,44 +231,44 @@ final class Table implements AcquisitioningBlock, ContinuableBlock
|
||||
return new Element('table', [], [
|
||||
new Element('thead', [], [new Element('tr', [], \array_map(
|
||||
/**
|
||||
* @param string $cell
|
||||
* @param Inline[] $Cell
|
||||
* @param _Alignment|null $alignment
|
||||
* @return Element
|
||||
*/
|
||||
function ($cell, $alignment) use ($State) {
|
||||
function ($Cell, $alignment) use ($State) {
|
||||
return new Element(
|
||||
'th',
|
||||
isset($alignment) ? ['style' => "text-align: $alignment;"] : [],
|
||||
$State->applyTo(Parsedown::line($cell, $State))
|
||||
$State->applyTo(Parsedown::stateRenderablesFrom($Cell))
|
||||
);
|
||||
},
|
||||
$this->headerCells,
|
||||
$this->alignments
|
||||
$this->headerRow($State),
|
||||
$this->alignments()
|
||||
))]),
|
||||
new Element('tbody', [], \array_map(
|
||||
/**
|
||||
* @param array<int, string> $cells
|
||||
* @param Inline[][] $Cells
|
||||
* @return Element
|
||||
*/
|
||||
function ($cells) use ($State) {
|
||||
function ($Cells) use ($State) {
|
||||
return new Element('tr', [], \array_map(
|
||||
/**
|
||||
* @param string $cell
|
||||
* @param Inline[] $Cell
|
||||
* @param _Alignment|null $alignment
|
||||
* @return Element
|
||||
*/
|
||||
function ($cell, $alignment) use ($State) {
|
||||
function ($Cell, $alignment) use ($State) {
|
||||
return new Element(
|
||||
'td',
|
||||
isset($alignment) ? ['style' => "text-align: $alignment;"] : [],
|
||||
$State->applyTo(Parsedown::line($cell, $State))
|
||||
$State->applyTo(Parsedown::stateRenderablesFrom($Cell))
|
||||
);
|
||||
},
|
||||
$cells,
|
||||
\array_slice($this->alignments, 0, \count($cells))
|
||||
$Cells,
|
||||
\array_slice($this->alignments(), 0, \count($Cells))
|
||||
));
|
||||
},
|
||||
$this->rows
|
||||
$this->rows($State)
|
||||
))
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user