From fe67e1acafc82ae671c3350eaf5fc001ae4d5f8a Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 5 Jan 2023 19:52:08 -0400 Subject: remove unused vertical-square CSS --- src/static/site2.css | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/static/site2.css b/src/static/site2.css index b5ae3ef9..41f1082f 100644 --- a/src/static/site2.css +++ b/src/static/site2.css @@ -1029,17 +1029,6 @@ html[data-url-key="localized.home"] .carousel-container { height: 100%; } -.vertical-square { - position: relative; - height: 100%; -} - -.vertical-square::after { - content: ""; - display: block; - padding-right: 100%; -} - /* Info card */ #info-card-container { -- cgit 1.3.0-6-gf8a5 From 6079f214f8ba5f95433f501c5408c3b9911ad23e Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 5 Jan 2023 19:52:22 -0400 Subject: remove image-inner-area --- src/static/site2.css | 7 ------- src/upd8.js | 1 - 2 files changed, 8 deletions(-) diff --git a/src/static/site2.css b/src/static/site2.css index 41f1082f..62b43ff3 100644 --- a/src/static/site2.css +++ b/src/static/site2.css @@ -709,13 +709,6 @@ li > ul { height: 100%; } -.image-inner-area { - overflow: hidden; - width: 100%; - height: 100%; - position: relative; -} - .image-text-area { position: absolute; top: 0; diff --git a/src/upd8.js b/src/upd8.js index fad3c7d8..51b098fd 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -703,7 +703,6 @@ function img({ function wrap(input, hide = false) { let wrapped = input; - wrapped = html.tag('div', {class: 'image-inner-area'}, wrapped); wrapped = html.tag('div', {class: 'image-container'}, wrapped); if (reveal) { -- cgit 1.3.0-6-gf8a5 From 8a875d610d16085132104b429c52aa39a6c7dad3 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 5 Jan 2023 19:53:45 -0400 Subject: remove unused img alt CSS --- src/static/site2.css | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/static/site2.css b/src/static/site2.css index 62b43ff3..0f408c61 100644 --- a/src/static/site2.css +++ b/src/static/site2.css @@ -730,12 +730,6 @@ li > ul { img { object-fit: cover; - /* these unfortunately dont take effect while loading, so... - text-align: center; - line-height: 2em; - text-shadow: 0 0 5px black; - font-style: oblique; - */ } .reveal { -- cgit 1.3.0-6-gf8a5 From 4088eabda9c6ca8a047b6849ca109fbc27d2acfa Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 5 Jan 2023 20:24:51 -0400 Subject: remove unused p img layouting rule --- src/static/site2.css | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/static/site2.css b/src/static/site2.css index 0f408c61..287bbd66 100644 --- a/src/static/site2.css +++ b/src/static/site2.css @@ -631,11 +631,6 @@ blockquote { padding-right: 12%; } -p img { - max-width: 100%; - height: auto; -} - dl dt { padding-left: 40px; max-width: 600px; -- cgit 1.3.0-6-gf8a5 From 3fb98fcbeab5173acec5222bec7a4adf597e88bb Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 7 Jan 2023 09:53:29 -0400 Subject: extract utility binds, content transform fns towards basic dynamics pt. 1 (#124) --- src/misc-templates.js | 105 ++++++ src/upd8.js | 776 +----------------------------------------- src/util/transform-content.js | 447 ++++++++++++++++++++++++ src/write/bind-utilities.js | 257 ++++++++++++++ 4 files changed, 825 insertions(+), 760 deletions(-) create mode 100644 src/util/transform-content.js create mode 100644 src/write/bind-utilities.js diff --git a/src/misc-templates.js b/src/misc-templates.js index a530a3cf..9a1bbf50 100644 --- a/src/misc-templates.js +++ b/src/misc-templates.js @@ -10,6 +10,8 @@ import { unique, } from './util/sugar.js'; +import {thumb} from './util/urls.js'; + import { getTotalDuration, sortAlbumsTracksChronologically, @@ -642,6 +644,107 @@ function unbound_getFlashGridHTML({ }); } +// Images + +function unbound_img({ + html, + + src, + alt, + noSrcText = '', + thumb: thumbKey, + reveal, + id, + class: className, + width, + height, + link = false, + lazy = false, + square = false, +}) { + const willSquare = square; + const willLink = typeof link === 'string' || link; + + const originalSrc = src; + const thumbSrc = src && (thumbKey ? thumb[thumbKey](src) : src); + + const imgAttributes = { + id: link ? '' : id, + class: className, + alt, + width, + height, + }; + + const noSrcHTML = + !src && + wrap( + html.tag('div', + {class: 'image-text-area'}, + noSrcText)); + + const nonlazyHTML = + src && + wrap( + html.tag('img', { + ...imgAttributes, + src: thumbSrc, + })); + + const lazyHTML = + src && + lazy && + wrap( + html.tag('img', + { + ...imgAttributes, + class: [className, 'lazy'], + 'data-original': thumbSrc, + }), + true); + + if (!src) { + return noSrcHTML; + } else if (lazy) { + return html.tag('noscript', nonlazyHTML) + '\n' + lazyHTML; + } else { + return nonlazyHTML; + } + + function wrap(input, hide = false) { + let wrapped = input; + + wrapped = html.tag('div', {class: 'image-inner-area'}, wrapped); + wrapped = html.tag('div', {class: 'image-container'}, wrapped); + + if (reveal) { + wrapped = html.tag('div', {class: 'reveal'}, [ + wrapped, + html.tag('span', {class: 'reveal-text'}, reveal), + ]); + } + + if (willSquare) { + wrapped = html.tag('div', {class: 'square-content'}, wrapped); + wrapped = html.tag('div', + {class: ['square', hide && !willLink && 'js-hide']}, + wrapped); + } + + if (willLink) { + wrapped = html.tag('a', + { + id, + class: ['box', hide && 'js-hide'], + href: typeof link === 'string' ? link : originalSrc, + }, + wrapped); + } + + return wrapped; + } +} + // Carousel reels // Layout constants: @@ -940,6 +1043,8 @@ export { unbound_getCarouselHTML as getCarouselHTML, + unbound_img as img, + unbound_generateInfoGalleryLinks as generateInfoGalleryLinks, unbound_generateNavigationLinks as generateNavigationLinks, diff --git a/src/upd8.js b/src/upd8.js index fad3c7d8..0573dcd2 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -31,11 +31,11 @@ // Oh yeah, like. Just run this through some relatively recent version of // node.js and you'll 8e fine. ...Within the project root. O8viously. +import chroma from 'chroma-js'; + import * as path from 'path'; import {fileURLToPath} from 'url'; -import chroma from 'chroma-js'; - import { copyFile, mkdir, @@ -52,17 +52,20 @@ import {listingSpec, listingTargetSpec} from './listing-spec.js'; import urlSpec from './url-spec.js'; import * as pageSpecs from './page/index.js'; -import find, {bindFind} from './util/find.js'; +import find from './util/find.js'; import * as html from './util/html.js'; import {getColors} from './util/colors.js'; import {findFiles} from './util/io.js'; import {isMain} from './util/node-utils.js'; +import {replacerSpec} from './util/transform-content.js'; import CacheableObject from './data/things/cacheable-object.js'; import {processLanguageFile} from './data/language.js'; import {serializeThings} from './data/serialize.js'; +import {bindUtilities} from './write/bind-utilities.js'; + import { filterDuplicateDirectories, filterReferenceErrors, @@ -73,32 +76,12 @@ import { } from './data/yaml.js'; import { - fancifyFlashURL, - fancifyURL, - generateAdditionalFilesShortcut, - generateAdditionalFilesList, - generateChronologyLinks, - generateCoverLink, - generateInfoGalleryLinks, - generateNavigationLinks, - generateStickyHeadingContainer, - generateTrackListDividedByGroups, - getAlbumGridHTML, - getAlbumStylesheet, - getArtistString, - getFlashGridHTML, getFooterLocalizationLinks, - getGridHTML, - getCarouselHTML, - getRevealStringFromTags, getRevealStringFromWarnings, - getThemeString as unbound_getThemeString, - iconifyURL, + img, } from './misc-templates.js'; -import unbound_link, { - getLinkThemeString as unbound_getLinkThemeString, -} from './util/link.js'; +import link from './util/link.js'; import { color, @@ -111,14 +94,7 @@ import { progressPromiseAll, } from './util/cli.js'; -import {validateReplacerSpec, transformInline} from './util/replacer.js'; - -import { - getAlbumCover, - getArtistAvatar, - getFlashCover, - getTrackCover, -} from './util/wiki-data.js'; +import {validateReplacerSpec} from './util/replacer.js'; /* import { @@ -131,14 +107,9 @@ import { } from './util/serialize.js'; */ -import { - bindOpts, - queue, - showAggregate, - withEntries, -} from './util/sugar.js'; +import {queue, showAggregate} from './util/sugar.js'; -import {generateURLs, thumb} from './util/urls.js'; +import {generateURLs} from './util/urls.js'; // Pensive emoji! import { OFFICIAL_GROUP_DIRECTORY } from './util/magic-constants.js'; @@ -199,541 +170,14 @@ let queueSize; const urls = generateURLs(urlSpec); -function splitLines(text) { - return text.split(/\r\n|\r|\n/); -} - -const replacerSpec = { - album: { - find: 'album', - link: 'album', - }, - 'album-commentary': { - find: 'album', - link: 'albumCommentary', - }, - 'album-gallery': { - find: 'album', - link: 'albumGallery', - }, - artist: { - find: 'artist', - link: 'artist', - }, - 'artist-gallery': { - find: 'artist', - link: 'artistGallery', - }, - 'commentary-index': { - find: null, - link: 'commentaryIndex', - }, - date: { - find: null, - value: (ref) => new Date(ref), - html: (date, {language}) => - html.tag('time', - {datetime: date.toString()}, - language.formatDate(date)), - }, - 'flash-index': { - find: null, - link: 'flashIndex', - }, - flash: { - find: 'flash', - link: 'flash', - transformName(name, node, input) { - const nextCharacter = input[node.iEnd]; - const lastCharacter = name[name.length - 1]; - if (![' ', '\n', '<'].includes(nextCharacter) && lastCharacter === '.') { - return name.slice(0, -1); - } else { - return name; - } - }, - }, - group: { - find: 'group', - link: 'groupInfo', - }, - 'group-gallery': { - find: 'group', - link: 'groupGallery', - }, - home: { - find: null, - link: 'home', - }, - 'listing-index': { - find: null, - link: 'listingIndex', - }, - listing: { - find: 'listing', - link: 'listing', - }, - media: { - find: null, - link: 'media', - }, - 'news-index': { - find: null, - link: 'newsIndex', - }, - 'news-entry': { - find: 'newsEntry', - link: 'newsEntry', - }, - root: { - find: null, - link: 'root', - }, - site: { - find: null, - link: 'site', - }, - static: { - find: 'staticPage', - link: 'staticPage', - }, - string: { - find: null, - value: (ref) => ref, - html: (ref, {language, args}) => language.$(ref, args), - }, - tag: { - find: 'artTag', - link: 'tag', - }, - track: { - find: 'track', - link: 'track', - }, -}; - -if (!validateReplacerSpec(replacerSpec, {find, link: unbound_link})) { +if (!validateReplacerSpec(replacerSpec, {find, link})) { process.exit(); } -function parseAttributes(string, {to}) { - const attributes = Object.create(null); - const skipWhitespace = (i) => { - const ws = /\s/; - if (ws.test(string[i])) { - const match = string.slice(i).match(/[^\s]/); - if (match) { - return i + match.index; - } else { - return string.length; - } - } else { - return i; - } - }; - - for (let i = 0; i < string.length; ) { - i = skipWhitespace(i); - const aStart = i; - const aEnd = i + string.slice(i).match(/[\s=]|$/).index; - const attribute = string.slice(aStart, aEnd); - i = skipWhitespace(aEnd); - if (string[i] === '=') { - i = skipWhitespace(i + 1); - let end, endOffset; - if (string[i] === '"' || string[i] === "'") { - end = string[i]; - endOffset = 1; - i++; - } else { - end = '\\s'; - endOffset = 0; - } - const vStart = i; - const vEnd = i + string.slice(i).match(new RegExp(`${end}|$`)).index; - const value = string.slice(vStart, vEnd); - i = vEnd + endOffset; - if (attribute === 'src' && value.startsWith('media/')) { - attributes[attribute] = to('media.path', value.slice('media/'.length)); - } else { - attributes[attribute] = value; - } - } else { - attributes[attribute] = attribute; - } - } - return Object.fromEntries( - Object.entries(attributes).map(([key, val]) => [ - key, - val === 'true' - ? true - : val === 'false' - ? false - : val === key - ? true - : val, - ]) - ); -} - -function joinLineBreaks(sourceLines) { - const outLines = []; - - let lineSoFar = ''; - for (let i = 0; i < sourceLines.length; i++) { - const line = sourceLines[i]; - lineSoFar += line; - if (!line.endsWith('
')) { - outLines.push(lineSoFar); - lineSoFar = ''; - } - } - - if (lineSoFar) { - outLines.push(lineSoFar); - } - - return outLines; -} - -function transformMultiline(text, { - parseAttributes, - transformInline, - thumb = null, -}) { - // Heck yes, HTML magics. - - text = transformInline(text.trim()); - - const outLines = []; - - const indentString = ' '.repeat(4); - - let levelIndents = []; - const openLevel = (indent) => { - // opening a sublist is a pain: to be semantically *and* visually - // correct, we have to append the
    at the end of the existing - // previous
  • - const previousLine = outLines[outLines.length - 1]; - if (previousLine?.endsWith('
  • ')) { - // we will re-close the
  • later - outLines[outLines.length - 1] = previousLine.slice(0, -5) + '
      '; - } else { - // if the previous line isn't a list item, this is the opening of - // the first list level, so no need for indent - outLines.push('
        '); - } - levelIndents.push(indent); - }; - const closeLevel = () => { - levelIndents.pop(); - if (levelIndents.length) { - // closing a sublist, so close the list item containing it too - outLines.push(indentString.repeat(levelIndents.length) + '
      '); - } else { - // closing the final list level! no need for indent here - outLines.push('
    '); - } - }; - - // okay yes we should support nested formatting, more than one blockquote - // layer, etc, but hear me out here: making all that work would basically - // be the same as implementing an entire markdown converter, which im not - // interested in doing lol. sorry!!! - let inBlockquote = false; - - let lines = splitLines(text); - lines = joinLineBreaks(lines); - for (let line of lines) { - const imageLine = line.startsWith('/g, (match, attributes) => - img({ - lazy: true, - link: true, - thumb, - ...parseAttributes(attributes), - }) - ); - - let indentThisLine = 0; - let lineContent = line; - let lineTag = 'p'; - - const listMatch = line.match(/^( *)- *(.*)$/); - if (listMatch) { - // is a list item! - if (!levelIndents.length) { - // first level is always indent = 0, regardless of actual line - // content (this is to avoid going to a lesser indent than the - // initial level) - openLevel(0); - } else { - // find level corresponding to indent - const indent = listMatch[1].length; - let i; - for (i = levelIndents.length - 1; i >= 0; i--) { - if (levelIndents[i] <= indent) break; - } - // note: i cannot equal -1 because the first indentation level - // is always 0, and the minimum indentation is also 0 - if (levelIndents[i] === indent) { - // same indent! return to that level - while (levelIndents.length - 1 > i) closeLevel(); - // (if this is already the current level, the above loop - // will do nothing) - } else if (levelIndents[i] < indent) { - // lesser indent! branch based on index - if (i === levelIndents.length - 1) { - // top level is lesser: add a new level - openLevel(indent); - } else { - // lower level is lesser: return to that level - while (levelIndents.length - 1 > i) closeLevel(); - } - } - } - // finally, set variables for appending content line - indentThisLine = levelIndents.length; - lineContent = listMatch[2]; - lineTag = 'li'; - } else { - // not a list item! close any existing list levels - while (levelIndents.length) closeLevel(); - - // like i said, no nested shenanigans - quotes only appear outside - // of lists. sorry! - const quoteMatch = line.match(/^> *(.*)$/); - if (quoteMatch) { - // is a quote! open a blockquote tag if it doesnt already exist - if (!inBlockquote) { - inBlockquote = true; - outLines.push('
    '); - } - indentThisLine = 1; - lineContent = quoteMatch[1]; - } else if (inBlockquote) { - // not a quote! close a blockquote tag if it exists - inBlockquote = false; - outLines.push('
    '); - } - - // let some escaped symbols display as the normal symbol, since the - // point of escaping them is just to avoid having them be treated as - // syntax markers! - if (lineContent.match(/( *)\\-/)) { - lineContent = lineContent.replace('\\-', '-'); - } else if (lineContent.match(/( *)\\>/)) { - lineContent = lineContent.replace('\\>', '>'); - } - } - - if (lineTag === 'p') { - // certain inline element tags should still be postioned within a - // paragraph; other elements (e.g. headings) should be added as-is - const elementMatch = line.match(/^<(.*?)[ >]/); - if ( - elementMatch && - !imageLine && - ![ - 'a', - 'abbr', - 'b', - 'bdo', - 'br', - 'cite', - 'code', - 'data', - 'datalist', - 'del', - 'dfn', - 'em', - 'i', - 'img', - 'ins', - 'kbd', - 'mark', - 'output', - 'picture', - 'q', - 'ruby', - 'samp', - 'small', - 'span', - 'strong', - 'sub', - 'sup', - 'svg', - 'time', - 'var', - 'wbr', - ].includes(elementMatch[1]) - ) { - lineTag = ''; - } - - // for sticky headings! - if (elementMatch) { - lineContent = lineContent.replace(/

    ${lineContent}`; - } else { - pushString += lineContent; - } - outLines.push(pushString); - } - - // after processing all lines... - - // if still in a list, close all levels - while (levelIndents.length) closeLevel(); - - // if still in a blockquote, close its tag - if (inBlockquote) { - inBlockquote = false; - outLines.push(''); - } - - return outLines.join('\n'); -} - -function transformLyrics(text, {transformInline, transformMultiline}) { - // Different from transformMultiline 'cuz it joins multiple lines together - // with line 8reaks (
    ); transformMultiline treats each line as its own - // complete paragraph (or list, etc). - - // If it looks like old data, then like, oh god. - // Use the normal transformMultiline tool. - if (text.includes(' outLines.push(`

    ${buildLine}

    `); - const outLines = []; - for (const line of text.split('\n')) { - if (line.length) { - if (buildLine.length) { - buildLine += '
    '; - } - buildLine += line; - } else if (buildLine.length) { - addLine(); - buildLine = ''; - } - } - if (buildLine.length) { - addLine(); - } - return outLines.join('\n'); -} - function stringifyThings(thingData) { return JSON.stringify(serializeThings(thingData)); } -function img({ - src, - alt, - noSrcText = '', - thumb: thumbKey, - reveal, - id, - class: className, - width, - height, - link = false, - lazy = false, - square = false, -}) { - const willSquare = square; - const willLink = typeof link === 'string' || link; - - const originalSrc = src; - const thumbSrc = src && (thumbKey ? thumb[thumbKey](src) : src); - - const imgAttributes = { - id: link ? '' : id, - class: className, - alt, - width, - height, - }; - - const noSrcHTML = - !src && - wrap( - html.tag('div', - {class: 'image-text-area'}, - noSrcText)); - - const nonlazyHTML = - src && - wrap( - html.tag('img', { - ...imgAttributes, - src: thumbSrc, - })); - - const lazyHTML = - src && - lazy && - wrap( - html.tag('img', - { - ...imgAttributes, - class: [className, 'lazy'], - 'data-original': thumbSrc, - }), - true); - - if (!src) { - return noSrcHTML; - } else if (lazy) { - return html.tag('noscript', nonlazyHTML) + '\n' + lazyHTML; - } else { - return nonlazyHTML; - } - - function wrap(input, hide = false) { - let wrapped = input; - - wrapped = html.tag('div', {class: 'image-inner-area'}, wrapped); - wrapped = html.tag('div', {class: 'image-container'}, wrapped); - - if (reveal) { - wrapped = html.tag('div', {class: 'reveal'}, [ - wrapped, - html.tag('span', {class: 'reveal-text'}, reveal), - ]); - } - - if (willSquare) { - wrapped = html.tag('div', {class: 'square-content'}, wrapped); - wrapped = html.tag('div', - {class: ['square', hide && !willLink && 'js-hide']}, - wrapped); - } - - if (willLink) { - wrapped = html.tag('a', - { - id, - class: ['box', hide && 'js-hide'], - href: typeof link === 'string' ? link : originalSrc, - }, - wrapped); - } - - return wrapped; - } -} - function validateWritePath(path, urlGroup) { if (!Array.isArray(path)) { return {error: `Expected array, got ${path}`}; @@ -1199,6 +643,7 @@ export function generateDocumentHTML(pageInfo, { html.tag('div', {id: 'info-card'}, [ html.tag('div', {class: ['info-card-art-container', 'no-reveal']}, img({ + html, class: 'info-card-art', src: '', link: true, @@ -1206,6 +651,7 @@ export function generateDocumentHTML(pageInfo, { })), html.tag('div', {class: ['info-card-art-container', 'reveal']}, img({ + html, class: 'info-card-art', src: '', link: true, @@ -1791,7 +1237,7 @@ async function main() { const appendIndexHTML = miscOptions['append-index-html'] ?? false; if (appendIndexHTML) { logWarn`Appending index.html to link hrefs. (Note: not recommended for production release!)`; - unbound_link.globalOptions.appendIndexHTML = true; + link.globalOptions.appendIndexHTML = true; } const skipThumbs = miscOptions['skip-thumbs'] ?? false; @@ -2334,202 +1780,12 @@ async function main() { ); }; - // TODO: Is there some nicer way to define these, - // may8e without totally re-8inding everything for - // each page? - const bound = {}; - - bound.html = html; - - bound.getColors = bindOpts(getColors, { - chroma, - }); - - bound.getLinkThemeString = bindOpts(unbound_getLinkThemeString, { - getColors: bound.getColors, - }); - - bound.getThemeString = bindOpts(unbound_getThemeString, { - getColors: bound.getColors, - }); - - bound.link = withEntries(unbound_link, (entries) => - entries - .map(([key, fn]) => [key, bindOpts(fn, { - getLinkThemeString: bound.getLinkThemeString, - to, - })])); - - bound.parseAttributes = bindOpts(parseAttributes, { - to, - }); - - bound.find = bindFind(wikiData, {mode: 'warn'}); - - bound.transformInline = bindOpts(transformInline, { - find: bound.find, - link: bound.link, - replacerSpec, + const bound = bindUtilities({ language, to, wikiData, }); - bound.transformMultiline = bindOpts(transformMultiline, { - transformInline: bound.transformInline, - parseAttributes: bound.parseAttributes, - }); - - bound.transformLyrics = bindOpts(transformLyrics, { - transformInline: bound.transformInline, - transformMultiline: bound.transformMultiline, - }); - - bound.iconifyURL = bindOpts(iconifyURL, { - html, - language, - to, - }); - - bound.fancifyURL = bindOpts(fancifyURL, { - html, - language, - }); - - bound.fancifyFlashURL = bindOpts(fancifyFlashURL, { - [bindOpts.bindIndex]: 2, - html, - language, - - fancifyURL: bound.fancifyURL, - }); - - bound.getRevealStringFromWarnings = bindOpts(getRevealStringFromWarnings, { - html, - language, - }); - - bound.getRevealStringFromTags = bindOpts(getRevealStringFromTags, { - language, - - getRevealStringFromWarnings: bound.getRevealStringFromWarnings, - }); - - bound.getArtistString = bindOpts(getArtistString, { - html, - link: bound.link, - language, - - iconifyURL: bound.iconifyURL, - }); - - bound.getAlbumCover = bindOpts(getAlbumCover, { - to, - }); - - bound.getTrackCover = bindOpts(getTrackCover, { - to, - }); - - bound.getFlashCover = bindOpts(getFlashCover, { - to, - }); - - bound.getArtistAvatar = bindOpts(getArtistAvatar, { - to, - }); - - bound.generateAdditionalFilesShortcut = bindOpts(generateAdditionalFilesShortcut, { - html, - language, - }); - - bound.generateAdditionalFilesList = bindOpts(generateAdditionalFilesList, { - html, - language, - }); - - bound.generateNavigationLinks = bindOpts(generateNavigationLinks, { - link: bound.link, - language, - }); - - bound.generateStickyHeadingContainer = bindOpts(generateStickyHeadingContainer, { - [bindOpts.bindIndex]: 0, - getRevealStringFromTags: bound.getRevealStringFromTags, - html, - img, - }); - - bound.generateChronologyLinks = bindOpts(generateChronologyLinks, { - html, - language, - link: bound.link, - wikiData, - - generateNavigationLinks: bound.generateNavigationLinks, - }); - - bound.generateCoverLink = bindOpts(generateCoverLink, { - [bindOpts.bindIndex]: 0, - html, - img, - link: bound.link, - language, - to, - wikiData, - - getRevealStringFromTags: bound.getRevealStringFromTags, - }); - - bound.generateInfoGalleryLinks = bindOpts(generateInfoGalleryLinks, { - [bindOpts.bindIndex]: 2, - link: bound.link, - language, - }); - - bound.generateTrackListDividedByGroups = bindOpts(generateTrackListDividedByGroups, { - html, - language, - wikiData, - }); - - bound.getGridHTML = bindOpts(getGridHTML, { - [bindOpts.bindIndex]: 0, - img, - html, - language, - - getRevealStringFromTags: bound.getRevealStringFromTags, - }); - - bound.getAlbumGridHTML = bindOpts(getAlbumGridHTML, { - [bindOpts.bindIndex]: 0, - link: bound.link, - language, - - getAlbumCover: bound.getAlbumCover, - getGridHTML: bound.getGridHTML, - }); - - bound.getFlashGridHTML = bindOpts(getFlashGridHTML, { - [bindOpts.bindIndex]: 0, - link: bound.link, - - getFlashCover: bound.getFlashCover, - getGridHTML: bound.getGridHTML, - }); - - bound.getCarouselHTML = bindOpts(getCarouselHTML, { - [bindOpts.bindIndex]: 0, - img, - html, - }) - - bound.getAlbumStylesheet = bindOpts(getAlbumStylesheet, { - to, - }); - const pageInfo = page({ ...bound, diff --git a/src/util/transform-content.js b/src/util/transform-content.js new file mode 100644 index 00000000..2a7d859b --- /dev/null +++ b/src/util/transform-content.js @@ -0,0 +1,447 @@ +// See also replacer.js, which covers the actual syntax parser and node +// interpreter. This file works with replacer.js to provide higher-level +// interfaces for converting various content found in wiki data to HTML for +// display on the site. + +import * as html from './html.js'; +export {transformInline} from './replacer.js'; + +export const replacerSpec = { + album: { + find: 'album', + link: 'album', + }, + 'album-commentary': { + find: 'album', + link: 'albumCommentary', + }, + 'album-gallery': { + find: 'album', + link: 'albumGallery', + }, + artist: { + find: 'artist', + link: 'artist', + }, + 'artist-gallery': { + find: 'artist', + link: 'artistGallery', + }, + 'commentary-index': { + find: null, + link: 'commentaryIndex', + }, + date: { + find: null, + value: (ref) => new Date(ref), + html: (date, {language}) => + html.tag('time', + {datetime: date.toString()}, + language.formatDate(date)), + }, + 'flash-index': { + find: null, + link: 'flashIndex', + }, + flash: { + find: 'flash', + link: 'flash', + transformName(name, node, input) { + const nextCharacter = input[node.iEnd]; + const lastCharacter = name[name.length - 1]; + if (![' ', '\n', '<'].includes(nextCharacter) && lastCharacter === '.') { + return name.slice(0, -1); + } else { + return name; + } + }, + }, + group: { + find: 'group', + link: 'groupInfo', + }, + 'group-gallery': { + find: 'group', + link: 'groupGallery', + }, + home: { + find: null, + link: 'home', + }, + 'listing-index': { + find: null, + link: 'listingIndex', + }, + listing: { + find: 'listing', + link: 'listing', + }, + media: { + find: null, + link: 'media', + }, + 'news-index': { + find: null, + link: 'newsIndex', + }, + 'news-entry': { + find: 'newsEntry', + link: 'newsEntry', + }, + root: { + find: null, + link: 'root', + }, + site: { + find: null, + link: 'site', + }, + static: { + find: 'staticPage', + link: 'staticPage', + }, + string: { + find: null, + value: (ref) => ref, + html: (ref, {language, args}) => language.$(ref, args), + }, + tag: { + find: 'artTag', + link: 'tag', + }, + track: { + find: 'track', + link: 'track', + }, +}; + +function splitLines(text) { + return text.split(/\r\n|\r|\n/); +} + +function joinLineBreaks(sourceLines) { + const outLines = []; + + let lineSoFar = ''; + for (let i = 0; i < sourceLines.length; i++) { + const line = sourceLines[i]; + lineSoFar += line; + if (!line.endsWith('
    ')) { + outLines.push(lineSoFar); + lineSoFar = ''; + } + } + + if (lineSoFar) { + outLines.push(lineSoFar); + } + + return outLines; +} + +function parseAttributes(string, {to}) { + const attributes = Object.create(null); + const skipWhitespace = (i) => { + const ws = /\s/; + if (ws.test(string[i])) { + const match = string.slice(i).match(/[^\s]/); + if (match) { + return i + match.index; + } else { + return string.length; + } + } else { + return i; + } + }; + + for (let i = 0; i < string.length; ) { + i = skipWhitespace(i); + const aStart = i; + const aEnd = i + string.slice(i).match(/[\s=]|$/).index; + const attribute = string.slice(aStart, aEnd); + i = skipWhitespace(aEnd); + if (string[i] === '=') { + i = skipWhitespace(i + 1); + let end, endOffset; + if (string[i] === '"' || string[i] === "'") { + end = string[i]; + endOffset = 1; + i++; + } else { + end = '\\s'; + endOffset = 0; + } + const vStart = i; + const vEnd = i + string.slice(i).match(new RegExp(`${end}|$`)).index; + const value = string.slice(vStart, vEnd); + i = vEnd + endOffset; + if (attribute === 'src' && value.startsWith('media/')) { + attributes[attribute] = to('media.path', value.slice('media/'.length)); + } else { + attributes[attribute] = value; + } + } else { + attributes[attribute] = attribute; + } + } + return Object.fromEntries( + Object.entries(attributes).map(([key, val]) => [ + key, + val === 'true' + ? true + : val === 'false' + ? false + : val === key + ? true + : val, + ]) + ); +} + +function unbound_transformMultiline(text, { + img, + to, + transformInline, + + thumb = null, +}) { + // Heck yes, HTML magics. + + text = transformInline(text.trim()); + + const outLines = []; + + const indentString = ' '.repeat(4); + + let levelIndents = []; + const openLevel = (indent) => { + // opening a sublist is a pain: to be semantically *and* visually + // correct, we have to append the
      at the end of the existing + // previous
    • + const previousLine = outLines[outLines.length - 1]; + if (previousLine?.endsWith('
    • ')) { + // we will re-close the
    • later + outLines[outLines.length - 1] = previousLine.slice(0, -5) + '
        '; + } else { + // if the previous line isn't a list item, this is the opening of + // the first list level, so no need for indent + outLines.push('
          '); + } + levelIndents.push(indent); + }; + const closeLevel = () => { + levelIndents.pop(); + if (levelIndents.length) { + // closing a sublist, so close the list item containing it too + outLines.push(indentString.repeat(levelIndents.length) + '
        '); + } else { + // closing the final list level! no need for indent here + outLines.push('
      '); + } + }; + + // okay yes we should support nested formatting, more than one blockquote + // layer, etc, but hear me out here: making all that work would basically + // be the same as implementing an entire markdown converter, which im not + // interested in doing lol. sorry!!! + let inBlockquote = false; + + let lines = splitLines(text); + lines = joinLineBreaks(lines); + for (let line of lines) { + const imageLine = line.startsWith('/g, (match, attributes) => + img({ + lazy: true, + link: true, + thumb, + ...parseAttributes(attributes, {to}), + }) + ); + + let indentThisLine = 0; + let lineContent = line; + let lineTag = 'p'; + + const listMatch = line.match(/^( *)- *(.*)$/); + if (listMatch) { + // is a list item! + if (!levelIndents.length) { + // first level is always indent = 0, regardless of actual line + // content (this is to avoid going to a lesser indent than the + // initial level) + openLevel(0); + } else { + // find level corresponding to indent + const indent = listMatch[1].length; + let i; + for (i = levelIndents.length - 1; i >= 0; i--) { + if (levelIndents[i] <= indent) break; + } + // note: i cannot equal -1 because the first indentation level + // is always 0, and the minimum indentation is also 0 + if (levelIndents[i] === indent) { + // same indent! return to that level + while (levelIndents.length - 1 > i) closeLevel(); + // (if this is already the current level, the above loop + // will do nothing) + } else if (levelIndents[i] < indent) { + // lesser indent! branch based on index + if (i === levelIndents.length - 1) { + // top level is lesser: add a new level + openLevel(indent); + } else { + // lower level is lesser: return to that level + while (levelIndents.length - 1 > i) closeLevel(); + } + } + } + // finally, set variables for appending content line + indentThisLine = levelIndents.length; + lineContent = listMatch[2]; + lineTag = 'li'; + } else { + // not a list item! close any existing list levels + while (levelIndents.length) closeLevel(); + + // like i said, no nested shenanigans - quotes only appear outside + // of lists. sorry! + const quoteMatch = line.match(/^> *(.*)$/); + if (quoteMatch) { + // is a quote! open a blockquote tag if it doesnt already exist + if (!inBlockquote) { + inBlockquote = true; + outLines.push('
      '); + } + indentThisLine = 1; + lineContent = quoteMatch[1]; + } else if (inBlockquote) { + // not a quote! close a blockquote tag if it exists + inBlockquote = false; + outLines.push('
      '); + } + + // let some escaped symbols display as the normal symbol, since the + // point of escaping them is just to avoid having them be treated as + // syntax markers! + if (lineContent.match(/( *)\\-/)) { + lineContent = lineContent.replace('\\-', '-'); + } else if (lineContent.match(/( *)\\>/)) { + lineContent = lineContent.replace('\\>', '>'); + } + } + + if (lineTag === 'p') { + // certain inline element tags should still be postioned within a + // paragraph; other elements (e.g. headings) should be added as-is + const elementMatch = line.match(/^<(.*?)[ >]/); + if ( + elementMatch && + !imageLine && + ![ + 'a', + 'abbr', + 'b', + 'bdo', + 'br', + 'cite', + 'code', + 'data', + 'datalist', + 'del', + 'dfn', + 'em', + 'i', + 'img', + 'ins', + 'kbd', + 'mark', + 'output', + 'picture', + 'q', + 'ruby', + 'samp', + 'small', + 'span', + 'strong', + 'sub', + 'sup', + 'svg', + 'time', + 'var', + 'wbr', + ].includes(elementMatch[1]) + ) { + lineTag = ''; + } + + // for sticky headings! + if (elementMatch) { + lineContent = lineContent.replace(/

      ${lineContent}`; + } else { + pushString += lineContent; + } + outLines.push(pushString); + } + + // after processing all lines... + + // if still in a list, close all levels + while (levelIndents.length) closeLevel(); + + // if still in a blockquote, close its tag + if (inBlockquote) { + inBlockquote = false; + outLines.push(''); + } + + return outLines.join('\n'); +} + +function unbound_transformLyrics(text, { + transformInline, + transformMultiline, +}) { + // Different from transformMultiline 'cuz it joins multiple lines together + // with line 8reaks (
      ); transformMultiline treats each line as its own + // complete paragraph (or list, etc). + + // If it looks like old data, then like, oh god. + // Use the normal transformMultiline tool. + if (text.includes(' outLines.push(`

      ${buildLine}

      `); + const outLines = []; + for (const line of text.split('\n')) { + if (line.length) { + if (buildLine.length) { + buildLine += '
      '; + } + buildLine += line; + } else if (buildLine.length) { + addLine(); + buildLine = ''; + } + } + if (buildLine.length) { + addLine(); + } + return outLines.join('\n'); +} + +export { + unbound_transformLyrics as transformLyrics, + unbound_transformMultiline as transformMultiline +} diff --git a/src/write/bind-utilities.js b/src/write/bind-utilities.js new file mode 100644 index 00000000..6632ba30 --- /dev/null +++ b/src/write/bind-utilities.js @@ -0,0 +1,257 @@ +// Ties lots and lots of functions together in a convenient package accessible +// to page write functions. This is kept in a separate file from other write +// areas to keep imports neat and isolated. + +import chroma from 'chroma-js'; + +import { + fancifyFlashURL, + fancifyURL, + getAlbumGridHTML, + getAlbumStylesheet, + getArtistString, + getCarouselHTML, + getFlashGridHTML, + getGridHTML, + getRevealStringFromTags, + getRevealStringFromWarnings, + getThemeString, + generateAdditionalFilesList, + generateAdditionalFilesShortcut, + generateChronologyLinks, + generateCoverLink, + generateInfoGalleryLinks, + generateTrackListDividedByGroups, + generateNavigationLinks, + generateStickyHeadingContainer, + iconifyURL, + img, +} from '../misc-templates.js'; + +import { + replacerSpec, + transformInline, + transformLyrics, + transformMultiline, +} from '../util/transform-content.js'; + +import * as html from '../util/html.js'; + +import {bindOpts, withEntries} from '../util/sugar.js'; +import {getColors} from '../util/colors.js'; +import {bindFind} from '../util/find.js'; + +import link, {getLinkThemeString} from '../util/link.js'; + +import { + getAlbumCover, + getArtistAvatar, + getFlashCover, + getTrackCover, +} from '../util/wiki-data.js'; + +export function bindUtilities({ + language, + to, + wikiData, +}) { + // TODO: Is there some nicer way to define these, + // may8e without totally re-8inding everything for + // each page? + const bound = {}; + + bound.html = html; + + bound.img = bindOpts(img, { + [bindOpts.bindIndex]: 0, + html, + }); + + bound.getColors = bindOpts(getColors, { + chroma, + }); + + bound.getLinkThemeString = bindOpts(getLinkThemeString, { + getColors: bound.getColors, + }); + + bound.getThemeString = bindOpts(getThemeString, { + getColors: bound.getColors, + }); + + bound.link = withEntries(link, (entries) => + entries + .map(([key, fn]) => [key, bindOpts(fn, { + getLinkThemeString: bound.getLinkThemeString, + to, + })])); + + bound.find = bindFind(wikiData, {mode: 'warn'}); + + bound.transformInline = bindOpts(transformInline, { + find: bound.find, + link: bound.link, + replacerSpec, + language, + to, + wikiData, + }); + + bound.transformMultiline = bindOpts(transformMultiline, { + img: bound.img, + to, + transformInline: bound.transformInline, + }); + + bound.transformLyrics = bindOpts(transformLyrics, { + transformInline: bound.transformInline, + transformMultiline: bound.transformMultiline, + }); + + bound.iconifyURL = bindOpts(iconifyURL, { + html, + language, + to, + }); + + bound.fancifyURL = bindOpts(fancifyURL, { + html, + language, + }); + + bound.fancifyFlashURL = bindOpts(fancifyFlashURL, { + [bindOpts.bindIndex]: 2, + html, + language, + + fancifyURL: bound.fancifyURL, + }); + + bound.getRevealStringFromWarnings = bindOpts(getRevealStringFromWarnings, { + html, + language, + }); + + bound.getRevealStringFromTags = bindOpts(getRevealStringFromTags, { + language, + + getRevealStringFromWarnings: bound.getRevealStringFromWarnings, + }); + + bound.getArtistString = bindOpts(getArtistString, { + html, + link: bound.link, + language, + + iconifyURL: bound.iconifyURL, + }); + + bound.getAlbumCover = bindOpts(getAlbumCover, { + to, + }); + + bound.getTrackCover = bindOpts(getTrackCover, { + to, + }); + + bound.getFlashCover = bindOpts(getFlashCover, { + to, + }); + + bound.getArtistAvatar = bindOpts(getArtistAvatar, { + to, + }); + + bound.generateAdditionalFilesShortcut = bindOpts(generateAdditionalFilesShortcut, { + html, + language, + }); + + bound.generateAdditionalFilesList = bindOpts(generateAdditionalFilesList, { + html, + language, + }); + + bound.generateNavigationLinks = bindOpts(generateNavigationLinks, { + link: bound.link, + language, + }); + + bound.generateStickyHeadingContainer = bindOpts(generateStickyHeadingContainer, { + [bindOpts.bindIndex]: 0, + getRevealStringFromTags: bound.getRevealStringFromTags, + html, + img: bound.img, + }); + + bound.generateChronologyLinks = bindOpts(generateChronologyLinks, { + html, + language, + link: bound.link, + wikiData, + + generateNavigationLinks: bound.generateNavigationLinks, + }); + + bound.generateCoverLink = bindOpts(generateCoverLink, { + [bindOpts.bindIndex]: 0, + html, + img: bound.img, + link: bound.link, + language, + to, + wikiData, + + getRevealStringFromTags: bound.getRevealStringFromTags, + }); + + bound.generateInfoGalleryLinks = bindOpts(generateInfoGalleryLinks, { + [bindOpts.bindIndex]: 2, + link: bound.link, + language, + }); + + bound.generateTrackListDividedByGroups = bindOpts(generateTrackListDividedByGroups, { + html, + language, + wikiData, + }); + + bound.getGridHTML = bindOpts(getGridHTML, { + [bindOpts.bindIndex]: 0, + img: bound.img, + html, + language, + + getRevealStringFromTags: bound.getRevealStringFromTags, + }); + + bound.getAlbumGridHTML = bindOpts(getAlbumGridHTML, { + [bindOpts.bindIndex]: 0, + link: bound.link, + language, + + getAlbumCover: bound.getAlbumCover, + getGridHTML: bound.getGridHTML, + }); + + bound.getFlashGridHTML = bindOpts(getFlashGridHTML, { + [bindOpts.bindIndex]: 0, + link: bound.link, + + getFlashCover: bound.getFlashCover, + getGridHTML: bound.getGridHTML, + }); + + bound.getCarouselHTML = bindOpts(getCarouselHTML, { + [bindOpts.bindIndex]: 0, + img: bound.img, + html, + }) + + bound.getAlbumStylesheet = bindOpts(getAlbumStylesheet, { + to, + }); + + return bound; +} -- cgit 1.3.0-6-gf8a5 From 613a6a6c253219d85211cdecedacbba45f44a1c2 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 7 Jan 2023 20:07:14 -0400 Subject: extract write validation fns towards basic dynamics pt. 1 (#124) --- src/upd8.js | 148 ++++--------------------------------------- src/write/validate-writes.js | 134 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 135 deletions(-) create mode 100644 src/write/validate-writes.js diff --git a/src/upd8.js b/src/upd8.js index 0573dcd2..fb1239a0 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -60,11 +60,11 @@ import {isMain} from './util/node-utils.js'; import {replacerSpec} from './util/transform-content.js'; import CacheableObject from './data/things/cacheable-object.js'; - import {processLanguageFile} from './data/language.js'; import {serializeThings} from './data/serialize.js'; import {bindUtilities} from './write/bind-utilities.js'; +import {validateWrites} from './write/validate-writes.js'; import { filterDuplicateDirectories, @@ -178,104 +178,6 @@ function stringifyThings(thingData) { return JSON.stringify(serializeThings(thingData)); } -function validateWritePath(path, urlGroup) { - if (!Array.isArray(path)) { - return {error: `Expected array, got ${path}`}; - } - - const {paths} = urlGroup; - - const definedKeys = Object.keys(paths); - const specifiedKey = path[0]; - - if (!definedKeys.includes(specifiedKey)) { - return {error: `Specified key ${specifiedKey} isn't defined`}; - } - - const expectedArgs = paths[specifiedKey].match(/<>/g)?.length ?? 0; - const specifiedArgs = path.length - 1; - - if (specifiedArgs !== expectedArgs) { - return { - error: `Expected ${expectedArgs} arguments, got ${specifiedArgs}`, - }; - } - - return {success: true}; -} - -function validateWriteObject(obj) { - if (typeof obj !== 'object') { - return {error: `Expected object, got ${typeof obj}`}; - } - - if (typeof obj.type !== 'string') { - return {error: `Expected type to be string, got ${obj.type}`}; - } - - switch (obj.type) { - case 'legacy': { - if (typeof obj.write !== 'function') { - return {error: `Expected write to be string, got ${obj.write}`}; - } - - break; - } - - case 'page': { - const path = validateWritePath(obj.path, urlSpec.localized); - if (path.error) { - return {error: `Path validation failed: ${path.error}`}; - } - - if (typeof obj.page !== 'function') { - return {error: `Expected page to be function, got ${obj.content}`}; - } - - break; - } - - case 'data': { - const path = validateWritePath(obj.path, urlSpec.data); - if (path.error) { - return {error: `Path validation failed: ${path.error}`}; - } - - if (typeof obj.data !== 'function') { - return {error: `Expected data to be function, got ${obj.data}`}; - } - - break; - } - - case 'redirect': { - const fromPath = validateWritePath(obj.fromPath, urlSpec.localized); - if (fromPath.error) { - return { - error: `Path (fromPath) validation failed: ${fromPath.error}`, - }; - } - - const toPath = validateWritePath(obj.toPath, urlSpec.localized); - if (toPath.error) { - return {error: `Path (toPath) validation failed: ${toPath.error}`}; - } - - if (typeof obj.title !== 'function') { - return {error: `Expected title to be function, got ${obj.title}`}; - } - - break; - } - - default: { - return {error: `Unknown type: ${obj.type}`}; - } - } - - return {success: true}; -} - export function getURLsFrom({ baseDirectory, pageSubKey, @@ -1623,50 +1525,26 @@ async function main() { return; } - const validateWrites = (writes, fnName) => { - // Do a quick valid8tion! If one of the writeThingPages functions go - // wrong, this will stall out early and tell us which did. - - if (!Array.isArray(writes)) { - logError`${fnName} didn't return an array!`; - error = true; - return false; - } - - if ( - !( - writes.every((obj) => typeof obj === 'object') && - writes.every((obj) => { - const result = validateWriteObject(obj); - if (result.error) { - logError`Validating write object failed: ${result.error}`; - return false; - } else { - return true; - } - }) - ) - ) { - logError`${fnName} returned invalid entries!`; - error = true; - return false; - } - - return true; - }; - - // return; - writes = progressCallAll('Computing page & data writes.', buildStepsWithTargets.flatMap(({flag, pageSpec, targets}) => { const writesFns = targets.map(target => () => { const writes = pageSpec.write(target, {wikiData})?.slice() || []; - return validateWrites(writes, flag + '.write') ? writes : []; + const valid = validateWrites(writes, { + functionName: flag + '.write', + urlSpec, + }); + error ||=! valid; + return valid ? writes : []; }); if (pageSpec.writeTargetless) { writesFns.push(() => { const writes = pageSpec.writeTargetless({wikiData}); - return validateWrites(writes, flag + '.writeTargetless') ? writes : []; + const valid = validateWrites(writes, { + functionName: flag + '.writeTargetless', + urlSpec, + }); + error ||=! valid; + return valid ? writes : []; }); } diff --git a/src/write/validate-writes.js b/src/write/validate-writes.js new file mode 100644 index 00000000..5d61d0e7 --- /dev/null +++ b/src/write/validate-writes.js @@ -0,0 +1,134 @@ +import {logError} from '../util/cli.js'; + +function validateWritePath(path, urlGroup) { + if (!Array.isArray(path)) { + return {error: `Expected array, got ${path}`}; + } + + const {paths} = urlGroup; + + const definedKeys = Object.keys(paths); + const specifiedKey = path[0]; + + if (!definedKeys.includes(specifiedKey)) { + return {error: `Specified key ${specifiedKey} isn't defined`}; + } + + const expectedArgs = paths[specifiedKey].match(/<>/g)?.length ?? 0; + const specifiedArgs = path.length - 1; + + if (specifiedArgs !== expectedArgs) { + return { + error: `Expected ${expectedArgs} arguments, got ${specifiedArgs}`, + }; + } + + return {success: true}; +} + +function validateWriteObject(obj, { + urlSpec, +}) { + if (typeof obj !== 'object') { + return {error: `Expected object, got ${typeof obj}`}; + } + + if (typeof obj.type !== 'string') { + return {error: `Expected type to be string, got ${obj.type}`}; + } + + switch (obj.type) { + case 'legacy': { + if (typeof obj.write !== 'function') { + return {error: `Expected write to be string, got ${obj.write}`}; + } + + break; + } + + case 'page': { + const path = validateWritePath(obj.path, urlSpec.localized); + if (path.error) { + return {error: `Path validation failed: ${path.error}`}; + } + + if (typeof obj.page !== 'function') { + return {error: `Expected page to be function, got ${obj.content}`}; + } + + break; + } + + case 'data': { + const path = validateWritePath(obj.path, urlSpec.data); + if (path.error) { + return {error: `Path validation failed: ${path.error}`}; + } + + if (typeof obj.data !== 'function') { + return {error: `Expected data to be function, got ${obj.data}`}; + } + + break; + } + + case 'redirect': { + const fromPath = validateWritePath(obj.fromPath, urlSpec.localized); + if (fromPath.error) { + return { + error: `Path (fromPath) validation failed: ${fromPath.error}`, + }; + } + + const toPath = validateWritePath(obj.toPath, urlSpec.localized); + if (toPath.error) { + return {error: `Path (toPath) validation failed: ${toPath.error}`}; + } + + if (typeof obj.title !== 'function') { + return {error: `Expected title to be function, got ${obj.title}`}; + } + + break; + } + + default: { + return {error: `Unknown type: ${obj.type}`}; + } + } + + return {success: true}; +} + +export function validateWrites(writes, { + functionName, + urlSpec, +}) { + // Do a quick valid8tion! If one of the writeThingPages functions go + // wrong, this will stall out early and tell us which did. + + if (!Array.isArray(writes)) { + logError`${functionName} didn't return an array!`; + return false; + } + + if (!( + writes.every((obj) => typeof obj === 'object') && + writes.every((obj) => { + const result = validateWriteObject(obj, { + urlSpec, + }); + if (result.error) { + logError`Validating write object failed: ${result.error}`; + return false; + } else { + return true; + } + }) + )) { + logError`${functionName} returned invalid entries!`; + return false; + } + + return true; +} -- cgit 1.3.0-6-gf8a5 From d59ed9687eb8e38aac3ca7dd5b190ba78b3b5556 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 7 Jan 2023 20:18:51 -0400 Subject: extract page templates (generateDocumentJSON, etc) towards basic dynamcis pt. 1 (#124) --- src/upd8.js | 630 ++------------------------------------------- src/write/page-template.js | 608 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 627 insertions(+), 611 deletions(-) create mode 100644 src/write/page-template.js diff --git a/src/upd8.js b/src/upd8.js index fb1239a0..a8dda34d 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -31,8 +31,7 @@ // Oh yeah, like. Just run this through some relatively recent version of // node.js and you'll 8e fine. ...Within the project root. O8viously. -import chroma from 'chroma-js'; - +import {execSync} from 'child_process'; import * as path from 'path'; import {fileURLToPath} from 'url'; @@ -45,26 +44,14 @@ import { unlink, } from 'fs/promises'; -import { execSync } from 'child_process'; - import genThumbs from './gen-thumbs.js'; import {listingSpec, listingTargetSpec} from './listing-spec.js'; import urlSpec from './url-spec.js'; -import * as pageSpecs from './page/index.js'; - -import find from './util/find.js'; -import * as html from './util/html.js'; -import {getColors} from './util/colors.js'; -import {findFiles} from './util/io.js'; -import {isMain} from './util/node-utils.js'; -import {replacerSpec} from './util/transform-content.js'; -import CacheableObject from './data/things/cacheable-object.js'; import {processLanguageFile} from './data/language.js'; import {serializeThings} from './data/serialize.js'; -import {bindUtilities} from './write/bind-utilities.js'; -import {validateWrites} from './write/validate-writes.js'; +import CacheableObject from './data/things/cacheable-object.js'; import { filterDuplicateDirectories, @@ -75,13 +62,14 @@ import { WIKI_INFO_FILE, } from './data/yaml.js'; -import { - getFooterLocalizationLinks, - getRevealStringFromWarnings, - img, -} from './misc-templates.js'; +import * as pageSpecs from './page/index.js'; +import find from './util/find.js'; +import {findFiles} from './util/io.js'; import link from './util/link.js'; +import {isMain} from './util/node-utils.js'; +import {validateReplacerSpec} from './util/replacer.js'; +import {replacerSpec} from './util/transform-content.js'; import { color, @@ -94,7 +82,14 @@ import { progressPromiseAll, } from './util/cli.js'; -import {validateReplacerSpec} from './util/replacer.js'; +import {bindUtilities} from './write/bind-utilities.js'; +import {validateWrites} from './write/validate-writes.js'; + +import { + generateDocumentHTML, + generateRedirectHTML, + generateOEmbedJSON, +} from './write/page-template.js'; /* import { @@ -223,566 +218,6 @@ export function getURLsFrom({ }; } -export function generateDocumentHTML(pageInfo, { - defaultLanguage, - getThemeString, - language, - languages, - localizedPaths, - paths, - oEmbedJSONHref, - to, - transformMultiline, - wikiData, -}) { - const {wikiInfo} = wikiData; - - let { - title = '', - meta = {}, - theme = '', - stylesheet = '', - - showWikiNameInTitle = true, - themeColor = '', - - // missing properties are auto-filled, see below! - body = {}, - banner = {}, - main = {}, - sidebarLeft = {}, - sidebarRight = {}, - nav = {}, - secondaryNav = {}, - footer = {}, - socialEmbed = {}, - } = pageInfo; - - body.style ??= ''; - - theme = theme || getThemeString(wikiInfo.color); - - banner ||= {}; - banner.classes ??= []; - banner.src ??= ''; - banner.position ??= ''; - banner.dimensions ??= [0, 0]; - - main.classes ??= []; - main.content ??= ''; - - sidebarLeft ??= {}; - sidebarRight ??= {}; - - for (const sidebar of [sidebarLeft, sidebarRight]) { - sidebar.classes ??= []; - sidebar.content ??= ''; - sidebar.collapse ??= true; - } - - nav.classes ??= []; - nav.content ??= ''; - nav.bottomRowContent ??= ''; - nav.links ??= []; - nav.linkContainerClasses ??= []; - - secondaryNav ??= {}; - secondaryNav.content ??= ''; - secondaryNav.content ??= ''; - - footer.classes ??= []; - footer.content ??= wikiInfo.footerContent - ? transformMultiline(wikiInfo.footerContent) - : ''; - - const colors = themeColor - ? getColors(themeColor, {chroma}) - : null; - - const canonical = wikiInfo.canonicalBase - ? wikiInfo.canonicalBase + (paths.pathname === '/' ? '' : paths.pathname) - : ''; - - const localizedCanonical = wikiInfo.canonicalBase - ? Object.entries(localizedPaths).map(([code, {pathname}]) => ({ - lang: code, - href: wikiInfo.canonicalBase + (pathname === '/' ? '' : pathname), - })) - : []; - - const collapseSidebars = - sidebarLeft.collapse !== false && sidebarRight.collapse !== false; - - const mainHTML = - main.content && - html.tag('main', - { - id: 'content', - class: main.classes, - }, - main.content); - - const footerHTML = - html.tag('footer', - { - [html.onlyIfContent]: true, - id: 'footer', - class: footer.classes, - }, - [ - html.tag('div', - { - [html.onlyIfContent]: true, - class: 'footer-content', - }, - footer.content), - - getFooterLocalizationLinks(paths.pathname, { - defaultLanguage, - html, - language, - languages, - paths, - to, - }), - ]); - - const generateSidebarHTML = (id, { - content, - multiple, - classes, - collapse = true, - wide = false, - - // 'last' - last or only sidebar box is sticky - // 'column' - entire column, incl. multiple boxes from top, is sticky - // 'none' - sidebar not sticky at all, stays at top of page - stickyMode = 'last', - }) => - content - ? html.tag('div', - { - id, - class: [ - 'sidebar-column', - 'sidebar', - wide && 'wide', - !collapse && 'no-hide', - stickyMode !== 'none' && 'sticky-' + stickyMode, - ...classes, - ], - }, - content) - : multiple - ? html.tag('div', - { - id, - class: [ - 'sidebar-column', - 'sidebar-multiple', - wide && 'wide', - !collapse && 'no-hide', - stickyMode !== 'none' && 'sticky-' + stickyMode, - ], - }, - multiple - .map((infoOrContent) => - (typeof infoOrContent === 'object' && !Array.isArray(infoOrContent)) - ? infoOrContent - : {content: infoOrContent}) - .filter(({content}) => content) - .map(({ - content, - classes: classes2 = [], - }) => - html.tag('div', - { - class: ['sidebar', ...classes, ...classes2], - }, - html.fragment(content)))) - : ''; - - const sidebarLeftHTML = generateSidebarHTML('sidebar-left', sidebarLeft); - const sidebarRightHTML = generateSidebarHTML('sidebar-right', sidebarRight); - - if (nav.simple) { - nav.linkContainerClasses = ['nav-links-hierarchy']; - nav.links = [{toHome: true}, {toCurrentPage: true}]; - } - - const links = (nav.links || []).filter(Boolean); - - const navLinkParts = []; - for (let i = 0; i < links.length; i++) { - let cur = links[i]; - - let {title: linkTitle} = cur; - - if (cur.toHome) { - linkTitle ??= wikiInfo.nameShort; - } else if (cur.toCurrentPage) { - linkTitle ??= title; - } - - let partContent; - - if (typeof cur.html === 'string') { - partContent = cur.html; - } else { - const attributes = { - class: (cur.toCurrentPage || i === links.length - 1) && 'current', - href: cur.toCurrentPage - ? '' - : cur.toHome - ? to('localized.home') - : cur.path - ? to(...cur.path) - : cur.href - ? (() => { - logWarn`Using legacy href format nav link in ${paths.pathname}`; - return cur.href; - })() - : null, - }; - if (attributes.href === null) { - throw new Error( - `Expected some href specifier for link to ${linkTitle} (${JSON.stringify( - cur - )})` - ); - } - partContent = html.tag('a', attributes, linkTitle); - } - - if (!partContent) continue; - - const part = html.tag('span', - {class: cur.divider === false && 'no-divider'}, - partContent); - - navLinkParts.push(part); - } - - const navHTML = html.tag('nav', - { - [html.onlyIfContent]: true, - id: 'header', - class: [ - ...nav.classes, - links.length && 'nav-has-main-links', - nav.content && 'nav-has-content', - nav.bottomRowContent && 'nav-has-bottom-row', - ], - }, - [ - links.length && - html.tag( - 'div', - {class: ['nav-main-links', ...nav.linkContainerClasses]}, - navLinkParts - ), - nav.bottomRowContent && - html.tag('div', {class: 'nav-bottom-row'}, nav.bottomRowContent), - nav.content && html.tag('div', {class: 'nav-content'}, nav.content), - ]); - - const secondaryNavHTML = html.tag('nav', - { - [html.onlyIfContent]: true, - id: 'secondary-nav', - class: secondaryNav.classes, - }, - secondaryNav.content); - - const bannerSrc = banner.src - ? banner.src - : banner.path - ? to(...banner.path) - : null; - - const bannerHTML = - banner.position && - bannerSrc && - html.tag('div', - { - id: 'banner', - class: banner.classes, - }, - html.tag('img', { - src: bannerSrc, - alt: banner.alt, - width: banner.dimensions[0] || 1100, - height: banner.dimensions[1] || 200, - })); - - const layoutHTML = [ - navHTML, - banner.position === 'top' && bannerHTML, - secondaryNavHTML, - html.tag('div', - { - class: [ - 'layout-columns', - !collapseSidebars && 'vertical-when-thin', - (sidebarLeftHTML || sidebarRightHTML) && 'has-one-sidebar', - (sidebarLeftHTML && sidebarRightHTML) && 'has-two-sidebars', - !(sidebarLeftHTML || sidebarRightHTML) && 'has-zero-sidebars', - sidebarLeftHTML && 'has-sidebar-left', - sidebarRightHTML && 'has-sidebar-right', - ], - }, - [ - sidebarLeftHTML, - mainHTML, - sidebarRightHTML, - ]), - banner.position === 'bottom' && bannerHTML, - footerHTML, - ].filter(Boolean).join('\n'); - - const infoCardHTML = html.tag('div', {id: 'info-card-container'}, - html.tag('div', {id: 'info-card-decor'}, - html.tag('div', {id: 'info-card'}, [ - html.tag('div', {class: ['info-card-art-container', 'no-reveal']}, - img({ - html, - class: 'info-card-art', - src: '', - link: true, - square: true, - })), - html.tag('div', {class: ['info-card-art-container', 'reveal']}, - img({ - html, - class: 'info-card-art', - src: '', - link: true, - square: true, - reveal: getRevealStringFromWarnings( - html.tag('span', {class: 'info-card-art-warnings'}), - {html, language}), - })), - html.tag('h1', {class: 'info-card-name'}, - html.tag('a')), - html.tag('p', {class: 'info-card-album'}, - language.$('releaseInfo.from', { - album: html.tag('a'), - })), - html.tag('p', {class: 'info-card-artists'}, - language.$('releaseInfo.by', { - artists: html.tag('span'), - })), - html.tag('p', {class: 'info-card-cover-artists'}, - language.$('releaseInfo.coverArtBy', { - artists: html.tag('span'), - })), - ]))); - - const socialEmbedHTML = [ - socialEmbed.title && - html.tag('meta', {property: 'og:title', content: socialEmbed.title}), - - socialEmbed.description && - html.tag('meta', { - property: 'og:description', - content: socialEmbed.description, - }), - - socialEmbed.image && - html.tag('meta', {property: 'og:image', content: socialEmbed.image}), - - ...html.fragment( - colors && [ - html.tag('meta', { - name: 'theme-color', - content: colors.dark, - media: '(prefers-color-scheme: dark)', - }), - - html.tag('meta', { - name: 'theme-color', - content: colors.light, - media: '(prefers-color-scheme: light)', - }), - - html.tag('meta', { - name: 'theme-color', - content: colors.primary, - }), - ]), - - oEmbedJSONHref && - html.tag('link', { - type: 'application/json+oembed', - href: oEmbedJSONHref, - }), - ].filter(Boolean).join('\n'); - - return `\n` + html.tag('html', - { - lang: language.intlCode, - 'data-language-code': language.code, - 'data-url-key': paths.urlPath[0], - ...Object.fromEntries( - paths.urlPath.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'), - 'data-rebase-data': to('data.root'), - }, - [ - ``, - - html.tag('head', [ - html.tag('title', - showWikiNameInTitle - ? language.formatString('misc.pageTitle.withWikiName', { - title, - wikiName: wikiInfo.nameShort, - }) - : language.formatString('misc.pageTitle', {title})), - - html.tag('meta', {charset: 'utf-8'}), - html.tag('meta', { - name: 'viewport', - content: 'width=device-width, initial-scale=1', - }), - - ...( - Object.entries(meta) - .filter(([key, value]) => value) - .map(([key, value]) => html.tag('meta', {[key]: value}))), - - canonical && - html.tag('link', { - rel: 'canonical', - href: canonical, - }), - - ...( - localizedCanonical - .map(({lang, href}) => html.tag('link', { - rel: 'alternate', - hreflang: lang, - href, - }))), - - socialEmbedHTML, - - html.tag('link', { - rel: 'stylesheet', - href: to('shared.staticFile', `site2.css?${CACHEBUST}`), - }), - - html.tag('style', - {[html.onlyIfContent]: true}, - [ - theme, - stylesheet, - ]), - - html.tag('script', { - src: to('shared.staticFile', `lazy-loading.js?${CACHEBUST}`), - }), - ]), - - html.tag('body', - {style: body.style || ''}, - [ - html.tag('div', {id: 'page-container'}, [ - mainHTML && - html.tag('div', {id: 'skippers'}, - [ - ['#content', language.$('misc.skippers.skipToContent')], - sidebarLeftHTML && - [ - '#sidebar-left', - sidebarRightHTML - ? language.$('misc.skippers.skipToSidebar.left') - : language.$('misc.skippers.skipToSidebar'), - ], - sidebarRightHTML && - [ - '#sidebar-right', - sidebarLeftHTML - ? language.$('misc.skippers.skipToSidebar.right') - : language.$('misc.skippers.skipToSidebar'), - ], - footerHTML && - ['#footer', language.$('misc.skippers.skipToFooter')], - ] - .filter(Boolean) - .map(([href, title]) => - html.tag('span', {class: 'skipper'}, - html.tag('a', {href}, title)))), - layoutHTML, - ]), - - infoCardHTML, - - html.tag('script', { - type: 'module', - src: to('shared.staticFile', `client.js?${CACHEBUST}`), - }), - ]), - ]); -} - -function generateOEmbedJSON(pageInfo, {language, wikiData}) { - const {socialEmbed} = pageInfo; - const {wikiInfo} = wikiData; - const {canonicalBase, nameShort} = wikiInfo; - - if (!socialEmbed) return ''; - - const entries = [ - socialEmbed.heading && [ - 'author_name', - language.$('misc.socialEmbed.heading', { - wikiName: nameShort, - heading: socialEmbed.heading, - }), - ], - socialEmbed.headingLink && - canonicalBase && [ - 'author_url', - canonicalBase.replace(/\/$/, '') + - '/' + - socialEmbed.headingLink.replace(/^\//, ''), - ], - ].filter(Boolean); - - if (!entries.length) return ''; - - return JSON.stringify(Object.fromEntries(entries)); -} - async function writePage({ html, oEmbedJSON = '', @@ -945,36 +380,6 @@ function writeSharedFilesAndPages({language, wikiData}) { ].filter(Boolean)); } -function generateRedirectHTML(title, target, {language}) { - return `\n` + html.tag('html', [ - html.tag('head', [ - html.tag('title', language.$('redirectPage.title', {title})), - html.tag('meta', {charset: 'utf-8'}), - - html.tag('meta', { - 'http-equiv': 'refresh', - content: `0;url=${target}`, - }), - - // TODO: Is this OK for localized pages? - html.tag('link', { - rel: 'canonical', - href: target, - }), - ]), - - html.tag('body', - html.tag('main', [ - html.tag('h1', - language.$('redirectPage.title', {title})), - html.tag('p', - language.$('redirectPage.infoLine', { - target: html.tag('a', {href: target}, target), - })), - ])), - ]); -} - // Wrapper function for running a function once for all languages. async function wrapLanguages(fn, {languages, writeOneLanguage = null}) { const k = writeOneLanguage; @@ -1691,6 +1096,9 @@ async function main() { .to('shared.path', paths.pathname + OEMBED_JSON_FILE); const pageHTML = generateDocumentHTML(pageInfo, { + buildTime: BUILD_TIME, + cachebust: '?' + CACHEBUST, + commit: COMMIT, defaultLanguage: finalDefaultLanguage, getThemeString: bound.getThemeString, language, diff --git a/src/write/page-template.js b/src/write/page-template.js new file mode 100644 index 00000000..61579549 --- /dev/null +++ b/src/write/page-template.js @@ -0,0 +1,608 @@ +import chroma from 'chroma-js'; + +import * as html from '../util/html.js'; +import {logWarn} from '../util/cli.js'; +import {getColors} from '../util/colors.js'; + +import { + getFooterLocalizationLinks, + getRevealStringFromWarnings, + img, +} from '../misc-templates.js'; + +export function generateDocumentHTML(pageInfo, { + buildTime = null, + cachebust = '', + commit = '', + defaultLanguage, + getThemeString, + language, + languages, + localizedPaths, + paths, + oEmbedJSONHref, + to, + transformMultiline, + wikiData, +}) { + const {wikiInfo} = wikiData; + + let { + title = '', + meta = {}, + theme = '', + stylesheet = '', + + showWikiNameInTitle = true, + themeColor = '', + + // missing properties are auto-filled, see below! + body = {}, + banner = {}, + main = {}, + sidebarLeft = {}, + sidebarRight = {}, + nav = {}, + secondaryNav = {}, + footer = {}, + socialEmbed = {}, + } = pageInfo; + + body.style ??= ''; + + theme = theme || getThemeString(wikiInfo.color); + + banner ||= {}; + banner.classes ??= []; + banner.src ??= ''; + banner.position ??= ''; + banner.dimensions ??= [0, 0]; + + main.classes ??= []; + main.content ??= ''; + + sidebarLeft ??= {}; + sidebarRight ??= {}; + + for (const sidebar of [sidebarLeft, sidebarRight]) { + sidebar.classes ??= []; + sidebar.content ??= ''; + sidebar.collapse ??= true; + } + + nav.classes ??= []; + nav.content ??= ''; + nav.bottomRowContent ??= ''; + nav.links ??= []; + nav.linkContainerClasses ??= []; + + secondaryNav ??= {}; + secondaryNav.content ??= ''; + secondaryNav.content ??= ''; + + footer.classes ??= []; + footer.content ??= wikiInfo.footerContent + ? transformMultiline(wikiInfo.footerContent) + : ''; + + const colors = themeColor + ? getColors(themeColor, {chroma}) + : null; + + const canonical = wikiInfo.canonicalBase + ? wikiInfo.canonicalBase + (paths.pathname === '/' ? '' : paths.pathname) + : ''; + + const localizedCanonical = wikiInfo.canonicalBase + ? Object.entries(localizedPaths).map(([code, {pathname}]) => ({ + lang: code, + href: wikiInfo.canonicalBase + (pathname === '/' ? '' : pathname), + })) + : []; + + const collapseSidebars = + sidebarLeft.collapse !== false && sidebarRight.collapse !== false; + + const mainHTML = + main.content && + html.tag('main', + { + id: 'content', + class: main.classes, + }, + main.content); + + const footerHTML = + html.tag('footer', + { + [html.onlyIfContent]: true, + id: 'footer', + class: footer.classes, + }, + [ + html.tag('div', + { + [html.onlyIfContent]: true, + class: 'footer-content', + }, + footer.content), + + getFooterLocalizationLinks(paths.pathname, { + defaultLanguage, + html, + language, + languages, + paths, + to, + }), + ]); + + const generateSidebarHTML = (id, { + content, + multiple, + classes, + collapse = true, + wide = false, + + // 'last' - last or only sidebar box is sticky + // 'column' - entire column, incl. multiple boxes from top, is sticky + // 'none' - sidebar not sticky at all, stays at top of page + stickyMode = 'last', + }) => + content + ? html.tag('div', + { + id, + class: [ + 'sidebar-column', + 'sidebar', + wide && 'wide', + !collapse && 'no-hide', + stickyMode !== 'none' && 'sticky-' + stickyMode, + ...classes, + ], + }, + content) + : multiple + ? html.tag('div', + { + id, + class: [ + 'sidebar-column', + 'sidebar-multiple', + wide && 'wide', + !collapse && 'no-hide', + stickyMode !== 'none' && 'sticky-' + stickyMode, + ], + }, + multiple + .map((infoOrContent) => + (typeof infoOrContent === 'object' && !Array.isArray(infoOrContent)) + ? infoOrContent + : {content: infoOrContent}) + .filter(({content}) => content) + .map(({ + content, + classes: classes2 = [], + }) => + html.tag('div', + { + class: ['sidebar', ...classes, ...classes2], + }, + html.fragment(content)))) + : ''; + + const sidebarLeftHTML = generateSidebarHTML('sidebar-left', sidebarLeft); + const sidebarRightHTML = generateSidebarHTML('sidebar-right', sidebarRight); + + if (nav.simple) { + nav.linkContainerClasses = ['nav-links-hierarchy']; + nav.links = [{toHome: true}, {toCurrentPage: true}]; + } + + const links = (nav.links || []).filter(Boolean); + + const navLinkParts = []; + for (let i = 0; i < links.length; i++) { + let cur = links[i]; + + let {title: linkTitle} = cur; + + if (cur.toHome) { + linkTitle ??= wikiInfo.nameShort; + } else if (cur.toCurrentPage) { + linkTitle ??= title; + } + + let partContent; + + if (typeof cur.html === 'string') { + partContent = cur.html; + } else { + const attributes = { + class: (cur.toCurrentPage || i === links.length - 1) && 'current', + href: cur.toCurrentPage + ? '' + : cur.toHome + ? to('localized.home') + : cur.path + ? to(...cur.path) + : cur.href + ? (() => { + logWarn`Using legacy href format nav link in ${paths.pathname}`; + return cur.href; + })() + : null, + }; + if (attributes.href === null) { + throw new Error( + `Expected some href specifier for link to ${linkTitle} (${JSON.stringify( + cur + )})` + ); + } + partContent = html.tag('a', attributes, linkTitle); + } + + if (!partContent) continue; + + const part = html.tag('span', + {class: cur.divider === false && 'no-divider'}, + partContent); + + navLinkParts.push(part); + } + + const navHTML = html.tag('nav', + { + [html.onlyIfContent]: true, + id: 'header', + class: [ + ...nav.classes, + links.length && 'nav-has-main-links', + nav.content && 'nav-has-content', + nav.bottomRowContent && 'nav-has-bottom-row', + ], + }, + [ + links.length && + html.tag( + 'div', + {class: ['nav-main-links', ...nav.linkContainerClasses]}, + navLinkParts + ), + nav.bottomRowContent && + html.tag('div', {class: 'nav-bottom-row'}, nav.bottomRowContent), + nav.content && html.tag('div', {class: 'nav-content'}, nav.content), + ]); + + const secondaryNavHTML = html.tag('nav', + { + [html.onlyIfContent]: true, + id: 'secondary-nav', + class: secondaryNav.classes, + }, + secondaryNav.content); + + const bannerSrc = banner.src + ? banner.src + : banner.path + ? to(...banner.path) + : null; + + const bannerHTML = + banner.position && + bannerSrc && + html.tag('div', + { + id: 'banner', + class: banner.classes, + }, + html.tag('img', { + src: bannerSrc, + alt: banner.alt, + width: banner.dimensions[0] || 1100, + height: banner.dimensions[1] || 200, + })); + + const layoutHTML = [ + navHTML, + banner.position === 'top' && bannerHTML, + secondaryNavHTML, + html.tag('div', + { + class: [ + 'layout-columns', + !collapseSidebars && 'vertical-when-thin', + (sidebarLeftHTML || sidebarRightHTML) && 'has-one-sidebar', + (sidebarLeftHTML && sidebarRightHTML) && 'has-two-sidebars', + !(sidebarLeftHTML || sidebarRightHTML) && 'has-zero-sidebars', + sidebarLeftHTML && 'has-sidebar-left', + sidebarRightHTML && 'has-sidebar-right', + ], + }, + [ + sidebarLeftHTML, + mainHTML, + sidebarRightHTML, + ]), + banner.position === 'bottom' && bannerHTML, + footerHTML, + ].filter(Boolean).join('\n'); + + const infoCardHTML = html.tag('div', {id: 'info-card-container'}, + html.tag('div', {id: 'info-card-decor'}, + html.tag('div', {id: 'info-card'}, [ + html.tag('div', {class: ['info-card-art-container', 'no-reveal']}, + img({ + html, + class: 'info-card-art', + src: '', + link: true, + square: true, + })), + html.tag('div', {class: ['info-card-art-container', 'reveal']}, + img({ + html, + class: 'info-card-art', + src: '', + link: true, + square: true, + reveal: getRevealStringFromWarnings( + html.tag('span', {class: 'info-card-art-warnings'}), + {html, language}), + })), + html.tag('h1', {class: 'info-card-name'}, + html.tag('a')), + html.tag('p', {class: 'info-card-album'}, + language.$('releaseInfo.from', { + album: html.tag('a'), + })), + html.tag('p', {class: 'info-card-artists'}, + language.$('releaseInfo.by', { + artists: html.tag('span'), + })), + html.tag('p', {class: 'info-card-cover-artists'}, + language.$('releaseInfo.coverArtBy', { + artists: html.tag('span'), + })), + ]))); + + const socialEmbedHTML = [ + socialEmbed.title && + html.tag('meta', {property: 'og:title', content: socialEmbed.title}), + + socialEmbed.description && + html.tag('meta', { + property: 'og:description', + content: socialEmbed.description, + }), + + socialEmbed.image && + html.tag('meta', {property: 'og:image', content: socialEmbed.image}), + + ...html.fragment( + colors && [ + html.tag('meta', { + name: 'theme-color', + content: colors.dark, + media: '(prefers-color-scheme: dark)', + }), + + html.tag('meta', { + name: 'theme-color', + content: colors.light, + media: '(prefers-color-scheme: light)', + }), + + html.tag('meta', { + name: 'theme-color', + content: colors.primary, + }), + ]), + + oEmbedJSONHref && + html.tag('link', { + type: 'application/json+oembed', + href: oEmbedJSONHref, + }), + ].filter(Boolean).join('\n'); + + return `\n` + html.tag('html', + { + lang: language.intlCode, + 'data-language-code': language.code, + 'data-url-key': paths.urlPath[0], + ...Object.fromEntries( + paths.urlPath.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'), + 'data-rebase-data': to('data.root'), + }, + [ + ``, + + html.tag('head', [ + html.tag('title', + showWikiNameInTitle + ? language.formatString('misc.pageTitle.withWikiName', { + title, + wikiName: wikiInfo.nameShort, + }) + : language.formatString('misc.pageTitle', {title})), + + html.tag('meta', {charset: 'utf-8'}), + html.tag('meta', { + name: 'viewport', + content: 'width=device-width, initial-scale=1', + }), + + ...( + Object.entries(meta) + .filter(([key, value]) => value) + .map(([key, value]) => html.tag('meta', {[key]: value}))), + + canonical && + html.tag('link', { + rel: 'canonical', + href: canonical, + }), + + ...( + localizedCanonical + .map(({lang, href}) => html.tag('link', { + rel: 'alternate', + hreflang: lang, + href, + }))), + + socialEmbedHTML, + + html.tag('link', { + rel: 'stylesheet', + href: to('shared.staticFile', `site2.css?${cachebust}`), + }), + + html.tag('style', + {[html.onlyIfContent]: true}, + [ + theme, + stylesheet, + ]), + + html.tag('script', { + src: to('shared.staticFile', `lazy-loading.js?${cachebust}`), + }), + ]), + + html.tag('body', + {style: body.style || ''}, + [ + html.tag('div', {id: 'page-container'}, [ + mainHTML && + html.tag('div', {id: 'skippers'}, + [ + ['#content', language.$('misc.skippers.skipToContent')], + sidebarLeftHTML && + [ + '#sidebar-left', + sidebarRightHTML + ? language.$('misc.skippers.skipToSidebar.left') + : language.$('misc.skippers.skipToSidebar'), + ], + sidebarRightHTML && + [ + '#sidebar-right', + sidebarLeftHTML + ? language.$('misc.skippers.skipToSidebar.right') + : language.$('misc.skippers.skipToSidebar'), + ], + footerHTML && + ['#footer', language.$('misc.skippers.skipToFooter')], + ] + .filter(Boolean) + .map(([href, title]) => + html.tag('span', {class: 'skipper'}, + html.tag('a', {href}, title)))), + layoutHTML, + ]), + + infoCardHTML, + + html.tag('script', { + type: 'module', + src: to('shared.staticFile', `client.js?${cachebust}`), + }), + ]), + ]); +} + +export function generateOEmbedJSON(pageInfo, {language, wikiData}) { + const {socialEmbed} = pageInfo; + const {wikiInfo} = wikiData; + const {canonicalBase, nameShort} = wikiInfo; + + if (!socialEmbed) return ''; + + const entries = [ + socialEmbed.heading && [ + 'author_name', + language.$('misc.socialEmbed.heading', { + wikiName: nameShort, + heading: socialEmbed.heading, + }), + ], + socialEmbed.headingLink && + canonicalBase && [ + 'author_url', + canonicalBase.replace(/\/$/, '') + + '/' + + socialEmbed.headingLink.replace(/^\//, ''), + ], + ].filter(Boolean); + + if (!entries.length) return ''; + + return JSON.stringify(Object.fromEntries(entries)); +} + +export function generateRedirectHTML(title, target, { + language, +}) { + return `\n` + html.tag('html', [ + html.tag('head', [ + html.tag('title', language.$('redirectPage.title', {title})), + html.tag('meta', {charset: 'utf-8'}), + + html.tag('meta', { + 'http-equiv': 'refresh', + content: `0;url=${target}`, + }), + + // TODO: Is this OK for localized pages? + html.tag('link', { + rel: 'canonical', + href: target, + }), + ]), + + html.tag('body', + html.tag('main', [ + html.tag('h1', + language.$('redirectPage.title', {title})), + html.tag('p', + language.$('redirectPage.infoLine', { + target: html.tag('a', {href: target}, target), + })), + ])), + ]); +} -- cgit 1.3.0-6-gf8a5 From 1ced8788ca64ed430fac003dc9281f7194193956 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 7 Jan 2023 20:31:16 -0400 Subject: extract getPagePaths, getURLsFrom functions towards basic dynamics pt. 1 (#124) --- src/upd8.js | 149 ++++++++++++++----------------------------------------- src/util/urls.js | 91 +++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 112 deletions(-) diff --git a/src/upd8.js b/src/upd8.js index a8dda34d..c81735e1 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -82,6 +82,18 @@ import { progressPromiseAll, } from './util/cli.js'; +import { + queue, + showAggregate, + withEntries, +} from './util/sugar.js'; + +import { + generateURLs, + getPagePaths, + getURLsFrom, +} from './util/urls.js'; + import {bindUtilities} from './write/bind-utilities.js'; import {validateWrites} from './write/validate-writes.js'; @@ -102,10 +114,6 @@ import { } from './util/serialize.js'; */ -import {queue, showAggregate} from './util/sugar.js'; - -import {generateURLs} from './util/urls.js'; - // Pensive emoji! import { OFFICIAL_GROUP_DIRECTORY } from './util/magic-constants.js'; @@ -139,9 +147,6 @@ const UTILITY_DIRECTORY = 'util'; // (This gets symlinked into the --data-path directory.) const STATIC_DIRECTORY = 'static'; -// This exists adjacent to index.html for any page with oEmbed metadata. -const OEMBED_JSON_FILE = 'oembed.json'; - // Automatically copied (if present) from media directory to site root. const FAVICON_FILE = 'favicon.ico'; @@ -173,51 +178,6 @@ function stringifyThings(thingData) { return JSON.stringify(serializeThings(thingData)); } -export function getURLsFrom({ - baseDirectory, - pageSubKey, - paths, -}) { - return (targetFullKey, ...args) => { - const [groupKey, subKey] = targetFullKey.split('.'); - let path = paths.subdirectoryPrefix; - - let from; - let to; - - // When linking to *outside* the localized area of the site, we need to - // make sure the result is correctly relative to the 8ase directory. - if ( - groupKey !== 'localized' && - groupKey !== 'localizedDefaultLanguage' && - baseDirectory - ) { - from = 'localizedWithBaseDirectory.' + pageSubKey; - to = targetFullKey; - } else if (groupKey === 'localizedDefaultLanguage' && baseDirectory) { - // Special case for specifically linking *from* a page with base - // directory *to* a page without! Used for the language switcher and - // hopefully nothing else oh god. - from = 'localizedWithBaseDirectory.' + pageSubKey; - to = 'localized.' + subKey; - } else if (groupKey === 'localizedDefaultLanguage') { - // Linking to the default, except surprise, we're already IN the default - // (no baseDirectory set). - from = 'localized.' + pageSubKey; - to = 'localized.' + subKey; - } else { - // If we're linking inside the localized area (or there just is no - // 8ase directory), the 8ase directory doesn't matter. - from = 'localized.' + pageSubKey; - to = targetFullKey; - } - - path += urls.from(from).to(to, ...args); - - return path; - }; -} - async function writePage({ html, oEmbedJSON = '', @@ -235,49 +195,6 @@ async function writePage({ ); } -function getPagePaths({ - baseDirectory, - fullKey, - urlArgs, - - file = 'index.html', -}) { - const [groupKey, subKey] = fullKey.split('.'); - - const pathname = - groupKey === 'localized' && baseDirectory - ? urls - .from('shared.root') - .toDevice( - 'localizedWithBaseDirectory.' + subKey, - baseDirectory, - ...urlArgs) - : urls - .from('shared.root') - .toDevice(fullKey, ...urlArgs); - - // Needed for the rare path arguments which themselves contains one or more - // slashes, e.g. for listings, with arguments like 'albums/by-name'. - const subdirectoryPrefix = - '../'.repeat(urlArgs.join('/').split('/').length - 1); - - const outputDirectory = path.join(outputPath, pathname); - - const output = { - directory: outputDirectory, - documentHTML: path.join(outputDirectory, file), - oEmbedJSON: path.join(outputDirectory, OEMBED_JSON_FILE) - }; - - return { - urlPath: [fullKey, ...urlArgs], - - output, - pathname, - subdirectoryPrefix, - }; -} - async function writeFavicon() { try { await stat(path.join(mediaPath, FAVICON_FILE)); @@ -1019,30 +936,34 @@ async function main() { const pageSubKey = path[0]; const urlArgs = path.slice(1); - const localizedPaths = Object.fromEntries( - Object.entries(languages) - .filter(([key, language]) => - key !== 'default' && - !language.hidden) - .map(([_key, language]) => [ - language.code, - getPagePaths({ - baseDirectory: - (language === finalDefaultLanguage - ? '' - : language.code), - fullKey: 'localized.' + pageSubKey, - urlArgs, - }), - ])); + const localizedPaths = withEntries(languages, entries => entries + .filter(([key, language]) => key !== 'default' && !language.hidden) + .map(([_key, language]) => [ + language.code, + getPagePaths({ + outputPath, + urls, + + baseDirectory: + (language === finalDefaultLanguage + ? '' + : language.code), + fullKey: 'localized.' + pageSubKey, + urlArgs, + }), + ])); const paths = getPagePaths({ + outputPath, + urls, + baseDirectory, fullKey: 'localized.' + pageSubKey, urlArgs, }); const to = getURLsFrom({ + urls, baseDirectory, pageSubKey, paths, @@ -1093,7 +1014,7 @@ async function main() { wikiData.wikiInfo.canonicalBase + urls .from('shared.root') - .to('shared.path', paths.pathname + OEMBED_JSON_FILE); + .to('shared.path', paths.pathname + 'oembed.json'); const pageHTML = generateDocumentHTML(pageInfo, { buildTime: BUILD_TIME, @@ -1123,12 +1044,16 @@ async function main() { }); const from = getPagePaths({ + outputPath, + urls, + baseDirectory, fullKey: 'localized.' + fromPath[0], urlArgs: fromPath.slice(1), }); const to = getURLsFrom({ + urls, baseDirectory, pageSubKey: fromPath[0], paths: from, diff --git a/src/util/urls.js b/src/util/urls.js index 1f9cd9c0..f05f134b 100644 --- a/src/util/urls.js +++ b/src/util/urls.js @@ -136,3 +136,94 @@ export const thumb = { medium: thumbnailHelper('.medium'), small: thumbnailHelper('.small'), }; + +export function getURLsFrom({ + urls, + + baseDirectory, + pageSubKey, + paths, +}) { + return (targetFullKey, ...args) => { + const [groupKey, subKey] = targetFullKey.split('.'); + let path = paths.subdirectoryPrefix; + + let from; + let to; + + // When linking to *outside* the localized area of the site, we need to + // make sure the result is correctly relative to the 8ase directory. + if ( + groupKey !== 'localized' && + groupKey !== 'localizedDefaultLanguage' && + baseDirectory + ) { + from = 'localizedWithBaseDirectory.' + pageSubKey; + to = targetFullKey; + } else if (groupKey === 'localizedDefaultLanguage' && baseDirectory) { + // Special case for specifically linking *from* a page with base + // directory *to* a page without! Used for the language switcher and + // hopefully nothing else oh god. + from = 'localizedWithBaseDirectory.' + pageSubKey; + to = 'localized.' + subKey; + } else if (groupKey === 'localizedDefaultLanguage') { + // Linking to the default, except surprise, we're already IN the default + // (no baseDirectory set). + from = 'localized.' + pageSubKey; + to = 'localized.' + subKey; + } else { + // If we're linking inside the localized area (or there just is no + // 8ase directory), the 8ase directory doesn't matter. + from = 'localized.' + pageSubKey; + to = targetFullKey; + } + + path += urls.from(from).to(to, ...args); + + return path; + }; +} + +export function getPagePaths({ + outputPath, + urls, + + baseDirectory, + fullKey, + urlArgs, +}) { + const [groupKey, subKey] = fullKey.split('.'); + + const pathname = + groupKey === 'localized' && baseDirectory + ? urls + .from('shared.root') + .toDevice( + 'localizedWithBaseDirectory.' + subKey, + baseDirectory, + ...urlArgs) + : urls + .from('shared.root') + .toDevice(fullKey, ...urlArgs); + + // Needed for the rare path arguments which themselves contains one or more + // slashes, e.g. for listings, with arguments like 'albums/by-name'. + const subdirectoryPrefix = + '../'.repeat(urlArgs.join('/').split('/').length - 1); + + const outputDirectory = path.join(outputPath, pathname); + + const output = { + directory: outputDirectory, + documentHTML: path.join(outputDirectory, 'index.html'), + oEmbedJSON: path.join(outputDirectory, 'oembed.json'), + }; + + return { + urlPath: [fullKey, ...urlArgs], + + output, + pathname, + subdirectoryPrefix, + }; +} -- cgit 1.3.0-6-gf8a5 From 078b9656fe16738967943e4ca94866481f4f1d21 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 7 Jan 2023 21:08:08 -0400 Subject: extract actual file IO functions --- src/upd8.js | 179 +++++++-------------------------------------- src/write/page-template.js | 21 ++++++ src/write/write-files.js | 161 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+), 152 deletions(-) create mode 100644 src/write/write-files.js diff --git a/src/upd8.js b/src/upd8.js index c81735e1..65835dc1 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -35,15 +35,6 @@ import {execSync} from 'child_process'; import * as path from 'path'; import {fileURLToPath} from 'url'; -import { - copyFile, - mkdir, - stat, - symlink, - writeFile, - unlink, -} from 'fs/promises'; - import genThumbs from './gen-thumbs.js'; import {listingSpec, listingTargetSpec} from './listing-spec.js'; import urlSpec from './url-spec.js'; @@ -99,10 +90,17 @@ import {validateWrites} from './write/validate-writes.js'; import { generateDocumentHTML, - generateRedirectHTML, + generateGlobalWikiDataJSON, generateOEmbedJSON, + generateRedirectHTML, } from './write/page-template.js'; +import { + writePage, + writeSharedFilesAndPages, + writeSymlinks, +} from './write/write-files.js'; + /* import { serializeContribs, @@ -134,22 +132,6 @@ const BUILD_TIME = new Date(); const DEFAULT_STRINGS_FILE = 'strings-default.json'; -// Code that's common 8etween the 8uild code (i.e. upd8.js) and gener8ted -// site code should 8e put here. Which, uh, ~~only really means this one -// file~~ is now a variety of useful utilities! -// -// Rather than hard code it, anything in this directory can 8e shared across -// 8oth ends of the code8ase. -// (This gets symlinked into the --data-path directory.) -const UTILITY_DIRECTORY = 'util'; - -// Code that's used only in the static site! CSS, cilent JS, etc. -// (This gets symlinked into the --data-path directory.) -const STATIC_DIRECTORY = 'static'; - -// Automatically copied (if present) from media directory to site root. -const FAVICON_FILE = 'favicon.ico'; - // Shared varia8les! These are more efficient to access than a shared varia8le // (or at least I h8pe so), and are easier to pass across functions than a // 8unch of specific arguments. @@ -174,129 +156,6 @@ if (!validateReplacerSpec(replacerSpec, {find, link})) { process.exit(); } -function stringifyThings(thingData) { - return JSON.stringify(serializeThings(thingData)); -} - -async function writePage({ - html, - oEmbedJSON = '', - paths, -}) { - await mkdir(paths.output.directory, {recursive: true}); - - await Promise.all( - [ - writeFile(paths.output.documentHTML, html), - - oEmbedJSON && - writeFile(paths.output.oEmbedJSON, oEmbedJSON), - ].filter(Boolean) - ); -} - -async function writeFavicon() { - try { - await stat(path.join(mediaPath, FAVICON_FILE)); - } catch (error) { - return; - } - - try { - await copyFile( - path.join(mediaPath, FAVICON_FILE), - path.join(outputPath, FAVICON_FILE) - ); - } catch (error) { - logWarn`Failed to copy favicon! ${error.message}`; - return; - } - - logInfo`Copied favicon to site root.`; -} - -function writeSymlinks() { - return progressPromiseAll('Writing site symlinks.', [ - link(path.join(__dirname, UTILITY_DIRECTORY), 'shared.utilityRoot'), - link(path.join(__dirname, STATIC_DIRECTORY), 'shared.staticRoot'), - link(mediaPath, 'media.root'), - ]); - - async function link(directory, urlKey) { - const pathname = urls.from('shared.root').toDevice(urlKey); - const file = path.join(outputPath, pathname); - try { - await unlink(file); - } catch (error) { - if (error.code !== 'ENOENT') { - throw error; - } - } - try { - await symlink(path.resolve(directory), file); - } catch (error) { - if (error.code === 'EPERM') { - await symlink(path.resolve(directory), file, 'junction'); - } - } - } -} - -function writeSharedFilesAndPages({language, wikiData}) { - const {groupData, wikiInfo} = wikiData; - - const redirect = async (title, from, urlKey, directory) => { - const target = path.relative( - from, - urls.from('shared.root').to(urlKey, directory) - ); - const content = generateRedirectHTML(title, target, {language}); - await mkdir(path.join(outputPath, from), {recursive: true}); - await writeFile(path.join(outputPath, from, 'index.html'), content); - }; - - return progressPromiseAll(`Writing files & pages shared across languages.`, [ - groupData?.some((group) => group.directory === 'fandom') && - redirect( - 'Fandom - Gallery', - 'albums/fandom', - 'localized.groupGallery', - 'fandom' - ), - - groupData?.some((group) => group.directory === 'official') && - redirect( - 'Official - Gallery', - 'albums/official', - 'localized.groupGallery', - 'official' - ), - - wikiInfo.enableListings && - redirect( - 'Album Commentary', - 'list/all-commentary', - 'localized.commentaryIndex', - '' - ), - - writeFile( - path.join(outputPath, 'data.json'), - ( - '{\n' + - [ - `"albumData": ${stringifyThings(wikiData.albumData)},`, - wikiInfo.enableFlashesAndGames && - `"flashData": ${stringifyThings(wikiData.flashData)},`, - `"artistData": ${stringifyThings(wikiData.artistData)}`, - ] - .filter(Boolean) - .map(line => ' ' + line) - .join('\n') + - '\n}')), - ].filter(Boolean)); -} - // Wrapper function for running a function once for all languages. async function wrapLanguages(fn, {languages, writeOneLanguage = null}) { const k = writeOneLanguage; @@ -802,9 +661,25 @@ async function main() { const buildDictionary = pageSpecs; - await writeFavicon(); - await writeSymlinks(); - await writeSharedFilesAndPages({language: finalDefaultLanguage, wikiData}); + await writeSymlinks({ + srcRootDirname: __dirname, + mediaPath, + outputPath, + urls, + }); + + await writeSharedFilesAndPages({ + mediaPath, + outputPath, + urls, + + language: finalDefaultLanguage, + wikiData, + wikiDataJSON: generateGlobalWikiDataJSON({ + serializeThings, + wikiData, + }) + }); const buildSteps = writeAll ? Object.entries(buildDictionary) diff --git a/src/write/page-template.js b/src/write/page-template.js index 61579549..f7faeed0 100644 --- a/src/write/page-template.js +++ b/src/write/page-template.js @@ -606,3 +606,24 @@ export function generateRedirectHTML(title, target, { ])), ]); } + +export function generateGlobalWikiDataJSON({ + serializeThings, + wikiData, +}) { + return '{\n' + + ([ + `"albumData": ${stringifyThings(wikiData.albumData)},`, + wikiData.wikiInfo.enableFlashesAndGames && + `"flashData": ${stringifyThings(wikiData.flashData)},`, + `"artistData": ${stringifyThings(wikiData.artistData)}`, + ] + .filter(Boolean) + .map(line => ' ' + line) + .join('\n')) + + '\n}'; + + function stringifyThings(thingData) { + return JSON.stringify(serializeThings(thingData)); + } +} diff --git a/src/write/write-files.js b/src/write/write-files.js new file mode 100644 index 00000000..e448df3f --- /dev/null +++ b/src/write/write-files.js @@ -0,0 +1,161 @@ +import * as path from 'path'; + +import {generateRedirectHTML} from './page-template.js'; + +import { + logInfo, + logWarn, + progressPromiseAll, +} from '../util/cli.js'; + +// Code that's common 8etween the 8uild code (i.e. upd8.js) and gener8ted +// site code should 8e put here. Which, uh, ~~only really means this one +// file~~ is now a variety of useful utilities! +// +// Rather than hard code it, anything in this directory can 8e shared across +// 8oth ends of the code8ase. +// (This gets symlinked into the --data-path directory.) +const UTILITY_DIRECTORY = 'util'; + +// Code that's used only in the static site! CSS, cilent JS, etc. +// (This gets symlinked into the --data-path directory.) +const STATIC_DIRECTORY = 'static'; + +import { + copyFile, + mkdir, + stat, + symlink, + writeFile, + unlink, +} from 'fs/promises'; + +export async function writePage({ + html, + oEmbedJSON = '', + paths, +}) { + await mkdir(paths.output.directory, {recursive: true}); + + await Promise.all([ + writeFile(paths.output.documentHTML, html), + + oEmbedJSON && + writeFile(paths.output.oEmbedJSON, oEmbedJSON), + ].filter(Boolean)); +} + +export function writeSymlinks({ + srcRootDirname, + mediaPath, + outputPath, + urls, +}) { + return progressPromiseAll('Writing site symlinks.', [ + link(path.join(srcRootDirname, UTILITY_DIRECTORY), 'shared.utilityRoot'), + link(path.join(srcRootDirname, STATIC_DIRECTORY), 'shared.staticRoot'), + link(mediaPath, 'media.root'), + ]); + + async function link(directory, urlKey) { + const pathname = urls.from('shared.root').toDevice(urlKey); + const file = path.join(outputPath, pathname); + + try { + await unlink(file); + } catch (error) { + if (error.code !== 'ENOENT') { + throw error; + } + } + + try { + await symlink(path.resolve(directory), file); + } catch (error) { + if (error.code === 'EPERM') { + await symlink(path.resolve(directory), file, 'junction'); + } + } + } +} + +export async function writeFavicon({ + mediaPath, + outputPath, +}) { + const faviconFile = 'favicon.ico'; + + try { + await stat(path.join(mediaPath, faviconFile)); + } catch (error) { + return; + } + + try { + await copyFile( + path.join(mediaPath, faviconFile), + path.join(outputPath, faviconFile)); + } catch (error) { + logWarn`Failed to copy favicon! ${error.message}`; + return; + } + + logInfo`Copied favicon to site root.`; +} + +export async function writeSharedFilesAndPages({ + language, + mediaPath, + outputPath, + urls, + wikiData, + wikiDataJSON, +}) { + const {groupData, wikiInfo} = wikiData; + + await writeFavicon({ + mediaPath, + outputPath, + }); + + return progressPromiseAll(`Writing files & pages shared across languages.`, [ + groupData?.some((group) => group.directory === 'fandom') && + redirect( + 'Fandom - Gallery', + 'albums/fandom', + 'localized.groupGallery', + 'fandom' + ), + + groupData?.some((group) => group.directory === 'official') && + redirect( + 'Official - Gallery', + 'albums/official', + 'localized.groupGallery', + 'official' + ), + + wikiInfo.enableListings && + redirect( + 'Album Commentary', + 'list/all-commentary', + 'localized.commentaryIndex', + '' + ), + + wikiDataJSON && + writeFile( + path.join(outputPath, 'data.json'), + wikiDataJSON), + ].filter(Boolean)); + + async function redirect(title, from, urlKey, directory) { + const target = path.relative( + from, + urls.from('shared.root').to(urlKey, directory) + ); + const content = generateRedirectHTML(title, target, {language}); + await mkdir(path.join(outputPath, from), {recursive: true}); + await writeFile(path.join(outputPath, from, 'index.html'), content); + } +} -- cgit 1.3.0-6-gf8a5 From 5b7a195b7075ee83501aeb65a421a81c3e360542 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 7 Jan 2023 21:22:29 -0400 Subject: move global variables inside main --- src/upd8.js | 89 +++++++++++++++++++++++++------------------------------------ 1 file changed, 36 insertions(+), 53 deletions(-) diff --git a/src/upd8.js b/src/upd8.js index 65835dc1..6d6990bf 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -132,26 +132,6 @@ const BUILD_TIME = new Date(); const DEFAULT_STRINGS_FILE = 'strings-default.json'; -// Shared varia8les! These are more efficient to access than a shared varia8le -// (or at least I h8pe so), and are easier to pass across functions than a -// 8unch of specific arguments. -// -// Upd8: Okay yeah these aren't actually any different. Still cleaner than -// passing around a data object containing all this, though. -let dataPath; -let mediaPath; -let langPath; -let outputPath; - -// Glo8al data o8ject shared 8etween 8uild functions and all that. This keeps -// everything encapsul8ted in one place, so it's easy to pass and share across -// modules! -let wikiData = {}; - -let queueSize; - -const urls = generateURLs(urlSpec); - if (!validateReplacerSpec(replacerSpec, {find, link})) { process.exit(); } @@ -175,10 +155,11 @@ async function wrapLanguages(fn, {languages, writeOneLanguage = null}) { async function main() { Error.stackTraceLimit = Infinity; - const WD = wikiData; - - WD.listingSpec = listingSpec; - WD.listingTargetSpec = listingTargetSpec; + // This is about to get a whole lot more stuff put in it. + const wikiData = { + listingSpec, + listingTargetSpec, + }; const miscOptions = await parseOptions(process.argv.slice(2), { // Data files for the site, including flash, artist, and al8um data, @@ -294,13 +275,29 @@ async function main() { [parseOptions.handleUnknown]: () => {}, }); - dataPath = miscOptions['data-path'] || process.env.HSMUSIC_DATA; - mediaPath = miscOptions['media-path'] || process.env.HSMUSIC_MEDIA; - langPath = miscOptions['lang-path'] || process.env.HSMUSIC_LANG; // Can 8e left unset! - outputPath = miscOptions['out-path'] || process.env.HSMUSIC_OUT; + const dataPath = miscOptions['data-path'] || process.env.HSMUSIC_DATA; + const mediaPath = miscOptions['media-path'] || process.env.HSMUSIC_MEDIA; + const langPath = miscOptions['lang-path'] || process.env.HSMUSIC_LANG; // Can 8e left unset! + const outputPath = miscOptions['out-path'] || process.env.HSMUSIC_OUT; + const skipThumbs = miscOptions['skip-thumbs'] ?? false; + const thumbsOnly = miscOptions['thumbs-only'] ?? false; + const noBuild = miscOptions['no-build'] ?? false; const writeOneLanguage = miscOptions['lang']; + const showAggregateTraces = miscOptions['show-traces'] ?? false; + + const appendIndexHTML = miscOptions['append-index-html'] ?? false; + + const precacheData = miscOptions['precache-data'] ?? false; + const showInvalidPropertyAccesses = miscOptions['show-invalid-property-accesses'] ?? false; + + // Makes writing a little nicer on CPU theoretically, 8ut also costs in + // performance right now 'cuz it'll w8 for file writes to 8e completed + // 8efore moving on to more data processing. So, defaults to zero, which + // disa8les the queue feature altogether. + const queueSize = +(miscOptions['queue-size'] ?? 0); + { let errored = false; const error = (cond, msg) => { @@ -317,18 +314,11 @@ async function main() { } } - const appendIndexHTML = miscOptions['append-index-html'] ?? false; if (appendIndexHTML) { logWarn`Appending index.html to link hrefs. (Note: not recommended for production release!)`; link.globalOptions.appendIndexHTML = true; } - const skipThumbs = miscOptions['skip-thumbs'] ?? false; - const thumbsOnly = miscOptions['thumbs-only'] ?? false; - const noBuild = miscOptions['no-build'] ?? false; - const showAggregateTraces = miscOptions['show-traces'] ?? false; - const precacheData = miscOptions['precache-data'] ?? false; - // NOT for ena8ling or disa8ling specific features of the site! // This is only in charge of what general groups of files to 8uild. // They're here to make development quicker when you're only working @@ -374,9 +364,6 @@ async function main() { if (thumbsOnly) return; } - const showInvalidPropertyAccesses = - miscOptions['show-invalid-property-accesses'] ?? false; - if (showInvalidPropertyAccesses) { CacheableObject.DEBUG_SLOW_TRACK_INVALID_PROPERTIES = true; } @@ -435,7 +422,7 @@ async function main() { } } - if (!WD.wikiInfo) { + if (!wikiData.wikiInfo) { logError`Can't proceed without wiki info file (${WIKI_INFO_FILE}) successfully loading`; return; } @@ -542,15 +529,15 @@ async function main() { } const customDefaultLanguage = - languages[WD.wikiInfo.defaultLanguage ?? internalDefaultLanguage.code]; + languages[wikiData.wikiInfo.defaultLanguage ?? internalDefaultLanguage.code]; let finalDefaultLanguage; if (customDefaultLanguage) { logInfo`Applying new default strings from custom ${customDefaultLanguage.code} language file.`; customDefaultLanguage.inheritedStrings = internalDefaultLanguage.strings; finalDefaultLanguage = customDefaultLanguage; - } else if (WD.wikiInfo.defaultLanguage) { - logError`Wiki info file specified default language is ${WD.wikiInfo.defaultLanguage}, but no such language file exists!`; + } else if (wikiData.wikiInfo.defaultLanguage) { + logError`Wiki info file specified default language is ${wikiData.wikiInfo.defaultLanguage}, but no such language file exists!`; if (langPath) { logError`Check if an appropriate file exists in ${langPath}?`; } else { @@ -585,11 +572,11 @@ async function main() { { const tagRefs = new Set( - [...WD.trackData, ...WD.albumData] + [...wikiData.trackData, ...wikiData.albumData] .flatMap((thing) => thing.artTagsByRef ?? [])); for (const ref of tagRefs) { - if (find.artTag(ref, WD.artTagData)) { + if (find.artTag(ref, wikiData.artTagData)) { tagRefs.delete(ref); } } @@ -602,11 +589,13 @@ async function main() { } } - WD.officialAlbumData = WD.albumData + wikiData.officialAlbumData = wikiData.albumData .filter((album) => album.groups.some((group) => group.directory === OFFICIAL_GROUP_DIRECTORY)); - WD.fandomAlbumData = WD.albumData + wikiData.fandomAlbumData = wikiData.albumData .filter((album) => album.groups.every((group) => group.directory !== OFFICIAL_GROUP_DIRECTORY)); + const urls = generateURLs(urlSpec); + const fileSizePreloader = new FileSizePreloader(); // File sizes of additional files need to be precalculated before we can @@ -617,7 +606,7 @@ async function main() { // function between them so that when site code requests a site path, // it'll get the size of the file at the corresponding device path. const additionalFilePaths = [ - ...WD.albumData.flatMap((album) => + ...wikiData.albumData.flatMap((album) => [ ...(album.additionalFiles ?? []), ...album.tracks.flatMap((track) => track.additionalFiles ?? []), @@ -653,12 +642,6 @@ async function main() { if (noBuild) return; - // Makes writing a little nicer on CPU theoretically, 8ut also costs in - // performance right now 'cuz it'll w8 for file writes to 8e completed - // 8efore moving on to more data processing. So, defaults to zero, which - // disa8les the queue feature altogether. - queueSize = +(miscOptions['queue-size'] ?? 0); - const buildDictionary = pageSpecs; await writeSymlinks({ -- cgit 1.3.0-6-gf8a5 From 782a63e25304b7f17b216a231d243f3154ae1128 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 7 Jan 2023 21:47:34 -0400 Subject: quarantine hard-coded official/fandomAlbumData --- src/listing-spec.js | 12 +++++++++--- src/upd8.js | 12 +----------- src/util/magic-constants.js | 1 - 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/listing-spec.js b/src/listing-spec.js index 636e5f67..1538757c 100644 --- a/src/listing-spec.js +++ b/src/listing-spec.js @@ -1,3 +1,5 @@ +import {OFFICIAL_GROUP_DIRECTORY} from './util/magic-constants.js'; + import { empty, accumulateSum, @@ -876,16 +878,20 @@ const listingSpec = [ directory: 'random', stringsKey: 'other.randomPages', - data: ({wikiData: {fandomAlbumData, officialAlbumData}}) => [ + data: ({wikiData: {albumData}}) => [ { - albums: officialAlbumData, name: 'Official', randomCode: 'official', + albums: albumData + .filter((album) => album.groups + .some((group) => group.directory === OFFICIAL_GROUP_DIRECTORY)), }, { - albums: fandomAlbumData, name: 'Fandom', randomCode: 'fandom', + albums: albumData + .filter((album) => album.groups + .every((group) => group.directory !== OFFICIAL_GROUP_DIRECTORY)), }, ], diff --git a/src/upd8.js b/src/upd8.js index 6d6990bf..d5a5c2a7 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -112,9 +112,6 @@ import { } from './util/serialize.js'; */ -// Pensive emoji! -import { OFFICIAL_GROUP_DIRECTORY } from './util/magic-constants.js'; - import FileSizePreloader from './file-size-preloader.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); @@ -495,9 +492,7 @@ async function main() { progressCallAll('Caching all data values', Object.entries(wikiData) .filter(([key]) => key !== 'listingSpec' && - key !== 'listingTargetSpec' && - key !== 'officialAlbumData' && - key !== 'fandomAlbumData') + key !== 'listingTargetSpec') .map(([key, value]) => key === 'wikiInfo' ? [key, [value]] : key === 'homepageLayout' ? [key, [value]] : @@ -589,11 +584,6 @@ async function main() { } } - wikiData.officialAlbumData = wikiData.albumData - .filter((album) => album.groups.some((group) => group.directory === OFFICIAL_GROUP_DIRECTORY)); - wikiData.fandomAlbumData = wikiData.albumData - .filter((album) => album.groups.every((group) => group.directory !== OFFICIAL_GROUP_DIRECTORY)); - const urls = generateURLs(urlSpec); const fileSizePreloader = new FileSizePreloader(); diff --git a/src/util/magic-constants.js b/src/util/magic-constants.js index 73fdbc6d..83dd7db5 100644 --- a/src/util/magic-constants.js +++ b/src/util/magic-constants.js @@ -7,4 +7,3 @@ // (TM). export const OFFICIAL_GROUP_DIRECTORY = 'official'; -export const FANDOM_GROUP_DIRECTORY = 'fandom'; -- cgit 1.3.0-6-gf8a5 From db816222a07a4a8dff9f0f8b66e7aa7cb7c15eb5 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 8 Jan 2023 10:52:12 -0400 Subject: extract static-build, new modular build modes --- src/upd8.js | 494 +++++-------------------------- src/write/build-modes/index.js | 2 + src/write/build-modes/live-dev-server.js | 31 ++ src/write/build-modes/static-build.js | 423 ++++++++++++++++++++++++++ src/write/page-template.js | 70 +++-- src/write/write-files.js | 6 +- 6 files changed, 575 insertions(+), 451 deletions(-) create mode 100644 src/write/build-modes/index.js create mode 100644 src/write/build-modes/live-dev-server.js create mode 100644 src/write/build-modes/static-build.js diff --git a/src/upd8.js b/src/upd8.js index d5a5c2a7..c3934a4c 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -40,7 +40,6 @@ import {listingSpec, listingTargetSpec} from './listing-spec.js'; import urlSpec from './url-spec.js'; import {processLanguageFile} from './data/language.js'; -import {serializeThings} from './data/serialize.js'; import CacheableObject from './data/things/cacheable-object.js'; @@ -53,14 +52,17 @@ import { WIKI_INFO_FILE, } from './data/yaml.js'; -import * as pageSpecs from './page/index.js'; - import find from './util/find.js'; import {findFiles} from './util/io.js'; import link from './util/link.js'; import {isMain} from './util/node-utils.js'; import {validateReplacerSpec} from './util/replacer.js'; +import {empty, showAggregate} from './util/sugar.js'; import {replacerSpec} from './util/transform-content.js'; +import {generateURLs} from './util/urls.js'; + +import {generateDevelopersCommentHTML} from './write/page-template.js'; +import * as buildModes from './write/build-modes/index.js'; import { color, @@ -73,34 +75,6 @@ import { progressPromiseAll, } from './util/cli.js'; -import { - queue, - showAggregate, - withEntries, -} from './util/sugar.js'; - -import { - generateURLs, - getPagePaths, - getURLsFrom, -} from './util/urls.js'; - -import {bindUtilities} from './write/bind-utilities.js'; -import {validateWrites} from './write/validate-writes.js'; - -import { - generateDocumentHTML, - generateGlobalWikiDataJSON, - generateOEmbedJSON, - generateRedirectHTML, -} from './write/page-template.js'; - -import { - writePage, - writeSharedFilesAndPages, - writeSymlinks, -} from './write/write-files.js'; - /* import { serializeContribs, @@ -133,24 +107,32 @@ if (!validateReplacerSpec(replacerSpec, {find, link})) { process.exit(); } -// Wrapper function for running a function once for all languages. -async function wrapLanguages(fn, {languages, writeOneLanguage = null}) { - const k = writeOneLanguage; - const languagesToRun = k ? {[k]: languages[k]} : languages; +async function main() { + Error.stackTraceLimit = Infinity; + + const selectedBuildModeFlags = Object.keys( + await parseOptions(process.argv.slice(2), { + [parseOptions.handleUnknown]: () => {}, - const entries = Object.entries(languagesToRun).filter( - ([key]) => key !== 'default' - ); + ...Object.fromEntries(Object.keys(buildModes) + .map((key) => [key, {type: 'flag'}])), + })); - for (let i = 0; i < entries.length; i++) { - const [_key, language] = entries[i]; + let selectedBuildModeFlag; - await fn(language, i, entries); + if (empty(selectedBuildModeFlags)) { + selectedBuildModeFlag = 'static-build'; + logInfo`No build mode specified, using default: ${selectedBuildModeFlag}`; + } else if (selectedBuildModeFlags.length > 1) { + logError`Building multiple modes (${selectedBuildModeFlags.join(', ')}) at once not supported.`; + logError`Please specify a maximum of one build mode.`; + return; + } else { + selectedBuildModeFlag = selectedBuildModeFlags[0]; + logInfo`Using specified build mode: ${selectedBuildModeFlag}`; } -} -async function main() { - Error.stackTraceLimit = Infinity; + const selectedBuildMode = buildModes[selectedBuildModeFlag]; // This is about to get a whole lot more stuff put in it. const wikiData = { @@ -158,7 +140,9 @@ async function main() { listingTargetSpec, }; - const miscOptions = await parseOptions(process.argv.slice(2), { + const cliOptions = await parseOptions(process.argv.slice(2), { + ...selectedBuildMode.getCLIOptions(), + // Data files for the site, including flash, artist, and al8um data, // and like a jillion other things too. Pretty much everything which // makes an individual wiki what it is goes here! @@ -187,16 +171,6 @@ async function main() { type: 'value', }, - // This is the output directory. It's the one you'll upload online with - // rsync or whatever when you're pushing an upd8, and also the one - // you'd archive if you wanted to make a 8ackup of the whole dang - // site. Just keep in mind that the gener8ted result will contain a - // couple symlinked directories, so if you're uploading, you're pro8a8ly - // gonna want to resolve those yourself. - 'out-path': { - type: 'value', - }, - // Thum8nail gener8tion is *usually* something you want, 8ut it can 8e // kinda a pain to run every time, since it does necessit8te reading // every media file at run time. Pass this to skip it. @@ -217,20 +191,6 @@ async function main() { type: 'flag', }, - // Only want to 8uild one language during testing? This can chop down - // 8uild times a pretty 8ig chunk! Just pass a single language code. - lang: { - type: 'value', - }, - - // Working without a dev server and just using file:// URLs in your we8 - // 8rowser? This will automatically append index.html to links across - // the site. Not recommended for production, since it isn't guaranteed - // 100% error-free (and index.html-style links are less pretty anyway). - 'append-index-html': { - type: 'flag', - }, - // Want sweet, sweet trace8ack info in aggreg8te error messages? This // will print all the juicy details (or at least the first relevant // line) right to your output, 8ut also pro8a8ly give you a headache @@ -268,32 +228,26 @@ async function main() { 'precache-data': { type: 'flag', }, - - [parseOptions.handleUnknown]: () => {}, }); - const dataPath = miscOptions['data-path'] || process.env.HSMUSIC_DATA; - const mediaPath = miscOptions['media-path'] || process.env.HSMUSIC_MEDIA; - const langPath = miscOptions['lang-path'] || process.env.HSMUSIC_LANG; // Can 8e left unset! - const outputPath = miscOptions['out-path'] || process.env.HSMUSIC_OUT; - - const skipThumbs = miscOptions['skip-thumbs'] ?? false; - const thumbsOnly = miscOptions['thumbs-only'] ?? false; - const noBuild = miscOptions['no-build'] ?? false; - const writeOneLanguage = miscOptions['lang']; + const dataPath = cliOptions['data-path'] || process.env.HSMUSIC_DATA; + const mediaPath = cliOptions['media-path'] || process.env.HSMUSIC_MEDIA; + const langPath = cliOptions['lang-path'] || process.env.HSMUSIC_LANG; // Can 8e left unset! - const showAggregateTraces = miscOptions['show-traces'] ?? false; + const skipThumbs = cliOptions['skip-thumbs'] ?? false; + const thumbsOnly = cliOptions['thumbs-only'] ?? false; + const noBuild = cliOptions['no-build'] ?? false; - const appendIndexHTML = miscOptions['append-index-html'] ?? false; + const showAggregateTraces = cliOptions['show-traces'] ?? false; - const precacheData = miscOptions['precache-data'] ?? false; - const showInvalidPropertyAccesses = miscOptions['show-invalid-property-accesses'] ?? false; + const precacheData = cliOptions['precache-data'] ?? false; + const showInvalidPropertyAccesses = cliOptions['show-invalid-property-accesses'] ?? false; // Makes writing a little nicer on CPU theoretically, 8ut also costs in // performance right now 'cuz it'll w8 for file writes to 8e completed // 8efore moving on to more data processing. So, defaults to zero, which // disa8les the queue feature altogether. - const queueSize = +(miscOptions['queue-size'] ?? 0); + const queueSize = +(cliOptions['queue-size'] ?? 0); { let errored = false; @@ -305,39 +259,11 @@ async function main() { }; error(!dataPath, `Expected --data-path option or HSMUSIC_DATA to be set`); error(!mediaPath, `Expected --media-path option or HSMUSIC_MEDIA to be set`); - error(!outputPath, `Expected --out-path option or HSMUSIC_OUT to be set`); if (errored) { return; } } - if (appendIndexHTML) { - logWarn`Appending index.html to link hrefs. (Note: not recommended for production release!)`; - link.globalOptions.appendIndexHTML = true; - } - - // NOT for ena8ling or disa8ling specific features of the site! - // This is only in charge of what general groups of files to 8uild. - // They're here to make development quicker when you're only working - // on some particular area(s) of the site rather than making changes - // across all of them. - const writeFlags = await parseOptions(process.argv.slice(2), { - all: {type: 'flag'}, // Defaults to true if none 8elow specified. - - // Kinda a hack t8h! - ...Object.fromEntries( - Object.keys(pageSpecs).map((key) => [key, {type: 'flag'}]) - ), - - [parseOptions.handleUnknown]: () => {}, - }); - - const writeAll = !Object.keys(writeFlags).length || writeFlags.all; - - logInfo`Writing site pages: ${ - writeAll ? 'all' : Object.keys(writeFlags).join(', ') - }`; - const niceShowAggregate = (error, ...opts) => { showAggregate(error, { showTraces: showAggregateTraces, @@ -502,8 +428,7 @@ async function main() { } const internalDefaultLanguage = await processLanguageFile( - path.join(__dirname, DEFAULT_STRINGS_FILE) - ); + path.join(__dirname, DEFAULT_STRINGS_FILE)); let languages; if (langPath) { @@ -511,14 +436,11 @@ async function main() { filter: (f) => path.extname(f) === '.json', }); - const results = await progressPromiseAll( - `Reading & processing language files.`, - languageDataFiles.map((file) => processLanguageFile(file)) - ); + const results = await progressPromiseAll(`Reading & processing language files.`, + languageDataFiles.map((file) => processLanguageFile(file))); languages = Object.fromEntries( - results.map((language) => [language.code, language]) - ); + results.map((language) => [language.code, language])); } else { languages = {}; } @@ -556,13 +478,6 @@ async function main() { if (noBuild) { logInfo`Not generating any site or page files this run (--no-build passed).`; - } else if (writeOneLanguage && !(writeOneLanguage in languages)) { - logError`Specified to write only ${writeOneLanguage}, but there is no strings file with this language code!`; - return; - } else if (writeOneLanguage) { - logInfo`Writing only language ${writeOneLanguage} this run.`; - } else { - logInfo`Writing all languages.`; } { @@ -632,310 +547,55 @@ async function main() { if (noBuild) return; - const buildDictionary = pageSpecs; - - await writeSymlinks({ - srcRootDirname: __dirname, - mediaPath, - outputPath, - urls, + const developersComment = generateDevelopersCommentHTML({ + buildTime: BUILD_TIME, + commit: COMMIT, + wikiData, }); - await writeSharedFilesAndPages({ + return selectedBuildMode.go({ + cliOptions, + dataPath, mediaPath, - outputPath, - urls, + queueSize, + srcRootPath: __dirname, - language: finalDefaultLanguage, + defaultLanguage: finalDefaultLanguage, + languages, wikiData, - wikiDataJSON: generateGlobalWikiDataJSON({ - serializeThings, - wikiData, - }) - }); - - const buildSteps = writeAll - ? Object.entries(buildDictionary) - : Object.entries(buildDictionary).filter(([flag]) => writeFlags[flag]); - - let writes; - { - let error = false; - - const buildStepsWithTargets = buildSteps - .map(([flag, pageSpec]) => { - // Condition not met: skip this build step altogether. - if (pageSpec.condition && !pageSpec.condition({wikiData})) { - return null; - } - - // May still call writeTargetless if present. - if (!pageSpec.targets) { - return {flag, pageSpec, targets: []}; - } - - if (!pageSpec.write) { - logError`${flag + '.targets'} is specified, but ${flag + '.write'} is missing!`; - error = true; - return null; - } - - const targets = pageSpec.targets({wikiData}); - if (!Array.isArray(targets)) { - logError`${flag + '.targets'} was called, but it didn't return an array! (${typeof targets})`; - error = true; - return null; - } - - return {flag, pageSpec, targets}; - }) - .filter(Boolean); - - if (error) { - return; - } - - writes = progressCallAll('Computing page & data writes.', buildStepsWithTargets.flatMap(({flag, pageSpec, targets}) => { - const writesFns = targets.map(target => () => { - const writes = pageSpec.write(target, {wikiData})?.slice() || []; - const valid = validateWrites(writes, { - functionName: flag + '.write', - urlSpec, - }); - error ||=! valid; - return valid ? writes : []; - }); - - if (pageSpec.writeTargetless) { - writesFns.push(() => { - const writes = pageSpec.writeTargetless({wikiData}); - const valid = validateWrites(writes, { - functionName: flag + '.writeTargetless', - urlSpec, - }); - error ||=! valid; - return valid ? writes : []; - }); - } - - return writesFns; - })).flat(); - - if (error) { - return; - } - } - - const pageWrites = writes.filter(({type}) => type === 'page'); - const dataWrites = writes.filter(({type}) => type === 'data'); - const redirectWrites = writes.filter(({type}) => type === 'redirect'); - - if (writes.length) { - logInfo`Total of ${writes.length} writes returned. (${pageWrites.length} page, ${dataWrites.length} data [currently skipped], ${redirectWrites.length} redirect)`; - } else { - logWarn`No writes returned at all, so exiting early. This is probably a bug!`; - return; - } - - /* - await progressPromiseAll(`Writing data files shared across languages.`, queue( - dataWrites.map(({path, data}) => () => { - const bound = {}; - - bound.serializeLink = bindOpts(serializeLink, {}); - - bound.serializeContribs = bindOpts(serializeContribs, {}); - - bound.serializeImagePaths = bindOpts(serializeImagePaths, { - thumb - }); - - bound.serializeCover = bindOpts(serializeCover, { - [bindOpts.bindIndex]: 2, - serializeImagePaths: bound.serializeImagePaths, - urls - }); - - bound.serializeGroupsForAlbum = bindOpts(serializeGroupsForAlbum, { - serializeLink - }); - - bound.serializeGroupsForTrack = bindOpts(serializeGroupsForTrack, { - serializeLink - }); - - // TODO: This only supports one <>-style argument. - return writeData(path[0], path[1], data({...bound})); - }), - queueSize - )); - */ - - const perLanguageFn = async (language, i, entries) => { - const baseDirectory = - language === finalDefaultLanguage ? '' : language.code; - - console.log(`\x1b[34;1m${`[${i + 1}/${entries.length}] ${language.code} (-> /${baseDirectory}) `.padEnd(60, '-')}\x1b[0m`); - - await progressPromiseAll(`Writing ${language.code}`, queue([ - ...pageWrites.map((props) => () => { - const {path, page} = props; - - const pageSubKey = path[0]; - const urlArgs = path.slice(1); - - const localizedPaths = withEntries(languages, entries => entries - .filter(([key, language]) => key !== 'default' && !language.hidden) - .map(([_key, language]) => [ - language.code, - getPagePaths({ - outputPath, - urls, - - baseDirectory: - (language === finalDefaultLanguage - ? '' - : language.code), - fullKey: 'localized.' + pageSubKey, - urlArgs, - }), - ])); - - const paths = getPagePaths({ - outputPath, - urls, - - baseDirectory, - fullKey: 'localized.' + pageSubKey, - urlArgs, - }); - - const to = getURLsFrom({ - urls, - baseDirectory, - pageSubKey, - paths, - }); - - const absoluteTo = (targetFullKey, ...args) => { - const [groupKey, subKey] = targetFullKey.split('.'); - const from = urls.from('shared.root'); - return ( - '/' + - (groupKey === 'localized' && baseDirectory - ? from.to( - 'localizedWithBaseDirectory.' + subKey, - baseDirectory, - ...args - ) - : from.to(targetFullKey, ...args)) - ); - }; - - const bound = bindUtilities({ - language, - to, - wikiData, - }); - - const pageInfo = page({ - ...bound, - - language, - - absoluteTo, - relativeTo: to, - to, - urls, - - getSizeOfAdditionalFile, - }); - - const oEmbedJSON = generateOEmbedJSON(pageInfo, { - language, - wikiData, - }); - - const oEmbedJSONHref = - oEmbedJSON && - wikiData.wikiInfo.canonicalBase && - wikiData.wikiInfo.canonicalBase + - urls - .from('shared.root') - .to('shared.path', paths.pathname + 'oembed.json'); - - const pageHTML = generateDocumentHTML(pageInfo, { - buildTime: BUILD_TIME, - cachebust: '?' + CACHEBUST, - commit: COMMIT, - defaultLanguage: finalDefaultLanguage, - getThemeString: bound.getThemeString, - language, - languages, - localizedPaths, - oEmbedJSONHref, - paths, - to, - transformMultiline: bound.transformMultiline, - wikiData, - }); - - return writePage({ - html: pageHTML, - oEmbedJSON, - paths, - }); - }), - ...redirectWrites.map(({fromPath, toPath, title: titleFn}) => () => { - const title = titleFn({ - language, - }); - - const from = getPagePaths({ - outputPath, - urls, - - baseDirectory, - fullKey: 'localized.' + fromPath[0], - urlArgs: fromPath.slice(1), - }); - - const to = getURLsFrom({ - urls, - baseDirectory, - pageSubKey: fromPath[0], - paths: from, - }); - - const target = to('localized.' + toPath[0], ...toPath.slice(1)); - const html = generateRedirectHTML(title, target, {language}); - return writePage({html, paths: from}); - }), - ], queueSize)); - }; + urls, + urlSpec, - await wrapLanguages(perLanguageFn, { - languages, - writeOneLanguage, + cachebust: '?' + CACHEBUST, + developersComment, + getSizeOfAdditionalFile, }); - - // The single most important step. - logInfo`Written!`; } // TODO: isMain detection isn't consistent across platforms here /* eslint-disable-next-line no-constant-condition */ if (true || isMain(import.meta.url) || path.basename(process.argv[1]) === 'hsmusic') { - main() - .catch((error) => { + (async () => { + let result; + + try { + result = await main(); + } catch (error) { if (error instanceof AggregateError) { showAggregate(error); } else { console.error(error); } - }) - .then(() => { - decorateTime.displayTime(); - CacheableObject.showInvalidAccesses(); - }); + } + + if (result !== true) { + process.exit(1); + return; + } + + decorateTime.displayTime(); + CacheableObject.showInvalidAccesses(); + + process.exit(0); + })(); } diff --git a/src/write/build-modes/index.js b/src/write/build-modes/index.js new file mode 100644 index 00000000..91e39009 --- /dev/null +++ b/src/write/build-modes/index.js @@ -0,0 +1,2 @@ +export * as 'live-dev-server' from './live-dev-server.js'; +export * as 'static-build' from './static-build.js'; diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js new file mode 100644 index 00000000..76b6d47f --- /dev/null +++ b/src/write/build-modes/live-dev-server.js @@ -0,0 +1,31 @@ +import {logInfo} from '../../util/cli.js'; + +export function getCLIOptions() { + // Stub. + return {}; +} + +export async function go({ + _cliOptions, + _dataPath, + _mediaPath, + _queueSize, + + _defaultLanguage, + _languages, + _srcRootPath, + _urls, + _urlSpec, + _wikiData, + + _cachebust, + _developersComment, + _getSizeOfAdditionalFile, +}) { + // Stub. + logInfo`So we are back in the mine!`; + logInfo`We are swinging our pickaxe in-`; + logInfo`...multiple directions,`; + logInfo`...multiple directions.`; + return true; +} diff --git a/src/write/build-modes/static-build.js b/src/write/build-modes/static-build.js new file mode 100644 index 00000000..eafb53d6 --- /dev/null +++ b/src/write/build-modes/static-build.js @@ -0,0 +1,423 @@ +import {bindUtilities} from '../bind-utilities.js'; +import {validateWrites} from '../validate-writes.js'; + +import { + generateDocumentHTML, + generateGlobalWikiDataJSON, + generateOEmbedJSON, + generateRedirectHTML, +} from '../page-template.js'; + +import { + writePage, + writeSharedFilesAndPages, + writeSymlinks, +} from '../write-files.js'; + +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 {getPagePaths, getURLsFrom} from '../../util/urls.js'; + +import { + logError, + logInfo, + logWarn, + progressCallAll, + progressPromiseAll, +} from '../../util/cli.js'; + +const pageFlags = Object.keys(pageSpecs); + +export function getCLIOptions() { + return { + // This is the output directory. It's the one you'll upload online with + // rsync or whatever when you're pushing an upd8, and also the one + // you'd archive if you wanted to make a 8ackup of the whole dang + // site. Just keep in mind that the gener8ted result will contain a + // couple symlinked directories, so if you're uploading, you're pro8a8ly + // gonna want to resolve those yourself. + 'out-path': { + type: 'value', + }, + + // Working without a dev server and just using file:// URLs in your we8 + // 8rowser? This will automatically append index.html to links across + // the site. Not recommended for production, since it isn't guaranteed + // 100% error-free (and index.html-style links are less pretty anyway). + 'append-index-html': { + type: 'flag', + }, + + // Only want to 8uild one language during testing? This can chop down + // 8uild times a pretty 8ig chunk! Just pass a single language code. + 'lang': { + type: 'value', + }, + + // NOT for neatly ena8ling or disa8ling specific features of the site! + // This is only in charge of what general groups of files to write. + // They're here to make development quicker when you're only working + // on some particular area(s) of the site rather than making changes + // across all of them. + ...Object.fromEntries(pageFlags.map((key) => [key, {type: 'flag'}])), + }; +} + +export async function go({ + cliOptions, + _dataPath, + mediaPath, + queueSize, + + defaultLanguage, + languages, + srcRootPath, + urls, + urlSpec, + wikiData, + + cachebust, + developersComment, + getSizeOfAdditionalFile, +}) { + const outputPath = cliOptions['out-path'] || process.env.HSMUSIC_OUT; + const appendIndexHTML = cliOptions['append-index-html'] ?? false; + const writeOneLanguage = cliOptions['lang'] ?? null; + + if (!outputPath) { + logError`Expected ${'--out-path'} option or ${'HSMUSIC_OUT'} to be set`; + return false; + } + + if (appendIndexHTML) { + logWarn`Appending index.html to link hrefs. (Note: not recommended for production release!)`; + link.globalOptions.appendIndexHTML = true; + } + + if (writeOneLanguage && !(writeOneLanguage in languages)) { + logError`Specified to write only ${writeOneLanguage}, but there is no strings file with this language code!`; + return false; + } else if (writeOneLanguage) { + logInfo`Writing only language ${writeOneLanguage} this run.`; + } else { + logInfo`Writing all languages.`; + } + + const selectedPageFlags = Object.keys(cliOptions) + .filter(key => pageFlags.includes(key)); + + const writeAll = empty(selectedPageFlags) || selectedPageFlags.includes('all'); + logInfo`Writing site pages: ${writeAll ? 'all' : selectedPageFlags.join(', ')}`; + + await writeSymlinks({ + srcRootPath, + mediaPath, + outputPath, + urls, + }); + + await writeSharedFilesAndPages({ + mediaPath, + outputPath, + urls, + + language: defaultLanguage, + wikiData, + wikiDataJSON: generateGlobalWikiDataJSON({ + serializeThings, + wikiData, + }) + }); + + const buildSteps = writeAll + ? Object.entries(pageSpecs) + : Object.entries(pageSpecs) + .filter(([flag]) => selectedPageFlags.includes(flag)); + + let writes; + { + let error = false; + + const buildStepsWithTargets = buildSteps + .map(([flag, pageSpec]) => { + // Condition not met: skip this build step altogether. + if (pageSpec.condition && !pageSpec.condition({wikiData})) { + return null; + } + + // May still call writeTargetless if present. + if (!pageSpec.targets) { + return {flag, pageSpec, targets: []}; + } + + if (!pageSpec.write) { + logError`${flag + '.targets'} is specified, but ${flag + '.write'} is missing!`; + error = true; + return null; + } + + const targets = pageSpec.targets({wikiData}); + if (!Array.isArray(targets)) { + logError`${flag + '.targets'} was called, but it didn't return an array! (${typeof targets})`; + error = true; + return null; + } + + return {flag, pageSpec, targets}; + }) + .filter(Boolean); + + if (error) { + return false; + } + + writes = progressCallAll('Computing page & data writes.', buildStepsWithTargets.flatMap(({flag, pageSpec, targets}) => { + const writesFns = targets.map(target => () => { + const writes = pageSpec.write(target, {wikiData})?.slice() || []; + const valid = validateWrites(writes, { + functionName: flag + '.write', + urlSpec, + }); + error ||=! valid; + return valid ? writes : []; + }); + + if (pageSpec.writeTargetless) { + writesFns.push(() => { + const writes = pageSpec.writeTargetless({wikiData}); + const valid = validateWrites(writes, { + functionName: flag + '.writeTargetless', + urlSpec, + }); + error ||=! valid; + return valid ? writes : []; + }); + } + + return writesFns; + })).flat(); + + if (error) { + return false; + } + } + + const pageWrites = writes.filter(({type}) => type === 'page'); + const dataWrites = writes.filter(({type}) => type === 'data'); + const redirectWrites = writes.filter(({type}) => type === 'redirect'); + + if (writes.length) { + logInfo`Total of ${writes.length} writes returned. (${pageWrites.length} page, ${dataWrites.length} data [currently skipped], ${redirectWrites.length} redirect)`; + } else { + logWarn`No writes returned at all, so exiting early. This is probably a bug!`; + return false; + } + + /* + await progressPromiseAll(`Writing data files shared across languages.`, queue( + dataWrites.map(({path, data}) => () => { + const bound = {}; + + bound.serializeLink = bindOpts(serializeLink, {}); + + bound.serializeContribs = bindOpts(serializeContribs, {}); + + bound.serializeImagePaths = bindOpts(serializeImagePaths, { + thumb + }); + + bound.serializeCover = bindOpts(serializeCover, { + [bindOpts.bindIndex]: 2, + serializeImagePaths: bound.serializeImagePaths, + urls + }); + + bound.serializeGroupsForAlbum = bindOpts(serializeGroupsForAlbum, { + serializeLink + }); + + bound.serializeGroupsForTrack = bindOpts(serializeGroupsForTrack, { + serializeLink + }); + + // TODO: This only supports one <>-style argument. + return writeData(path[0], path[1], data({...bound})); + }), + queueSize + )); + */ + + const perLanguageFn = async (language, i, entries) => { + const baseDirectory = + language === defaultLanguage ? '' : language.code; + + console.log(`\x1b[34;1m${`[${i + 1}/${entries.length}] ${language.code} (-> /${baseDirectory}) `.padEnd(60, '-')}\x1b[0m`); + + await progressPromiseAll(`Writing ${language.code}`, queue([ + ...pageWrites.map((props) => () => { + const {path, page} = props; + + const pageSubKey = path[0]; + const urlArgs = path.slice(1); + + const localizedPaths = withEntries(languages, entries => entries + .filter(([key, language]) => key !== 'default' && !language.hidden) + .map(([_key, language]) => [ + language.code, + getPagePaths({ + outputPath, + urls, + + baseDirectory: + (language === defaultLanguage + ? '' + : language.code), + fullKey: 'localized.' + pageSubKey, + urlArgs, + }), + ])); + + const paths = getPagePaths({ + outputPath, + urls, + + baseDirectory, + fullKey: 'localized.' + pageSubKey, + urlArgs, + }); + + const to = getURLsFrom({ + urls, + baseDirectory, + pageSubKey, + paths, + }); + + const absoluteTo = (targetFullKey, ...args) => { + const [groupKey, subKey] = targetFullKey.split('.'); + const from = urls.from('shared.root'); + return ( + '/' + + (groupKey === 'localized' && baseDirectory + ? from.to( + 'localizedWithBaseDirectory.' + subKey, + baseDirectory, + ...args + ) + : from.to(targetFullKey, ...args)) + ); + }; + + const bound = bindUtilities({ + language, + to, + wikiData, + }); + + const pageInfo = page({ + ...bound, + + language, + + absoluteTo, + relativeTo: to, + to, + urls, + + getSizeOfAdditionalFile, + }); + + const oEmbedJSON = generateOEmbedJSON(pageInfo, { + language, + wikiData, + }); + + const oEmbedJSONHref = + oEmbedJSON && + wikiData.wikiInfo.canonicalBase && + wikiData.wikiInfo.canonicalBase + + urls + .from('shared.root') + .to('shared.path', paths.pathname + 'oembed.json'); + + const pageHTML = generateDocumentHTML(pageInfo, { + cachebust, + defaultLanguage, + developersComment, + getThemeString: bound.getThemeString, + language, + languages, + localizedPaths, + oEmbedJSONHref, + paths, + to, + transformMultiline: bound.transformMultiline, + wikiData, + }); + + return writePage({ + html: pageHTML, + oEmbedJSON, + paths, + }); + }), + ...redirectWrites.map(({fromPath, toPath, title: titleFn}) => () => { + const title = titleFn({ + language, + }); + + const from = getPagePaths({ + outputPath, + urls, + + baseDirectory, + fullKey: 'localized.' + fromPath[0], + urlArgs: fromPath.slice(1), + }); + + const to = getURLsFrom({ + urls, + baseDirectory, + pageSubKey: fromPath[0], + paths: from, + }); + + const target = to('localized.' + toPath[0], ...toPath.slice(1)); + const html = generateRedirectHTML(title, target, {language}); + return writePage({html, paths: from}); + }), + ], queueSize)); + }; + + await wrapLanguages(perLanguageFn, { + languages, + writeOneLanguage, + }); + + // The single most important step. + logInfo`Written!`; + return true; +} + +// Wrapper function for running a function once for all languages. +async function wrapLanguages(fn, { + languages, + writeOneLanguage = null, +}) { + const k = writeOneLanguage; + const languagesToRun = k ? {[k]: languages[k]} : languages; + + const entries = Object.entries(languagesToRun).filter( + ([key]) => key !== 'default' + ); + + for (let i = 0; i < entries.length; i++) { + const [_key, language] = entries[i]; + + await fn(language, i, entries); + } +} diff --git a/src/write/page-template.js b/src/write/page-template.js index f7faeed0..efbc5795 100644 --- a/src/write/page-template.js +++ b/src/write/page-template.js @@ -10,11 +10,46 @@ import { img, } from '../misc-templates.js'; +export function generateDevelopersCommentHTML({ + buildTime, + commit, + wikiData, +}) { + const {name, canonicalBase} = wikiData.wikiInfo; + return ``; +} + export function generateDocumentHTML(pageInfo, { - buildTime = null, - cachebust = '', - commit = '', + cachebust, defaultLanguage, + developersComment, getThemeString, language, languages, @@ -422,34 +457,7 @@ export function generateDocumentHTML(pageInfo, { 'data-rebase-data': to('data.root'), }, [ - ``, + developersComment, html.tag('head', [ html.tag('title', diff --git a/src/write/write-files.js b/src/write/write-files.js index e448df3f..8b6ac3af 100644 --- a/src/write/write-files.js +++ b/src/write/write-files.js @@ -46,14 +46,14 @@ export async function writePage({ } export function writeSymlinks({ - srcRootDirname, + srcRootPath, mediaPath, outputPath, urls, }) { return progressPromiseAll('Writing site symlinks.', [ - link(path.join(srcRootDirname, UTILITY_DIRECTORY), 'shared.utilityRoot'), - link(path.join(srcRootDirname, STATIC_DIRECTORY), 'shared.staticRoot'), + link(path.join(srcRootPath, UTILITY_DIRECTORY), 'shared.utilityRoot'), + link(path.join(srcRootPath, STATIC_DIRECTORY), 'shared.staticRoot'), link(mediaPath, 'media.root'), ]); -- cgit 1.3.0-6-gf8a5 From 2e90eaed378491142cb9d57ce705c58f4a598a10 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 8 Jan 2023 10:59:13 -0400 Subject: move static-specific write fns into build mode --- src/write/build-modes/static-build.js | 148 +++++++++++++++++++++++++++++-- src/write/write-files.js | 161 ---------------------------------- 2 files changed, 140 insertions(+), 169 deletions(-) delete mode 100644 src/write/write-files.js diff --git a/src/write/build-modes/static-build.js b/src/write/build-modes/static-build.js index eafb53d6..b3700c43 100644 --- a/src/write/build-modes/static-build.js +++ b/src/write/build-modes/static-build.js @@ -1,3 +1,5 @@ +import * as path from 'path'; + import {bindUtilities} from '../bind-utilities.js'; import {validateWrites} from '../validate-writes.js'; @@ -8,12 +10,6 @@ import { generateRedirectHTML, } from '../page-template.js'; -import { - writePage, - writeSharedFilesAndPages, - writeSymlinks, -} from '../write-files.js'; - import {serializeThings} from '../../data/serialize.js'; import * as pageSpecs from '../../page/index.js'; @@ -120,12 +116,15 @@ export async function go({ urls, }); - await writeSharedFilesAndPages({ + await writeFavicon({ mediaPath, outputPath, - urls, + }); + await writeSharedFilesAndPages({ language: defaultLanguage, + outputPath, + urls, wikiData, wikiDataJSON: generateGlobalWikiDataJSON({ serializeThings, @@ -421,3 +420,136 @@ async function wrapLanguages(fn, { await fn(language, i, entries); } } + +import { + copyFile, + mkdir, + stat, + symlink, + writeFile, + unlink, +} from 'fs/promises'; + +async function writePage({ + html, + oEmbedJSON = '', + paths, +}) { + await mkdir(paths.output.directory, {recursive: true}); + + await Promise.all([ + writeFile(paths.output.documentHTML, html), + + oEmbedJSON && + writeFile(paths.output.oEmbedJSON, oEmbedJSON), + ].filter(Boolean)); +} + +function writeSymlinks({ + srcRootPath, + mediaPath, + outputPath, + urls, +}) { + return progressPromiseAll('Writing site symlinks.', [ + link(path.join(srcRootPath, 'util'), 'shared.utilityRoot'), + link(path.join(srcRootPath, 'static'), 'shared.staticRoot'), + link(mediaPath, 'media.root'), + ]); + + async function link(directory, urlKey) { + const pathname = urls.from('shared.root').toDevice(urlKey); + const file = path.join(outputPath, pathname); + + try { + await unlink(file); + } catch (error) { + if (error.code !== 'ENOENT') { + throw error; + } + } + + try { + await symlink(path.resolve(directory), file); + } catch (error) { + if (error.code === 'EPERM') { + await symlink(path.resolve(directory), file, 'junction'); + } + } + } +} + +async function writeFavicon({ + mediaPath, + outputPath, +}) { + const faviconFile = 'favicon.ico'; + + try { + await stat(path.join(mediaPath, faviconFile)); + } catch (error) { + return; + } + + try { + await copyFile( + path.join(mediaPath, faviconFile), + path.join(outputPath, faviconFile)); + } catch (error) { + logWarn`Failed to copy favicon! ${error.message}`; + return; + } + + logInfo`Copied favicon to site root.`; +} + +async function writeSharedFilesAndPages({ + language, + outputPath, + urls, + wikiData, + wikiDataJSON, +}) { + const {groupData, wikiInfo} = wikiData; + + return progressPromiseAll(`Writing files & pages shared across languages.`, [ + groupData?.some((group) => group.directory === 'fandom') && + redirect( + 'Fandom - Gallery', + 'albums/fandom', + 'localized.groupGallery', + 'fandom' + ), + + groupData?.some((group) => group.directory === 'official') && + redirect( + 'Official - Gallery', + 'albums/official', + 'localized.groupGallery', + 'official' + ), + + wikiInfo.enableListings && + redirect( + 'Album Commentary', + 'list/all-commentary', + 'localized.commentaryIndex', + '' + ), + + wikiDataJSON && + writeFile( + path.join(outputPath, 'data.json'), + wikiDataJSON), + ].filter(Boolean)); + + async function redirect(title, from, urlKey, directory) { + const target = path.relative( + from, + urls.from('shared.root').to(urlKey, directory) + ); + const content = generateRedirectHTML(title, target, {language}); + await mkdir(path.join(outputPath, from), {recursive: true}); + await writeFile(path.join(outputPath, from, 'index.html'), content); + } +} diff --git a/src/write/write-files.js b/src/write/write-files.js deleted file mode 100644 index 8b6ac3af..00000000 --- a/src/write/write-files.js +++ /dev/null @@ -1,161 +0,0 @@ -import * as path from 'path'; - -import {generateRedirectHTML} from './page-template.js'; - -import { - logInfo, - logWarn, - progressPromiseAll, -} from '../util/cli.js'; - -// Code that's common 8etween the 8uild code (i.e. upd8.js) and gener8ted -// site code should 8e put here. Which, uh, ~~only really means this one -// file~~ is now a variety of useful utilities! -// -// Rather than hard code it, anything in this directory can 8e shared across -// 8oth ends of the code8ase. -// (This gets symlinked into the --data-path directory.) -const UTILITY_DIRECTORY = 'util'; - -// Code that's used only in the static site! CSS, cilent JS, etc. -// (This gets symlinked into the --data-path directory.) -const STATIC_DIRECTORY = 'static'; - -import { - copyFile, - mkdir, - stat, - symlink, - writeFile, - unlink, -} from 'fs/promises'; - -export async function writePage({ - html, - oEmbedJSON = '', - paths, -}) { - await mkdir(paths.output.directory, {recursive: true}); - - await Promise.all([ - writeFile(paths.output.documentHTML, html), - - oEmbedJSON && - writeFile(paths.output.oEmbedJSON, oEmbedJSON), - ].filter(Boolean)); -} - -export function writeSymlinks({ - srcRootPath, - mediaPath, - outputPath, - urls, -}) { - return progressPromiseAll('Writing site symlinks.', [ - link(path.join(srcRootPath, UTILITY_DIRECTORY), 'shared.utilityRoot'), - link(path.join(srcRootPath, STATIC_DIRECTORY), 'shared.staticRoot'), - link(mediaPath, 'media.root'), - ]); - - async function link(directory, urlKey) { - const pathname = urls.from('shared.root').toDevice(urlKey); - const file = path.join(outputPath, pathname); - - try { - await unlink(file); - } catch (error) { - if (error.code !== 'ENOENT') { - throw error; - } - } - - try { - await symlink(path.resolve(directory), file); - } catch (error) { - if (error.code === 'EPERM') { - await symlink(path.resolve(directory), file, 'junction'); - } - } - } -} - -export async function writeFavicon({ - mediaPath, - outputPath, -}) { - const faviconFile = 'favicon.ico'; - - try { - await stat(path.join(mediaPath, faviconFile)); - } catch (error) { - return; - } - - try { - await copyFile( - path.join(mediaPath, faviconFile), - path.join(outputPath, faviconFile)); - } catch (error) { - logWarn`Failed to copy favicon! ${error.message}`; - return; - } - - logInfo`Copied favicon to site root.`; -} - -export async function writeSharedFilesAndPages({ - language, - mediaPath, - outputPath, - urls, - wikiData, - wikiDataJSON, -}) { - const {groupData, wikiInfo} = wikiData; - - await writeFavicon({ - mediaPath, - outputPath, - }); - - return progressPromiseAll(`Writing files & pages shared across languages.`, [ - groupData?.some((group) => group.directory === 'fandom') && - redirect( - 'Fandom - Gallery', - 'albums/fandom', - 'localized.groupGallery', - 'fandom' - ), - - groupData?.some((group) => group.directory === 'official') && - redirect( - 'Official - Gallery', - 'albums/official', - 'localized.groupGallery', - 'official' - ), - - wikiInfo.enableListings && - redirect( - 'Album Commentary', - 'list/all-commentary', - 'localized.commentaryIndex', - '' - ), - - wikiDataJSON && - writeFile( - path.join(outputPath, 'data.json'), - wikiDataJSON), - ].filter(Boolean)); - - async function redirect(title, from, urlKey, directory) { - const target = path.relative( - from, - urls.from('shared.root').to(urlKey, directory) - ); - const content = generateRedirectHTML(title, target, {language}); - await mkdir(path.join(outputPath, from), {recursive: true}); - await writeFile(path.join(outputPath, from, 'index.html'), content); - } -} -- cgit 1.3.0-6-gf8a5 From 47a4404b4a294e7e127b64f9f930236072b56194 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 8 Jan 2023 11:04:53 -0400 Subject: everything is definitely fine --- src/upd8.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/upd8.js b/src/upd8.js index c3934a4c..9f0b2dba 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -228,6 +228,8 @@ async function main() { 'precache-data': { type: 'flag', }, + + [parseOptions.handleUnknown]: () => {}, }); const dataPath = cliOptions['data-path'] || process.env.HSMUSIC_DATA; -- cgit 1.3.0-6-gf8a5 From 154c050db72ebf06d4514326c696ab43fb3f8dc8 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 8 Jan 2023 11:49:44 -0400 Subject: basic actual stub for live-dev-server --- src/write/build-modes/live-dev-server.js | 43 ++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js index 76b6d47f..c3094712 100644 --- a/src/write/build-modes/live-dev-server.js +++ b/src/write/build-modes/live-dev-server.js @@ -1,4 +1,9 @@ -import {logInfo} from '../../util/cli.js'; +import * as pageSpecs from '../../page/index.js'; + +import { + logInfo, + progressCallAll, +} from '../../util/cli.js'; export function getCLIOptions() { // Stub. @@ -16,16 +21,40 @@ export async function go({ _srcRootPath, _urls, _urlSpec, - _wikiData, + wikiData, _cachebust, _developersComment, _getSizeOfAdditionalFile, }) { - // Stub. - logInfo`So we are back in the mine!`; - logInfo`We are swinging our pickaxe in-`; - logInfo`...multiple directions,`; - logInfo`...multiple directions.`; + let targetSpecPairs = getPageSpecsWithTargets({wikiData}); + const writes = progressCallAll(`Computing page data & paths for ${targetSpecPairs.length} targets.`, + targetSpecPairs.map(({ + pageSpec, + target, + targetless, + }) => () => + targetless + ? pageSpec.writeTargetless({wikiData}) + : pageSpec.write(target, {wikiData}))).flat(); + + logInfo`Will be serving a total of ${writes.length} pages.`; + return true; } + +function getPageSpecsWithTargets({ + wikiData, +}) { + return Object.values(pageSpecs) + .filter(pageSpec => pageSpec.condition?.({wikiData}) ?? true) + .flatMap(pageSpec => [ + ...pageSpec.targets + ? pageSpec.targets({wikiData}) + .map(target => ({pageSpec, target})) + : [], + Object.hasOwn(pageSpec, 'writeTargetless') && + {pageSpec, targetless: true}, + ]) + .filter(Boolean); +} -- cgit 1.3.0-6-gf8a5 From 594e8dd46f9e6cc74c680536a1d820eef27133f0 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 9 Jan 2023 21:07:16 -0400 Subject: most essential behavior for live-dev-server --- src/misc-templates.js | 43 ++--- src/util/urls.js | 56 +++--- src/write/bind-utilities.js | 1 + src/write/build-modes/live-dev-server.js | 287 +++++++++++++++++++++++++++++-- src/write/build-modes/static-build.js | 28 +-- src/write/page-template.js | 21 ++- 6 files changed, 355 insertions(+), 81 deletions(-) diff --git a/src/misc-templates.js b/src/misc-templates.js index 9a1bbf50..bccb8831 100644 --- a/src/misc-templates.js +++ b/src/misc-templates.js @@ -971,46 +971,37 @@ function unbound_generateStickyHeadingContainer({ function unbound_getFooterLocalizationLinks(pathname, { html, + defaultLanguage, language, + languages, to, - paths, - defaultLanguage, - languages, + pageSubKey, + urlArgs, }) { - const {urlPath} = paths; - const keySuffix = urlPath[0].replace(/^localized\./, '.'); - const toArgs = urlPath.slice(1); - const links = Object.entries(languages) .filter(([code, language]) => code !== 'default' && !language.hidden) .map(([code, language]) => language) .sort(({name: a}, {name: b}) => (a < b ? -1 : a > b ? 1 : 0)) .map((language) => - html.tag( - 'span', - html.tag( - 'a', + html.tag('span', + html.tag('a', { href: language === defaultLanguage - ? to('localizedDefaultLanguage' + keySuffix, ...toArgs) + ? to( + 'localizedDefaultLanguage.' + pageSubKey, + ...urlArgs) : to( - 'localizedWithBaseDirectory' + keySuffix, - language.code, - ...toArgs - ), + 'localizedWithBaseDirectory.' + pageSubKey, + language.code, ...urlArgs), }, - language.name - ) - ) - ); - - return html.tag( - 'div', - {class: 'footer-localization-links'}, - language.$('misc.uiLanguage', {languages: links.join('\n')}) - ); + language.name))); + + return html.tag('div', {class: 'footer-localization-links'}, + language.$('misc.uiLanguage', { + languages: links.join('\n'), + })); } // Exports diff --git a/src/util/urls.js b/src/util/urls.js index f05f134b..69ff1d7e 100644 --- a/src/util/urls.js +++ b/src/util/urls.js @@ -142,11 +142,11 @@ export function getURLsFrom({ baseDirectory, pageSubKey, - paths, + subdirectoryPrefix, }) { return (targetFullKey, ...args) => { const [groupKey, subKey] = targetFullKey.split('.'); - let path = paths.subdirectoryPrefix; + let path = subdirectoryPrefix; let from; let to; @@ -184,32 +184,47 @@ export function getURLsFrom({ }; } -export function getPagePaths({ - outputPath, +export function getPagePathname({ + baseDirectory, + fullKey, + urlArgs, urls, +}) { + const [groupKey, subKey] = fullKey.split('.'); + return (groupKey === 'localized' && baseDirectory + ? urls + .from('shared.root') + .toDevice( + 'localizedWithBaseDirectory.' + subKey, + baseDirectory, + ...urlArgs) + : urls + .from('shared.root') + .toDevice(fullKey, ...urlArgs)); +} + +// Needed for the rare path arguments which themselves contains one or more +// slashes, e.g. for listings, with arguments like 'albums/by-name'. +export function getPageSubdirectoryPrefix({urlArgs}) { + return '../'.repeat(urlArgs.join('/').split('/').length - 1); +} + +export function getPagePaths({ baseDirectory, fullKey, + outputPath, urlArgs, + urls, }) { const [groupKey, subKey] = fullKey.split('.'); - const pathname = - groupKey === 'localized' && baseDirectory - ? urls - .from('shared.root') - .toDevice( - 'localizedWithBaseDirectory.' + subKey, - baseDirectory, - ...urlArgs) - : urls - .from('shared.root') - .toDevice(fullKey, ...urlArgs); - - // Needed for the rare path arguments which themselves contains one or more - // slashes, e.g. for listings, with arguments like 'albums/by-name'. - const subdirectoryPrefix = - '../'.repeat(urlArgs.join('/').split('/').length - 1); + const pathname = getPagePathname({ + baseDirectory, + fullKey, + urlArgs, + urls, + }); const outputDirectory = path.join(outputPath, pathname); @@ -224,6 +239,5 @@ export function getPagePaths({ output, pathname, - subdirectoryPrefix, }; } diff --git a/src/write/bind-utilities.js b/src/write/bind-utilities.js index 6632ba30..1c4dd282 100644 --- a/src/write/bind-utilities.js +++ b/src/write/bind-utilities.js @@ -61,6 +61,7 @@ export function bindUtilities({ const bound = {}; bound.html = html; + bound.language = language; bound.img = bindOpts(img, { [bindOpts.bindIndex]: 0, diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js index c3094712..80badeb9 100644 --- a/src/write/build-modes/live-dev-server.js +++ b/src/write/build-modes/live-dev-server.js @@ -1,34 +1,55 @@ +import * as http from 'http'; +import {createReadStream} from 'fs'; +import {stat} from 'fs/promises'; +import * as path from 'path'; +import {pipeline} from 'stream/promises' + +import {bindUtilities} from '../bind-utilities.js'; + +import {serializeThings} from '../../data/serialize.js'; + import * as pageSpecs from '../../page/index.js'; +import {logInfo, progressCallAll} from '../../util/cli.js'; +import {withEntries} from '../../util/sugar.js'; + +import { + getPagePathname, + getPageSubdirectoryPrefix, + getURLsFrom, +} from '../../util/urls.js'; + import { - logInfo, - progressCallAll, -} from '../../util/cli.js'; + generateDocumentHTML, + generateGlobalWikiDataJSON, + generateRedirectHTML, +} from '../page-template.js'; export function getCLIOptions() { - // Stub. return {}; } export async function go({ _cliOptions, _dataPath, - _mediaPath, + mediaPath, _queueSize, - _defaultLanguage, - _languages, - _srcRootPath, - _urls, + defaultLanguage, + languages, + srcRootPath, + urls, _urlSpec, wikiData, - _cachebust, - _developersComment, - _getSizeOfAdditionalFile, + cachebust, + developersComment, + getSizeOfAdditionalFile, }) { + const port = 8002; + let targetSpecPairs = getPageSpecsWithTargets({wikiData}); - const writes = progressCallAll(`Computing page data & paths for ${targetSpecPairs.length} targets.`, + const pages = progressCallAll(`Computing page data & paths for ${targetSpecPairs.length} targets.`, targetSpecPairs.map(({ pageSpec, target, @@ -38,7 +59,245 @@ export async function go({ ? pageSpec.writeTargetless({wikiData}) : pageSpec.write(target, {wikiData}))).flat(); - logInfo`Will be serving a total of ${writes.length} pages.`; + logInfo`Will be serving a total of ${pages.length} pages.`; + + const urlToPageMap = Object.fromEntries(pages + .filter(page => page.type === 'page' || page.type === 'redirect') + .flatMap(page => { + let servePath; + if (page.type === 'page') + servePath = page.path; + 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, + urls, + }); + + return [pathname, { + baseDirectory, + language, + page, + servePath, + }]; + }); + })); + + const server = http.createServer(async (request, response) => { + const contentTypeHTML = {'Content-Type': 'text/html; charset=utf-8'}; + const contentTypeJSON = {'Content-Type': 'text/json; charset=utf-8'}; + const contentTypePlain = {'Content-Type': 'text/plain; charset=utf-8'}; + + const requestTime = new Date().toLocaleDateString('en-US', {hour: '2-digit', minute: '2-digit', second: '2-digit'}); + const requestHead = `${requestTime} - ${request.socket.remoteAddress}`; + + let url; + try { + url = new URL(request.url, `http://${request.headers.host}`); + } catch (error) { + response.writeHead(500, contentTypePlain); + response.end('Failed to parse request URL\n'); + return; + } + + const {pathname} = url; + + // Specialized routes + + if (pathname === '/data.json') { + response.writeHead(200, contentTypeJSON); + response.end(generateGlobalWikiDataJSON({ + serializeThings, + wikiData, + })); + return; + } + + const { + area: localFileArea, + path: localFilePath + } = pathname.match(/^\/(?static|media)\/(?.*)/)?.groups ?? {}; + + if (localFileArea) { + // Not security tested, man, this is a dev server!! + const safePath = path.resolve('/', localFilePath).replace(/^\//, ''); + + let localDirectory; + if (localFileArea === 'static') { + localDirectory = path.join(srcRootPath, 'static'); + } else if (localFileArea === 'media') { + localDirectory = mediaPath; + } + + const filePath = path.resolve(localDirectory, safePath); + + try { + await stat(filePath); + } catch (error) { + if (error.code === 'ENOENT') { + response.writeHead(404, contentTypePlain); + response.end(`No ${localFileArea} file found for: ${safePath}`); + console.log(`${requestHead} [404] ${pathname}`); + console.log(`ENOENT for stat: ${filePath}`); + } else { + response.writeHead(500, contentTypePlain); + response.end(`Internal error accessing ${localFileArea} file for: ${safePath}`); + console.error(`${requestHead} [500] ${pathname}`); + console.error(error); + } + return; + } + + try { + response.writeHead(200); // Sorry, no MIME type for now + await pipeline( + createReadStream(filePath), + response); + console.log(`${requestHead} [200] ${pathname}`); + } catch (error) { + response.writeHead(500, contentTypePlain); + response.end(`Failed during file-to-response pipeline`); + console.error(`${requestHead} [500] ${pathname}`); + console.error(error); + return; + } + } + + // Other routes determined by page and URL specs + + // URL to page map expects trailing slash but no leading slash. + const pathnameKey = pathname.replace(/^\//, '') + (pathname.endsWith('/') ? '' : '/'); + + if (!Object.hasOwn(urlToPageMap, pathnameKey)) { + response.writeHead(404, contentTypePlain); + response.end(`No page found for: ${pathnameKey}\n`); + console.log(`${requestHead} [404] ${pathname}`); + return; + } + + const { + baseDirectory, + language, + page, + servePath, + } = urlToPageMap[pathnameKey]; + + const to = getURLsFrom({ + urls, + baseDirectory, + pageSubKey: servePath[0], + subdirectoryPrefix: getPageSubdirectoryPrefix({ + urlArgs: servePath.slice(1), + }), + }); + + const absoluteTo = (targetFullKey, ...args) => { + const [groupKey, subKey] = targetFullKey.split('.'); + const from = urls.from('shared.root'); + return ( + '/' + + (groupKey === 'localized' && baseDirectory + ? from.to( + 'localizedWithBaseDirectory.' + subKey, + baseDirectory, + ...args + ) + : from.to(targetFullKey, ...args)) + ); + }; + + try { + const pageSubKey = servePath[0]; + const urlArgs = servePath.slice(1); + + if (page.type === 'redirect') { + response.writeHead(301, contentTypeHTML); + + const target = to('localized.' + page.toPath[0], ...page.toPath.slice(1)); + const redirectHTML = generateRedirectHTML(page.title, target, {language}); + + response.end(redirectHTML); + + console.log(`${requestHead} [301] (redirect) ${pathname}`); + return; + } + + 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 bound = bindUtilities({ + language, + to, + wikiData, + }); + + const pageInfo = page.page({ + ...bound, + + absoluteTo, + relativeTo: to, + to, + urls, + + getSizeOfAdditionalFile, + }); + + const pageHTML = generateDocumentHTML(pageInfo, { + cachebust, + defaultLanguage, + developersComment, + getThemeString: bound.getThemeString, + language, + languages, + localizedPathnames, + oEmbedJSONHref: null, // No oEmbed support for live dev server + pageSubKey, + pathname, + urlArgs, + to, + transformMultiline: bound.transformMultiline, + wikiData, + }); + + console.log(`${requestHead} [200] ${pathname}`); + response.end(pageHTML); + } catch (error) { + response.writeHead(500, contentTypePlain); + response.end(`Error generating page, view server log for details\n`); + console.error(`${requestHead} [500] ${pathname}`); + console.error(error); + } + }); + + server.listen(port); + logInfo`${'All done!'} Listening at ${`http://0.0.0.0:${port}/`}`; + + // Just keep going... forever!!! + await new Promise(() => {}); return true; } diff --git a/src/write/build-modes/static-build.js b/src/write/build-modes/static-build.js index b3700c43..1544a122 100644 --- a/src/write/build-modes/static-build.js +++ b/src/write/build-modes/static-build.js @@ -16,7 +16,6 @@ import * as pageSpecs from '../../page/index.js'; import link from '../../util/link.js'; import {empty, queue, withEntries} from '../../util/sugar.js'; -import {getPagePaths, getURLsFrom} from '../../util/urls.js'; import { logError, @@ -26,6 +25,13 @@ import { progressPromiseAll, } from '../../util/cli.js'; +import { + getPagePathname, + getPagePaths, + getPageSubdirectoryPrefix, + getURLsFrom, +} from '../../util/urls.js'; + const pageFlags = Object.keys(pageSpecs); export function getCLIOptions() { @@ -263,20 +269,18 @@ export async function go({ const pageSubKey = path[0]; const urlArgs = path.slice(1); - const localizedPaths = withEntries(languages, entries => entries + const localizedPathnames = withEntries(languages, entries => entries .filter(([key, language]) => key !== 'default' && !language.hidden) .map(([_key, language]) => [ language.code, - getPagePaths({ - outputPath, - urls, - + getPagePathname({ baseDirectory: (language === defaultLanguage ? '' : language.code), fullKey: 'localized.' + pageSubKey, urlArgs, + urls, }), ])); @@ -293,7 +297,9 @@ export async function go({ urls, baseDirectory, pageSubKey, - paths, + subdirectoryPrefix: getPageSubdirectoryPrefix({ + urlArgs: page.path.slice(1), + }), }); const absoluteTo = (targetFullKey, ...args) => { @@ -320,8 +326,6 @@ export async function go({ const pageInfo = page({ ...bound, - language, - absoluteTo, relativeTo: to, to, @@ -350,7 +354,7 @@ export async function go({ getThemeString: bound.getThemeString, language, languages, - localizedPaths, + localizedPathnames, oEmbedJSONHref, paths, to, @@ -382,7 +386,9 @@ export async function go({ urls, baseDirectory, pageSubKey: fromPath[0], - paths: from, + subdirectoryPrefix: getPageSubdirectoryPrefix({ + urlArgs: fromPath.slice(1), + }), }); const target = to('localized.' + toPath[0], ...toPath.slice(1)); diff --git a/src/write/page-template.js b/src/write/page-template.js index efbc5795..88d81c23 100644 --- a/src/write/page-template.js +++ b/src/write/page-template.js @@ -53,11 +53,13 @@ export function generateDocumentHTML(pageInfo, { getThemeString, language, languages, - localizedPaths, - paths, + localizedPathnames, oEmbedJSONHref, + pageSubKey, + pathname, to, transformMultiline, + urlArgs, wikiData, }) { const {wikiInfo} = wikiData; @@ -125,11 +127,11 @@ export function generateDocumentHTML(pageInfo, { : null; const canonical = wikiInfo.canonicalBase - ? wikiInfo.canonicalBase + (paths.pathname === '/' ? '' : paths.pathname) + ? wikiInfo.canonicalBase + (pathname === '/' ? '' : pathname) : ''; const localizedCanonical = wikiInfo.canonicalBase - ? Object.entries(localizedPaths).map(([code, {pathname}]) => ({ + ? Object.entries(localizedPathnames).map(([code, pathname]) => ({ lang: code, href: wikiInfo.canonicalBase + (pathname === '/' ? '' : pathname), })) @@ -162,13 +164,14 @@ export function generateDocumentHTML(pageInfo, { }, footer.content), - getFooterLocalizationLinks(paths.pathname, { + getFooterLocalizationLinks(pathname, { defaultLanguage, html, language, languages, - paths, + pageSubKey, to, + urlArgs, }), ]); @@ -264,7 +267,7 @@ export function generateDocumentHTML(pageInfo, { ? to(...cur.path) : cur.href ? (() => { - logWarn`Using legacy href format nav link in ${paths.pathname}`; + logWarn`Using legacy href format nav link in ${pathname}`; return cur.href; })() : null, @@ -447,9 +450,9 @@ export function generateDocumentHTML(pageInfo, { { lang: language.intlCode, 'data-language-code': language.code, - 'data-url-key': paths.urlPath[0], + 'data-url-key': 'localized.' + pageSubKey, ...Object.fromEntries( - paths.urlPath.slice(1).map((v, i) => [['data-url-value' + i], v]) + urlArgs.map((v, i) => [['data-url-value' + i], v]) ), 'data-rebase-localized': to('localized.root'), 'data-rebase-shared': to('shared.root'), -- cgit 1.3.0-6-gf8a5 From a532f066ffbe0387216c85323fdf81aff36c1d6c Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 9 Jan 2023 21:07:37 -0400 Subject: finish up the rest necessary for live-dev-server --- src/write/build-modes/live-dev-server.js | 113 +++++++++++++++++++++++++------ 1 file changed, 94 insertions(+), 19 deletions(-) diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js index 80badeb9..f322a79b 100644 --- a/src/write/build-modes/live-dev-server.js +++ b/src/write/build-modes/live-dev-server.js @@ -10,7 +10,7 @@ import {serializeThings} from '../../data/serialize.js'; import * as pageSpecs from '../../page/index.js'; -import {logInfo, progressCallAll} from '../../util/cli.js'; +import {logInfo, logWarn, progressCallAll} from '../../util/cli.js'; import {withEntries} from '../../util/sugar.js'; import { @@ -26,27 +26,39 @@ import { } from '../page-template.js'; export function getCLIOptions() { - return {}; + return { + host: { + type: 'value', + }, + + port: { + type: 'value', + validate(size) { + if (parseInt(size) !== parseFloat(size)) return 'an integer'; + if (parseInt(size) < 1024 || parseInt(size) > 49151) return 'a user/registered port (1024-49151)'; + return true; + }, + }, + }; } export async function go({ - _cliOptions, + cliOptions, _dataPath, mediaPath, - _queueSize, defaultLanguage, languages, srcRootPath, urls, - _urlSpec, wikiData, cachebust, developersComment, getSizeOfAdditionalFile, }) { - const port = 8002; + const host = cliOptions['host'] ?? '0.0.0.0'; + const port = parseInt(cliOptions['port'] ?? 8002); let targetSpecPairs = getPageSpecsWithTargets({wikiData}); const pages = progressCallAll(`Computing page data & paths for ${targetSpecPairs.length} targets.`, @@ -95,7 +107,7 @@ export async function go({ const server = http.createServer(async (request, response) => { const contentTypeHTML = {'Content-Type': 'text/html; charset=utf-8'}; - const contentTypeJSON = {'Content-Type': 'text/json; charset=utf-8'}; + const contentTypeJSON = {'Content-Type': 'application/json; charset=utf-8'}; const contentTypePlain = {'Content-Type': 'text/plain; charset=utf-8'}; const requestTime = new Date().toLocaleDateString('en-US', {hour: '2-digit', minute: '2-digit', second: '2-digit'}); @@ -115,26 +127,35 @@ export async function go({ // Specialized routes if (pathname === '/data.json') { - response.writeHead(200, contentTypeJSON); - response.end(generateGlobalWikiDataJSON({ - serializeThings, - wikiData, - })); + try { + const json = generateGlobalWikiDataJSON({ + serializeThings, + wikiData, + }); + response.writeHead(200, contentTypeJSON); + response.end(json); + console.log(`${requestHead} [200] /data.json`); + } catch (error) { + response.writeHead(500, contentTypeJSON); + response.end({error: `Internal error serializing wiki JSON`}); + console.error(`${requestHead} [500] /data.json`); + console.error(error); + } return; } const { area: localFileArea, path: localFilePath - } = pathname.match(/^\/(?static|media)\/(?.*)/)?.groups ?? {}; + } = pathname.match(/^\/(?static|util|media)\/(?.*)/)?.groups ?? {}; if (localFileArea) { // Not security tested, man, this is a dev server!! const safePath = path.resolve('/', localFilePath).replace(/^\//, ''); let localDirectory; - if (localFileArea === 'static') { - localDirectory = path.join(srcRootPath, 'static'); + if (localFileArea === 'static' || localFileArea === 'util') { + localDirectory = path.join(srcRootPath, localFileArea); } else if (localFileArea === 'media') { localDirectory = mediaPath; } @@ -158,8 +179,42 @@ export async function go({ return; } + const extname = path.extname(safePath).slice(1).toLowerCase(); + + const contentType = { + // BRB covering all my bases + 'aac': 'audio/aac', + 'bmp': 'image/bmp', + 'css': 'text/css', + 'csv': 'text/csv', + 'gif': 'image/gif', + 'ico': 'image/vnd.microsoft.icon', + 'jpg': 'image/jpeg', + 'jpeg:': 'image/jpeg', + 'js': 'text/javascript', + 'mjs': 'text/javascript', + 'mp3': 'audio/mpeg', + 'mp4': 'video/mp4', + 'oga': 'audio/ogg', + 'ogg': 'audio/ogg', + 'ogv': 'video/ogg', + 'opus': 'audio/opus', + 'png': 'image/png', + 'pdf': 'application/pdf', + 'svg': 'image/svg+xml', + 'ttf': 'font/ttf', + 'txt': 'text/plain', + 'wav': 'audio/wav', + 'weba': 'audio/webm', + 'webm': 'video/webm', + 'woff': 'font/woff', + 'woff2': 'font/woff2', + 'xml': 'application/xml', + 'zip': 'application/zip', + }[extname]; + try { - response.writeHead(200); // Sorry, no MIME type for now + response.writeHead(200, contentType ? {'Content-Type': contentType} : {}); await pipeline( createReadStream(filePath), response); @@ -169,8 +224,8 @@ export async function go({ response.end(`Failed during file-to-response pipeline`); console.error(`${requestHead} [500] ${pathname}`); console.error(error); - return; } + return; } // Other routes determined by page and URL specs @@ -293,8 +348,28 @@ export async function go({ } }); - server.listen(port); - logInfo`${'All done!'} Listening at ${`http://0.0.0.0:${port}/`}`; + const address = `http://${host}:${port}/`; + + server.on('error', error => { + if (error.code === 'EADDRINUSE') { + logWarn`Port ${port} is already in use - will (continually) retry after 10 seconds.`; + logWarn`Press ^C here (control+C) to exit and change ${'--port'} number, or stop the server currently running on port ${port}.`; + setTimeout(() => { + server.close(); + server.listen(port, host); + }, 10_000); + } else { + console.error(`Server error detected (code: ${error.code})`); + console.error(error); + } + }); + + server.on('listening', () => { + logInfo`${'All done!'} Listening at: ${address}`; + logInfo`Press ^C here (control+C) to stop the server and exit.`; + }); + + server.listen(port, host); // Just keep going... forever!!! await new Promise(() => {}); -- cgit 1.3.0-6-gf8a5 From 0480e9a6502695fb741fc300db03ef734f38b51c Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 9 Jan 2023 21:42:25 -0400 Subject: attempt to fix path issues on windows --- src/write/build-modes/live-dev-server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js index f322a79b..a8e54247 100644 --- a/src/write/build-modes/live-dev-server.js +++ b/src/write/build-modes/live-dev-server.js @@ -151,7 +151,7 @@ export async function go({ if (localFileArea) { // Not security tested, man, this is a dev server!! - const safePath = path.resolve('/', localFilePath).replace(/^\//, ''); + const safePath = path.posix.resolve('/', localFilePath).replace(/^\//, ''); let localDirectory; if (localFileArea === 'static' || localFileArea === 'util') { @@ -160,7 +160,7 @@ export async function go({ localDirectory = mediaPath; } - const filePath = path.resolve(localDirectory, safePath); + const filePath = path.resolve(localDirectory, safePath.split('/').join(path.sep)); try { await stat(filePath); -- cgit 1.3.0-6-gf8a5 From 091d5221948ca4a6decd091bc3a5e0063adf1443 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 9 Jan 2023 21:58:20 -0400 Subject: hopefully fix normal pages on windows --- src/util/urls.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/util/urls.js b/src/util/urls.js index 69ff1d7e..78bcd200 100644 --- a/src/util/urls.js +++ b/src/util/urls.js @@ -186,22 +186,25 @@ export function getURLsFrom({ export function getPagePathname({ baseDirectory, + device = false, fullKey, urlArgs, urls, }) { const [groupKey, subKey] = fullKey.split('.'); + const toKey = device ? 'toDevice' : 'to'; + return (groupKey === 'localized' && baseDirectory ? urls - .from('shared.root') - .toDevice( + .from('shared.root')[toKey]( 'localizedWithBaseDirectory.' + subKey, baseDirectory, ...urlArgs) : urls - .from('shared.root') - .toDevice(fullKey, ...urlArgs)); + .from('shared.root')[toKey]( + fullKey, + ...urlArgs)); } // Needed for the rare path arguments which themselves contains one or more @@ -221,6 +224,7 @@ export function getPagePaths({ const pathname = getPagePathname({ baseDirectory, + device: true, fullKey, urlArgs, urls, -- cgit 1.3.0-6-gf8a5 From d442546057f8280a141d4aa54f633a09c429e2d3 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 10 Jan 2023 16:55:04 -0400 Subject: extract absoluteTo --- src/util/urls.js | 26 ++++++++++++++++++++++++++ src/write/build-modes/live-dev-server.js | 20 +++++--------------- src/write/build-modes/static-build.js | 20 +++++--------------- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/util/urls.js b/src/util/urls.js index 78bcd200..4672c6a2 100644 --- a/src/util/urls.js +++ b/src/util/urls.js @@ -137,6 +137,8 @@ export const thumb = { small: thumbnailHelper('.small'), }; +// Makes the generally-used and wiki-specialized "to" page utility. +// "to" returns a relative path from the current page to the target. export function getURLsFrom({ urls, @@ -184,6 +186,30 @@ export function getURLsFrom({ }; } +// Makes the generally-used and wiki-specialized "absoluteTo" page utility. +// "absoluteTo" returns an absolute path, starting at site root (/) leading +// to the target. +export function getURLsFromRoot({ + baseDirectory, + urls, +}) { + const from = urls.from('shared.root'); + + return (targetFullKey, ...args) => { + const [groupKey, subKey] = targetFullKey.split('.'); + return ( + '/' + + (groupKey === 'localized' && baseDirectory + ? from.to( + 'localizedWithBaseDirectory.' + subKey, + baseDirectory, + ...args + ) + : from.to(targetFullKey, ...args)) + ); + }; +} + export function getPagePathname({ baseDirectory, device = false, diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js index a8e54247..d481b480 100644 --- a/src/write/build-modes/live-dev-server.js +++ b/src/write/build-modes/live-dev-server.js @@ -17,6 +17,7 @@ import { getPagePathname, getPageSubdirectoryPrefix, getURLsFrom, + getURLsFromRoot, } from '../../util/urls.js'; import { @@ -256,20 +257,10 @@ export async function go({ }), }); - const absoluteTo = (targetFullKey, ...args) => { - const [groupKey, subKey] = targetFullKey.split('.'); - const from = urls.from('shared.root'); - return ( - '/' + - (groupKey === 'localized' && baseDirectory - ? from.to( - 'localizedWithBaseDirectory.' + subKey, - baseDirectory, - ...args - ) - : from.to(targetFullKey, ...args)) - ); - }; + const absoluteTo = getURLsFromRoot({ + baseDirectory, + urls, + }); try { const pageSubKey = servePath[0]; @@ -314,7 +305,6 @@ export async function go({ ...bound, absoluteTo, - relativeTo: to, to, urls, diff --git a/src/write/build-modes/static-build.js b/src/write/build-modes/static-build.js index 1544a122..c2bb02f2 100644 --- a/src/write/build-modes/static-build.js +++ b/src/write/build-modes/static-build.js @@ -30,6 +30,7 @@ import { getPagePaths, getPageSubdirectoryPrefix, getURLsFrom, + getURLsFromRoot, } from '../../util/urls.js'; const pageFlags = Object.keys(pageSpecs); @@ -302,20 +303,10 @@ export async function go({ }), }); - const absoluteTo = (targetFullKey, ...args) => { - const [groupKey, subKey] = targetFullKey.split('.'); - const from = urls.from('shared.root'); - return ( - '/' + - (groupKey === 'localized' && baseDirectory - ? from.to( - 'localizedWithBaseDirectory.' + subKey, - baseDirectory, - ...args - ) - : from.to(targetFullKey, ...args)) - ); - }; + const absoluteTo = getURLsFromRoot({ + baseDirectory, + urls, + }); const bound = bindUtilities({ language, @@ -327,7 +318,6 @@ export async function go({ ...bound, absoluteTo, - relativeTo: to, to, urls, -- cgit 1.3.0-6-gf8a5 From 4de56d21db5004e5b2c2a49caedee0908b9c6a9f Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 10 Jan 2023 17:01:54 -0400 Subject: bind more utilities in bindUtilities --- src/write/bind-utilities.js | 14 ++++++++++++-- src/write/build-modes/live-dev-server.js | 13 ++++--------- src/write/build-modes/static-build.js | 13 ++++--------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/write/bind-utilities.js b/src/write/bind-utilities.js index 1c4dd282..4b037a91 100644 --- a/src/write/bind-utilities.js +++ b/src/write/bind-utilities.js @@ -51,8 +51,11 @@ import { } from '../util/wiki-data.js'; export function bindUtilities({ + absoluteTo, + getSizeOfAdditionalFile, language, to, + urls, wikiData, }) { // TODO: Is there some nicer way to define these, @@ -60,8 +63,15 @@ export function bindUtilities({ // each page? const bound = {}; - bound.html = html; - bound.language = language; + Object.assign(bound, { + absoluteTo, + getSizeOfAdditionalFile, + html, + language, + to, + urls, + wikiData, + }) bound.img = bindOpts(img, { [bindOpts.bindIndex]: 0, diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js index d481b480..b6bf662b 100644 --- a/src/write/build-modes/live-dev-server.js +++ b/src/write/build-modes/live-dev-server.js @@ -296,20 +296,15 @@ export async function go({ ])); const bound = bindUtilities({ + absoluteTo, + getSizeOfAdditionalFile, language, to, + urls, wikiData, }); - const pageInfo = page.page({ - ...bound, - - absoluteTo, - to, - urls, - - getSizeOfAdditionalFile, - }); + const pageInfo = page.page(bound); const pageHTML = generateDocumentHTML(pageInfo, { cachebust, diff --git a/src/write/build-modes/static-build.js b/src/write/build-modes/static-build.js index c2bb02f2..dab2598a 100644 --- a/src/write/build-modes/static-build.js +++ b/src/write/build-modes/static-build.js @@ -309,20 +309,15 @@ export async function go({ }); const bound = bindUtilities({ + absoluteTo, + getSizeOfAdditionalFile, language, to, + urls, wikiData, }); - const pageInfo = page({ - ...bound, - - absoluteTo, - to, - urls, - - getSizeOfAdditionalFile, - }); + const pageInfo = page(bound); const oEmbedJSON = generateOEmbedJSON(pageInfo, { language, -- cgit 1.3.0-6-gf8a5