Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

164 строки
3.5 KiB

  1. #!/usr/bin/node
  2. const fs = require('fs');
  3. var DIR = process.argv[2];
  4. var TITLE = process.argv[3] || "Gallery";
  5. if(!DIR || !TITLE) {
  6. console.log(`### ERROR! No directory and/or title provided!`);
  7. console.log(`Usage: node gallery.js {directory} {title}`);
  8. process.exit(1);
  9. }
  10. outHtml = `
  11. <head><title>${TITLE}</title>
  12. </head>
  13. <style>
  14. body {
  15. font-family: 'Segoe UI', Arial;
  16. background-color: #aaa;
  17. padding: 0;
  18. margin: 0;
  19. }
  20. .bg0 {
  21. background-image: url("https://github.com/benc-uk/icon-collection/raw/master/_tools/background.png")
  22. }
  23. .bg1 {
  24. background-color: #333;
  25. }
  26. .bg2 {
  27. background-color: #fff;
  28. }
  29. .imgbox img {
  30. width: 10.5rem;
  31. height: 10.5rem;
  32. object-fit: contain;
  33. cursor: pointer;
  34. }
  35. .imgbox {
  36. margin: 0.6rem;
  37. padding: 0.5rem;
  38. text-align: center;
  39. width: 12rem;
  40. height: 12rem;
  41. background-color: #e0e0e0;
  42. }
  43. h1 {
  44. font-size: 2.6rem;
  45. }
  46. .find, .find input {
  47. font-size: 1.5rem;
  48. }
  49. .top {
  50. position: -webkit-sticky;
  51. position: sticky;
  52. top: 0;
  53. background-color: #aaa;
  54. padding: 1rem;
  55. padding-top: 0;
  56. box-shadow: 0px 5px 18px 3px rgba(0,0,0,0.59);
  57. }
  58. .top button {
  59. float: right;
  60. position: relative;
  61. top: -2rem;
  62. font-size: 1.5rem;
  63. }
  64. .pngbox {
  65. position: absolute;
  66. top: 2rem;
  67. right: 1rem;
  68. font-size: 1.5rem;
  69. }
  70. input[type="checkbox"] {
  71. width: 1.6em;
  72. height: 1.6em;
  73. }
  74. .label {
  75. height: 2.5em;
  76. line-height: 1em;
  77. overflow: hidden;
  78. font-size: 0.9rem;
  79. }
  80. .grid {
  81. display: flex;
  82. flex-wrap: wrap;
  83. width: 100%;
  84. }
  85. </style>
  86. <body>
  87. <div class="top">
  88. <h1>${TITLE}</h1>
  89. <span class="find">Finder: <input onkeyup="search()" id="finder"><br></span>
  90. <div class="pngbox"><input type="checkbox" id="pngcheck"><label for="pngcheck">Download as PNG</label></div>
  91. <button onclick="toggleBg()">Change Background</button>
  92. </div>
  93. <div class="grid">
  94. `
  95. var files = fs.readdirSync(DIR);
  96. for(let f of files) {
  97. if(!f.endsWith(".svg")) continue;
  98. outHtml += `<div id="${f}" class="imgbox"><img src="${f}" class="bg0" onclick="download('${f}', this)"/><div class="label">${f}</div></div>\n`
  99. }
  100. outHtml += `
  101. </div>
  102. <script>
  103. var bg = 0;
  104. function toggleBg() {
  105. bg++
  106. if(bg > 2) bg = 0;
  107. for(let img of document.getElementsByTagName("img")) {
  108. img.className = \`bg\${bg}\`;
  109. }
  110. }
  111. function search() {
  112. let q = document.getElementById('finder').value.trim().toLowerCase()
  113. if(q.length <= 0) {
  114. for(let imgbox of document.getElementsByClassName("imgbox")) {
  115. imgbox.style.display = "inline-block"
  116. }
  117. }
  118. for(let imgbox of document.getElementsByClassName("imgbox")) {
  119. imgbox.style.display = "inline-block"
  120. if(!imgbox.id.includes(q)) {
  121. imgbox.style.display = "none"
  122. }
  123. }
  124. }
  125. function download(f, e) {
  126. var downloadPNG = document.getElementById("pngcheck").checked;
  127. if(downloadPNG) {
  128. var data = getBase64Image(e);
  129. let a = document.createElement('a')
  130. a.href = "data:application/octet-stream;base64," + data
  131. a.download = f.replace('.svg', '.png')
  132. a.click()
  133. } else {
  134. let a = document.createElement('a')
  135. a.href = f
  136. a.download = f
  137. a.click()
  138. }
  139. }
  140. function getBase64Image(img) {
  141. var canvas = document.createElement("canvas");
  142. canvas.width = img.naturalWidth;
  143. canvas.height = img.naturalHeight;
  144. console.log(canvas.width , canvas.height)
  145. var ctx = canvas.getContext("2d");
  146. ctx.drawImage(img, 0, 0);
  147. var dataURL = canvas.toDataURL("image/png");
  148. return dataURL.replace(/^data:image\\/(png|jpg);base64,/, "");
  149. }
  150. </script>
  151. </body></html>`
  152. console.log(`### Generating gallery index.html for ${DIR}`);
  153. fs.writeFileSync(DIR + '/index.html', outHtml);