1<?php 2/** 3 * DokuWiki Plugin googledrawing (Syntax Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Linus Brimstedt <linus@brimstedt.se> 7 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) die(); 11 12if (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 15 16require_once DOKU_PLUGIN.'syntax.php'; 17 18class syntax_plugin_googledrawing extends DokuWiki_Syntax_Plugin { 19 public function getType() { 20 return 'substition'; 21 } 22 23 public function getPType() { 24 return 'normal'; 25 } 26 27 public function getSort() { 28 // Must be before external link (330) 29 return 305; 30 } 31 32 33 private function getGoogleUrl() 34 { 35 $url = $this->getConf('googleUrl'); 36 if(substr($url, -1) != '/') 37 { 38 $url .= '/'; 39 } 40 return $url; 41 42 } 43 44 // Url for use to linking to google image document 45 private function getImageUrl($imageId) 46 { 47 $baseUrl = $this->getGoogleUrl(); 48 $imageUrl = $baseUrl."drawings/d/".$imageId; 49 return $imageUrl; 50 } 51 52 public function connectTo($mode) { 53 $this->Lexer->addSpecialPattern('{{gdraw>.*?}}',$mode,'plugin_googledrawing'); 54 55 $baseUrl = $this->getGoogleUrl(); 56 $baseUrl = str_replace('/', '\\/', $baseUrl); 57 58 $this->Lexer->addSpecialPattern( 59 "{$baseUrl}drawings\\/(?:[a-z]+\\?id=|d\\/)[a-zA-Z\\-_0-9]+(?:\\/edit)?(?:\\&w=[0-9]+)?(?:\\&h=[0-9]+)?", 60 $mode, 61 'plugin_googledrawing'); 62 } 63 64 65 public function handle($match, $state, $pos, Doku_Handler $handler){ 66 67 $data = array( 68 'id' => '1EJyBXdSdnJ1mi6h371GXWdL0lz0Oj0lO9vcq_burdfs', 69 'width' => 0, 70 'height' => 0, 71 'align' => '', 72 'title' => '', 73 ); 74 75 // Code shamelessly stolen from gchart plugin 76 $lines = explode("\n",$match); 77 $conf = array_shift($lines); 78 array_pop($lines); 79 80 81 // Check for our special {{ }} syntax 82 if(substr($match, 0, 2) == '{{') 83 { 84 if(preg_match('/\bgdraw>([^ ]+?) /i',$conf,$match)) $data['id'] = $match[1]; 85 if(preg_match('/\b(left|center|right)\b/i',$conf,$match)) $data['align'] = $match[1]; 86 if(preg_match('/\btitle="([^"]+?)"/i',$conf,$match)) $data['title'] = $match[1]; 87 if(preg_match('/\b(width|height)=([^\b\}]+?)(\b|})/i',$conf,$match)) $data[$match[1]] = $match[2]; 88 } 89 // Parse a drawings URL 90 else 91 { 92 preg_match("/{$baseUrl}drawings\\/(?:[a-z]+\\?id=|d\\/)([^\\/\\&]+)(?:\\/edit)?(?:\\&w=([0-9]+))?(?:\\&h=([0-9]+))?/", 93 $conf, 94 $match); 95 96 $data['id'] = $match[1]; 97 $data['width'] = $match[2]; 98 $data['height'] = $match[3]; 99 } 100 if($data['width'] == null) 101 { 102 $data['width'] = $this->getConf('defaultImageWidth'); 103 } 104 if($data['height'] == null) 105 { 106 $data['height'] = $this->getConf('defaultImageHeight'); 107 } 108 109 return $data; 110 } 111 112 // TODO: Clean up the image tag generated in this function 113 public function render($mode, Doku_Renderer $renderer, $data) { 114 if($mode != 'xhtml') return false; 115 116 $baseUrl = $this->getGoogleUrl(); 117 $imageUrl = $this->getImageUrl($data['id']); 118 $tag = '<img'; 119 $url = ' src="' . $baseUrl . 'drawings/pub?id=' . $data['id']; 120 if($data['width'] > 0) 121 { 122 $url .= '&w=' . $data['width']; 123 $tag .= ' width="' . $data['width'] . '"'; 124 } 125 126 if($data['height'] > 0) 127 { 128 $url .= '&h=' . $data['height']; 129 $tag .= ' height="' . $data['height'] . '"'; 130 } 131 132 $tag .= ' title="' . $data['title'] . " \n(Click to edit)\""; 133 $tag .= ' onError="this.title=\'If the image does not show up, log on to your google docs account!\';"'; 134 if($data['align'] == 'left') 135 $tag .= ' style="display: block; float: left;"'; 136 if($data['align'] == 'right') 137 $tag .= ' style="display: block; float: right;"'; 138 if($data['align'] == 'center') 139 $tag .= ' style="display: block; margin-left: auto; margin-right: auto"'; 140 141 $url .= '"'; 142 $tag .= $url . ' />'; 143 $tag = "<a target='_blank' href='{$imageUrl}'>{$tag}</a>"; 144 $renderer->doc .= $tag; 145 146 return true; 147 } 148} 149