]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Processing.php
New FSF address
[SourceForge/phpwiki.git] / lib / plugin / Processing.php
1 <?php // -*-php-*-
2 // $Id$
3 /**
4  * Copyright 2009 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * Interface to http://ejohn.org/blog/processingjs/
25  * Syntax: http://ejohn.org/blog/overview-of-processing/
26  */
27 class WikiPlugin_Processing
28 extends WikiPlugin
29 {
30     function getName() {
31         return _("Processing");
32     }
33
34     function getDescription() {
35         return _("Render inline Processing");
36     }
37
38     function getDefaultArguments() {
39         return array('width'  => 200,
40                      'height' => 200,
41                      'script' => false, // one line script. not very likely
42                      'onmousemove' => false
43                      );
44     }
45     function handle_plugin_args_cruft(&$argstr, &$args) {
46         $this->source = $argstr;
47     }
48
49     function run($dbi, $argstr, &$request, $basepage) {
50         global $WikiTheme;
51         $args = $this->getArgs($argstr, $request);
52         if (empty($this->source))
53             return '';
54         $html = HTML();
55         if (empty($WikiTheme->_asciiSVG)) {
56             $js = JavaScript('', array
57                              ('src' => $WikiTheme->_findData('Processing.js')));
58             if (empty($WikiTheme->_headers_printed))
59                 $WikiTheme->addMoreHeaders($js);
60             else
61                 $html->pushContent($js);
62             $WikiTheme->_processing = 1; // prevent duplicates
63         }
64         // extract <script>
65         if (preg_match("/^(.*)<script>(.*)<\/script>/ism", $this->source, $m)) {
66             $this->source = $m[1];
67             $args['script'] = $m[2];
68         }
69         $embedargs = array('width'  => $args['width'],
70                            'height' => $args['height'],
71                            //'src'    => "d.svg",
72                            'script' => $this->source);
73         // additional onmousemove argument
74         if ($args['onmousemove']) $embedargs['onmousemove'] = $args['onmousemove'];
75         // we need script='data' and not script="data"
76         $embed = new Processing_HTML("embed", $embedargs);
77         $html->pushContent($embed);
78         if ($args['script']) $html->pushContent(JavaScript($args['script']));
79         return $html;
80     }
81 };
82
83 class Processing_HTML extends HtmlElement {
84     function startTag() {
85         $start = "<" . $this->_tag;
86         $this->_setClasses();
87         foreach ($this->_attr as $attr => $val) {
88             if (is_bool($val)) {
89                 if (!$val)
90                     continue;
91                 $val = $attr;
92             }
93             $qval = str_replace("\"", '&quot;', $this->_quote((string)$val));
94             if ($attr == 'script')
95                 // note the ' not "
96                 $start .= " $attr='$qval'";
97             else
98                 $start .= " $attr=\"$qval\"";
99         }
100         $start .= ">";
101         return $start;
102     }
103 }
104
105 // Local Variables:
106 // mode: php
107 // tab-width: 8
108 // c-basic-offset: 4
109 // c-hanging-comment-ender-p: nil
110 // indent-tabs-mode: nil
111 // End:
112 ?>