Added experimental support for aligning images
[mirrors/DokuWiki-Plugin-SVGEdit.git] / syntax.php
CommitLineData
b3a6ad9d
H
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
18if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
19if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
20require_once(DOKU_PLUGIN.'syntax.php');
21
22class 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',
ec719f7d 29 'date' => '2010-06-20',
b3a6ad9d
H
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) {
6711df56
H
41 $this->Lexer->addSpecialPattern("{{ ?svg>.+?}}", $mode, 'plugin_svgedit');
42 $this->Lexer->addSpecialPattern("{{ ?SVG>.+?}}", $mode, 'plugin_svgedit');
b3a6ad9d
H
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
76281a1f
H
51 function svg_base64_encode($svg) { //create base64 encoded svg for use as svglink in svg_format_embed
52 return 'data:image/svg+xml;base64,'.base64_encode($svg).'" type="image/svg+xml';
53 }
54
6711df56 55 function svg_format_embed($svglink, $title, $svg_parameters, $align='') { //create xhtml code for svg embeding
b3a6ad9d
H
56 global $ID;
57
3955ca30
H
58 //use object tag for stupid browsers (like firefox) - ugly (relies on browser identification)
59 $is_webkit= preg_match('/webkit/', strtolower($_SERVER['HTTP_USER_AGENT']));
906f00f3
H
60 if ($is_webkit) $svgtag='img src';
61 else $svgtag='object '.$svg_parameters.' data';
62 $svgtag_close = array_shift(preg_split('/ /', $svgtag, 2));
32165dcc 63
6711df56 64 return '<a href="'.$svglink.'" type="image/svg+xml" /><'.$svgtag.'="'.$svglink.'" class="media'.$align.'" alt="'.$title.'" title="'.$title.'" type="image/svg+xml">'."</$svgtag_close></a>";
32165dcc
H
65 }
66
67 function render($format, &$renderer, $data) {
68 if ($format!='xhtml') return;
69 global $ID;
70
71 $svg_wiki_page = trim(substr($data[1], 6, -2)); //name of wiki page containing SVG image
61ec98e5 72 resolve_pageid(getNS($ID),$svg_wiki_page,$exists); //resolve relative IDs
32165dcc 73
3aaf5eee
H
74 //detect image size for stupid browsers (like firefox) - ugly (fails if svg does not contain information about it's size)
75 $svg_dimensions = '';
76 preg_match('/width="[0-9]+" height="[0-9]+"/', $data[1].rawWiki($svg_wiki_page), $_);
77 if(isset($_[0])) $svg_dimensions = $_[0];
78
6711df56
H
79 // Check alignment
80 $ralign = (bool)preg_match('/^\{\{ /',$data[1]);
81 $lalign = (bool)preg_match('/ \}\}$/',$data[1]);
82
83 switch(true) {
84 case $lalign & $ralign: $align='center'; break;
85 case $ralign: $align='right'; break;
86 case $lalign: $align='left'; break;
87 default: $align='';
88 }
89
b3a6ad9d 90 if($data[0]==='<svg') {
76281a1f 91 $svgenc = $this->svg_base64_encode($data[1]);
d7de41a4 92 $renderer->doc .= $this->svg_format_embed($svgenc, 'inline-svg@'.$ID, $svg_dimensions);
b3a6ad9d
H
93 return true;
94 }
6711df56 95 if($data[0]==='{{sv' || $data[0]==='{{ s') {
32165dcc 96 $svglink = exportlink($svg_wiki_page,'svg');
6711df56
H
97 $renderer->doc .= $this->svg_format_embed($svglink, 'image:'.htmlspecialchars($svg_wiki_page), $svg_dimensions, $align);
98 $renderer->doc .= '<br /><small>'.html_wikilink($svg_wiki_page,'svg@'.$svg_wiki_page).'</small>';
b3a6ad9d
H
99 return true;
100 }
6711df56 101 if($data[0]==='{{SV' || $data[0]==='{{ S') {
76281a1f 102 $svgenc = $this->svg_base64_encode(rawWiki($svg_wiki_page));
6711df56
H
103 $renderer->doc .= $this->svg_format_embed($svgenc, 'image:'.htmlspecialchars($svg_wiki_page), $svg_dimensions, $align);
104 $renderer->doc .= '<br /><small>'.html_wikilink($svg_wiki_page,'SVG@'.$svg_wiki_page).'</small>';
b3a6ad9d
H
105 return true;
106 }
107 }
108}
This page took 0.382142 seconds and 4 git commands to generate.