1<?php
2/**
3 * Plugin Mozshot: Inserts a website screenshot.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Takao Yokoyama <cb.yokoyama@gmail.com>
7 */
8
9// must be run within DokuWiki
10if(!defined('DOKU_INC')) die();
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once DOKU_PLUGIN.'syntax.php';
14
15class syntax_plugin_mozshot extends DokuWiki_Syntax_Plugin {
16	function getInfo() {
17		return array(
18			'author' => 'Takao Yokoyama',
19			'email'  => 'cb.yokoyama@gmail.com',
20			'date'   => '2010-03-13',
21			'name'   => 'Mozshot Plugin',
22			'desc'   => 'Inserts a website screenshot',
23			'url'    => 'http://dokuwiki.org/plugin:mozshot');
24 	}
25	var $pattern;
26	function syntax_plugin_mozshot(){
27		$this->pattern = '/\{\{(\s?)mozshot>(small|large)?:?([^} |]+)\|?(.*?)(\s?)\}\}/';
28	}
29
30  function getType() { return 'substition'; }
31		function getSort() { return 32; }
32		function connectTo($mode) {
33      $this->Lexer->addSpecialPattern('\{\{\s?mozshot>[^}]*\s?\}\}',$mode,'plugin_mozshot');
34  }
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		$url    = $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,$url,$align,$title));
51  }
52
53	function render($mode, &$renderer, $data) {
54		if($mode != 'xhtml'){ return false;}
55	  list($state, $match) = $data;
56		list($cmd,$url,$align,$title) = $match;
57		switch($cmd) {
58		case 'small':
59			if($align == 'center'){$renderer->doc .= "<center>";}
60			$renderer->doc.=sprintf("<a href='".$url."' target='_blank'><img src='http://mozshot.nemui.org/shot/80x60?"."$url' alt='screenshot' title='".$title."'/></a>");
61			if($align == 'center'){$renderer->doc .= "</center>";}
62			$renderer->doc.=NL;
63     return true;
64		case 'large':
65			if($align == 'center'){$renderer->doc .= "<center>";}
66			$renderer->doc.=sprintf("<a href='".$url."' target='_blank'><img src='http://mozshot.nemui.org/shot/200x150?"."$url' alt='screenshot' title='".$title."'/></a>");
67			if($align == 'center'){$renderer->doc .= "</center>";}
68			$renderer->doc.=NL;
69			return true;
70		default :
71			if($align == 'center'){$renderer->doc .= "<center>";}
72			$renderer->doc.=sprintf("<a href='".$url."' target='_blank'><img src='http://mozshot.nemui.org/shot/120x90?"."$url' alt='screenshot' title='".$title."'/></a>");
73			if($align == 'center'){$renderer->doc .= "</center>";}
74			$renderer->doc.=NL;
75			return true;
76		}
77		$renderer->doc.=NL;
78	}
79}
80