Created
July 10, 2015 05:06
-
-
Save 0b10011/9017f38586404f161298 to your computer and use it in GitHub Desktop.
Revisions
-
0b10011 created this gist
Jul 10, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ <?php namespace CommonMark; use CommonMark\Inline\Processor\YouTubeVideoProcessor; use CommonMark\Inline\Renderer\YouTubeVideoRenderer; use League\CommonMark\Converter; use League\CommonMark\Environment; use League\CommonMark\DocParser; use League\CommonMark\HtmlRenderer; class CommonMarkConverter extends Converter { public function __construct(array $config = []){ $environment = new Environment($config); $environment->addExtension(new Extension\CommonMarkExtension()); $environment->addInlineProcessor(new YouTubeVideoProcessor()); $environment->addInlineRenderer( "CommonMark\Inline\Element\YouTubeVideo", new YouTubeVideoRenderer() ); parent::__construct( new DocParser($environment), new HtmlRenderer($environment) ); } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ <?php namespace CommonMark\Inline\Element; class YouTubeVideo extends \League\CommonMark\Inline\Element\Link {} This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ <?php namespace CommonMark\Inline\Processor; use CommonMark\Inline\Element\YouTubeVideo; use League\CommonMark\Inline\Processor\InlineProcessorInterface; use League\CommonMark\Delimiter\Delimiter; use League\CommonMark\Delimiter\DelimiterStack; use League\CommonMark\Inline\Element\Link; use League\CommonMark\Util\ArrayCollection; class YouTubeVideoProcessor implements InlineProcessorInterface { public function processInlines(ArrayCollection $inlines, DelimiterStack $delimiterStack, Delimiter $stackBottom = null) { foreach ($inlines as $key => $inline) { if (!($inline instanceof Link)) { continue; } $matched = preg_match( '%^https://(?:www\.youtube\.com/watch\?v=|youtu\.be/)([^&#]+)%', $inline->getUrl(), $matches ); if (!$matched) { continue; } $inlines->set($key, new YouTubeVideo( "https://youtu.be/$matches[1]", $inline->getChildren(), isset($inline->data['title']) ? $inline->data['title'] : null )); } } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ <?php namespace CommonMark\Inline\Renderer; use CommonMark\Inline\Element\YouTubeVideo; use League\CommonMark\HtmlElement; use League\CommonMark\HtmlRendererInterface; use League\CommonMark\Inline\Element\AbstractInline; use League\CommonMark\Inline\Renderer\LinkRenderer; class YouTubeVideoRenderer extends LinkRenderer { public function render( AbstractInline $inline, HtmlRendererInterface $htmlRenderer ){ if (!($inline instanceof YouTubeVideo)) { throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline)); } $matched = preg_match( '%^https://(?:www\.youtube\.com/watch\?v=|youtu\.be/)([^&#]+)%', $inline->getUrl(), $matches ); if(!$matched){ return parent::render($inline, $htmlRenderer); } if (count($inline->getParent()->getInlines()) > 1) { return parent::render($inline, $htmlRenderer); } $iframe = new HtmlElement('iframe', [ 'width' => 640, 'height' => 390, 'src' => "https://www.youtube.com/embed/$matches[1]", 'type' => "text/html", 'frameborder' => 0, ]); $div = new HtmlElement('span', ['class' => 'youtube-video'], $iframe); return $div; } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ .youtube-video { display:block; position: relative; padding-bottom: 56.25%; /* 16:9 */ padding-top: 25px; height: 0; > iframe { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } }