1
0
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:
Aidan Woods
2019-02-16 20:31:20 +00:00
parent 3c0b528d54
commit 3ccd64a9a1
43 changed files with 813 additions and 76 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -59,12 +59,18 @@ final class Code implements Inline
return null;
}
/** @return string */
public function text()
{
return $this->text;
}
/**
* @return Element
*/
public function stateRenderable()
{
return new Element('code', [], [new Text($this->text)]);
return new Element('code', [], [new Text($this->text())]);
}
/**
@@ -72,6 +78,6 @@ final class Code implements Inline
*/
public function bestPlaintext()
{
return new Text($this->text);
return new Text($this->text());
}
}

View File

@@ -54,12 +54,24 @@ final class Email implements Inline
}
}
/** @return string */
public function text()
{
return $this->text;
}
/** @return string */
public function url()
{
return $this->url;
}
/**
* @return Element
*/
public function stateRenderable()
{
return new Element('a', ['href' => $this->url], [new Text($this->text)]);
return new Element('a', ['href' => $this->url()], [new Text($this->text())]);
}
/**
@@ -67,6 +79,6 @@ final class Email implements Inline
*/
public function bestPlaintext()
{
return new Text($this->text);
return new Text($this->text());
}
}

View File

@@ -69,6 +69,12 @@ final class Emphasis implements Inline
return new self($matches[1], $emphasis, \strlen($matches[0]));
}
/** @return string */
public function text()
{
return $this->text;
}
/**
* @return Handler<Element>
*/
@@ -80,7 +86,7 @@ final class Emphasis implements Inline
return new Element(
$this->type,
[],
$State->applyTo(Parsedown::line($this->text, $State))
$State->applyTo(Parsedown::line($this->text(), $State))
);
}
);
@@ -91,6 +97,6 @@ final class Emphasis implements Inline
*/
public function bestPlaintext()
{
return new Text($this->text);
return new Text($this->text());
}
}

View File

@@ -42,12 +42,18 @@ final class EscapeSequence implements Inline
return null;
}
/** @return string */
public function char()
{
return $this->text;
}
/**
* @return Text
*/
public function stateRenderable()
{
return new Text($this->text);
return new Text($this->char());
}
/**
@@ -55,6 +61,6 @@ final class EscapeSequence implements Inline
*/
public function bestPlaintext()
{
return new Text($this->text);
return new Text($this->char());
}
}

View File

@@ -52,6 +52,24 @@ final class Image implements Inline
return new self($Link);
}
/** @return string */
public function label()
{
return $this->Link->label();
}
/** @return string */
public function url()
{
return $this->Link->url();
}
/** @return string|null */
public function title()
{
return $this->Link->title();
}
/**
* @return Handler<Element|Text>
*/
@@ -61,9 +79,9 @@ final class Image implements Inline
/** @return Element|Text */
function (State $State) {
$attributes = [
'src' => $this->Link->url(),
'src' => $this->url(),
'alt' => \array_reduce(
Parsedown::inlines($this->Link->label(), $State),
Parsedown::inlines($this->label(), $State),
/**
* @param string $text
* @return string
@@ -78,7 +96,7 @@ final class Image implements Inline
),
];
$title = $this->Link->title();
$title = $this->title();
if (isset($title)) {
$attributes['title'] = $title;
@@ -98,6 +116,6 @@ final class Image implements Inline
*/
public function bestPlaintext()
{
return new Text($this->Link->label());
return new Text($this->label());
}
}

View File

@@ -118,10 +118,12 @@ final class Link implements Inline
return new Handler(
/** @return Element|Text */
function (State $State) {
$attributes = ['href' => $this->url];
$attributes = ['href' => $this->url()];
if (isset($this->title)) {
$attributes['title'] = $this->title;
$title = $this->title();
if (isset($title)) {
$attributes['title'] = $title;
}
if ($State->get(SafeMode::class)->isEnabled()) {
@@ -135,7 +137,7 @@ final class Link implements Inline
return new Element(
'a',
$attributes,
$State->applyTo(Parsedown::line($this->label, $State))
$State->applyTo(Parsedown::line($this->label(), $State))
);
}
);
@@ -146,6 +148,6 @@ final class Link implements Inline
*/
public function bestPlaintext()
{
return new Text($this->label);
return new Text($this->label());
}
}

View File

@@ -51,6 +51,12 @@ final class Markup implements Inline
}
}
/** @return string */
public function html()
{
return $this->html;
}
/**
* @return Handler<Text|RawHtml>
*/
@@ -60,9 +66,9 @@ final class Markup implements Inline
/** @return Text|RawHtml */
function (State $State) {
if ($State->get(SafeMode::class)->isEnabled()) {
return new Text($this->html);
return new Text($this->html());
} else {
return new RawHtml($this->html);
return new RawHtml($this->html());
}
}
);
@@ -73,6 +79,6 @@ final class Markup implements Inline
*/
public function bestPlaintext()
{
return new Text($this->html);
return new Text($this->html());
}
}

View File

@@ -29,7 +29,7 @@ final class PlainText implements Inline
* @param State $State
* @return static
*/
public static function build(Excerpt $Excerpt, State $State = null)
public static function build(Excerpt $Excerpt, State $State)
{
return new self($Excerpt->text());
}
@@ -45,7 +45,7 @@ final class PlainText implements Inline
*/
public function stateRenderable()
{
return new Text($this->text);
return new Text($this->text());
}
/**
@@ -53,6 +53,6 @@ final class PlainText implements Inline
*/
public function bestPlaintext()
{
return new Text($this->text);
return new Text($this->text());
}
}

View File

@@ -39,13 +39,19 @@ final class SpecialCharacter implements Inline
return null;
}
/** @return string */
public function charCode()
{
return $this->charCodeHtml;
}
/**
* @return RawHtml
*/
public function stateRenderable()
{
return new RawHtml(
'&' . (new Text($this->charCodeHtml))->getHtml() . ';'
'&' . (new Text($this->charCode()))->getHtml() . ';'
);
}
@@ -54,6 +60,6 @@ final class SpecialCharacter implements Inline
*/
public function bestPlaintext()
{
return new Text('&'.$this->charCodeHtml.';');
return new Text('&'.$this->charCode().';');
}
}

View File

@@ -44,6 +44,12 @@ final class Strikethrough implements Inline
return null;
}
/** @return string */
public function text()
{
return $this->text;
}
/**
* @return Handler<Element>
*/
@@ -55,7 +61,7 @@ final class Strikethrough implements Inline
return new Element(
'del',
[],
$State->applyTo(Parsedown::line($this->text, $State))
$State->applyTo(Parsedown::line($this->text(), $State))
);
}
);
@@ -66,6 +72,6 @@ final class Strikethrough implements Inline
*/
public function bestPlaintext()
{
return new Text($this->text);
return new Text($this->text());
}
}

View File

@@ -55,12 +55,18 @@ final class Url implements Inline
return $this->position;
}
/** @return string */
public function url()
{
return $this->url;
}
/**
* @return Element
*/
public function stateRenderable()
{
return new Element('a', ['href' => $this->url], [new Text($this->url)]);
return new Element('a', ['href' => $this->url()], [new Text($this->url())]);
}
/**
@@ -68,6 +74,6 @@ final class Url implements Inline
*/
public function bestPlaintext()
{
return new Text($this->url);
return new Text($this->url());
}
}

View File

@@ -40,12 +40,18 @@ final class UrlTag implements Inline
return null;
}
/** @return string */
public function url()
{
return $this->url;
}
/**
* @return Element
*/
public function stateRenderable()
{
return new Element('a', ['href' => $this->url], [new Text($this->url)]);
return new Element('a', ['href' => $this->url()], [new Text($this->url())]);
}
/**

View File

@@ -220,7 +220,7 @@ final class Parsedown
continue;
}
$Inlines[] = Plaintext::build($Excerpt->choppingUpToOffset($startPosition));
$Inlines[] = Plaintext::build($Excerpt->choppingUpToOffset($startPosition), $State);
$Inlines[] = $Inline;
@@ -234,7 +234,7 @@ final class Parsedown
$Excerpt = $Excerpt->addingToOffset(1);
}
$Inlines[] = Plaintext::build($Excerpt->choppingFromOffset(0));
$Inlines[] = Plaintext::build($Excerpt->choppingFromOffset(0), $State);
return $Inlines;
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Blocks;
use Erusev\Parsedown\Components\Blocks\BlockQuote;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\Parsing\Line;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class BlockQuoteTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testBlockQuoteAPI()
{
$BlockQuote = BlockQuote::build(new Context(new Line('> foo'), ''), new State);
$this->assertSame('foo', $BlockQuote->contents(new State)[0][0]->text());
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Blocks;
use Erusev\Parsedown\Components\Blocks\FencedCode;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\Parsing\Line;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class FencedCodeTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testFencedCodeAPI()
{
$FencedCode = FencedCode::build(new Context(new Line('```foo'), ''), new State);
$FencedCode = $FencedCode->advance(new Context(new Line('bar'), ''), new State);
$FencedCode = $FencedCode->advance(new Context(new Line('```'), ''), new State);
$this->assertSame('foo', $FencedCode->infostring());
$this->assertSame("bar\n", $FencedCode->code());
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Blocks;
use Erusev\Parsedown\Components\Blocks\Header;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\Parsing\Line;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class HeaderTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testHeaderAPI()
{
$Header = Header::build(new Context(new Line('## foo'), ''), new State);
$this->assertSame('foo', $Header->text());
$this->assertSame(2, $Header->level());
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Blocks;
use Erusev\Parsedown\Components\Blocks\IndentedCode;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\Parsing\Line;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class IndentedCodeTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testIndentedCodeAPI()
{
$IndentedCode = IndentedCode::build(new Context(new Line(' foo'), ''), new State);
$this->assertSame("foo\n", $IndentedCode->code());
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Blocks;
use Erusev\Parsedown\Components\Blocks\Markup;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\Parsing\Line;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class MarkupTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testMarkupAPI()
{
$Markup = Markup::build(new Context(new Line('<pre>'), ''), new State);
$this->assertSame('<pre>', $Markup->html());
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Blocks;
use Erusev\Parsedown\Components\Blocks\Paragraph;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\Parsing\Line;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class ParagraphTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testParagraphAPI()
{
$Paragraph = Paragraph::build(new Context(new Line('foo'), ''), new State);
$this->assertSame('foo', $Paragraph->text());
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Blocks;
use Erusev\Parsedown\Components\Blocks\Paragraph;
use Erusev\Parsedown\Components\Blocks\SetextHeader;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\Parsing\Line;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class SetextHeaderTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testSetextHeaderAPI()
{
$Paragraph = Paragraph::build(new Context(new Line('foo'), ''), new State);
$SetextHeader = SetextHeader::build(new Context(new Line('==='), ''), new State, $Paragraph);
$this->assertSame('foo', $SetextHeader->text());
$this->assertSame(1, $SetextHeader->level());
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Blocks;
use Erusev\Parsedown\Components\Blocks\TList;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\Parsing\Line;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class TListTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testTListAPI()
{
$List = TList::build(new Context(new Line('* foo'), ''), new State);
$this->assertSame('foo', $List->items(new State)[0][0][0]->text());
$this->assertSame('ul', $List->type());
$this->assertSame(null, $List->listStart());
$List = TList::build(new Context(new Line('00100. foo'), ''), new State);
$this->assertSame('foo', $List->items(new State)[0][0][0]->text());
$this->assertSame('ol', $List->type());
$this->assertSame(100, $List->listStart());
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Blocks;
use Erusev\Parsedown\Components\Blocks\Paragraph;
use Erusev\Parsedown\Components\Blocks\Table;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\Parsing\Line;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class TableTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testTableAPI()
{
$Paragraph = Paragraph::build(new Context(new Line('foo | bar'), ''), new State);
$Table = Table::build(new Context(new Line('--- | :---'), ''), new State, $Paragraph);
$Table = $Table->advance(new Context(new Line('baz | boo'), ''), new State);
$this->assertSame('foo', $Table->headerRow(new State)[0][0]->text());
$this->assertSame('bar', $Table->headerRow(new State)[1][0]->text());
$this->assertSame('baz', $Table->rows(new State)[0][0][0]->text());
$this->assertSame('boo', $Table->rows(new State)[0][1][0]->text());
$this->assertSame(null, $Table->alignments()[0]);
$this->assertSame('left', $Table->alignments()[1]);
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Inlines;
use Erusev\Parsedown\Components\Inlines\Code;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class CodeTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testCodeAPI()
{
$Code = Code::build(new Excerpt('`foo`', 0), new State);
$this->assertSame('foo', $Code->text());
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Inlines;
use Erusev\Parsedown\Components\Inlines\Email;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class EmailTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testEmailAPI()
{
$Email = Email::build(new Excerpt('<foo@bar.com>', 0), new State);
$this->assertSame('foo@bar.com', $Email->text());
$this->assertSame('mailto:foo@bar.com', $Email->url());
$Email = Email::build(new Excerpt('<mailto:foo@bar.com>', 0), new State);
$this->assertSame('mailto:foo@bar.com', $Email->text());
$this->assertSame('mailto:foo@bar.com', $Email->url());
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Inlines;
use Erusev\Parsedown\Components\Inlines\Emphasis;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class EmphasisTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testEmphasisAPI()
{
$Emphasis = Emphasis::build(new Excerpt('*foo*', 0), new State);
$this->assertSame('foo', $Emphasis->text());
$Emphasis = Emphasis::build(new Excerpt('**foo**', 0), new State);
$this->assertSame('foo', $Emphasis->text());
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Inlines;
use Erusev\Parsedown\Components\Inlines\EscapeSequence;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class EscapeSequenceTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testEscapeSequenceAPI()
{
$EscapeSequence = EscapeSequence::build(new Excerpt('\`', 0), new State);
$this->assertSame('`', $EscapeSequence->char());
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Inlines;
use Erusev\Parsedown\Components\Inlines\Image;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class ImageTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testImageAPI()
{
$Image = Image::build(new Excerpt('![foo](https://example.com)', 0), new State);
$this->assertSame('foo', $Image->label());
$this->assertSame('https://example.com', $Image->url());
$this->assertSame(null, $Image->title());
$Image = Image::build(new Excerpt('![foo](https://example.com "bar")', 0), new State);
$this->assertSame('foo', $Image->label());
$this->assertSame('https://example.com', $Image->url());
$this->assertSame("bar", $Image->title());
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Inlines;
use Erusev\Parsedown\Components\Inlines\Link;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class LinkTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testLinkAPI()
{
$Link = Link::build(new Excerpt('[foo](https://example.com)', 0), new State);
$this->assertSame('foo', $Link->label());
$this->assertSame('https://example.com', $Link->url());
$this->assertSame(null, $Link->title());
$Link = Link::build(new Excerpt('[foo](https://example.com "bar")', 0), new State);
$this->assertSame('foo', $Link->label());
$this->assertSame('https://example.com', $Link->url());
$this->assertSame("bar", $Link->title());
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Inlines;
use Erusev\Parsedown\Components\Inlines\Markup;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class MarkupTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testMarkupAPI()
{
$Markup = Markup::build(new Excerpt('<foo>', 0), new State);
$this->assertSame('<foo>', $Markup->html());
}
}

View File

@@ -4,6 +4,7 @@ namespace Erusev\Parsedown\Tests\Components\Inlines;
use Erusev\Parsedown\Components\Inlines\PlainText;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class PlainTextTest extends TestCase
@@ -13,9 +14,9 @@ final class PlainTextTest extends TestCase
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testPlainTextText()
public function testPlainTextAPI()
{
$Plaintext = Plaintext::build(new Excerpt('foo', 0));
$Plaintext = Plaintext::build(new Excerpt('foo', 0), new State);
$this->assertSame('foo', $Plaintext->text());
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Inlines;
use Erusev\Parsedown\Components\Inlines\SpecialCharacter;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class SpecialCharacterTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testSpecialCharacterAPI()
{
$SpecialCharacter = SpecialCharacter::build(new Excerpt('&nbsp;', 0), new State);
$this->assertSame('nbsp', $SpecialCharacter->charCode());
$SpecialCharacter = SpecialCharacter::build(new Excerpt('&#3C;', 0), new State);
$this->assertSame('#3C', $SpecialCharacter->charCode());
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Inlines;
use Erusev\Parsedown\Components\Inlines\Strikethrough;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class StrikethroughTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testStrikethroughAPI()
{
$Strikethrough = Strikethrough::build(new Excerpt('~~foo~~', 0), new State);
$this->assertSame('foo', $Strikethrough->text());
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Inlines;
use Erusev\Parsedown\Components\Inlines\UrlTag;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class UrlTagTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testUrlTagAPI()
{
$UrlTag = UrlTag::build(new Excerpt('<https://example.com>', 0), new State);
$this->assertSame('https://example.com', $UrlTag->url());
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Erusev\Parsedown\Tests\Components\Inlines;
use Erusev\Parsedown\Components\Inlines\Url;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
use PHPUnit\Framework\TestCase;
final class UrlTest extends TestCase
{
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testUrlAPI()
{
$Url = Url::build(new Excerpt('https://example.com', 0), new State);
$this->assertSame('https://example.com', $Url->url());
}
}