1<?php
2/**
3 * Plugin YouTube: Create YouTube link and object from ID.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Ikuo Obataya <I.Obataya[at]gmail.com
7 * @version    2008-04-05
8 * @update     2008-04-05
9 */
10
11if(!defined('DOKU_INC')) die();
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'syntax.php');
14
15class syntax_plugin_youtube extends DokuWiki_Syntax_Plugin {
16  var $html;
17  var $pattern;
18  function syntax_plugin_youtube(){
19    $this->html    = @file_get_contents(DOKU_PLUGIN.'youtube/object.htm');
20    $this->pattern = '/\{\{(\s?)youtube>(small|large|link):([^} |]+)\|?(.*?)(\s?)\}\}/';
21  }
22  function getInfo(){
23    return array(
24    'author' => 'Ikuo Obataya',
25    'email'  => 'I.Obataya@gmail.com',
26    'date'   => '2008-04-05',
27    'name'   => 'YouTube Plugin',
28    'desc'   => 'YouTube link and object{{youtube>[small|large|link]:ID}}',
29    'url'    => 'http://wiki.symplus.co.jp/computer/en/youtube_plugin',
30    );
31  }
32  function getType(){ return 'substition'; }
33  function getSort(){ return 159; }
34  function connectTo($mode) { $this->Lexer->addSpecialPattern('\{\{\s?youtube>[^}]*\s?\}\}',$mode,'plugin_youtube'); }
35
36  function handle($match, $state, $pos, &$handler){
37    $pm = preg_match_all($this->pattern,$match,$result);
38    $left  = ($result[1][0]==" ");
39    $right = ($result[5][0]==" ");
40    $cmd   = $result[2][0];
41    $id    = $result[3][0];
42    $title = $result[4][0];
43    if ($left==true && $right==true){
44      $align = 'center';
45    }else if($left==true){
46      $align = 'right';
47    }else if($right==true){
48      $align = 'left';
49    }
50    return array($state, array($cmd,$id,$align,$title));
51  }
52
53  function render($mode, &$renderer, $data){
54    if($mode != 'xhtml'){return false;}
55    list($state, $match) = $data;
56    list($cmd,$id,$align,$title) = $match;
57    $id    = urlencode($id);
58    $title = urlencode($title);
59    $title = str_replace("+"," ",$title);
60    switch($cmd){
61      case 'link':
62        $lnkFormat='<a href="http://www.youtube.com/watch?v=%s" title="%s">';
63        $href_start=sprintf($lnkFormat,$id,empty($title)?$id:$title.' ('.$id.')');
64        $renderer->doc.=$href_start.'<div class="youtube_icon">'.$title.'</div></a>';
65        return true;
66
67      case 'large':
68        if ($align=='center'){$renderer->doc.="<center>";}
69        $renderer->doc.=sprintf($this->html,425,350,$id,$align,$title,$id);
70        if ($align=='center'){$renderer->doc.="</center>";}
71        $renderer->doc.=NL;
72        return true;
73
74      case 'small':
75        if ($align=='center'){$renderer->doc.="<center>";}
76        $renderer->doc.=sprintf($this->html,255,210,$id,$align,$title,$id);
77        if ($align=='center'){$renderer->doc.="</center>";}
78        return true;
79    }
80    $renderer->doc.=NL;
81  }
82}
83?>