-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
228 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace JohnnyHuy\Laravel\Inline\Element; | ||
|
||
use League\CommonMark\Inline\Element\AbstractStringContainer; | ||
|
||
class Color extends AbstractStringContainer {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JohnnyHuy\Laravel\Inline\Parser; | ||
|
||
use JohnnyHuy\Laravel\Inline\Element\Color; | ||
use League\CommonMark\InlineParserContext; | ||
use League\CommonMark\Inline\Parser\AbstractInlineParser; | ||
|
||
class ColorParser extends AbstractInlineParser | ||
{ | ||
/** | ||
* @param InlineParserContext $inlineContext | ||
* @return bool | ||
*/ | ||
public function parse(InlineParserContext $inlineContext) | ||
{ | ||
$cursor = $inlineContext->getCursor(); | ||
$savedState = $cursor->saveState(); | ||
|
||
$cursor->advance(); | ||
|
||
$regex = '/(?:color|colour)(?:\s(?:(\#?[A-z]+|\d{1,3}\,\s?\d{1,3}\,\s?\d{1,3}(\,\s?\d{1,3})?)))\s(.*(?!:color))\s(?:\:color)/'; | ||
$validate = $cursor->match($regex); | ||
|
||
if (!$validate) { | ||
$cursor->restoreState($savedState); | ||
return false; | ||
} | ||
|
||
$matches = []; | ||
preg_match($regex, $validate, $matches); | ||
[, $color, $alpha, $content] = $matches; | ||
|
||
if (preg_match('/[\,]+/', $color, $_)) { | ||
if (empty($alpha)) { | ||
$color = "rgb({$color})"; | ||
} else { | ||
$color = "rgba({$color})"; | ||
} | ||
} else if (preg_match('/[\#]+/', $color, $_)) { | ||
$color = "#{$color}"; | ||
} | ||
|
||
$data['color'] = $color; | ||
|
||
$inlineContext->getContainer()->appendChild(new Color($content, $data)); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getCharacters() | ||
{ | ||
return [':']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace JohnnyHuy\Laravel\Inline\Renderer; | ||
|
||
use InvalidArgumentException; | ||
use JohnnyHuy\Laravel\Inline\Element\Color; | ||
use JohnnyHuy\Laravel\Inline\Element\YouTube; | ||
use League\CommonMark\ElementRendererInterface; | ||
use League\CommonMark\HtmlElement; | ||
use League\CommonMark\Inline\Element\AbstractInline; | ||
use League\CommonMark\Inline\Element\AbstractWebResource; | ||
use League\CommonMark\Inline\Renderer\InlineRendererInterface; | ||
use League\CommonMark\Util\Configuration; | ||
use League\CommonMark\Util\ConfigurationAwareInterface; | ||
|
||
class ColorRenderer implements InlineRendererInterface, ConfigurationAwareInterface | ||
{ | ||
/** | ||
* @var Configuration | ||
*/ | ||
protected $config; | ||
|
||
/** | ||
* @param AbstractInline|AbstractWebResource $inline | ||
* @param ElementRendererInterface $htmlRenderer | ||
* | ||
* @return HtmlElement|string | ||
*/ | ||
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) | ||
{ | ||
if (!($inline instanceof Color)) { | ||
throw new InvalidArgumentException('Incompatible inline type: ' . get_class($inline)); | ||
} | ||
|
||
return new HtmlElement('span', ['style' => "color: {$inline->getData('color')}"], $inline->getContent()); | ||
} | ||
|
||
/** | ||
* @param Configuration $configuration | ||
*/ | ||
public function setConfiguration(Configuration $configuration) | ||
{ | ||
$this->config = $configuration; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.