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