You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

172 lines
3.7 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. <link rel="icon" type="image/png" href="../favicon.png">
  13. </head>
  14. <style>
  15. body {
  16. font-family: 'Segoe UI', Arial;
  17. background-color: #aaa;
  18. padding: 0;
  19. margin: 0;
  20. }
  21. .bg0 {
  22. background-image: url("https://github.com/benc-uk/icon-collection/raw/master/_tools/background.png")
  23. }
  24. .bg1 {
  25. background-color: #333;
  26. }
  27. .bg2 {
  28. background-color: #fff;
  29. }
  30. .imgbox img {
  31. width: 10.5rem;
  32. height: 10.5rem;
  33. object-fit: contain;
  34. cursor: pointer;
  35. }
  36. .imgbox {
  37. margin: 0.6rem;
  38. padding: 0.5rem;
  39. text-align: center;
  40. width: 12rem;
  41. height: 12rem;
  42. background-color: #e0e0e0;
  43. }
  44. h1 {
  45. font-size: 2.6rem;
  46. }
  47. .find, .find input {
  48. font-size: 1.5rem;
  49. }
  50. .top {
  51. position: -webkit-sticky;
  52. position: sticky;
  53. top: 0;
  54. background-color: #aaa;
  55. padding: 1rem;
  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. h1 {
  86. display:inline;
  87. }
  88. .home {
  89. font-size: 3rem;
  90. text-decoration: none
  91. }
  92. </style>
  93. <body>
  94. <div class="top">
  95. <a href=".." class="home">⮤</a> &nbsp; <h1>${TITLE}</h1>
  96. <br/><br/>
  97. <span class="find">Finder: <input onkeyup="search()" id="finder"><br></span>
  98. <div class="pngbox"><input type="checkbox" id="pngcheck"><label for="pngcheck">Download as PNG</label></div>
  99. <button onclick="toggleBg()">Change Background</button>
  100. </div>
  101. <div class="grid">
  102. `
  103. var files = fs.readdirSync(DIR);
  104. for(let f of files) {
  105. if(!f.endsWith(".svg")) continue;
  106. outHtml += `<div id="${f}" class="imgbox"><img src="${f}" class="bg0" onclick="download('${f}', this)"/><div class="label">${f}</div></div>\n`
  107. }
  108. outHtml += `
  109. </div>
  110. <script>
  111. var bg = 0;
  112. function toggleBg() {
  113. bg++
  114. if(bg > 2) bg = 0;
  115. for(let img of document.getElementsByTagName("img")) {
  116. img.className = \`bg\${bg}\`;
  117. }
  118. }
  119. function search() {
  120. let q = document.getElementById('finder').value.trim().toLowerCase()
  121. if(q.length <= 0) {
  122. for(let imgbox of document.getElementsByClassName("imgbox")) {
  123. imgbox.style.display = "inline-block"
  124. }
  125. }
  126. for(let imgbox of document.getElementsByClassName("imgbox")) {
  127. imgbox.style.display = "inline-block"
  128. if(!imgbox.id.includes(q)) {
  129. imgbox.style.display = "none"
  130. }
  131. }
  132. }
  133. function download(f, e) {
  134. var downloadPNG = document.getElementById("pngcheck").checked;
  135. if(downloadPNG) {
  136. var data = getBase64Image(e);
  137. let a = document.createElement('a')
  138. a.href = "data:application/octet-stream;base64," + data
  139. a.download = f.replace('.svg', '.png')
  140. a.click()
  141. } else {
  142. let a = document.createElement('a')
  143. a.href = f
  144. a.download = f
  145. a.click()
  146. }
  147. }
  148. function getBase64Image(img) {
  149. var canvas = document.createElement("canvas");
  150. canvas.width = img.naturalWidth;
  151. canvas.height = img.naturalHeight;
  152. console.log(canvas.width , canvas.height)
  153. var ctx = canvas.getContext("2d");
  154. ctx.drawImage(img, 0, 0);
  155. var dataURL = canvas.toDataURL("image/png");
  156. return dataURL.replace(/^data:image\\/(png|jpg);base64,/, "");
  157. }
  158. </script>
  159. </body></html>`
  160. console.log(`### Generating gallery index.html for ${DIR}`);
  161. fs.writeFileSync(DIR + '/index.html', outHtml);