1<?php 2/** 3 * DokuWiki Plugin stlviewer (Syntax Component) 4 * 5 * @author Damien Degois <damien@degois.info> 6 * @license MIT 7 * 8 * Options are URL encoded a=1&b=2 etc 9 * - s: size in pixel (defines height and width in one call) 10 * - h: height in pixel 11 * - w: width in pixel 12 * - color: color of the object 13 * - bgcolor: background color 14 * - display: Model shading to display (String: "flat" / "smooth"/ "wireframe", Default: "flat") 15 * 16 */ 17 18// must be run within Dokuwiki 19if(!defined('DOKU_INC')) die(); 20if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 21require_once DOKU_PLUGIN . 'syntax.php'; 22 23class syntax_plugin_stlviewer extends DokuWiki_Syntax_Plugin { 24 25 public function getType() { 26 return 'substition'; 27 } 28 29 public function getPType() { 30 return 'normal'; 31 } 32 33 public function getSort() { 34 return 302; 35 } 36 37 public function connectTo($mode) { 38 $this->Lexer->addSpecialPattern('\{\{[^\}]*?(?:\.stl)[^\}]*?\}\}', $mode, 'plugin_stlviewer'); 39 } 40 41 /** 42 * handle syntax 43 */ 44 public function handle($match, $state, $pos, Doku_Handler $handler) { 45 46 $opts = array( // set default 47 'id' => '', 48 'pos' => $pos, 49 'title' => '', 50 'width' => '500px', 51 'height' => '500px', 52 'bgcolor' => '#aaaaaa', 53 'color' => '#be5050', 54 'display' => 'flat', 55 ); 56 57 $cleanmatch = trim($match, '{}'); 58 if (strpos($cleanmatch, ">") === false) { 59 $params="stlviewer"; 60 $media = $cleanmatch; 61 } else { 62 list($params, $media) = explode('>', $cleanmatch, 2); 63 } 64 65 // handle media parameters (linkId and title) 66 list($link, $title) = explode('|', $media, 2); 67 68 list($id, $args) = explode('?', $link, 2); 69 $args = trim($args); 70 parse_str($args, $pargs); 71 72 // msg('stlviewer args: ' . print_r($pargs,1), 1); 73 74 if (isset($pargs['s'])) { 75 $opts['width'] = $pargs['s']; 76 $opts['height'] = $pargs['s']; 77 } 78 if (isset($pargs['h'])) { 79 $opts['height'] = $pargs['h']; 80 } 81 if (isset($pargs['w'])) { 82 $opts['width'] = $pargs['w']; 83 } 84 if (isset($pargs['noop'])) { 85 $opts['noop'] = true; 86 } 87 if (isset($pargs['manual'])) { 88 $opts['manual'] = true; 89 } 90 if (isset($pargs['display'])) { 91 $opts['display'] = $pargs['display']; 92 } 93 94 foreach (['bgcolor', 'color'] as $k) { 95 if (isset($pargs[$k]) && $pargs[$k] != "") { 96 $opts[$k] = $pargs[$k]; 97 } 98 } 99 100 //add default px unit 101 if(is_numeric($opts['width'])) $opts['width'] .= 'px'; 102 if(is_numeric($opts['height'])) $opts['height'] .= 'px'; 103 104 $opts['id'] = trim($id); 105 if ($opts['title'] == "") { 106 $opts['title'] = $id; 107 } 108 if (!empty($title)) { 109 $opts['title'] = trim($title); 110 } 111 112 return array($state, $opts); 113 } 114 115 public function render($format, Doku_Renderer $renderer, $data) { 116 117 if ($format != 'xhtml') { 118 return false; 119 } 120 121 list($state, $opts) = $data; 122 if ($opts['id'] == '') { 123 return false; 124 } 125 126 $mediaurl = DOKU_URL . "lib/exe/fetch.php?media=" . $opts['id']; 127 if (isset($opts['noop'])) { 128 $renderer->doc .= "<a href=\"" . $mediaurl . "\">".$opts['id']."</a>"; 129 return false; 130 } 131 132 $buff = array(); 133 $buff[] = "<b>".$opts['title']."</b><br/>"; 134 $buff[] = "<div id=\"stl_cont".$opts['pos']."\" class=\"media\">"; 135 $buff[] = "<a href=\"javascript:init_stl_".$opts['pos']."()\">Show stl</a>"; 136 $buff[] = "</div>"; 137 $buff[] = "<script>"; 138 $buff[] = "function init_stl_".$opts['pos']."() {"; 139 $buff[] = " var destdiv = document.getElementById(\"stl_cont".$opts['pos']."\");"; 140 $buff[] = " destdiv.innerHTML = \"\";"; 141 $buff[] = " destdiv.style.height = \"".$opts['height']."\";"; 142 $buff[] = " destdiv.style.width = \"".$opts['width']."\";"; 143 $buff[] = " var destdiv = document.getElementById(\"stl_cont".$opts['pos']."\");"; 144 $buff[] = " var stl_viewer".$opts['pos']."=new StlViewer("; 145 $buff[] = " destdiv,"; 146 $buff[] = " {"; 147 $buff[] = " load_three_files: \"" . DOKU_URL . "lib/plugins/stlviewer/stlviewer/\","; 148 $buff[] = " auto_rotate: false,"; 149 $buff[] = " controls: 1,"; 150 $buff[] = " cameray: 100,"; 151 $buff[] = " canvas_width: \"".$opts['width']."\","; 152 $buff[] = " canvas_height: \"".$opts['height']."\","; 153 $buff[] = " bg_color: \"".$opts['bgcolor']."\","; 154 $buff[] = " models: [ {"; 155 $buff[] = " id:0,"; 156 $buff[] = " color: \"".$opts['color']."\","; 157 $buff[] = " display: \"".$opts['display']."\","; 158 $buff[] = " filename: \"" . $mediaurl . "\""; 159 $buff[] = " } ]"; 160 $buff[] = " }"; 161 $buff[] = " );"; 162 $buff[] = "}"; 163 if (!$opts['manual']) { 164 $buff[] = "document.addEventListener(\"DOMContentLoaded\", init_stl_".$opts['pos'].");"; 165 } 166 $buff[] = ""; 167 $buff[] = "</script>"; 168 $buff[] = "<a href=\"" . DOKU_URL . ml($opts['id']) . "\" title=\"".$opts['id']."\">Download</a><br/>"; 169 170 $renderer->doc .= implode("\n", $buff); 171 172 return true; 173 } 174} 175