From adb41fa1c75de3967b7016e63edd20f149d74d16 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 22 Feb 2025 12:36:54 -0400 Subject: yaml: matchFilenameToThings --- src/data/yaml.js | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 96 insertions(+), 5 deletions(-) diff --git a/src/data/yaml.js b/src/data/yaml.js index 4548c536..1045da4f 100644 --- a/src/data/yaml.js +++ b/src/data/yaml.js @@ -33,6 +33,7 @@ import { getNestedProp, stitchArrays, typeAppearance, + unique, withEntries, } from '#sugar'; @@ -918,7 +919,8 @@ export function processThingsFromDataStep(documents, dataStep) { processDocument(document, dataStep.documentThing); thing[Thing.yamlSourceDocument] = document; - thing[Thing.yamlSourceDocumentPlacement] = ['all in one', index]; + thing[Thing.yamlSourceDocumentPlacement] = + [documentModes.allInOne, index]; result.push(thing); aggregate.call(subAggregate.close); @@ -939,7 +941,8 @@ export function processThingsFromDataStep(documents, dataStep) { processDocument(documents[0], dataStep.documentThing); thing[Thing.yamlSourceDocument] = documents[0]; - thing[Thing.yamlSourceDocumentPlacement] = ['one document total']; + thing[Thing.yamlSourceDocumentPlacement] = + [documentModes.oneDocumentTotal]; return { aggregate, @@ -961,7 +964,8 @@ export function processThingsFromDataStep(documents, dataStep) { processDocument(headerDocument, dataStep.headerDocumentThing); headerThing[Thing.yamlSourceDocument] = headerDocument; - headerThing[Thing.yamlSourceDocumentPlacement] = ['header']; + headerThing[Thing.yamlSourceDocumentPlacement] = + [documentModes.headerAndEntries, 'header']; try { headerAggregate.close(); @@ -977,7 +981,8 @@ export function processThingsFromDataStep(documents, dataStep) { processDocument(entryDocument, dataStep.entryDocumentThing); entryThing[Thing.yamlSourceDocument] = entryDocument; - entryThing[Thing.yamlSourceDocumentPlacement] = ['entry under header', index]; + entryThing[Thing.yamlSourceDocumentPlacement] = + [documentModes.headerAndEntries, 'entry', index]; entryThings.push(entryThing); @@ -1010,7 +1015,8 @@ export function processThingsFromDataStep(documents, dataStep) { processDocument(documents[0], dataStep.documentThing); thing[Thing.yamlSourceDocument] = documents[0]; - thing[Thing.yamlSourceDocumentPlacement] = ['one per file']; + thing[Thing.yamlSourceDocumentPlacement] = + [documentModes.onePerFile]; return { aggregate, @@ -1440,3 +1446,88 @@ export async function quickLoadAllFromYAML(dataPath, { return wikiData; } + +export function cruddilyGetAllThings(wikiData) { + const allThings = []; + + for (const v of Object.values(wikiData)) { + if (Array.isArray(v)) { + allThings.push(...v); + } else { + allThings.push(v); + } + } + + return allThings; +} + +export function matchFilenameToThings(filename, wikiData) { + const things = + cruddilyGetAllThings(wikiData) + .filter(thing => + thing[Thing.yamlSourceFilename] === filename); + + if (empty(things)) { + return null; + } + + const allDocumentModes = + unique(things.map(thing => + thing[Thing.yamlSourceDocumentPlacement][0])); + + if (allDocumentModes.length > 1) { + throw new Error(`More than one document mode for documents from ${filename}`); + } + + const documentMode = allDocumentModes[0]; + + switch (documentMode) { + case documentModes.allInOne: { + return { + documentMode, + things: + things.sort((a, b) => + a[Thing.yamlSourceDocumentPlacement][1] - + b[Thing.yamlSourceDocumentPlacement][1]), + }; + } + + case documentModes.oneDocumentTotal: + case documentModes.onePerFile: { + if (things.length > 1) { + throw new Error(`More than one document for ${filename}`); + } + + return { + documentMode, + thing: things[0], + }; + } + + case documentModes.headerAndEntries: { + const headerThings = + things.filter(thing => + thing[Thing.yamlSourceDocumentPlacement][1] === 'header'); + + if (headerThings.length > 1) { + throw new Error(`More than one header document for ${filename}`); + } + + return { + documentMode, + headerThing: headerThings[0] ?? null, + entryThings: + things + .filter(thing => + thing[Thing.yamlSourceDocumentPlacement][1] === 'entry') + .sort((a, b) => + a[Thing.yamlSourceDocumentPlacement][2] - + b[Thing.yamlSourceDocumentPlacement][2]), + }; + } + + default: { + return {documentMode}; + } + } +} -- cgit 1.3.0-6-gf8a5