]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/AsciiSVG.php
New FSF address
[SourceForge/phpwiki.git] / lib / plugin / AsciiSVG.php
1 <?php // -*-php-*-
2 // $Id$
3 /*
4  * Copyright 2007 $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://www1.chapman.edu/~jipsen/svg/asciisvg.html
25  * Requires ENABLE_XHTML_XML = true
26  * Syntax: http://www1.chapman.edu/~jipsen/svg/asciisvgcommands.html
27  */
28 class WikiPlugin_AsciiSVG
29 extends WikiPlugin
30 {
31     function getName() {
32         return _("AsciiSVG");
33     }
34
35     function getDescription() {
36         return _("Render inline ASCII SVG");
37     }
38
39     function getDefaultArguments() {
40         return array('width'  => 200,
41                      'height' => 200,
42                      'script' => false, // one line script. not very likely
43                      'onmousemove' => false
44                      );
45     }
46     function handle_plugin_args_cruft(&$argstr, &$args) {
47         $this->source = $argstr;
48     }
49
50     function run($dbi, $argstr, &$request, $basepage) {
51         global $WikiTheme;
52         $args = $this->getArgs($argstr, $request);
53         if (empty($this->source))
54             return '';
55         $html = HTML();
56         if (empty($WikiTheme->_asciiSVG)) {
57             $js = JavaScript('', array
58                              ('src' => $WikiTheme->_findData('ASCIIsvg.js')));
59             if (empty($WikiTheme->_headers_printed))
60                 $WikiTheme->addMoreHeaders($js);
61             else
62                 $html->pushContent($js);
63             $WikiTheme->_asciiSVG = 1; // prevent duplicates
64         }
65         // extract <script>
66         if (preg_match("/^(.*)<script>(.*)<\/script>/ism", $this->source, $m)) {
67             $this->source = $m[1];
68             $args['script'] = $m[2];
69         }
70         $embedargs = array('width'  => $args['width'],
71                            'height' => $args['height'],
72                            //'src'    => "d.svg",
73                            'script' => $this->source);
74         // additional onmousemove argument
75         if ($args['onmousemove']) $embedargs['onmousemove'] = $args['onmousemove'];
76         // we need script='data' and not script="data"
77         $embed = new AsciiSVG_HTML("embed", $embedargs);
78         $html->pushContent($embed);
79         if ($args['script']) $html->pushContent(JavaScript($args['script']));
80         return $html;
81     }
82 };
83
84 class AsciiSVG_HTML extends HtmlElement {
85     function startTag() {
86         $start = "<" . $this->_tag;
87         $this->_setClasses();
88         foreach ($this->_attr as $attr => $val) {
89             if (is_bool($val)) {
90                 if (!$val)
91                     continue;
92                 $val = $attr;
93             }
94             $qval = str_replace("\"", '&quot;', $this->_quote((string)$val));
95             if ($attr == 'script')
96                 // note the ' not "
97                 $start .= " $attr='$qval'";
98             else
99                 $start .= " $attr=\"$qval\"";
100         }
101         $start .= ">";
102         return $start;
103     }
104 }
105
106 // Local Variables:
107 // mode: php
108 // tab-width: 8
109 // c-basic-offset: 4
110 // c-hanging-comment-ender-p: nil
111 // indent-tabs-mode: nil
112 // End:
113 ?>