1<?php 2/** 3 * Plugin svgpureInsert: Inserts a non png or other modified svg file, just its pure version 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Leszek Piatek <lpiatek@gmail.com> 7 */ 8 9if(!defined('DOKU_INC')) exit; 10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 11 12 13/** 14 * All DokuWiki plugins to extend the parser/rendering mechanism 15 * need to inherit from this class 16 */ 17class syntax_plugin_svgpureinsert extends DokuWiki_Syntax_Plugin { 18 19 function getType() { 20 return 'substition'; 21 } 22 23 /** 24 * Returns a lower sort than image syntax 25 * 26 * @return int 319 27 */ 28 function getSort() { 29 return 319; 30 } 31 32 /** 33 * Register pattern 34 * 35 * Just like image syntax but grab any .svg 36 * 37 * @param string $mode 38 */ 39 function connectTo($mode) { 40 $this->Lexer->addSpecialPattern('\{\{[^\}]+?(?:\.svg)[^\}]*?\}\}', $mode, 'plugin_svgpureinsert'); 41 } 42 43 /** 44 * Parse parameters from syntax 45 * 46 * @param string $match 47 * @param int $state 48 * @param int $pos 49 * @param Doku_Handler $handler 50 * @return array|bool 51 */ 52 function handle($match, $state, $pos, Doku_Handler $handler) { 53 // default data 54 $data = array( 55 'id' => '', 56 'title' => '', 57 'align' => '', 58 'width' => 0, 59 'height' => 0, 60 'cache' => 'cache' 61 ); 62 63 $match = substr($match, 2, -2); 64 list($id, $title) = explode('|', $match, 2); 65 66 // alignment 67 if(substr($id, 0, 1) == ' ') { 68 if(substr($id, -1, 1) == ' ') { 69 $data['align'] = 'center'; 70 } else { 71 $data['align'] = 'right'; 72 } 73 } elseif(substr($id, -1, 1) == ' ') { 74 $data['align'] = 'left'; 75 } 76 77 list($id, $params) = explode('?', $id, 2); 78 79 // id and title 80 $data['id'] = trim($id); 81 $data['title'] = trim($title); 82 83 // size 84 if(preg_match('/(\d+)(x(\d+))?/', $params, $m)) { 85 $data['width'] = (int) $m[1]; 86 $data['height'] = (int) $m[3]; 87 } 88 89 // read missing size values from the file itself 90 if(!$data['width'] || !$data['height']) { 91 /** @var helper_plugin_svgpureinsert $hlp */ 92 $hlp = plugin_load('helper', 'svgpureinsert'); 93 $res = $hlp->getAdjustedSVG($data['id']); 94 if($res) { 95 list(, $w, $h) = $res; 96 97 if(!$data['width']) { 98 $data['width'] = $w; 99 $data['height'] = $h; 100 } else { 101 $data['height'] = ceil($data['width'] * $h / $w); 102 } 103 } 104 } 105 106 return $data; 107 } 108 109 function render($format, Doku_Renderer $renderer, $data) { 110 if($format != 'xhtml') return false; 111 112 $attr = array( 113 'src' => ml($data['id'], array('w'=>$data['width'], 'h'=>$data['height']), true, '&'), 114 'width' => $data['width'], 115 'height' => $data['height'], 116 'class' => 'svgpureinsert media'.$data['align'], 117 'frameborder' => 0, 118 'title' => $data['title'] 119 ); 120 121 $renderer->doc .= '<iframe '.buildAttributes($attr).'></iframe>'; 122 return true; 123 } 124} 125