From: Lady Date: Sun, 7 May 2023 04:22:05 +0000 (-0700) Subject: Fix sorting of entries and simplify processing X-Git-Tag: 0.2.2 X-Git-Url: https://git.ladys.computer/Beorn/commitdiff_plain/1d0d66bc803b169edbcf87f1dbee20832ac9ad0c?ds=sidebyside;hp=9f2c90d3378fe8e25f2a8cc53b9640a3d30507e7 Fix sorting of entries and simplify processing Both the date directories and the entry directories need to be sorted, but the former initially wasn’t. This also filters out uninteresting files prior to sorting in both cases, instead of waiting to filter within the `for` loop itself. --- diff --git a/build.js b/build.js index 9ae91c9..b6ab486 100755 --- a/build.js +++ b/build.js @@ -1011,90 +1011,79 @@ await (async () => { // this is the run script const feedEntries = feedTemplate.createDocumentFragment(); // Process entries and save the resulting index files. - for await ( - const { name: date, isDirectory } of Deno.readDir( - `${basePath}/`, + for ( + const { name: date } of Array.from( + Deno.readDirSync(`${basePath}/`), + ).filter(({ name: date, isDirectory }) => + // Exclude non‐dated directories. + isDirectory && /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/u.test(date) + ).sort(({ name: a }, { name: b }) => + // Sort the directories. + a < b ? -1 : a > b ? 1 : 0 ) ) { - // Iterate over each directory and process the ones which are - // dates. - if (!isDirectory || !/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/u.test(date)) { - // This isn’t a dated directory. - continue; - } else { - // This is a dated directory. - for ( - const { name: entryName, isDirectory } of Array.from( - Deno.readDirSync(`${basePath}/${date}/`), - ).sort(({ name: a }, { name: b }) => - a < b ? -1 : a > b ? 1 : 0 - ) - ) { - // Iterate over each directory and process the ones which look - // like entries. - if ( - !isDirectory || - //deno-lint-ignore no-control-regex - /[\x00-\x20\x22#%/<>?\\^\x60{|}\x7F]/u.test(entryName) - ) { - // This isn’t an entry directory. - continue; - } else { - // Process the entry. - const entry = document.createElement("entry"); - const entryPath = - `${basePath}/${date}/${entryName}/#entry.rdf`; - const entryDocument = parser.parseFromString( - await Deno.readTextFile(entryPath), - "application/xml", - ); - const { documentElement: entryRoot } = entryDocument; - entryDocument.lastModified = - (await Deno.lstat(entryPath)).mtime; - if (!entryRoot.hasAttributeNS(RDF, "about")) { - // The entry doesn’t have an identifier; let’s give it one. - entryRoot.setAttributeNS( - RDF, - "about", - new URL(`./${date}/${entryName}/`, feedURI), - ); - } else { - // The entry already has an identifier. - /* do nothing */ - } - const entryMetadata = metadataFromDocument(entryDocument); - if (entryMetadata.author.length == 0) { - // The entry metadata did not supply an author. - entryMetadata.author = feedMetadata.author; - } else { - // The entry metadata supplied its own author. - /* do nothing */ - } - const entryTemplate = await documentFromTemplate("entry"); - const { documentElement: templateRoot } = entryTemplate; - const lang = getLanguage(entryRoot); - if (lang && !getLanguage(templateRoot)) { - // The root element of the template does not have an - // assigned language, but the entry does. - setLanguage(templateRoot, lang); - } else { - // Either the template root already has a language, or the - // entry doesn’t either. - /* do nothing */ - } - writes.push( - Deno.writeTextFile( - `${basePath}/${date}/${entryName}/index.xhtml`, - serializer.serializeToString( - applyMetadata(entryTemplate, entryMetadata), - ) + "\n", - ), - ); - applyMetadata(entry, entryMetadata); - applyMetadata(feedEntries, entryMetadata); - feed.appendChild(entry); - } + // Iterate over each dated directory and process its entries. + for ( + const { name: entryName } of Array.from( + Deno.readDirSync(`${basePath}/${date}/`), + ).filter(({ name: entryName, isDirectory }) => + // Exclude non‐entry directories. + isDirectory && + //deno-lint-ignore no-control-regex + !/[\x00-\x20\x22#%/<>?\\^\x60{|}\x7F]/u.test(entryName) + ).sort(({ name: a }, { name: b }) => a < b ? -1 : a > b ? 1 : 0) + ) { + // Iterate over each entry directory and process its contents. + const entry = document.createElement("entry"); + const entryPath = `${basePath}/${date}/${entryName}/#entry.rdf`; + const entryDocument = parser.parseFromString( + await Deno.readTextFile(entryPath), + "application/xml", + ); + const { documentElement: entryRoot } = entryDocument; + entryDocument.lastModified = (await Deno.lstat(entryPath)).mtime; + if (!entryRoot.hasAttributeNS(RDF, "about")) { + // The entry doesn’t have an identifier; let’s give it one. + entryRoot.setAttributeNS( + RDF, + "about", + new URL(`./${date}/${entryName}/`, feedURI), + ); + } else { + // The entry already has an identifier. + /* do nothing */ } + const entryMetadata = metadataFromDocument(entryDocument); + if (entryMetadata.author.length == 0) { + // The entry metadata did not supply an author. + entryMetadata.author = feedMetadata.author; + } else { + // The entry metadata supplied its own author. + /* do nothing */ + } + const entryTemplate = await documentFromTemplate("entry"); + const { documentElement: templateRoot } = entryTemplate; + const lang = getLanguage(entryRoot); + if (lang && !getLanguage(templateRoot)) { + // The root element of the template does not have an + // assigned language, but the entry does. + setLanguage(templateRoot, lang); + } else { + // Either the template root already has a language, or the + // entry doesn’t either. + /* do nothing */ + } + writes.push( + Deno.writeTextFile( + `${basePath}/${date}/${entryName}/index.xhtml`, + serializer.serializeToString( + applyMetadata(entryTemplate, entryMetadata), + ) + "\n", + ), + ); + applyMetadata(entry, entryMetadata); + applyMetadata(feedEntries, entryMetadata); + feed.appendChild(entry); } }