Skip to content

Commit

Permalink
add files and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
james2doyle committed Mar 29, 2014
0 parents commit 682143e
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
<div class="youtube-video"><iframe width="853" height="480" src="//www.youtube.com/embed/8GLMe371RuI?rel=0" frameborder="0" allowfullscreen=""></iframe><div class="youtube-title"><h2>All About PhileCMS</h2></div></div>
```

### 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
<div class="youtube-video"><iframe width="853" height="480" src="//www.youtube.com/embed/8GLMe371RuI?rel=0" frameborder="0" allowfullscreen=""></iframe><div class="youtube-title"><h2>All About PhileCMS</h2></div></div>
```

#### 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.
10 changes: 10 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return array(
'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
);
52 changes: 52 additions & 0 deletions plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* Render a youtube video based on it's ID
* Usage: {{ youtube(id) }}
* Usage: youtube=id
*/

class PhileYoutube extends \Phile\Plugin\AbstractPlugin implements \Phile\EventObserverInterface {
public function __construct() {
\Phile\Event::registerEvent('template_engine_registered', $this);
\Phile\Event::registerEvent('after_parse_content', $this);
}

public function get_video($id)
{
$title = "";
if ($this->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>(.+?)<\/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);
}
}
}

0 comments on commit 682143e

Please sign in to comment.