@sigrix-io/gatehouse 0.2.0 → 0.3.0

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.
package/README.md CHANGED
@@ -8,7 +8,9 @@ A browser client for [Postern](https://github.com/sigrix-io/postern). Given a ru
8
8
 
9
9
  Beside the run page, four blocks a host draws from its own data, for a page that shows several apps at once: a card per app, a list of them, a run history and a usage summary. They are handed documents rather than clients, so drawing a page of apps wakes none of their runners.
10
10
 
11
- Five scripts and a stylesheet. No build step, no runtime dependency, no second origin.
11
+ And one block for a playbook, a process a business runs by hand and sells as files: handed its `playbook.json`, it draws what is inside, the files in the order a person works through them and the placeholders a buyer fills in, and never a file's text.
12
+
13
+ Six scripts and a stylesheet. No build step, no runtime dependency, no second origin.
12
14
 
13
15
  > **0.x.** Nothing is stable before 1.0; pin the minor. See `VERSIONING.md`.
14
16
 
@@ -18,7 +20,7 @@ Five scripts and a stylesheet. No build step, no runtime dependency, no second o
18
20
  npm install @sigrix-io/gatehouse
19
21
  ```
20
22
 
21
- Serve the six files it installs in `node_modules/@sigrix-io/gatehouse/src/` (or copy them from this repository's `src/`; there is nothing to build). A run page loads four of the scripts, in this order:
23
+ Serve the seven files it installs in `node_modules/@sigrix-io/gatehouse/src/` (or copy them from this repository's `src/`; there is nothing to build). A run page loads four of the scripts, in this order:
22
24
 
23
25
  ```html
24
26
  <link rel="stylesheet" href="gatehouse.css">
@@ -48,7 +50,7 @@ Each script is an IIFE that adds to one global, `window.Gatehouse`. `window.Sigr
48
50
 
49
51
  `renderer.load()` reads `describe` and `status` and draws the page. Running, stopping and retrying are the page's own buttons.
50
52
 
51
- A page of apps loads `host_blocks.js`, alone or after the four; see [Host blocks](#host-blocks).
53
+ A page of apps loads `host_blocks.js`, alone or after the four; see [Host blocks](#host-blocks). A page showing a playbook loads `playbook_contents.js`, alone or beside the others; see [A playbook's contents](#a-playbooks-contents).
52
54
 
53
55
  ## The rules that are easy to erode
54
56
 
@@ -58,7 +60,7 @@ Each is held by a test rather than by memory (`tests/contract.test.js` and the s
58
60
  - **No markup from the agent.** `innerHTML` appears nowhere. Agent output and every streamed `delta.text` reach the page through `textContent`.
59
61
  - **No `EventSource`, and no retry loop around the stream.** It cannot POST and it reconnects on its own, and a reopened stream is a new run: the way a client spends someone's money twice. A disconnect is a cancel, so Stop is an `AbortController`.
60
62
  - **Zero imports, and no distributor.** Nothing here names a marketplace or a listing.
61
- - **Documents are handed in.** The composition and the choice arrive as data from whoever mounted the page, and so does everything the host blocks draw. Fetching them here would be reaching for a fifth verb, and Postern has four.
63
+ - **Documents are handed in.** The composition and the choice arrive as data from whoever mounted the page, and so does everything the host blocks and the playbook block draw. Fetching them here would be reaching for a fifth verb, and Postern has four.
62
64
  - **A link goes only to a web address.** An address a runner's author wrote becomes a link only when it is an absolute `http` or `https` one, so a `javascript:` address in `describe` is never something a click runs.
63
65
 
64
66
  ## The composition
@@ -188,9 +190,44 @@ Each carries `"v": 1`. A block refuses a document in a version it does not read,
188
190
  - **A link is a web address.** `signup_url` must be an absolute `http` or `https` address, and `run_url` may also be a path on the host's page; anything else draws no link.
189
191
  - **Nothing is fetched.** A test spies on `fetch` while every block draws.
190
192
 
193
+ ## A playbook's contents
194
+
195
+ A playbook is a process a business runs by hand, sold as the files a person works through: the steps, the copy to send, the checklists, an offline tool or two. Its `playbook.json`, in the Muster format, says what each file is for and which placeholders a buyer fills in. This block draws that document, so a page can show what is inside a playbook to someone deciding whether to buy it, without handing over a file.
196
+
197
+ ```html
198
+ <link rel="stylesheet" href="gatehouse.css">
199
+ <div id="inside"></div>
200
+
201
+ <script src="playbook_contents.js"></script>
202
+ <script>
203
+ new Gatehouse.PlaybookContents(document.getElementById('inside'), playbookJson, {
204
+ fileNotes: { 'tools/reconcile.html': 'Its scripts run under a policy that blocks every network request.' },
205
+ }).render();
206
+ </script>
207
+ ```
208
+
209
+ | Constructor | Draws | Options |
210
+ | --- | --- | --- |
211
+ | `PlaybookContents(container, playbook, options)` | the files in `order`, each with its role, then the placeholders by when a buyer fills them in | `headingLevel` (2 to 6, default 3; the placeholders' kinds take the level below), `fileNotes` |
212
+
213
+ It has `render()` and `update(playbook)`, as the host blocks do. `Gatehouse.playbookData.readPlaybook` answers `{ value }` or `{ refusal }` as the block reads a document, so a host can test what it hands in.
214
+
215
+ ### The document
216
+
217
+ A playbook's own `playbook.json`, parsed, in format 0.x. The block reads `format_version`, `files` (each one's `path`, `role`, `same_as` and `self_contained`), `order` and the two lists of `slots`, `setup` and `message`, and ignores every other member, as a reader of the format may: a minor version only adds members an earlier reader can ignore, so any 0.x is drawn, and a major it does not know is refused whole, saying so.
218
+
219
+ - **The files, in `order`.** Each with its role in words (`guide`, `steps`, `checklist`, `copy`, `reference`, `tool`, `license`, `changelog`; a role it does not know is shown as spelled). A file that is another form of one (`same_as`) says which, and an `.html` file says it opens in a browser, and that it works offline only when the document calls it `self_contained`.
220
+ - **The placeholders, by when they are filled in.** `setup` once, for the buyer's business, and `message` each time the playbook is used, each token as the files write it beside its label. Two tokens one case apart, `[NAME]` and `[name]`, are two placeholders, and the block says so.
221
+ - **What it refuses.** A document in another major, or one whose `files`, `order` or `slots` are missing or not what the format says they are, or whose `order` and `same_as` name a file `files` does not list (paths match exactly, as in the format). It checks what drawing needs and no more: whether the files really say what the document claims is a checker's question, asked of the files, which the block never has.
222
+ - **`fileNotes`.** A sentence of the host's own about a file, keyed by its path and drawn beside it: something the host knows that the document does not say, such as what it found when it read the file. A note for a path the playbook does not hold is not drawn.
223
+
224
+ ### What it keeps
225
+
226
+ The host blocks' rules: every string reaches the page through `textContent`, no attribute is written from the document, and nothing is fetched. And one of its own: it never reads or draws a file's text, only what the document says the files are.
227
+
191
228
  ## Styling
192
229
 
193
- `gatehouse.css` styles every class the scripts draw, the run page's `run-*` and the host blocks' `gh-*`, and `tests/stylesheet.test.js` fails when one has no rule. Colours are custom properties on `.run-shell` and on `.gh-block`, the root of every host block: each `--run-*` reads a host's `--sx-*` token when the page defines one and falls back to its own value, so the file works alone and a host can theme it. A status colour comes as a fill (`--run-ok`, `--run-warn`, `--run-danger`) for borders and bars, and as an ink (`--run-ok-text` and the rest, from a host's `--sx-*-text`) for words. Dark values apply under `[data-theme="dark"]` on any ancestor.
230
+ `gatehouse.css` styles every class the scripts draw, the run page's `run-*` and the blocks' `gh-*`, and `tests/stylesheet.test.js` fails when one has no rule. Colours are custom properties on `.run-shell` and on `.gh-block`, the root of every block: each `--run-*` reads a host's `--sx-*` token when the page defines one and falls back to its own value, so the file works alone and a host can theme it. A status colour comes as a fill (`--run-ok`, `--run-warn`, `--run-danger`) for borders and bars, and as an ink (`--run-ok-text` and the rest, from a host's `--sx-*-text`) for words. Dark values apply under `[data-theme="dark"]` on any ancestor.
194
231
 
195
232
  ## Where it fits
196
233
 
@@ -200,6 +237,7 @@ Gatehouse is one of the open-source projects [Sigrix](https://sigrix.io) publish
200
237
  - **[sigrix-runtime](https://github.com/sigrix-io/sigrix-runtime)** (`pip install sigrix-runtime`), a runner to point it at: the one inside every bundle Sigrix delivers, which can also serve an MCP server's tools as one agent. Start it with `--allow-origin` set to the page's origin.
201
238
  - **[postern-conformance](https://pypi.org/project/postern-conformance/)** (`pip install postern-conformance`), which reports the level a runner really meets before you draw a page for it.
202
239
  - **[Bailey](https://github.com/sigrix-io/bailey)**, the open-source assistant: the app planned for its 0.1 draws a solution's apps with Gatehouse.
240
+ - **Muster**, the format a playbook's `playbook.json` is written in, which the playbook block reads. Its schema is at [sigrix.io/schemas/muster/0.1/playbook.schema.json](https://sigrix.io/schemas/muster/0.1/playbook.schema.json).
203
241
 
204
242
  Every project Sigrix publishes, and a map of how they connect: [sigrix.io/open-source](https://sigrix.io/open-source).
205
243
 
@@ -210,7 +248,7 @@ npm ci
210
248
  npm test
211
249
  ```
212
250
 
213
- The scripts are loaded the way a browser loads them, read and evaluated in order, rather than imported. The fixtures in `tests/fixtures/postern_describe/` are real documents: the Postern conformance fake's, a pipeline crew's, and an MCP server's as a runner builds it from a real `tools/list` answer. Those in `tests/fixtures/host_blocks/` are the three documents a host hands the blocks, one of each state and outcome among them.
251
+ The scripts are loaded the way a browser loads them, read and evaluated in order, rather than imported. The fixtures in `tests/fixtures/postern_describe/` are real documents: the Postern conformance fake's, a pipeline crew's, and an MCP server's as a runner builds it from a real `tools/list` answer. Those in `tests/fixtures/host_blocks/` are the three documents a host hands the blocks, one of each state and outcome among them, and `tests/fixtures/playbooks/` holds a `playbook.json` as an author writes one.
214
252
 
215
253
  ## Licence
216
254
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sigrix-io/gatehouse",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "A browser client for Postern: draws a run page from a describe document and connects it to the four verbs. No build step and no dependencies.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Sigrix",
@@ -15,6 +15,7 @@
15
15
  "agent",
16
16
  "mcp",
17
17
  "sse",
18
+ "playbook",
18
19
  "browser",
19
20
  "no-build"
20
21
  ],
@@ -29,6 +30,7 @@
29
30
  "./refusal_copy.js": "./src/refusal_copy.js",
30
31
  "./run_renderer.js": "./src/run_renderer.js",
31
32
  "./host_blocks.js": "./src/host_blocks.js",
33
+ "./playbook_contents.js": "./src/playbook_contents.js",
32
34
  "./gatehouse.css": "./src/gatehouse.css",
33
35
  "./package.json": "./package.json"
34
36
  },
@@ -39,7 +41,7 @@
39
41
  "test": "vitest run"
40
42
  },
41
43
  "devDependencies": {
42
- "jsdom": "30.1.1",
43
- "vitest": "5.0.3"
44
+ "jsdom": "30.1.3",
45
+ "vitest": "5.0.5"
44
46
  }
45
47
  }
package/src/gatehouse.css CHANGED
@@ -349,6 +349,43 @@ tbody tr:first-child > .gh-table-cell { border-top: 0; }
349
349
  .gh-days-table > .gh-table { margin-top: 8px; border: 1px solid var(--run-line); }
350
350
  .gh-days-table-toggle { cursor: pointer; color: var(--run-text-2); }
351
351
 
352
+ /* ---------- a playbook ----------
353
+ *
354
+ * Its files in order, then its placeholders. A path, a token and a label are
355
+ * someone's typing and may be one long word, so each wraps anywhere rather
356
+ * than pushing the block past a phone's edge. */
357
+ .gh-playbook { display: grid; gap: 22px; min-width: 0; }
358
+ .gh-playbook-part { display: grid; gap: 8px; min-width: 0; }
359
+ .gh-playbook-heading { font-size: 16px; font-weight: 650; letter-spacing: -0.01em; margin: 0; }
360
+ .gh-playbook-subheading { font-size: 12.5px; font-weight: 600; color: var(--run-text-2); margin: 8px 0 0; }
361
+ .gh-playbook-lead { font-size: 12.5px; line-height: 1.5; color: var(--run-text-2); margin: 0; max-width: 72ch; }
362
+ /* The browser's own numbers, so each file keeps its place in the order
363
+ * however its row wraps. A list item laid out as a flex row is drawn with no
364
+ * number, so the row's parts sit in a box of their own. */
365
+ .gh-playbook-files { margin: 0; padding: 0 0 0 1.8em; }
366
+ .gh-playbook-file { padding: 9px 0; border-top: 1px solid var(--run-line); min-width: 0; }
367
+ .gh-playbook-file:first-child { border-top: 0; }
368
+ .gh-playbook-file::marker { font-size: 12px; color: var(--run-text-3); font-variant-numeric: tabular-nums; }
369
+ .gh-playbook-file-body { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; min-width: 0; }
370
+ .gh-playbook-path { font-family: var(--run-mono); font-size: 12.5px; color: var(--run-text); min-width: 0; overflow-wrap: anywhere; }
371
+ .gh-playbook-role {
372
+ font-size: 11px; font-weight: 600; letter-spacing: 0.02em; color: var(--run-text-2); white-space: nowrap;
373
+ background: var(--run-surface-2); border: 1px solid var(--run-line); border-radius: 999px; padding: 1px 8px;
374
+ }
375
+ /* A sentence of its own under the path, on a line the flex row gives it. */
376
+ .gh-playbook-about, .gh-playbook-note { flex: 1 1 100%; font-size: 12px; line-height: 1.5; overflow-wrap: anywhere; }
377
+ .gh-playbook-about { color: var(--run-text-3); }
378
+ .gh-playbook-note { color: var(--run-text-2); }
379
+ .gh-playbook-slot-group { display: grid; gap: 6px; min-width: 0; }
380
+ .gh-playbook-slots { margin: 0; display: grid; gap: 6px; }
381
+ .gh-playbook-slot { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 10px; min-width: 0; }
382
+ .gh-playbook-slot-name { margin: 0; min-width: 0; }
383
+ .gh-playbook-token {
384
+ font-family: var(--run-mono); font-size: 12px; color: var(--run-text); overflow-wrap: anywhere;
385
+ background: var(--run-surface-2); border: 1px solid var(--run-line); border-radius: 6px; padding: 1px 6px;
386
+ }
387
+ .gh-playbook-slot-label { margin: 0; flex: 1 1 16ch; min-width: 0; font-size: 12.5px; line-height: 1.5; color: var(--run-text-2); overflow-wrap: anywhere; }
388
+
352
389
  [data-theme="dark"] .run-shell, [data-theme="dark"] .gh-block {
353
390
  --run-line: var(--sx-line, #232936);
354
391
  --run-surface: var(--sx-surface, #14181F);
@@ -0,0 +1,329 @@
1
+ /* What is inside a playbook: its files, in the order a person works through
2
+ * them, with what each one is for, and the placeholders a buyer fills in.
3
+ *
4
+ * A playbook is a process a business runs by hand, sold as the files a person
5
+ * works through, and described by a `playbook.json` in the Muster format. The
6
+ * host hands that document in, parsed, and the block draws it. There is
7
+ * nothing to run and nothing to fetch: the block never reads a file and never
8
+ * draws a file's text, only what the document says the files are, so a page
9
+ * may show a playbook's contents to someone who has not bought it.
10
+ *
11
+ * It reads format 0.x. A minor version only adds members an earlier reader can
12
+ * ignore, so any 0.x is read and a member this file does not name is ignored;
13
+ * a major version it does not know is refused, whole, saying why.
14
+ *
15
+ * **Nothing here uses `innerHTML`.** A path, a placeholder's label and a
16
+ * host's note are someone's typing, and all of it reaches the page through
17
+ * `textContent`. No attribute is written from the document.
18
+ */
19
+ (function (global) {
20
+ const root = global || (typeof window !== 'undefined' ? window : {});
21
+
22
+ /* The major version of the format this file reads. */
23
+ const FORMAT_MAJOR = 0;
24
+
25
+ /* What each role the format names is called here. A role this client has
26
+ * not heard of is shown as the document spells it, rather than guessed into
27
+ * one of these. */
28
+ const ROLES = {
29
+ guide: 'Guide',
30
+ steps: 'Steps',
31
+ checklist: 'Checklist',
32
+ copy: 'Copy',
33
+ reference: 'Reference',
34
+ tool: 'Tool',
35
+ license: 'License',
36
+ changelog: 'Changelog',
37
+ };
38
+
39
+ /* The kinds of placeholder the format names, by when a buyer fills them in,
40
+ * in the format's order. A kind a later minor adds is ignored, as the
41
+ * format lets a reader ignore what it does not know. */
42
+ const SLOT_KINDS = [
43
+ ['setup', 'Once, for your business'],
44
+ ['message', 'Each time you use it'],
45
+ ];
46
+
47
+ function el(tag, className, textContent) {
48
+ const node = document.createElement(tag);
49
+ if (className) node.className = className;
50
+ if (textContent !== undefined && textContent !== null) node.textContent = String(textContent);
51
+ return node;
52
+ }
53
+
54
+ function clear(node) {
55
+ while (node.firstChild) node.removeChild(node.firstChild);
56
+ }
57
+
58
+ function isObject(value) {
59
+ return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
60
+ }
61
+
62
+ function has(table, key) {
63
+ return Object.prototype.hasOwnProperty.call(table, key);
64
+ }
65
+
66
+ /* ---------- reading the document ----------
67
+ *
68
+ * The reader answers `{ value }` or `{ refusal }`, never both. A refusal is
69
+ * one sentence naming what was wrong, for whoever built the host's data. The
70
+ * block checks only what drawing needs: that the members it reads are what
71
+ * the format says they are, and that `order` and `same_as` name the files
72
+ * `files` lists, matched exactly, as the format matches them. Whether the
73
+ * files really say what the document claims is a checker's question, asked
74
+ * of the files, which this block never has. */
75
+
76
+ class Fault extends Error {}
77
+
78
+ function formatOf(doc) {
79
+ const value = doc.format_version;
80
+ if (value === undefined || value === null) {
81
+ throw new Fault('It carries no "format_version", and this client reads format ' + FORMAT_MAJOR + '.x.');
82
+ }
83
+ if (typeof value !== 'string' || !/^\d+\.\d+$/.test(value)) {
84
+ throw new Fault('"format_version" is not a version such as 0.1.');
85
+ }
86
+ if (Number(value.split('.')[0]) !== FORMAT_MAJOR) {
87
+ throw new Fault('It is in format ' + value + ', and this client reads format ' + FORMAT_MAJOR + '.x.');
88
+ }
89
+ return value;
90
+ }
91
+
92
+ function textMember(doc, key, required) {
93
+ const value = doc[key];
94
+ if (value === undefined || value === null) {
95
+ if (required) throw new Fault('"' + key + '" is missing.');
96
+ return '';
97
+ }
98
+ if (typeof value !== 'string') throw new Fault('"' + key + '" is not text.');
99
+ if (required && !value.trim()) throw new Fault('"' + key + '" is empty.');
100
+ return value;
101
+ }
102
+
103
+ function listMember(doc, key, where) {
104
+ const value = doc[key];
105
+ const name = where ? where + '.' + key : key;
106
+ if (value === undefined || value === null) throw new Fault('"' + name + '" is missing.');
107
+ if (!Array.isArray(value)) throw new Fault('"' + name + '" is not a list.');
108
+ return value;
109
+ }
110
+
111
+ function readFiles(doc) {
112
+ const entries = listMember(doc, 'files');
113
+ if (!entries.length) throw new Fault('"files" lists no file.');
114
+ const byPath = new Map();
115
+ entries.forEach(function (entry) {
116
+ if (!isObject(entry)) throw new Fault('"files" holds something other than a file.');
117
+ const path = textMember(entry, 'path', true);
118
+ if (byPath.has(path)) throw new Fault('"files" lists ' + path + ' twice.');
119
+ byPath.set(path, {
120
+ path,
121
+ role: textMember(entry, 'role', true),
122
+ sameAs: textMember(entry, 'same_as', false),
123
+ selfContained: entry.self_contained === true,
124
+ });
125
+ });
126
+ byPath.forEach(function (file) {
127
+ if (file.sameAs && (file.sameAs === file.path || !byPath.has(file.sameAs))) {
128
+ throw new Fault(file.path + ' is the same as ' + file.sameAs + ', which is not another file in "files".');
129
+ }
130
+ });
131
+
132
+ /* Drawn in `order`, which names every file once: the order a person works
133
+ * through them is part of what the playbook is. */
134
+ const seen = new Set();
135
+ const ordered = listMember(doc, 'order').map(function (path) {
136
+ if (typeof path !== 'string') throw new Fault('"order" holds something other than a path.');
137
+ if (!byPath.has(path)) throw new Fault('"order" names ' + path + ', which "files" does not.');
138
+ if (seen.has(path)) throw new Fault('"order" names ' + path + ' twice.');
139
+ seen.add(path);
140
+ return byPath.get(path);
141
+ });
142
+ byPath.forEach(function (file) {
143
+ if (!seen.has(file.path)) throw new Fault('"order" leaves out ' + file.path + '.');
144
+ });
145
+ return ordered;
146
+ }
147
+
148
+ function readSlots(doc) {
149
+ if (!isObject(doc.slots)) throw new Fault('"slots" is missing.');
150
+ return SLOT_KINDS.map(function (kind) {
151
+ const entries = listMember(doc.slots, kind[0], 'slots');
152
+ return {
153
+ kind: kind[0],
154
+ heading: kind[1],
155
+ slots: entries.map(function (entry) {
156
+ if (!isObject(entry)) throw new Fault('"slots.' + kind[0] + '" holds something other than a placeholder.');
157
+ return { placeholder: textMember(entry, 'placeholder', true), label: textMember(entry, 'label', true) };
158
+ }),
159
+ };
160
+ });
161
+ }
162
+
163
+ function readPlaybook(doc) {
164
+ try {
165
+ if (!isObject(doc)) throw new Fault('The document is not an object.');
166
+ const formatVersion = formatOf(doc);
167
+ return { value: { formatVersion, files: readFiles(doc), slotGroups: readSlots(doc) } };
168
+ } catch (err) {
169
+ if (err instanceof Fault) return { refusal: err.message };
170
+ throw err;
171
+ }
172
+ }
173
+
174
+ /* ---------- writing it down ---------- */
175
+
176
+ function headingLevel(options) {
177
+ const level = Number(options && options.headingLevel);
178
+ return Number.isInteger(level) && level >= 2 && level <= 6 ? level : 3;
179
+ }
180
+
181
+ function plural(count, one, many) {
182
+ return count + ' ' + (count === 1 ? one : many);
183
+ }
184
+
185
+ /* `a`, `a and b`, `a, b and c`. */
186
+ function and(items) {
187
+ return items.length === 1 ? items[0] : items.slice(0, -1).join(', ') + ' and ' + items[items.length - 1];
188
+ }
189
+
190
+ /* What a file adds to its role: the file it is another form of, and, for a
191
+ * page, that it opens in a browser and loads nothing. Only an `.html` file
192
+ * the document calls self-contained is said to work offline. */
193
+ function about(file) {
194
+ const parts = [];
195
+ if (file.sameAs) parts.push('Another form of ' + file.sameAs + '.');
196
+ if (/\.html$/i.test(file.path)) {
197
+ parts.push(file.selfContained ? 'Opens in a browser, and works offline.' : 'Opens in a browser.');
198
+ }
199
+ return parts.join(' ');
200
+ }
201
+
202
+ /* A sentence the host adds to one file, keyed by its path: something the
203
+ * host knows that the document does not say, such as what it found when it
204
+ * read the file. Drawn as text, beside the file it names; a note for a path
205
+ * the playbook does not hold is not drawn. */
206
+ function hostNote(options, path) {
207
+ const notes = options && options.fileNotes;
208
+ if (!isObject(notes) || !has(notes, path)) return '';
209
+ const note = notes[path];
210
+ return typeof note === 'string' ? note.trim() : '';
211
+ }
212
+
213
+ /* Two placeholders one case apart are two placeholders, which a reader
214
+ * replacing them by eye could take for one, so the block says so. */
215
+ function caseNote(groups) {
216
+ const folded = new Map();
217
+ groups.forEach(function (group) {
218
+ group.slots.forEach(function (slot) {
219
+ const key = slot.placeholder.toLowerCase();
220
+ if (!folded.has(key)) folded.set(key, []);
221
+ if (folded.get(key).indexOf(slot.placeholder) === -1) folded.get(key).push(slot.placeholder);
222
+ });
223
+ });
224
+ const pairs = [];
225
+ folded.forEach(function (spellings) {
226
+ if (spellings.length > 1) pairs.push(and(spellings) + ' are different placeholders');
227
+ });
228
+ return pairs.length ? 'Case matters: ' + pairs.join('; ') + '.' : '';
229
+ }
230
+
231
+ function filesPart(files, level, options) {
232
+ const part = el('div', 'gh-playbook-part');
233
+ part.appendChild(el('h' + level, 'gh-playbook-heading', 'The files'));
234
+ part.appendChild(el('p', 'gh-playbook-lead', plural(files.length, 'file', 'files') + ', in the order you work through them.'));
235
+ const list = el('ol', 'gh-playbook-files');
236
+ files.forEach(function (file) {
237
+ /* The row's parts sit in a box of their own, so the item stays a list
238
+ * item and keeps the number the browser gives it: an item laid out as
239
+ * a flex row is drawn with none. */
240
+ const item = el('li', 'gh-playbook-file');
241
+ const body = el('div', 'gh-playbook-file-body');
242
+ body.appendChild(el('code', 'gh-playbook-path', file.path));
243
+ body.appendChild(el('span', 'gh-playbook-role', has(ROLES, file.role) ? ROLES[file.role] : file.role));
244
+ const sentence = about(file);
245
+ if (sentence) body.appendChild(el('span', 'gh-playbook-about', sentence));
246
+ const note = hostNote(options, file.path);
247
+ if (note) body.appendChild(el('span', 'gh-playbook-note', note));
248
+ item.appendChild(body);
249
+ list.appendChild(item);
250
+ });
251
+ part.appendChild(list);
252
+ return part;
253
+ }
254
+
255
+ function placeholdersPart(groups, level) {
256
+ const part = el('div', 'gh-playbook-part');
257
+ part.appendChild(el('h' + level, 'gh-playbook-heading', 'The placeholders'));
258
+ const total = groups.reduce((sum, group) => sum + group.slots.length, 0);
259
+ if (!total) {
260
+ part.appendChild(el('p', 'gh-playbook-lead', 'None. Every file is used as it is.'));
261
+ return part;
262
+ }
263
+ part.appendChild(el('p', 'gh-playbook-lead', total === 1
264
+ ? 'One placeholder, in square brackets. Replace it with your own words wherever it appears.'
265
+ : total + ' placeholders, each in square brackets. Replace each with your own words wherever it appears.'));
266
+ const note = caseNote(groups);
267
+ if (note) part.appendChild(el('p', 'gh-playbook-lead', note));
268
+ /* Hoisted rather than written in the call, so the stylesheet's test, which
269
+ * reads the class beside the first comma, sees this one too. */
270
+ const subheading = 'h' + Math.min(level + 1, 6);
271
+ groups.forEach(function (group) {
272
+ if (!group.slots.length) return;
273
+ const section = el('div', 'gh-playbook-slot-group');
274
+ section.dataset.slotKind = group.kind;
275
+ section.appendChild(el(subheading, 'gh-playbook-subheading', group.heading));
276
+ const list = el('dl', 'gh-playbook-slots');
277
+ group.slots.forEach(function (slot) {
278
+ const row = el('div', 'gh-playbook-slot');
279
+ const name = el('dt', 'gh-playbook-slot-name');
280
+ name.appendChild(el('code', 'gh-playbook-token', slot.placeholder));
281
+ row.appendChild(name);
282
+ row.appendChild(el('dd', 'gh-playbook-slot-label', slot.label));
283
+ list.appendChild(row);
284
+ });
285
+ section.appendChild(list);
286
+ part.appendChild(section);
287
+ });
288
+ return part;
289
+ }
290
+
291
+ class PlaybookContents {
292
+ /* `playbook` is a playbook's `playbook.json`, parsed. `options.headingLevel`
293
+ * (2 to 6, default 3) is the level of the block's two headings, and the
294
+ * placeholders' kinds take the level below. `options.fileNotes` maps a
295
+ * file's path to a sentence of the host's own, drawn beside that file. */
296
+ constructor(container, playbook, options) {
297
+ this.container = container;
298
+ this.playbook = playbook;
299
+ this.options = options || {};
300
+ }
301
+
302
+ render() {
303
+ clear(this.container);
304
+ const block = el('section', 'gh-block gh-playbook');
305
+ this.container.appendChild(block);
306
+ const read = readPlaybook(this.playbook);
307
+ if (read.refusal) {
308
+ block.appendChild(el('p', 'gh-refuse', 'This playbook could not be shown. ' + read.refusal));
309
+ return;
310
+ }
311
+ const level = headingLevel(this.options);
312
+ block.appendChild(filesPart(read.value.files, level, this.options));
313
+ block.appendChild(placeholdersPart(read.value.slotGroups, level));
314
+ }
315
+
316
+ update(playbook) {
317
+ this.playbook = playbook;
318
+ this.render();
319
+ }
320
+ }
321
+
322
+ root.Gatehouse = Object.assign({}, root.Gatehouse, {
323
+ PlaybookContents,
324
+ playbookData: { FORMAT_MAJOR, ROLES: Object.keys(ROLES), readPlaybook },
325
+ });
326
+
327
+ /* The deprecated alias the other scripts keep; see `host_blocks.js`. */
328
+ root.SigrixRun = root.Gatehouse;
329
+ })(typeof window !== 'undefined' ? window : globalThis);