last update: Mon, 05 Sep 2011 17:16:52 +0000
  1. <?
  2. class jass_formater
  3. {
  4. function fill_line_array($_text, &$text_line_array)
  5. {
  6. //$time = microtime(1);
  7. $line_start = array();
  8. $line_end = array();
  9. $line = 0;
  10. $pos = 0;
  11. $line_start[$line] = 0;
  12. while ($pos = (strpos($_text,"<br />",$pos)))
  13. {
  14. $line_end[$line] = $pos;
  15. $line++;
  16. $pos+=strlen("<br />");
  17. $line_start[$line] = $pos+2;
  18. }
  19. for ($i=0;$i<=$line+1;$i++)
  20. {
  21. if ($line > $i)
  22. {
  23. if ($i > 0)
  24. $text_line_array[$i] = rtrim(substr($_text, $line_start[$i], $line_end[$i] - $line_start[$i]));
  25. else
  26. $text_line_array[$i] = rtrim(substr($_text, $line_start[$i], $line_end[$i] - $line_start[$i]));
  27. }
  28. elseif ($line == $i)
  29. {
  30. $text_line_array[$i] = rtrim(substr($_text, $line_start[$i]));
  31. }
  32. else
  33. {
  34. //$text_line_array[$i] = 0;
  35. }
  36. }
  37. //echo "parsing done in ".(microtime(1)-$time)." secs<br />" ;
  38. }
  39. function fill_line_array2($s)
  40. {
  41. return explode("\n",$s);
  42. }
  43. function begins_with_keyword($line,$keywords,$ignore)
  44. {
  45. $line2 = $line;
  46. foreach ($ignore as $i)
  47. {
  48. $line2 = str_replace($i,"",$line2);
  49. }
  50. foreach ($keywords as $k)
  51. {
  52. if (substr($line2,0,strlen($k)) == $k)
  53. {
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. function get_first_word($line)
  60. {
  61. return substr(ltrim($line),0,strpos($line," "));
  62. }
  63. function strpos2($string,$suchwort,$index=0)
  64. {
  65. $i=$index;
  66. $pos=0;
  67. while (($i-->=0) && ($pos < strlen($string)))
  68. {
  69. $pos=strpos($string,$suchwort,$pos+1);
  70. if ($pos == false)
  71. {
  72. $pos=strlen($string);
  73. }
  74. }
  75. return $pos;
  76. }
  77. function fill_string ($string, $len)
  78. {
  79. $r = $string;
  80. $l = strlen($string);
  81. while ($l++<$len)
  82. {
  83. $r.=" ";
  84. }
  85. return $r;
  86. }
  87. function set_same_height($text,$suchwort,$index)
  88. {
  89. $lines = array();
  90. $part1 = array();
  91. $part2 = array();
  92. $linebreak="\n";
  93. $ausgabe="";
  94. //fill_line_array(nl2br(trim($text)), &$lines);
  95. $lines = $this->fill_line_array2(trim($text));
  96. $maxpos = 0;
  97. $n=0;
  98. foreach ($lines as $line)
  99. {
  100. $p = $this->strpos2($line,$suchwort,$index);
  101. $part1[$n] = substr($line,0,$p);
  102. $part2[$n] = substr($line,$p,strlen($line)-$p);
  103. if (($p > $maxpos) && ($p!=strlen($line)) && (($p < strpos($line,"//") || strpos($line,"//") == false)))
  104. {
  105. $maxpos = $p;
  106. }
  107. $n++;
  108. }
  109. for($i=0;$i<count($part1);$i++)
  110. {
  111. $ausgabe.=$this->fill_string($part1[$i], $maxpos);
  112. $ausgabe.=$part2[$i].$linebreak;
  113. }
  114. //echo "<hr>maxpos = $maxpos<br><pre>$ausgabe</pre><hr>";
  115. return $ausgabe;
  116. }
  117. function format_block($block,$type)
  118. {
  119. $ausgabe=$block;
  120. if ($type=="local")
  121. {
  122. //$ausgabe=set_same_height($ausgabe," array",0);
  123. $ausgabe=$this->set_same_height($ausgabe," ",1);
  124. $ausgabe=$this->set_same_height($ausgabe,"=",0);
  125. }
  126. elseif ($type=="set")
  127. {
  128. $ausgabe=$this->set_same_height($ausgabe,"=",0);
  129. }
  130. elseif ($type=="call")
  131. {
  132. $ausgabe=$this->set_same_height($ausgabe,"(",0);
  133. }
  134. return $ausgabe;
  135. }
  136. function format_line($line)
  137. {
  138. $ausgabe="";
  139. $has_space=false;
  140. $must_space=false;
  141. $lasttype="";
  142. $c_nr = 0;
  143. while ($c_nr < strlen($line) )
  144. { //jedes Zeichen durchgehen
  145. $c = substr($line, $c_nr, 1);
  146. $cc = substr($line, $c_nr, 2);
  147. $ccc = substr($line, $c_nr, 3);
  148. $cccc = substr($line, $c_nr, 4);
  149. $ccccc = substr($line, $c_nr, 5);
  150. if ($c==",")
  151. {
  152. //$ausgabe.=$space ? '' : ' ';
  153. $ausgabe.=", ";
  154. $has_space=true;
  155. $must_space=false;
  156. $lasttype=",";
  157. }
  158. elseif(in_array($ccccc,array(" and "," and(",")and ",")and(")))
  159. {
  160. //$ausgabe.=$space ? '' : ' ';
  161. $ausgabe.="$cccc ";
  162. $has_space=true;
  163. $must_space=false;
  164. $c_nr+=3;
  165. $lasttype="operator";
  166. }
  167. elseif(in_array($cccc,array(" or ",")or "," or(",")or(")))
  168. {
  169. //$ausgabe.=$space ? '' : ' ';
  170. $ausgabe.="$ccc ";
  171. $has_space=true;
  172. $must_space=false;
  173. $c_nr+=2;
  174. $lasttype="operator";
  175. }
  176. elseif(in_array($cc,array("==","<=",">=","!=")))
  177. {
  178. //$ausgabe.=$space ? '' : ' ';
  179. $ausgabe.=" $cc ";
  180. $has_space=true;
  181. $must_space=false;
  182. $c_nr++;
  183. $lasttype="operator";
  184. }
  185. elseif(in_array($cc,array("//")))
  186. {
  187. //$ausgabe.=$space ? '' : ' ';
  188. if ($c_nr > 2) {
  189. $ausgabe.= ' // ';
  190. }
  191. else
  192. {
  193. $ausgabe.= '//';
  194. }
  195. $ausgabe.=substr($line,$c_nr+2,strlen($line)-$c_nr-2);
  196. $c_nr = strlen($line) +1;
  197. $has_space=false;
  198. $must_space=false;
  199. $c_nr++;
  200. $lasttype="comment";
  201. }
  202. elseif(in_array($c, array("+","*","/","-","=","<",">")))
  203. {
  204. //$ausgabe.=$space ? '' : ' ';
  205. $ausgabe.=" $c ";
  206. $has_space=true;
  207. $must_space=false;
  208. $lasttype="operator";
  209. }
  210. elseif(in_array($c, array("(",")","[","]")))
  211. {
  212. //$ausgabe.=$space ? '' : ' ';
  213. if (in_array($c, array("(","[")))
  214. {
  215. if (in_array($lasttype,array("breaket","comment","char","space")))
  216. {
  217. $ausgabe.="$c";
  218. }
  219. else
  220. {
  221. $ausgabe.=" $c";
  222. }
  223. }
  224. else
  225. {
  226. //$ausgabe.='||'.$lasttype.'||';
  227. //if (in_array($lasttype,array("breaket","comment")))
  228. $oldc = substr($line, $c_nr-1, 1);
  229. if ((in_array($c, array("(",")"))) && (in_array($oldc, array("(",")"))))
  230. {
  231. $ausgabe.="$c";
  232. }
  233. else if ((in_array($c, array("[","]"))) && (in_array($oldc, array("[","]"))))
  234. {
  235. $ausgabe.="$c";
  236. }
  237. else
  238. {
  239. $ausgabe.=" $c";
  240. }
  241. }
  242. $has_space=true;
  243. $must_space=true;
  244. $lasttype="breaket";
  245. }
  246. elseif(($c==" ") || ($c==" "))
  247. {
  248. //$ausgabe.=$space ? '' : ' ';
  249. if (!$has_space)
  250. {
  251. $must_space=true;
  252. }
  253. $has_space=true;
  254. $lasttype="space";
  255. }
  256. else
  257. {
  258. $ausgabe.= $must_space ? ' ' : '';
  259. $ausgabe.= $c;
  260. $has_space=false;
  261. $must_space=false;
  262. $lasttype="char";
  263. }
  264. $c_nr++;
  265. }
  266. return $ausgabe;
  267. }
  268. function format_jass_same_height($input)
  269. {
  270. $tab=" ";
  271. $linebreak="\n";
  272. $signs = array("+","-","*",",","<",">","=");
  273. $jass_line = array();
  274. //unnötige leerzeichen entfernen
  275. $_input = $input;
  276. //$_input = preg_replace('/\s\s+/', ' ', $_input);
  277. $_input = str_replace(" "," ",$_input);
  278. $_input = trim($_input);
  279. $jass_line = $this->fill_line_array2($_input);
  280. //fill_line_array(nl2br(trim(str_replace(" "," ",$input2))), &$jass_line);
  281. //$ausgabe = "";
  282. $ausgabe = '';
  283. $block="";
  284. $current_type = "";
  285. $line_types = array("local","set","call");
  286. $line_nr=0;
  287. while ($line_nr < count($jass_line))
  288. {
  289. $line = trim($jass_line[$line_nr]);
  290. $line = $this->format_line($line);
  291. //unnötige leerzeichen entfernen
  292. //$line = preg_replace('/\s\s+/', ' ', $line);
  293. if (($line_nr < count($jass_line) - 1) || ($line!="0"))
  294. {
  295. $b = true;
  296. foreach ($line_types as $line_type)
  297. {
  298. if ($this->get_first_word($line) == $line_type)
  299. {
  300. if ($current_type == $line_type)
  301. {
  302. $block.=$line.$linebreak;
  303. }
  304. else
  305. {
  306. $ausgabe.=$this->format_block($block,$current_type);
  307. $current_type = $line_type;
  308. $block = $line.$linebreak;
  309. }
  310. $b=false;
  311. }
  312. }
  313. if ($b)
  314. {
  315. if ($current_type != "")
  316. {
  317. $ausgabe.=$this->format_block($block,$current_type);
  318. $block = "";
  319. $current_type = "";
  320. $ausgabe.=$line.$linebreak;
  321. }
  322. else
  323. {
  324. $ausgabe.=$line.$linebreak;
  325. }
  326. }
  327. }
  328. $line_nr++;
  329. }
  330. return $ausgabe;
  331. }
  332. function format_jass_indent($input)
  333. {
  334. $indent_keywords = array("function","if","else","elseif","loop","struct","scope","library","globals","method");
  335. $indent_end_keywords = array("endfunction","endif","else","elseif","endloop","endstruct","endscope","endlibrary","endglobals","endmethod");
  336. $indent_ignores = array("static","constant","private","public"," "," ");
  337. $same_height_keywords = array("(",")","=",",",);
  338. $tab=" ";
  339. $linebreak="\n";
  340. $jass_line = array();
  341. //fill_line_array(nl2br(trim(str_replace(" "," ",$input))), &$jass_line);
  342. $_input = $input;
  343. $_input = str_replace(" "," ",$_input);
  344. $_input = trim($_input);
  345. $jass_line = $this->fill_line_array2($_input);
  346. $ausgabe = "";
  347. $block="";
  348. $tabs = 0;
  349. $line_nr= 0;
  350. while ($line_nr < count($jass_line))
  351. {
  352. $line = trim($jass_line[$line_nr]);
  353. if (($line_nr < count($jass_line) - 1) || ($line!="0"))
  354. {
  355. if ($this->begins_with_keyword($line,$indent_end_keywords,$indent_ignores))
  356. {
  357. $tabs--;
  358. }
  359. for ($i=0;$i<$tabs;$i++)
  360. {
  361. $ausgabe.=$tab;
  362. }
  363. $ausgabe.=$line.$linebreak;
  364. if ($this->begins_with_keyword($line,$indent_keywords,$indent_ignores))
  365. {
  366. $tabs++;
  367. //function interface ausnahme
  368. if ($this->begins_with_keyword($line,array("functioninterface"),$indent_ignores))
  369. {
  370. $tabs--;
  371. }
  372. }
  373. }
  374. $line_nr++;
  375. }
  376. $ausgabe.=$block;
  377. //echo "<pre>$ausgabe</pre>";
  378. return $ausgabe;
  379. }
  380. function format($input, $indent = true, $same_height = true)
  381. {
  382. $ausgabe = $input;
  383. if ($same_height)
  384. {
  385. $ausgabe=$this->format_jass_same_height($ausgabe);
  386. }
  387. if ($indent)
  388. {
  389. $ausgabe=$this->format_jass_indent($ausgabe);
  390. }
  391. return $ausgabe;
  392. }
  393. }
  394. ?>

goto line:
Compare with:
text copy window edit this code post new code