« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/write
diff options
context:
space:
mode:
Diffstat (limited to 'src/write')
-rw-r--r--src/write/build-modes/live-dev-server.js42
-rw-r--r--src/write/build-modes/static-build.js93
-rw-r--r--src/write/page-template.js11
3 files changed, 54 insertions, 92 deletions
diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js
index b6bf662b..a4c4c871 100644
--- a/src/write/build-modes/live-dev-server.js
+++ b/src/write/build-modes/live-dev-server.js
@@ -11,11 +11,10 @@ import {serializeThings} from '../../data/serialize.js';
 import * as pageSpecs from '../../page/index.js';
 
 import {logInfo, logWarn, progressCallAll} from '../../util/cli.js';
-import {withEntries} from '../../util/sugar.js';
 
 import {
   getPagePathname,
-  getPageSubdirectoryPrefix,
+  getPagePathnameAcrossLanguages,
   getURLsFrom,
   getURLsFromRoot,
 } from '../../util/urls.js';
@@ -83,17 +82,13 @@ export async function go({
       else if (page.type === 'redirect')
         servePath = page.fromPath;
 
-      const fullKey = 'localized.' + servePath[0];
-      const urlArgs = servePath.slice(1);
-
       return Object.values(languages).map(language => {
         const baseDirectory =
           language === defaultLanguage ? '' : language.code;
 
         const pathname = getPagePathname({
           baseDirectory,
-          fullKey,
-          urlArgs,
+          pagePath: servePath,
           urls,
         });
 
@@ -249,12 +244,9 @@ export async function go({
     } = urlToPageMap[pathnameKey];
 
     const to = getURLsFrom({
-      urls,
       baseDirectory,
-      pageSubKey: servePath[0],
-      subdirectoryPrefix: getPageSubdirectoryPrefix({
-        urlArgs: servePath.slice(1),
-      }),
+      pagePath: servePath,
+      urls,
     });
 
     const absoluteTo = getURLsFromRoot({
@@ -263,9 +255,6 @@ export async function go({
     });
 
     try {
-      const pageSubKey = servePath[0];
-      const urlArgs = servePath.slice(1);
-
       if (page.type === 'redirect') {
         response.writeHead(301, contentTypeHTML);
 
@@ -280,20 +269,12 @@ export async function go({
 
       response.writeHead(200, contentTypeHTML);
 
-      const localizedPathnames = withEntries(languages, entries => entries
-        .filter(([key, language]) => key !== 'default' && !language.hidden)
-        .map(([_key, language]) => [
-          language.code,
-          getPagePathname({
-            baseDirectory:
-              (language === defaultLanguage
-                ? ''
-                : language.code),
-            fullKey: 'localized.' + pageSubKey,
-            urlArgs,
-            urls,
-          }),
-        ]));
+      const localizedPathnames = getPagePathnameAcrossLanguages({
+        defaultLanguage,
+        languages,
+        pagePath: servePath,
+        urls,
+      });
 
       const bound = bindUtilities({
         absoluteTo,
@@ -315,9 +296,8 @@ export async function go({
         languages,
         localizedPathnames,
         oEmbedJSONHref: null, // No oEmbed support for live dev server
-        pageSubKey,
+        pagePath: servePath,
         pathname,
-        urlArgs,
         to,
         transformMultiline: bound.transformMultiline,
         wikiData,
diff --git a/src/write/build-modes/static-build.js b/src/write/build-modes/static-build.js
index 90fc38ae..23c6470a 100644
--- a/src/write/build-modes/static-build.js
+++ b/src/write/build-modes/static-build.js
@@ -15,7 +15,7 @@ import {serializeThings} from '../../data/serialize.js';
 import * as pageSpecs from '../../page/index.js';
 
 import link from '../../util/link.js';
-import {empty, queue, withEntries} from '../../util/sugar.js';
+import {empty, queue} from '../../util/sugar.js';
 
 import {
   logError,
@@ -27,8 +27,7 @@ import {
 
 import {
   getPagePathname,
-  getPagePaths,
-  getPageSubdirectoryPrefix,
+  getPagePathnameAcrossLanguages,
   getURLsFrom,
   getURLsFromRoot,
 } from '../../util/urls.js';
@@ -265,40 +264,25 @@ export async function go({
 
     await progressPromiseAll(`Writing ${language.code}`, queue([
       ...pageWrites.map(page => () => {
-        const pageSubKey = page.path[0];
-        const urlArgs = page.path.slice(1);
-
-        const localizedPathnames = withEntries(languages, entries => entries
-          .filter(([key, language]) => key !== 'default' && !language.hidden)
-          .map(([_key, language]) => [
-            language.code,
-            getPagePathname({
-              baseDirectory:
-                (language === defaultLanguage
-                  ? ''
-                  : language.code),
-              fullKey: 'localized.' + pageSubKey,
-              urlArgs,
-              urls,
-            }),
-          ]));
-
-        const paths = getPagePaths({
-          outputPath,
+        const pagePath = page.path;
+
+        const localizedPathnames = getPagePathnameAcrossLanguages({
+          defaultLanguage,
+          languages,
+          pagePath,
           urls,
+        });
 
+        const pathname = getPagePathname({
           baseDirectory,
-          fullKey: 'localized.' + pageSubKey,
-          urlArgs,
+          pagePath,
+          urls,
         });
 
         const to = getURLsFrom({
-          urls,
           baseDirectory,
-          pageSubKey,
-          subdirectoryPrefix: getPageSubdirectoryPrefix({
-            urlArgs: page.path.slice(1),
-          }),
+          pagePath,
+          urls,
         });
 
         const absoluteTo = getURLsFromRoot({
@@ -328,7 +312,7 @@ export async function go({
           wikiData.wikiInfo.canonicalBase +
             urls
               .from('shared.root')
-              .to('shared.path', paths.pathname + 'oembed.json');
+              .to('shared.path', pathname + 'oembed.json');
 
         const pageHTML = generateDocumentHTML(pageInfo, {
           cachebust,
@@ -339,18 +323,22 @@ export async function go({
           languages,
           localizedPathnames,
           oEmbedJSONHref,
-          pageSubKey,
-          pathname: paths.pathname,
+          pagePath,
+          pathname,
           to,
           transformMultiline: bound.transformMultiline,
-          urlArgs,
           wikiData,
         });
 
         return writePage({
           html: pageHTML,
           oEmbedJSON,
-          paths,
+          outputDirectory: path.join(outputPath, getPagePathname({
+            baseDirectory,
+            device: true,
+            pagePath,
+            urls,
+          })),
         });
       }),
       ...redirectWrites.map(({fromPath, toPath, title: titleFn}) => () => {
@@ -358,27 +346,24 @@ export async function go({
           language,
         });
 
-        const from = getPagePaths({
-          outputPath,
-          urls,
-
-          baseDirectory,
-          fullKey: 'localized.' + fromPath[0],
-          urlArgs: fromPath.slice(1),
-        });
-
         const to = getURLsFrom({
-          urls,
           baseDirectory,
-          pageSubKey: fromPath[0],
-          subdirectoryPrefix: getPageSubdirectoryPrefix({
-            urlArgs: fromPath.slice(1),
-          }),
+          pagePath: fromPath,
+          urls,
         });
 
         const target = to('localized.' + toPath[0], ...toPath.slice(1));
         const html = generateRedirectHTML(title, target, {language});
-        return writePage({html, paths: from});
+
+        return writePage({
+          html,
+          outputDirectory: path.join(outputPath, getPagePathname({
+            baseDirectory,
+            device: true,
+            pagePath: fromPath,
+            urls,
+          })),
+        });
       }),
     ], queueSize));
   };
@@ -424,15 +409,15 @@ import {
 async function writePage({
   html,
   oEmbedJSON = '',
-  paths,
+  outputDirectory,
 }) {
-  await mkdir(paths.output.directory, {recursive: true});
+  await mkdir(outputDirectory, {recursive: true});
 
   await Promise.all([
-    writeFile(paths.output.documentHTML, html),
+    writeFile(path.join(outputDirectory, 'index.html'), html),
 
     oEmbedJSON &&
-      writeFile(paths.output.oEmbedJSON, oEmbedJSON),
+      writeFile(path.join(outputDirectory, 'oembed.json'), oEmbedJSON),
   ].filter(Boolean));
 }
 
diff --git a/src/write/page-template.js b/src/write/page-template.js
index 88d81c23..7c2f0c66 100644
--- a/src/write/page-template.js
+++ b/src/write/page-template.js
@@ -55,11 +55,10 @@ export function generateDocumentHTML(pageInfo, {
   languages,
   localizedPathnames,
   oEmbedJSONHref,
-  pageSubKey,
+  pagePath,
   pathname,
   to,
   transformMultiline,
-  urlArgs,
   wikiData,
 }) {
   const {wikiInfo} = wikiData;
@@ -169,9 +168,8 @@ export function generateDocumentHTML(pageInfo, {
           html,
           language,
           languages,
-          pageSubKey,
+          pagePath,
           to,
-          urlArgs,
         }),
       ]);
 
@@ -450,10 +448,9 @@ export function generateDocumentHTML(pageInfo, {
     {
       lang: language.intlCode,
       'data-language-code': language.code,
-      'data-url-key': 'localized.' + pageSubKey,
+      'data-url-key': 'localized.' + pagePath[0],
       ...Object.fromEntries(
-        urlArgs.map((v, i) => [['data-url-value' + i], v])
-      ),
+        pagePath.slice(1).map((v, i) => [['data-url-value' + i], v])),
       'data-rebase-localized': to('localized.root'),
       'data-rebase-shared': to('shared.root'),
       'data-rebase-media': to('media.root'),