1<?php
2/**
3 * Plugin animation: combine a sequence of images to create an animation"
4 *
5 * Syntax:
6 <ani id url type max interval autoplay | opts>
7 *
8 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author     Yihui Xie <xie@yihui.name>
10 */
11
12if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
13
14if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16require_once(DOKU_PLUGIN.'syntax.php');
17
18class syntax_plugin_animation extends DokuWiki_Syntax_Plugin {
19    function getInfo() {
20      return array(
21		   'author' => 'Yihui Xie',
22		   'email'  => 'xie@yihui.name',
23		   'date'   => '2012-02-22',
24		   'name'   => 'Animation Plugin',
25		   'desc'   => 'Generate an animation from a sequence of images, e.g. 1.png, 2.png, ...',
26		   'url'    => 'https://github.com/yihui/dokuwiki',
27		   );
28    }
29    function getType() { return 'substition';}
30    function getSort() {
31      return 122;
32    }
33    function connectTo($mode) {
34      $this->Lexer->addSpecialPattern('<ani.*?>',$mode,'plugin_animation');
35    }
36    function handle($match, $state, $pos, &$handler) {
37      $source = trim(substr($match, 4, -1));
38      list($para,$opts) = preg_split('/\|/u',$source,2);
39      if (strpos($opts, "': ") === false) $opts = '';
40      // 1st version:
41      // list($id, $url, $type, $max, $height, $interval) = preg_split('/\s+/u', trim($para), 8);
42      // 2nd version:
43      // list($id, $url, $type, $max, $width, $height, $interval, $autoplay) = preg_split('/\s+/u', trim($para), 8);
44      list($id, $url, $type, $max, $interval, $autoplay, $navigator, $width) = preg_split('/\s+/u', trim($para), 8);
45      if (floatval($interval) > 100 & floatval($autoplay) > 0) {
46	if (floatval($autoplay) < 10) {
47	  // you are using the 1st version
48	  $interval = $autoplay;
49	  $autoplay = '';
50	} else {
51	  // you are using the 2nd version
52	  $interval = $navigator;
53	  $autoplay = $width;
54	}
55      }
56      return array($state, array($id, $url, $type, $max, $interval, $autoplay, $navigator, $width, $opts));
57    }
58    function render($mode, &$renderer, $data) {
59      if($mode == 'xhtml'){
60	list($state, $match) = $data;
61	list($id, $url, $type, $max, $interval, $autoplay, $navigator, $width, $opts) = $match;
62	$id = 'animation_' . str_replace(array("!", '"', "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", ".", "/", ":", ";", "?", "@", "[", "]", "^", "`", "{", "|", "}", "~"), "_", $id);
63	if ($autoplay == 'autoplay') {
64	  $autoplay = "$('#$id').scianimator('play');";
65	} else {
66	  $autoplay = '';
67	}
68	if ($navigator != 'true') $opts = "'controls': ['first', 'previous', 'play', 'next', 'last', 'loop', 'speed'], " . $opts;
69	if (floatval($width) > 0) $opts = "'width': $width, " . $opts;
70	// if you have your format, just use yours, otherwise use the default %d
71	if (preg_match("/%[0-9]+d$/", $url) == 0) $url .= "%d";
72	$imglist = '';
73	for ($imgnum = 1; $imgnum <= intval($max); $imgnum++) {
74		$imglist .= "'" . sprintf($url, $imgnum) . '.' . $type . "', ";
75	}
76	$renderer->doc .=
77
78	  "   <div class=\"scianimator\"><div id=\"$id\" style=\"display: inline-block;\"></div></div>
79    <script type=\"text/javascript\">
80    (function($) {
81        $(document).ready(function() {
82
83        $('#$id').scianimator({
84            'images': [$imglist],
85            'delay': ". floatval($interval) * 1000 .",
86            $opts
87        });
88        ".
89	  $autoplay
90	  . "
91        });
92    })(jQuery);
93    </script>
94" .
95	  '';
96	return true;
97      }
98      return false;
99    }
100}
101