@weborigami/origami 0.0.69 → 0.0.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,7 @@ export { default as cache } from "../src/builtins/@cache.js";
7
7
  export { default as calendarTree } from "../src/builtins/@calendarTree.js";
8
8
  export { default as changes } from "../src/builtins/@changes.js";
9
9
  export { default as clean } from "../src/builtins/@clean.js";
10
+ export { default as code } from "../src/builtins/@code.js";
10
11
  export { default as concat } from "../src/builtins/@concat.js";
11
12
  export { default as config } from "../src/builtins/@config.js";
12
13
  export { default as copy } from "../src/builtins/@copy.js";
@@ -40,6 +41,7 @@ export { default as imageFormat } from "../src/builtins/@image/format.js";
40
41
  export { default as imageFormatFn } from "../src/builtins/@image/formatFn.js";
41
42
  export { default as imageResize } from "../src/builtins/@image/resize.js";
42
43
  export { default as imageResizeFn } from "../src/builtins/@image/resizeFn.js";
44
+ export { default as indent } from "../src/builtins/@indent.js";
43
45
  export { default as index } from "../src/builtins/@index.js";
44
46
  export { default as inherited } from "../src/builtins/@inherited.js";
45
47
  export { default as inline } from "../src/builtins/@inline.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weborigami/origami",
3
- "version": "0.0.69",
3
+ "version": "0.0.70",
4
4
  "description": "Web Origami language, CLI, framework, and server",
5
5
  "type": "module",
6
6
  "repository": {
@@ -17,9 +17,9 @@
17
17
  "typescript": "5.6.2"
18
18
  },
19
19
  "dependencies": {
20
- "@weborigami/async-tree": "0.0.69",
21
- "@weborigami/language": "0.0.69",
22
- "@weborigami/types": "0.0.69",
20
+ "@weborigami/async-tree": "0.0.70",
21
+ "@weborigami/language": "0.0.70",
22
+ "@weborigami/types": "0.0.70",
23
23
  "exif-parser": "0.1.12",
24
24
  "graphviz-wasm": "3.0.2",
25
25
  "highlight.js": "11.10.0",
@@ -0,0 +1,37 @@
1
+ import { symbols } from "@weborigami/language";
2
+ import getTreeArgument from "../misc/getTreeArgument.js";
3
+
4
+ /**
5
+ * @typedef {import("@weborigami/types").AsyncTree} AsyncTree
6
+ *
7
+ * @this {AsyncTree|null}
8
+ * @param {any} value
9
+ */
10
+ export default async function code(value) {
11
+ if (value === undefined) {
12
+ value = await getTreeArgument(this, arguments, value, "@clean");
13
+ }
14
+ if (value === undefined) {
15
+ return undefined;
16
+ }
17
+ const code = value.code ?? value[symbols.codeSymbol];
18
+ return code ? functionNames(code) : undefined;
19
+ }
20
+
21
+ function functionNames(code) {
22
+ if (!Array.isArray(code)) {
23
+ return code;
24
+ }
25
+ let [head, ...tail] = code;
26
+ if (typeof head === "function") {
27
+ const text = head.toString();
28
+ if (text.startsWith("«ops.")) {
29
+ head = text;
30
+ } else {
31
+ head = head.name;
32
+ }
33
+ } else {
34
+ head = functionNames(head);
35
+ }
36
+ return [head, ...tail.map(functionNames)];
37
+ }
@@ -0,0 +1,115 @@
1
+ const lastLineWhitespaceRegex = /\n(?<indent>[ \t]*)$/;
2
+
3
+ const mapStringsToModifications = new Map();
4
+
5
+ /**
6
+ * Normalize indentation in a tagged template string.
7
+ *
8
+ * @param {TemplateStringsArray} strings
9
+ * @param {...any} values
10
+ * @returns {string}
11
+ */
12
+ export default function indent(strings, ...values) {
13
+ let modified = mapStringsToModifications.get(strings);
14
+ if (!modified) {
15
+ modified = modifyStrings(strings);
16
+ mapStringsToModifications.set(strings, modified);
17
+ }
18
+ const { blockIndentations, strings: modifiedStrings } = modified;
19
+ return joinBlocks(modifiedStrings, values, blockIndentations);
20
+ }
21
+
22
+ // Join strings and values, applying the given block indentation to the lines of
23
+ // values for block placholders.
24
+ function joinBlocks(strings, values, blockIndentations) {
25
+ let result = strings[0];
26
+ for (let i = 0; i < values.length; i++) {
27
+ let text = values[i];
28
+ if (text) {
29
+ const blockIndentation = blockIndentations[i];
30
+ if (blockIndentation) {
31
+ const lines = text.split("\n");
32
+ text = "";
33
+ if (lines.at(-1) === "") {
34
+ // Drop empty last line
35
+ lines.pop();
36
+ }
37
+ for (let line of lines) {
38
+ text += blockIndentation + line + "\n";
39
+ }
40
+ }
41
+ result += text;
42
+ }
43
+ result += strings[i + 1];
44
+ }
45
+ return result;
46
+ }
47
+
48
+ // Given an array of template boilerplate strings, return an object { modified,
49
+ // blockIndentations } where `strings` is the array of strings with indentation
50
+ // removed, and `blockIndentations` is an array of indentation strings for each
51
+ // block placeholder.
52
+ function modifyStrings(strings) {
53
+ // Phase one: Identify the indentation based on the first real line of the
54
+ // first string (skipping the initial newline), and remove this indentation
55
+ // from all lines of all strings.
56
+ let indent;
57
+ if (strings.length > 0 && strings[0].startsWith("\n")) {
58
+ // Look for indenttation
59
+ const firstLineWhitespaceRegex = /^\n(?<indent>[ \t]*)/;
60
+ const match = strings[0].match(firstLineWhitespaceRegex);
61
+ indent = match?.groups.indent;
62
+ }
63
+
64
+ // Determine the modified strings. If this invoked as a JS tagged template
65
+ // literal, the `strings` argument will be an odd array-ish object that we'll
66
+ // want to convert to a real array.
67
+ let modified;
68
+ if (indent) {
69
+ // De-indent the strings.
70
+ const indentationRegex = new RegExp(`\n${indent}`, "g");
71
+ // The `replaceAll` also converts strings to a real array.
72
+ modified = strings.map((string) =>
73
+ string.replaceAll(indentationRegex, "\n")
74
+ );
75
+ // Remove indentation from last line of last string
76
+ modified[modified.length - 1] = modified
77
+ .at(-1)
78
+ .replace(lastLineWhitespaceRegex, "\n");
79
+ } else {
80
+ // No indentation; just copy the strings so we have a real array
81
+ modified = strings.slice();
82
+ }
83
+
84
+ // Phase two: Identify any block placholders, identify and remove their
85
+ // preceding indentation, and remove the following newline. Work backward from
86
+ // the end towards the start because we're modifying the strings in place and
87
+ // our pattern matching won't work going forward from start to end.
88
+ let blockIndentations = [];
89
+ for (let i = modified.length - 2; i >= 0; i--) {
90
+ // Get the modified before and after substitution with index `i`
91
+ const beforeString = modified[i];
92
+ const afterString = modified[i + 1];
93
+ const match = beforeString.match(lastLineWhitespaceRegex);
94
+ if (match && afterString.startsWith("\n")) {
95
+ // The substitution between these strings is a block substitution
96
+ let blockIndentation = match.groups.indent;
97
+ blockIndentations[i] = blockIndentation;
98
+ // Trim the before and after strings
99
+ if (blockIndentation) {
100
+ modified[i] = beforeString.slice(0, -blockIndentation.length);
101
+ }
102
+ modified[i + 1] = afterString.slice(1);
103
+ }
104
+ }
105
+
106
+ // Remove newline from start of first string *after* removing indentation.
107
+ if (modified[0].startsWith("\n")) {
108
+ modified[0] = modified[0].slice(1);
109
+ }
110
+
111
+ return {
112
+ blockIndentations,
113
+ strings: modified,
114
+ };
115
+ }
@@ -24,7 +24,11 @@ export default function processUnpackedContent(content, parent, attachedData) {
24
24
  } else {
25
25
  target = base;
26
26
  }
27
- return content.bind(target);
27
+ const result = content.bind(target);
28
+ if (content.code) {
29
+ result.code = content.code;
30
+ }
31
+ return result;
28
32
  } else if (Tree.isAsyncTree(content) && !content.parent) {
29
33
  const result = Object.create(content);
30
34
  result.parent = parent;
@@ -64,13 +64,16 @@ export function toFunction(obj) {
64
64
  return obj;
65
65
  } else if (isUnpackable(obj)) {
66
66
  // Extract the contents of the object and convert that to a function.
67
- let fn;
67
+ let fnPromise;
68
68
  /** @this {any} */
69
69
  return async function (...args) {
70
- if (!fn) {
71
- const content = await obj.unpack();
72
- fn = toFunction(content);
70
+ if (!fnPromise) {
71
+ // unpack() may return a function or a promise for a function; normalize
72
+ // to a promise for a function
73
+ const unpackPromise = Promise.resolve(obj.unpack());
74
+ fnPromise = unpackPromise.then((content) => toFunction(content));
73
75
  }
76
+ const fn = await fnPromise;
74
77
  return fn.call(this, ...args);
75
78
  };
76
79
  } else if (Tree.isTreelike(obj)) {
@@ -27,9 +27,10 @@ export default async function* crawlResources(tree, baseUrl) {
27
27
 
28
28
  let errorPaths = [];
29
29
 
30
- // Seed the promise dictionary with robots.txt at the root and an empty path
31
- // indicating the current directory (relative to the baseUrl).
32
- const initialPaths = ["/robots.txt", ""];
30
+ // Seed the promise dictionary with robots.txt at the root, a sitemap.xml at
31
+ // the root, and an empty path indicating the current directory (relative to
32
+ // the baseUrl).
33
+ const initialPaths = ["/robots.txt", "/sitemap.xml", ""];
33
34
  initialPaths.forEach((path) => {
34
35
  promisesForPaths[path] = processPath(tree, path, baseUrl);
35
36
  });