-
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.
Merge pull request #3 from noxify/master
Gist and Codepen shortcode
- Loading branch information
Showing
21 changed files
with
414 additions
and
38 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
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
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\AbstractWebResource; | ||
|
||
class Codepen extends AbstractWebResource {} |
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\AbstractWebResource; | ||
|
||
class Gist extends AbstractWebResource {} |
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace JohnnyHuy\Laravel\Inline\Parser; | ||
|
||
use League\CommonMark\InlineParserContext; | ||
use JohnnyHuy\Laravel\Inline\Element\Codepen; | ||
use League\CommonMark\Inline\Parser\AbstractInlineParser; | ||
|
||
class CodepenParser extends AbstractInlineParser | ||
{ | ||
/** | ||
* @param InlineParserContext $inlineContext | ||
* @return bool | ||
*/ | ||
public function parse(InlineParserContext $inlineContext) | ||
{ | ||
$cursor = $inlineContext->getCursor(); | ||
$savedState = $cursor->saveState(); | ||
|
||
$cursor->advance(); | ||
|
||
//check that the given user input is a valid codepen url | ||
//and the required `codepen:` prefix exists | ||
$regex = '/^(?:codepen)\s(https:\/\/codepen\.io\/([^\/]+\/)?([a-zA-Z0-9]+)\/pen\/([a-zA-Z0-9]+)?)/'; | ||
$validate = $cursor->match($regex); | ||
|
||
//the computer says no | ||
if (!$validate) { | ||
$cursor->restoreState($savedState); | ||
|
||
return false; | ||
} | ||
|
||
$matches = []; | ||
preg_match($regex, $validate, $matches); | ||
|
||
//return the given codepen url to the renderer class | ||
$inlineContext->getContainer()->appendChild(new Codepen($matches[1])); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace JohnnyHuy\Laravel\Inline\Parser; | ||
|
||
use JohnnyHuy\Laravel\Inline\Element\Gist; | ||
use League\CommonMark\InlineParserContext; | ||
use League\CommonMark\Inline\Parser\AbstractInlineParser; | ||
|
||
class GistParser extends AbstractInlineParser | ||
{ | ||
/** | ||
* @param InlineParserContext $inlineContext | ||
* @return bool | ||
*/ | ||
public function parse(InlineParserContext $inlineContext) | ||
{ | ||
$cursor = $inlineContext->getCursor(); | ||
$savedState = $cursor->saveState(); | ||
|
||
$cursor->advance(); | ||
|
||
//check that the given user input is a valid gist url | ||
//and the required `gist:` prefix exists | ||
$regex = '/^(?:gist)\s(https:\/\/gist.github.com\/([^\/]+\/)?([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)?)/'; | ||
$validate = $cursor->match($regex); | ||
|
||
//the computer says no | ||
if (!$validate) { | ||
$cursor->restoreState($savedState); | ||
|
||
return false; | ||
} | ||
|
||
$matches = []; | ||
preg_match($regex, $validate, $matches); | ||
|
||
//return the given gist url to the renderer class | ||
$inlineContext->getContainer()->appendChild(new Gist($matches[1])); | ||
|
||
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
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 | ||
|
||
namespace JohnnyHuy\Laravel\Inline\Renderer; | ||
|
||
use League\CommonMark\HtmlElement; | ||
use League\CommonMark\Util\Configuration; | ||
use JohnnyHuy\Laravel\Inline\Element\Codepen; | ||
use League\CommonMark\ElementRendererInterface; | ||
use League\CommonMark\Inline\Element\AbstractInline; | ||
use League\CommonMark\Util\ConfigurationAwareInterface; | ||
use League\CommonMark\Inline\Element\AbstractWebResource; | ||
use League\CommonMark\Inline\Renderer\InlineRendererInterface; | ||
|
||
class CodepenRenderer implements InlineRendererInterface | ||
{ | ||
/** | ||
* @var Configuration | ||
*/ | ||
protected $config; | ||
|
||
/** | ||
* @param AbstractInline|AbstractWebResource $inline | ||
* @param \League\CommonMark\ElementRendererInterface $htmlRenderer | ||
* | ||
* @return \League\CommonMark\HtmlElement|string | ||
* @throws \ErrorException | ||
*/ | ||
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) | ||
{ | ||
if (!($inline instanceof Codepen)) { | ||
throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline)); | ||
} | ||
|
||
// Use a oEmbed route to get codepen details | ||
$apiUrl = "https://codepen.io/api/oembed?url={$inline->getUrl()}&format=json"; | ||
|
||
$apiResponse = $this->getContent($apiUrl); | ||
|
||
//seems that the used codepen url is invalid | ||
//or codepen is currently not available | ||
if (is_null($apiResponse)) { | ||
throw new \ErrorException('Codepen request returned null: ' . $apiUrl); | ||
} | ||
|
||
//parse the oembed response | ||
$embed = json_decode($apiResponse); | ||
|
||
//return the oembed html snippet with a div as wrapper element | ||
return new HtmlElement('div', ['class' => 'codepen-container'], $embed->html); | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @return string | ||
*/ | ||
public function getContent(string $url): string | ||
{ | ||
return file_get_contents($url); | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
|
||
namespace JohnnyHuy\Laravel\Inline\Renderer; | ||
|
||
use League\CommonMark\HtmlElement; | ||
use League\CommonMark\Util\Configuration; | ||
use JohnnyHuy\Laravel\Inline\Element\Gist; | ||
use League\CommonMark\ElementRendererInterface; | ||
use League\CommonMark\Inline\Element\AbstractInline; | ||
use League\CommonMark\Util\ConfigurationAwareInterface; | ||
use League\CommonMark\Inline\Element\AbstractWebResource; | ||
use League\CommonMark\Inline\Renderer\InlineRendererInterface; | ||
|
||
class GistRenderer implements InlineRendererInterface | ||
{ | ||
/** | ||
* @var Configuration | ||
*/ | ||
protected $config; | ||
|
||
/** | ||
* @param AbstractInline|AbstractWebResource $inline | ||
* @param \League\CommonMark\ElementRendererInterface $htmlRenderer | ||
* | ||
* @return \League\CommonMark\HtmlElement|string | ||
*/ | ||
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) | ||
{ | ||
if (!($inline instanceof Gist)) { | ||
throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline)); | ||
} | ||
|
||
//generates the same script element, which you can see | ||
//in the "embed gist" input field | ||
$script = new HtmlElement('script', [ | ||
'src' => $inline->getUrl().'.js' | ||
]); | ||
|
||
//add a div wrapper around the script element | ||
return new HtmlElement('div', ['class' => 'gist-container'], $script); | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @return string | ||
*/ | ||
public function getContent(string $url) : string | ||
{ | ||
return file_get_contents($url); | ||
} | ||
} |
Oops, something went wrong.