started moving svg embeding to function
[mirrors/DokuWiki-Plugin-SVGEdit.git] / syntax.php
1 <?php
2 /**
3 * SVGEdit Plugin: Nice way, to create, store, edit and embed SVG images into DokuWiki
4 * Usage:
5 * embed svg using do=export_svg
6 * {{svg>page.svg}}
7 * {{svg>namespace:page.svg}}
8 * base64 encode svg directly (requires ~~NOCACHE~~)
9 * {{SVG>page.svg}}
10 * {{SVG>namespace:page.svg}}
11 * base64 encode inline svg directly
12 * <svg args...>...code...</svg>
13 *
14 * @license Copylefted
15 * @author Thomas Mudrunka <harvie--email-cz>
16 */
17
18 if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
19 if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
20 require_once(DOKU_PLUGIN.'syntax.php');
21
22 class syntax_plugin_svgedit extends DokuWiki_Syntax_Plugin {
23
24 var $helper = null;
25
26 function getInfo() {
27 return array('author' => 'Thomas Mudrunka',
28 'email' => 'harvie--email-cz',
29 'date' => '2010-02-21',
30 'name' => 'SVG-Edit Plugin',
31 'desc' => 'Nice way, to create, store, edit and embed SVG images into DokuWiki',
32 'url' => 'http://www.dokuwiki.org/plugin:svgedit'
33 );
34 }
35
36 function getType() { return 'substition'; }
37 function getSort() { return 303; }
38 function getPType() { return 'block'; }
39
40 function connectTo($mode) {
41 $this->Lexer->addSpecialPattern("{{svg>.+?}}", $mode, 'plugin_svgedit');
42 $this->Lexer->addSpecialPattern("{{SVG>.+?}}", $mode, 'plugin_svgedit');
43 $this->Lexer->addSpecialPattern("<svg.+?</svg>", $mode, 'plugin_svgedit');
44 }
45
46 function handle($match, $state, $pos, &$handler) {
47 $type = substr($match,0,4);
48 return array($type, $match);
49 }
50
51 function format_svg_embed($svglink, $alt) {
52 global $ID;
53
54 //detect image size for stupid browsers (like firefox) - ugly (fails if svg does not contain information about it's size)
55 $svg_dimensions = '';
56 preg_match('/width="[0-9]+" height="[0-9]+"/', $data[1].rawWiki($svg_wiki_page), $_);
57 if(isset($_[0])) $svg_dimensions = $_[0];
58
59 //use object tag for stupid browsers (like firefox) - ugly (relies on browser identification)
60 $is_webkit= preg_match('/webkit/', strtolower($_SERVER['HTTP_USER_AGENT']));
61 if ($is_webkit)
62 $svgtag='img src';
63 else
64 $svgtag='object '.$svg_dimensions.' data';
65
66 return '<a href="'.$svglink.'" type="image/svg+xml" /><'.$svgtag.'="'.$svglink.'" alt="'.$alt.'" type="image/svg+xml" /></a>'."<br />";
67 }
68
69 function render($format, &$renderer, $data) {
70 if ($format!='xhtml') return;
71 global $ID;
72
73 $svg_wiki_page = trim(substr($data[1], 6, -2)); //name of wiki page containing SVG image
74
75
76
77
78 if($data[0]==='<svg') {
79 $svglink = 'data:image/svg+xml;base64,'.base64_encode($data[1]).'" type="image/svg+xml';
80 $renderer->doc .= $this->format_svg_embed($svglink, 'svg-image@'.$ID);
81 return true;
82 }
83 if($data[0]==='{{sv') {
84 $svglink = exportlink($svg_wiki_page,'svg');
85 $renderer->doc .= $this->format_svg_embed($svglink, 'image:'.htmlspecialchars($svg_wiki_page));
86 //$renderer->doc .= '<a href="'.$svglink.'" type="image/svg+xml" />'.$svgtag.$svglink.'" alt="image:'.htmlspecialchars($svg_wiki_page).'" type="image/svg+xml"/></a><br />';
87 $renderer->doc .= html_wikilink($svg_wiki_page,'svg@'.$svg_wiki_page);
88 return true;
89 }
90 if($data[0]==='{{SV') {
91 $svglink = 'data:image/svg+xml;base64,'.base64_encode(rawWiki($svg_wiki_page)).'" type="image/svg+xml';
92 $renderer->doc .= $this->format_svg_embed($svglink, 'image:'.htmlspecialchars($svg_wiki_page));
93 //$renderer->doc .= '<a href="'.$svglink.'" type="image/svg+xml" />'.$svgtag.$svglink.'" alt="image:'.htmlspecialchars($svg_wiki_page).'" /></a><br />';
94 $renderer->doc .= html_wikilink($svg_wiki_page,'SVG@'.$svg_wiki_page);
95 return true;
96 }
97 }
98 }
This page took 0.311695 seconds and 4 git commands to generate.