From ef8acc5d50fa3c23bd7c9d4bb720b7ff78581981 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 19 Aug 2023 14:13:31 -0300 Subject: clean up imports & miscellaneous metastructures across codebase --- src/util/find.js | 162 ------------------------------------------------- src/util/html.js | 8 +-- src/util/node-utils.js | 6 +- src/util/replacer.js | 13 +++- src/util/serialize.js | 8 +++ src/util/urls.js | 16 ++++- src/util/wiki-data.js | 7 +-- 7 files changed, 39 insertions(+), 181 deletions(-) delete mode 100644 src/util/find.js (limited to 'src/util') diff --git a/src/util/find.js b/src/util/find.js deleted file mode 100644 index dcb15b3..0000000 --- a/src/util/find.js +++ /dev/null @@ -1,162 +0,0 @@ -import {color, logWarn} from './cli.js'; - -import {inspect} from 'util'; - -function warnOrThrow(mode, message) { - if (mode === 'error') { - throw new Error(message); - } - - if (mode === 'warn') { - logWarn(message); - } - - return null; -} - -function findHelper(keys, findFns = {}) { - // Note: This cache explicitly *doesn't* support mutable data arrays. If the - // data array is modified, make sure it's actually a new array object, not - // the original, or the cache here will break and act as though the data - // hasn't changed! - const cache = new WeakMap(); - - const byDirectory = findFns.byDirectory || matchDirectory; - const byName = findFns.byName || matchName; - - const keyRefRegex = new RegExp(String.raw`^(?:(${keys.join('|')}):(?=\S))?(.*)$`); - - // The mode argument here may be 'warn', 'error', or 'quiet'. 'error' throws - // errors for null matches (with details about the error), while 'warn' and - // 'quiet' both return null, with 'warn' logging details directly to the - // console. - return (fullRef, data, {mode = 'warn'} = {}) => { - if (!fullRef) return null; - if (typeof fullRef !== 'string') { - throw new Error(`Got a reference that is ${typeof fullRef}, not string: ${fullRef}`); - } - - if (!data) { - throw new Error(`Expected data to be present`); - } - - if (!Array.isArray(data) && data.wikiData) { - throw new Error(`Old {wikiData: {...}} format provided`); - } - - let cacheForThisData = cache.get(data); - const cachedValue = cacheForThisData?.[fullRef]; - if (cachedValue) { - globalThis.NUM_CACHE = (globalThis.NUM_CACHE || 0) + 1; - return cachedValue; - } - if (!cacheForThisData) { - cacheForThisData = Object.create(null); - cache.set(data, cacheForThisData); - } - - const match = fullRef.match(keyRefRegex); - if (!match) { - return warnOrThrow(mode, `Malformed link reference: "${fullRef}"`); - } - - const key = match[1]; - const ref = match[2]; - - const found = key ? byDirectory(ref, data, mode) : byName(ref, data, mode); - - if (!found) { - warnOrThrow(mode, `Didn't match anything for ${color.bright(fullRef)}`); - } - - cacheForThisData[fullRef] = found; - - return found; - }; -} - -function matchDirectory(ref, data) { - return data.find(({directory}) => directory === ref); -} - -function matchName(ref, data, mode) { - const matches = data.filter( - ({name}) => name.toLowerCase() === ref.toLowerCase() - ); - - if (matches.length > 1) { - return warnOrThrow( - mode, - `Multiple matches for reference "${ref}". Please resolve:\n` + - matches.map((match) => `- ${inspect(match)}\n`).join('') + - `Returning null for this reference.` - ); - } - - if (matches.length === 0) { - return null; - } - - const thing = matches[0]; - - if (ref !== thing.name) { - warnOrThrow( - mode, - `Bad capitalization: ${color.red(ref)} -> ${color.green(thing.name)}` - ); - } - - return thing; -} - -function matchTagName(ref, data, quiet) { - return matchName(ref.startsWith('cw: ') ? ref.slice(4) : ref, data, quiet); -} - -const find = { - album: findHelper(['album', 'album-commentary', 'album-gallery']), - artist: findHelper(['artist', 'artist-gallery']), - artTag: findHelper(['tag'], {byName: matchTagName}), - flash: findHelper(['flash']), - group: findHelper(['group', 'group-gallery']), - listing: findHelper(['listing']), - newsEntry: findHelper(['news-entry']), - staticPage: findHelper(['static']), - track: findHelper(['track']), -}; - -export default find; - -// Handy utility function for binding the find.thing() functions to a complete -// wikiData object, optionally taking default options to provide to the find -// function. Note that this caches the arrays read from wikiData right when it's -// called, so if their values change, you'll have to continue with a fresh call -// to bindFind. -export function bindFind(wikiData, opts1) { - return Object.fromEntries( - Object.entries({ - album: 'albumData', - artist: 'artistData', - artTag: 'artTagData', - flash: 'flashData', - group: 'groupData', - listing: 'listingSpec', - newsEntry: 'newsData', - staticPage: 'staticPageData', - track: 'trackData', - }).map(([key, value]) => { - const findFn = find[key]; - const thingData = wikiData[value]; - return [ - key, - opts1 - ? (ref, opts2) => - opts2 - ? findFn(ref, thingData, {...opts1, ...opts2}) - : findFn(ref, thingData, opts1) - : (ref, opts2) => - opts2 ? findFn(ref, thingData, opts2) : findFn(ref, thingData), - ]; - }) - ); -} diff --git a/src/util/html.js b/src/util/html.js index 5687bba..7ed363c 100644 --- a/src/util/html.js +++ b/src/util/html.js @@ -1,9 +1,9 @@ -// Some really simple functions for formatting HTML content. +// Some really, really simple functions for formatting HTML content. -import {inspect} from 'util'; +import {inspect} from 'node:util'; -import * as commonValidators from '../data/things/validators.js'; -import {empty} from './sugar.js'; +import {empty} from '#sugar'; +import * as commonValidators from '#validators'; // COMPREHENSIVE! // https://html.spec.whatwg.org/multipage/syntax.html#void-elements diff --git a/src/util/node-utils.js b/src/util/node-utils.js index 2fb7e8d..345d10a 100644 --- a/src/util/node-utils.js +++ b/src/util/node-utils.js @@ -1,8 +1,8 @@ // Utility functions which are only relevant to particular Node.js constructs. -import {readdir, stat} from 'fs/promises'; -import {fileURLToPath} from 'url'; -import * as path from 'path'; +import {readdir, stat} from 'node:fs/promises'; +import * as path from 'node:path'; +import {fileURLToPath} from 'node:url'; import _commandExists from 'command-exists'; diff --git a/src/util/replacer.js b/src/util/replacer.js index 278ffab..c5289cc 100644 --- a/src/util/replacer.js +++ b/src/util/replacer.js @@ -1,6 +1,13 @@ -import {logError, logWarn} from './cli.js'; -import {escapeRegex} from './sugar.js'; -import * as html from './html.js'; +// Regex-based forward parser for wiki content, breaking up text input into +// text and (possibly nested) tag nodes. +// +// The behavior here is quite tied into the `transformContent` content +// function, which converts nodes parsed here into actual HTML, links, etc +// for embedding in a wiki webpage. + +import {logError, logWarn} from '#cli'; +import * as html from '#html'; +import {escapeRegex} from '#sugar'; // Syntax literals. const tagBeginning = '[['; diff --git a/src/util/serialize.js b/src/util/serialize.js index 73a3137..4992e2b 100644 --- a/src/util/serialize.js +++ b/src/util/serialize.js @@ -1,3 +1,10 @@ +// Utils used when per-wiki-object data files. +// Retained for reference and/or later reorganization. +// +// Not to be confused with data/serialize.js, which provides a generic +// interface for serializing any Thing object. + +/* export function serializeLink(thing) { const ret = {}; ret.name = thing.name; @@ -67,3 +74,4 @@ export function serializeGroupsForTrack(track, {serializeLink}) { urls: group.urls, })); } +*/ diff --git a/src/util/urls.js b/src/util/urls.js index 0296174..d2b303e 100644 --- a/src/util/urls.js +++ b/src/util/urls.js @@ -4,9 +4,19 @@ // which can really quickly take su8stitute parameters to link from any one // place to another; 8ut there are also a few other utilities, too. -import * as path from 'path'; - -import {withEntries} from './sugar.js'; +import * as path from 'node:path'; + +import {withEntries} from '#sugar'; + +// This export is only provided for convenience, i.e. to enable the following: +// +// import {urlSpec} from '#urls'; +// +// It's not actually defined in this module's variable scope, and functions +// exported here require a urlSpec (whether this default one or another) to be +// passed directly. +// +export {default as urlSpec} from '../url-spec.js'; export function generateURLs(urlSpec) { const getValueForFullKey = (obj, fullKey) => { diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js index 6930496..ad2f82f 100644 --- a/src/util/wiki-data.js +++ b/src/util/wiki-data.js @@ -1,11 +1,6 @@ // Utility functions for interacting with wiki data. -import { - accumulateSum, - empty, - stitchArrays, - unique, -} from './sugar.js'; +import {accumulateSum, empty, stitchArrays, unique} from './sugar.js'; // Generic value operations -- cgit 1.3.0-6-gf8a5