« get me outta code hell

thumbnail support! - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/upd8.js
diff options
context:
space:
mode:
author(quasar) nebula <towerofnix@gmail.com>2021-03-04 20:32:59 -0400
committer(quasar) nebula <towerofnix@gmail.com>2021-03-04 20:33:52 -0400
commit10e9059c502db6586826f7c29c2d483b553d24c6 (patch)
tree5915c80d32f35cefbff2f6e167e6bdb2ae47dc97 /upd8.js
parent85a06307ae9aa8767f1458e6ea6859cad7293936 (diff)
thumbnail support!
contains a new gen-thumbs.js file, which can be run on its own and is
automatically called from the main hsmusic cli tool as well; see this
file for details!
Diffstat (limited to 'upd8.js')
-rwxr-xr-xupd8.js31
1 files changed, 28 insertions, 3 deletions
diff --git a/upd8.js b/upd8.js
index 07aaa13..21f1605 100755
--- a/upd8.js
+++ b/upd8.js
@@ -115,6 +115,7 @@ const {
     joinNoOxford,
     mapInPlace,
     logWarn,
+    logInfo,
     logError,
     parseOptions,
     progressPromiseAll,
@@ -126,6 +127,8 @@ const {
     unique
 } = require('./upd8-util');
 
+const genThumbs = require('./gen-thumbs');
+
 const C = require('./common/common');
 
 const CACHEBUST = 3;
@@ -263,6 +266,14 @@ const link = {
     site: linkPathname('site', {color: false})
 };
 
+const thumbnailHelper = name => file =>
+    file.replace(/\.(jpg|png)$/, name + '.jpg');
+
+const thumb = {
+    medium: thumbnailHelper('.medium'),
+    small: thumbnailHelper('.small')
+};
+
 function generateURLs(fromPath) {
     const helper = toPath => {
         let argIndex = 0;
@@ -958,6 +969,7 @@ function transformMultiline(text, {strings, to}) {
         line = line.replace(/<img (.*?)>/g, (match, attributes) => img({
             lazy: true,
             link: true,
+            thumb: 'medium',
             ...parseAttributes(attributes, {to})
         }));
 
@@ -1878,6 +1890,7 @@ function attributes(attribs) {
 function img({
     src = '',
     alt = '',
+    thumb: thumbKey = '',
     reveal = '',
     id = '',
     width = '',
@@ -1889,6 +1902,9 @@ function img({
     const willSquare = square;
     const willLink = typeof link === 'string' || link;
 
+    const originalSrc = src;
+    const thumbSrc = thumbKey ? thumb[thumbKey](src) : src;
+
     const imgAttributes = attributes({
         id: link ? '' : id,
         alt,
@@ -1896,8 +1912,8 @@ function img({
         height
     });
 
-    const nonlazyHTML = wrap(`<img src="${src}" ${imgAttributes}>`);
-    const lazyHTML = lazy && wrap(`<img class="lazy" data-original="${src}" ${imgAttributes}>`, true);
+    const nonlazyHTML = wrap(`<img src="${thumbSrc}" ${imgAttributes}>`);
+    const lazyHTML = lazy && wrap(`<img class="lazy" data-original="${thumbSrc}" ${imgAttributes}>`, true);
 
     if (lazy) {
         return fixWS`
@@ -1933,7 +1949,7 @@ function img({
         if (willLink) {
             html = `<a ${classes('box', hide && 'js-hide')} ${attributes({
                 id,
-                href: typeof link === 'string' ? link : src
+                href: typeof link === 'string' ? link : originalSrc
             })}>${html}</a>`;
         }
 
@@ -2202,6 +2218,7 @@ function getGridHTML({
             ${img({
                 src: srcFn(item),
                 alt: altFn(item),
+                thumb: 'small',
                 lazy: (typeof lazy === 'number' ? i >= lazy : lazy),
                 square: true,
                 reveal: getRevealString(item.artTags, {strings})
@@ -2534,6 +2551,7 @@ function generateCoverLink({
             ${img({
                 src,
                 alt,
+                thumb: 'medium',
                 id: 'cover-art',
                 link: true,
                 square: true,
@@ -5174,6 +5192,13 @@ async function main() {
         }
     }
 
+    logInfo`Begin thumbnail generation... -----+`;
+    const result = await genThumbs(mediaPath, {queueSize, quiet: true});
+    logInfo`Done thumbnail generation! --------+`;
+    if (!result) {
+        return;
+    }
+
     const defaultStrings = await processLanguageFile(path.join(__dirname, DEFAULT_STRINGS_FILE));
     if (defaultStrings.error) {
         logError`Error loading default strings: ${defaultStrings.error}`;