diff --git a/README.md b/README.md new file mode 100644 index 0000000..8a1fe55 --- /dev/null +++ b/README.md @@ -0,0 +1,73 @@ +phileYoutube +=========== + +A plugin that generates Youtube videos based on IDs. It can be used in your theme, or in your Markdown. + +### Features + +* use in Markdown +* use in templates with Twig +* auto grab the title + +### Installation + +* Install [Phile](https://github.com/PhileCMS/Phile) +* Clone this repo into `plugins/phileYoutube` +* add `$config['plugins']['phileYoutube'] = array('active' => true);` to your `config.php` + +### Markdown Usage + +You can use this plugin in your Markdown files. It allows videos to be easy rendered without using any HTML in your Markdown. + +#### Basic Examples: + +Put the code in there. Watch the HTML spew out. + +```html +youtube=8GLMe371RuI +``` + +Output: + +```html +

All About PhileCMS

+``` + +### Theme Usage + +There will now be a new twig function called `youtube`. It takes a YouTube ID, and renders the HTML for the video! + +#### Basic Examples: + +Put the code in there. Watch the HTML spew out. *Assumes you have set `Video: 8GLMe371RuI` in your pages meta*. + +```html +{{ youtube(meta.video) }} +``` + +Output: + +```html +

All About PhileCMS

+``` + +#### Config + +Here are the settings. See the above output for where everything goes. + +``` +'wrapper_class' => 'youtube-video', // parent class for iframe +'title_class' => 'youtube-title', // div clas for video title +'show_title' => true, // get the title +'title_tag' => 'h2', // wrap the title in this tag, can be false for none +'video_height' => 480, // standard height +'video_width' => 853 // standard width +``` + +**Fixed heights and widths?** + +Because clients are crazy and will try to add a video at a bad size. I like to control what they are doing in the theme. I know what size will work better than they do. + +### Why Use? + +Clients are crazy. You want to make sure the HTML output is good and not a huge mess. Also autofetching the title is sweet. diff --git a/config.php b/config.php new file mode 100644 index 0000000..e76352d --- /dev/null +++ b/config.php @@ -0,0 +1,10 @@ + 'youtube-video', // parent class for iframe + 'title_class' => 'youtube-title', // div clas for video title + 'show_title' => true, // get the title + 'title_tag' => 'h2', // wrap the title in this tag, can be false for none + 'video_height' => 480, // standard height + 'video_width' => 853 // standard width +); diff --git a/plugin.php b/plugin.php new file mode 100644 index 0000000..9b33e63 --- /dev/null +++ b/plugin.php @@ -0,0 +1,52 @@ +settings['show_title']) { + // returns a single line of XML that contains the video title. + // Not a giant request. Use '@' to suppress errors. + $videoTitle = @file_get_contents("http://gdata.youtube.com/feeds/api/videos/{$id}?v=2&fields=title"); + // look for that title tag and get the insides + preg_match("/(.+?)<\/title>/is", $videoTitle, $titleOfVideo); + $tags = array('',''); + if ($this->settings['title_tag']) { + $tags = array("<{$this->settings['title_tag']}>", "</{$this->settings['title_tag']}>"); + } + $title = "<div class=\"{$this->settings['title_class']}\">{$tags[0]}{$titleOfVideo[1]}{$tags[1]}</div>"; + } + return "<div class=\"{$this->settings['wrapper_class']}\"><iframe width=\"{$this->settings['video_width']}\" height=\"{$this->settings['video_height']}\" src=\"//www.youtube.com/embed/{$id}?rel=0\" frameborder=\"0\" allowfullscreen></iframe>{$title}</div>"; + } + + public function on($eventKey, $data = null) { + if ($eventKey == 'template_engine_registered') { + $youtube = new Twig_SimpleFunction('youtube', function ($id) { + return $this->get_video($id); + }); + $data['engine']->addFunction($youtube); + } else if ($eventKey == 'after_parse_content') { + // store the starting content + $content = $data['content']; + // this parse happens after the markdown + // which means that the potential ID is wrapped + // in p tags + $regex = "/(<p>)(youtube)=(.*?)(<\/p>)/"; + // add the modified content back in the data + $data['content'] = preg_replace_callback($regex, function($matches) { + return $this->get_video($matches[3]); + }, $content); + } + } +}