Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to use Commonmark 2.0 #12

Merged
merged 14 commits into from
Aug 15, 2021
Prev Previous commit
Next Next commit
Initial rendering
todo: update markdown files for breaking changes in commonmark html rendering
  • Loading branch information
joshbruce committed Aug 6, 2021
commit 98fafd1c762a9407e18be4ccd4dc1a1b764a8c72
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.1",
"8fold/php-shoop": "^0.10.17@dev"
"8fold/php-shoop": "^0.10.19"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 2 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 18 additions & 27 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
cacheResult="false">
<testsuites>
<testsuite name="base">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
<exclude>
<directory>vendor/</directory>
</exclude>
</whitelist>
</filter>
<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" cacheResult="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">./src/</directory>
</include>
<exclude>
<directory>vendor/</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="base">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<ini name="display_errors" value="On"/>
<ini name="display_startup_errors" value="On"/>
</php>
</phpunit>
29 changes: 29 additions & 0 deletions phpunit.xml.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
cacheResult="false">
<testsuites>
<testsuite name="base">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
<exclude>
<directory>vendor/</directory>
</exclude>
</whitelist>
</filter>
<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
</php>
</phpunit>
9 changes: 5 additions & 4 deletions src/Abbreviation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Eightfold\CommonMarkAbbreviations;

use League\CommonMark\Inline\Element\AbstractStringContainer;
use League\CommonMark\HtmlElement;
use League\CommonMark\Node\Inline\AbstractStringContainer;
use League\CommonMark\Util\HtmlElement;

class Abbreviation extends AbstractStringContainer
{
Expand All @@ -15,8 +15,9 @@ public function isContainer(): bool

public function element()
{
$attributes = $this->getData('attributes', []);
// die(var_dump());
// $attributes = $this->getData('attributes', []);

return new HtmlElement('abbr', $attributes, $this->content);
return new HtmlElement('abbr', $this->data->get("attributes"), $this->getLiteral());
}
}
11 changes: 7 additions & 4 deletions src/AbbreviationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

namespace Eightfold\CommonMarkAbbreviations;

use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;

use League\CommonMark\Extension\ExtensionInterface;
use League\CommonMark\ConfigurableEnvironmentInterface;
use League\CommonMark\Environment\EnvironmentBuilderInterface;

use Eightfold\CommonMarkAbbreviations\Abbreviation;
use Eightfold\CommonMarkAbbreviations\AbbreviationRenderer;
use Eightfold\CommonMarkAbbreviations\AbbreviationInlineParser;

class AbbreviationExtension implements ExtensionInterface
{
public function register(ConfigurableEnvironmentInterface $environment)
public function register(EnvironmentBuilderInterface $environment): void
{
$environment->addInlineParser(new AbbreviationInlineParser(), 100);
$environment->addInlineRenderer(Abbreviation::class, new AbbreviationRenderer());
$environment
->addInlineParser(new AbbreviationInlineParser(), 100)
->addRenderer(Abbreviation::class, new AbbreviationRenderer());
}
}
43 changes: 28 additions & 15 deletions src/AbbreviationInlineParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,55 @@

use Eightfold\Shoop\Shoop;

use League\CommonMark\Inline\Parser\InlineParserInterface;
use League\CommonMark\InlineParserContext;
use League\CommonMark\Parser\Inline\InlineParserInterface;
use League\CommonMark\Parser\InlineParserContext;
use League\CommonMark\Parser\Inline\InlineParserMatch;

use Eightfold\CommonMarkAbbreviations\Abbreviation;

class AbbreviationInlineParser implements InlineParserInterface
{
public function getCharacters(): array
// public function getCharacters(): array
public function getMatchDefinition(): InlineParserMatch
{
return ["["];
return InlineParserMatch::regex('\[\..+?\]\(.+?\)');
// return ["["];
}

public function parse(InlineParserContext $inlineContext): bool
{
$cursor = $inlineContext->getCursor();
$nextChar = $cursor->peek();
if ($nextChar !== null and $nextChar !== ".") {
$previousChar = $cursor->peek(-1);
if ($previousChar !== null and $previousChar !== ' ') {
return false;
}

$previousCursor = $cursor->saveState();
$cursor->advanceBy($inlineContext->getFullMatchLength());
// $nextChar = $cursor->peek();
// if ($nextChar !== null and $nextChar !== ".") {
// return false;
// }

$regex = '/^\[\..+?\]\(.+?\)/';
$abbr = $cursor->match($regex);
// $previousCursor = $cursor->saveState();

if (empty($abbr)) {
$cursor->restoreState($previousCursor);
return false;
}
// $regex = '/^\[\..+?\]\(.+?\)/';
// $abbr = $cursor->match($regex);

// if (empty($abbr)) {
// $cursor->restoreState($previousCursor);
// return false;
// }

$abbr = $inlineContext->getFullMatch();
$abbr = substr($abbr, 2);
$abbr = substr($abbr, 0, -1);

list($abbr, $title) = explode("](", $abbr, 2);
$elem = new Abbreviation($abbr, ['attributes' => ['title' => $title]]);

$elem = new Abbreviation($abbr, ['attributes' => ['title' => $title]]);
// die(var_dump($elem));
$inlineContext->getContainer()->appendChild($elem);

// die(var_dump($inlineContext));
return true;
}
}
22 changes: 13 additions & 9 deletions src/AbbreviationRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

namespace Eightfold\CommonMarkAbbreviations;

use League\CommonMark\ElementRendererInterface;
use League\CommonMark\HtmlElement;
use League\CommonMark\Inline\Element\AbstractInline;
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
// use League\CommonMark\ElementRendererInterface;
use League\CommonMark\Node\Node;
// use League\CommonMark\HtmlElement;
// use League\CommonMark\Inline\Element\AbstractInline;
use League\CommonMark\Renderer\NodeRendererInterface;
use League\CommonMark\Renderer\ChildNodeRendererInterface;

// use League\CommonMark\Inline\Renderer\InlineRendererInterface;

use Eightfold\Shoop\Shoop;

use Eightfold\CommonMarkAbbreviations\Abbreviation;


class AbbreviationRenderer implements InlineRendererInterface
class AbbreviationRenderer implements NodeRendererInterface
{
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
if (! ($inline instanceof Abbreviation)) {
throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
if (! ($node instanceof Abbreviation)) {
throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($node));
}
return $inline->element();
return $node->element();
}
}
55 changes: 34 additions & 21 deletions tests/AbbreviationAbstractStringContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use PHPUnit\Framework\TestCase;

use League\CommonMark\Environment;
use League\CommonMark\CommonMarkConverter;
// use League\CommonMark\Environment;
// use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Environment\Environment;
use League\CommonMark\MarkdownConverter;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\ExternalLink\ExternalLinkExtension;
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension;
use League\CommonMark\Extension\TableOfContents\TableOfContentsExtension;
Expand All @@ -27,30 +30,40 @@ class AbbreviationAbstractStringContainerTest extends TestCase
{
public function testParser()
{
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new AbbreviationExtension());
$environment->addExtension(new ExternalLinkExtension());
$environment->addExtension(new HeadingPermalinkExtension());
$environment->addExtension(new TableOfContentsExtension());
$converter = new CommonMarkConverter([
$config = [
"external_link" => ["open_in_new_window" => true]
], $environment);
];

$path = Shoop::this(__DIR__)->append("/short-doc-abstract-string-container.md");
$markdown = \file_get_contents($path);
$expected = \file_get_contents("short-doc-abstract-string-container.html");
$actual = $converter->convertToHtml($markdown);
$this->assertEquals($expected, $actual);
$environment = (new Environment($config))
->addExtension(new CommonMarkCoreExtension())
->addExtension(new AbbreviationExtension())
->addExtension(new ExternalLinkExtension())
->addExtension(new HeadingPermalinkExtension())
->addExtension(new TableOfContentsExtension());

$converter = new MarkdownConverter($environment);

$markdownPath = Shoop::this(__DIR__)
->append("/short-doc-abstract-string-container.md");
$markdown = \file_get_contents($markdownPath);

$path = Shoop::this(__DIR__)->divide("/")
->dropLast()->append(["readme.html"])->asString("/");
$expected = \file_get_contents($path);
$htmlPath = Shoop::this(__DIR__)
->append("/short-doc-abstract-string-container.html");
$expected = \file_get_contents($htmlPath);

$path = Shoop::this(__DIR__)->divide("/")
->dropLast()->append(["README.md"])->asString("/");
$markdown = \file_get_contents($path);
$actual = $converter->convertToHtml($markdown)->getContent();

$actual = $converter->convertToHtml($markdown);
$this->assertEquals($expected, $actual);

// $path = Shoop::this(__DIR__)->divide("/")
// ->dropLast()->append(["readme.html"])->asString("/");
// $expected = \file_get_contents($path);

// $path = Shoop::this(__DIR__)->divide("/")
// ->dropLast()->append(["README.md"])->asString("/");
// $markdown = \file_get_contents($path);

// $actual = $converter->convertToHtml($markdown);
// $this->assertEquals($expected, $actual);
}
}