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

Add getters for docParser/HtmlRenderer/extensions #46

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Block/Element/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Document extends AbstractBlock
/***
* @var ReferenceMap
*/
private $referenceMap;
protected $referenceMap;

public function __construct()
{
Expand Down
8 changes: 4 additions & 4 deletions src/Block/Element/FencedCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ class FencedCode extends AbstractBlock
/**
* @var string
*/
private $info;
protected $info;

/**
* @var int
*/
private $length;
protected $length;

/**
* @var string
*/
private $char;
protected $char;

/**
* @var int
*/
private $offset;
protected $offset;

/**
* @param int $length
Expand Down
4 changes: 2 additions & 2 deletions src/Block/Element/ListBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class ListBlock extends AbstractBlock
/**
* @var bool
*/
private $tight = false;
protected $tight = false;

/**
* @var ListData
*/
private $data;
protected $data;

public function __construct(ListData $listData)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Block/Element/ListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ListItem extends AbstractBlock
/**
* @var ListData
*/
private $data;
protected $data;

public function __construct(ListData $listData)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Block/Element/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function finalize(ContextInterface $context)
*
* @return bool
*/
private function parseReferences(ContextInterface $context, Cursor $cursor)
protected function parseReferences(ContextInterface $context, Cursor $cursor)
{
$referenceFound = false;
while ($cursor->getCharacter() === '[' && $context->getReferenceParser()->parse($cursor)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Block/Element/ReferenceDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ReferenceDefinition extends AbstractBlock
/**
* @var Reference
*/
private $reference;
protected $reference;

public function __construct(Reference $reference)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Block/Parser/ListParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function parse(ContextInterface $context, Cursor $cursor)
*
* @return int
*/
private function calculateListMarkerPadding($marker, $spacesAfterMarker, $rest)
protected function calculateListMarkerPadding($marker, $spacesAfterMarker, $rest)
{
$markerLength = strlen($marker);

Expand Down
16 changes: 16 additions & 0 deletions src/CommonMarkConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,20 @@ public function convertToHtml($commonMark)

return $this->htmlRenderer->renderBlock($documentAST);
}

/**
* @return DocParser
*/
public function getDocParser()
{
return $this->docParser;
}

/**
* @return HtmlRenderer
*/
public function getHtmlRenderer()
{
return $this->htmlRenderer;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm against this change, simply because CommonMarkConverter is a stupid-simple convenience wrapper for people who only care about simple conversions of CommonMark.

Anyone needing additional functionality can easily init those classes themselves and do whatever they feel like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make it more difficult? Also, makes it easier when you think about extensions and the possibility of a factory.

Why should I have to create an environment, docParser and htmlRenderer, when all I want is to change one thing in the htmlRenderer? Instead use this and ->getHtmlRenderer() and go from there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when all I want is to change one thing in the htmlRenderer

There's nothing you can really change though - if there was, then I'd be okay with this change.

}
2 changes: 1 addition & 1 deletion src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Context implements ContextInterface
/**
* @var callable|null
*/
private $unmatchedBlockCloser;
protected $unmatchedBlockCloser;

public function __construct(Document $document, Environment $environment)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ class Cursor
/**
* @var string
*/
private $line;
protected $line;

/**
* @var int
*/
private $length;
protected $length;

/**
* @var int
*
* It's possible for this to be 1 char past the end, meaning we've parsed all chars and have
* reached the end. In this state, any character-returning method MUST return null.
*/
private $currentPosition = 0;
protected $currentPosition = 0;

/**
* @var int
*/
private $previousPosition = 0;
protected $previousPosition = 0;

/**
* @var int|null
*/
private $firstNonSpaceCache;
protected $firstNonSpaceCache;

/**
* @param string $line
Expand Down
10 changes: 5 additions & 5 deletions src/CursorState.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ class CursorState
/**
* @var string
*/
private $line;
protected $line;

/**
* @var int
*/
private $length;
protected $length;

/**
* @var int
*/
private $currentPosition;
protected $currentPosition;

/**
* @var int
*/
private $previousPosition;
protected $previousPosition;

/**
* @var int|null
*/
private $firstNonSpaceCache;
protected $firstNonSpaceCache;

/**
* @param string $line
Expand Down
10 changes: 5 additions & 5 deletions src/DocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DocParser
/**
* @var InlineParserEngine
*/
private $inlineParserEngine;
protected $inlineParserEngine;

/**
* @param Environment $parsers
Expand All @@ -55,7 +55,7 @@ public function getEnvironment()
*
* @return string[]
*/
private function preProcessInput($input)
protected function preProcessInput($input)
{
// Remove any /n which appears at the very end of the string
if (substr($input, -1) == "\n") {
Expand Down Expand Up @@ -89,7 +89,7 @@ public function parse($input)
return $context->getDocument();
}

private function incorporateLine(ContextInterface $context)
protected function incorporateLine(ContextInterface $context)
{
$cursor = new Cursor($context->getLine());
$oldTip = $context->getTip();
Expand Down Expand Up @@ -158,7 +158,7 @@ private function incorporateLine(ContextInterface $context)
}
}

private function processInlines(ContextInterface $context, AbstractBlock $block)
protected function processInlines(ContextInterface $context, AbstractBlock $block)
{
if ($block instanceof AbstractInlineContainer) {
$cursor = new Cursor(trim($block->getStringContent()));
Expand All @@ -179,7 +179,7 @@ private function processInlines(ContextInterface $context, AbstractBlock $block)
* @param ContextInterface $context
* @param AbstractBlock $block
*/
private function breakOutOfLists(ContextInterface $context, AbstractBlock $block)
protected function breakOutOfLists(ContextInterface $context, AbstractBlock $block)
{
$b = $block;
$lastList = null;
Expand Down
15 changes: 15 additions & 0 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

class Environment
{
/**
* @var ExtensionInterface[]
*/
protected $extensions;

/**
* @var BlockParserInterface[]
*/
Expand Down Expand Up @@ -218,6 +223,8 @@ public function createInlineParserEngine()
*/
public function addExtension(ExtensionInterface $extension)
{
$this->extensions[] = $extension;

// Block parsers
foreach ($extension->getBlockParsers() as $blockParser) {
$this->addBlockParser($blockParser);
Expand All @@ -244,6 +251,14 @@ public function addExtension(ExtensionInterface $extension)
}
}

/**
* @return ExtensionInterface[]
*/
public function getExtensions()
{
return $this->extensions;
}

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about emulating Twig's methodology of lazy-initing the extensions, which would add this functionality (among other things), but I haven't thought of a compelling use-case for it yet.

Can you think of a situation where getExtensions() would be needed? If so I'd be happy to include it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed it to effectively unit test — to ensure that the right extension was registered by my package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay that makes sense.

* @return Environment
*/
Expand Down
8 changes: 4 additions & 4 deletions src/HtmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ class HtmlElement
/**
* @var string
*/
private $tagName;
protected $tagName;

/**
* @var string[]
*/
private $attributes = array();
protected $attributes = array();

/**
* @var HtmlElement|HtmlElement[]|string
*/
private $contents;
protected $contents;

/**
* @var bool
*/
private $selfClosing = false;
protected $selfClosing = false;

/**
* @param string $tagName
Expand Down
8 changes: 8 additions & 0 deletions src/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,12 @@ public function renderBlocks($blocks, $inTightList = false)

return implode($this->options['blockSeparator'], $result);
}

/**
* @return Environment
*/
public function getEnvironment()
{
return $this->environment;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be wrong, but I don't feel there's a valid use case for this. When would something need to obtain the environment from the renderer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, needed the environment to get the extensions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just worried about adding and supporting new public methods solely for testing purposes (no strong use-case otherwise).

}
2 changes: 1 addition & 1 deletion src/Inline/Element/InlineCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class InlineCollection extends AbstractInline
/**
* @var ArrayCollection|AbstractInline[]
*/
private $inlines;
protected $inlines;

public function __construct($inlines)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Inline/Element/Newline.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Newline extends AbstractInline
const HARDBREAK = 0;
const SOFTBREAK = 1;

private $type;
protected $type;

/**
* @param int $breakType
Expand Down
12 changes: 6 additions & 6 deletions src/Inline/Parser/CloseBracketParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CloseBracketParser extends AbstractInlineParser implements EnvironmentAwar
/**
* @var Environment
*/
private $environment;
protected $environment;

/**
* @return string[]
Expand Down Expand Up @@ -120,7 +120,7 @@ public function setEnvironment(Environment $environment)
* @param int $start
* @param int $end
*/
private function nullify(ArrayCollection $collection, $start, $end)
protected function nullify(ArrayCollection $collection, $start, $end)
{
for ($i = $start; $i < $end; $i++) {
$collection->set($i, null);
Expand All @@ -135,7 +135,7 @@ private function nullify(ArrayCollection $collection, $start, $end)
*
* @return array|bool
*/
private function tryParseLink(Cursor $cursor, ReferenceMap $referenceMap, Delimiter $opener, $startPos)
protected function tryParseLink(Cursor $cursor, ReferenceMap $referenceMap, Delimiter $opener, $startPos)
{
// Check to see if we have a link/image
// Inline link?
Expand All @@ -155,7 +155,7 @@ private function tryParseLink(Cursor $cursor, ReferenceMap $referenceMap, Delimi
*
* @return array|bool
*/
private function tryParseInlineLinkAndTitle(Cursor $cursor)
protected function tryParseInlineLinkAndTitle(Cursor $cursor)
{
$cursor->advance();
$cursor->advanceToFirstNonSpace();
Expand Down Expand Up @@ -188,7 +188,7 @@ private function tryParseInlineLinkAndTitle(Cursor $cursor)
*
* @return Reference|null
*/
private function tryParseReference(Cursor $cursor, ReferenceMap $referenceMap, Delimiter $opener, $startPos)
protected function tryParseReference(Cursor $cursor, ReferenceMap $referenceMap, Delimiter $opener, $startPos)
{
$savePos = $cursor->saveState();
$cursor->advanceToFirstNonSpace();
Expand Down Expand Up @@ -217,7 +217,7 @@ private function tryParseReference(Cursor $cursor, ReferenceMap $referenceMap, D
*
* @return AbstractWebResource
*/
private function createInline($url, InlineCollection $labelInlines, $title, $isImage)
protected function createInline($url, InlineCollection $labelInlines, $title, $isImage)
{
if ($isImage) {
return new Image($url, $labelInlines, $title);
Expand Down
6 changes: 3 additions & 3 deletions src/InlineParserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

class InlineParserContext
{
private $cursor;
private $inlines;
private $delimiterStack;
protected $cursor;
protected $inlines;
protected $delimiterStack;

public function __construct(Cursor $cursor)
{
Expand Down
Loading