]> Lady’s Gitweb - Beorn/commitdiff
Fix sorting of entries and simplify processing 0.2.2
authorLady <redacted>
Sun, 7 May 2023 04:22:05 +0000 (21:22 -0700)
committerLady <redacted>
Sun, 7 May 2023 04:22:05 +0000 (21:22 -0700)
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.

build.js

index 9ae91c9f68f5702518d1662b67eaf87aef9b86ce..b6ab4867b51373eaec63c6ef7b5f975018238329 100755 (executable)
--- 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);
     }
   }
 
This page took 0.023959 seconds and 4 git commands to generate.