Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3. if (count($argv) <= 1) {
  4. print("usage createMockup src_dir dst_dir images_dir\n");
  5. print("\tsrc_dir : dir where all bmml file are\n");
  6. print("\tdst_dir : dir where write all html files\n");
  7. print("\timages_dir : dir where all export mockup png are (will be copied to the \$dst_dir/images directory)\n");
  8. exit(1);
  9. }
  10. $src_dir = trim($argv[1],'/');
  11. $dst_dir = trim($argv[2],'/');
  12. $images_dir = trim($argv[3],'/');
  13. $links = array();
  14.  
  15. function filter($value) {
  16. $value = htmlentities($value, ENT_NOQUOTES);
  17. $value = preg_replace('#\&([A-za-z])(?:acute|cedil|circ|grave|ring|tilde|uml)\;#', '\1', $value);
  18. $value = preg_replace('#\&([A-za-z]{2})(?:lig)\;#', '\1', $value); // pour les ligatures e.g. 'œ'
  19. return $value;
  20. }
  21.  
  22. function cleanFileName($filename, $ext) {
  23. $filename = str_replace(array('%20', ' '), '_', $filename);
  24. $filename = filter($filename);
  25. return str_replace('.bmml', '.'.$ext, $filename);
  26. }
  27.  
  28. function crossControlList($controls, $x = 0, $y = 0) {
  29. global $links;
  30. foreach ($controls->children() as $control) {
  31. $coords = array('x' => $x + $control['x'], 'y' => $y + $control['y']);
  32. if (isset($control->groupChildrenDescriptors)) {
  33. crossControlList($control->groupChildrenDescriptors,$coords['x'],$coords['y']);
  34. }
  35. if (isset($control->controlProperties->map)) {
  36. $hrefs = isset($control->controlProperties->hrefs) ? explode('%2C',$control->controlProperties->hrefs) : array($control->controlProperties->href);// array_pop($hrefs);
  37. $maps = str_replace(array('<','>'),array('<','>'), filter(rawurldecode(((string)$control->controlProperties->map))));
  38. //print_r($maps);
  39. $mapsxml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><map>'. $maps .'</map>');
  40. //print_r($mapsxml);
  41. foreach ($mapsxml->area as $area) {
  42. $areacoords = explode(',',$area['coords']);
  43. $href = rawurldecode($area['href']);
  44. $listhref = explode('&bm;',$href);
  45. echo "maps = ", $listhref[0], "\n";
  46. $links[] = array(
  47. 'href' => $listhref[0],
  48. 'zOrder' => $control['zOrder'],
  49. 'x' => (string)$areacoords[0],
  50. 'y' => (string)$areacoords[1],
  51. 'w' => (string)$areacoords[2] - $areacoords[0],
  52. 'h' => (string)$areacoords[3] - $areacoords[1],
  53. );
  54. }
  55. } elseif ($href = (string)$control->controlProperties->href) {
  56. $href = rawurldecode($href);
  57. $listhref = explode('&bm;',$href);
  58. echo "href = ",$listhref[0], "\n";
  59. $links[] = array(
  60. 'href' => $listhref[0].'.bmml',
  61. 'zOrder' => $control['zOrder'],
  62. 'x' => (string)$coords['x'],
  63. 'y' => (string)$coords['y'],
  64. 'w' => (string)$control['w'] != '-1' ? (string)$control['w'] : (string)$control['measuredW'],
  65. 'h' => (string)$control['h'] != '-1' ? (string)$control['h'] : (string)$control['measuredH']
  66. );
  67. }
  68. }
  69. }
  70.  
  71. function createHtml($filename, $links, $title = '') {
  72. global $dst_dir, $images_dir;
  73. $contents = '<html><title>'.(empty($title)?$filename:$title).'</title><style> body {background: no-repeat url("images/'.cleanFileName($filename,'png').'") }</style><body>';
  74. $contents .= '<div style="width: 1024px; height: 1024px; position: absolute; left: 0; top: 0">&nbsp;</div>';
  75. foreach ($links as $key=>$link) {
  76. $contents .= '<a style="opacity: 0.7; display: block; width: '.$link['w'].'px; height: '.$link['h'].'px; position: absolute; left: '.($link['x']).'px; top: '.($link['y']).'px" href="'.cleanFileName($link['href'],'html').'">&nbsp;</a><br />';
  77. }
  78. $contents .= '</body></html>';
  79. file_put_contents($dst_dir.'/'.cleanFileName($filename,'html'),$contents);
  80. }
  81.  
  82. function sort_list($a,$b) {
  83. return $b['zOrder'] < $a['zOrder'];
  84. }
  85. if (!file_exists($dst_dir)) {
  86. echo "Create directory $dst_dir...\n";
  87. mkdir($dst_dir);
  88. }
  89. if ($dh = opendir($src_dir.'/')) {
  90. echo "Process mockup in $src_dir ...\n";
  91. while (($file = readdir($dh)) !== false) {
  92. $links = array();
  93. $filepath = $src_dir .'/'. $file;
  94. if (filetype($filepath) == 'file' && substr($file, strlen($file) - 5) == ".bmml") {
  95. echo "Process $file ...\n";
  96. $xml = simplexml_load_file($filepath);
  97. crossControlList($xml->controls);
  98. usort($links, "sort_list");
  99. $browserWindowValues = $xml->xpath('//control[@controlTypeID="com.balsamiq.mockups::BrowserWindow"]/controlProperties/text');
  100. if (count($browserWindowValues)) {
  101. $browserWindowValue = explode("\n",rawurldecode($browserWindowValues[0]));
  102. $title = $browserWindowValue[0];
  103. }
  104. else {
  105. $title = str_replace('.bmml', '', $file);
  106. }
  107. echo "title = ", $title, "\n";
  108. createHtml($file,$links, $title);
  109. }
  110. }
  111. closedir($dh);
  112. } else die ('Nie mo&#380;na otworzy&#263; katalogu src');
  113. if ($dh = opendir($images_dir.'/')) {
  114. echo "Process mockup images from $images_dir ...\n";
  115. if (!file_exists($dst_dir .'/images')) {
  116. echo "Create directory $dst_dir/images...\n";
  117. mkdir($dst_dir .'/images');
  118. }
  119. while (($file = readdir($dh)) !== false) {
  120. $filepath = $images_dir .'/'. $file;
  121. if (filetype($filepath) == 'file' && substr($file, strlen($file) - 4) == ".png") {
  122. echo "Copy $file ...\n";
  123. copy($filepath, $dst_dir .'/images/' . cleanFileName($file, 'png'));
  124. }
  125. }
  126. closedir($dh);
  127. } else die ('Nie mo&#380;na otworzy&#263; katalogu src');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement