@jonandev/longbeard 0.5.2 → 0.6.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.
@@ -0,0 +1,301 @@
1
+ #!/usr/bin/env node
2
+ // The adaptation, with this project's own words in it. Paper and night side
3
+ // by side, at reading size, on a page and on a phone. It is not the product
4
+ // and it is not a page build: no navigation, no feature, no layout invented
5
+ // here. Everything on it comes from Read this.
6
+ //
7
+ // node proof.mjs .design/board.md --out .design/proof.html
8
+ // node proof.mjs .design/board.md --out .design/proof.html \
9
+ // --name "Your Project" --sample PROJECT.md --meta "Draft · 1,240 words · edited today"
10
+ import { execSync } from "node:child_process";
11
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
12
+ import { basename, dirname, isAbsolute, resolve } from "node:path";
13
+
14
+ const args = process.argv.slice(2);
15
+ const flag = (name) => {
16
+ const i = args.indexOf(`--${name}`);
17
+ if (i === -1) return "";
18
+ const v = args[i + 1] ?? "";
19
+ args.splice(i, 2);
20
+ return v;
21
+ };
22
+
23
+ const outArg = flag("out");
24
+ const nameArg = flag("name");
25
+ const sampleArg = flag("sample");
26
+ const metaArg = flag("meta");
27
+ const boardArg = args[0];
28
+ if (!boardArg || !outArg || args.length !== 1) {
29
+ console.error(
30
+ 'usage: node proof.mjs .design/board.md --out .design/proof.html [--name "Project"] [--sample README.md] [--meta "Draft · 1,240 words"]',
31
+ );
32
+ process.exit(2);
33
+ }
34
+
35
+ function projectRoot() {
36
+ try {
37
+ return execSync("git rev-parse --show-toplevel", { encoding: "utf8" }).trim();
38
+ } catch {
39
+ return process.cwd();
40
+ }
41
+ }
42
+
43
+ const root = projectRoot();
44
+ const resolvePath = (path) => (!path ? "" : isAbsolute(path) ? path : resolve(root, path));
45
+ const boardPath = resolvePath(boardArg);
46
+ const outPath = resolvePath(outArg);
47
+ if (!existsSync(boardPath)) {
48
+ console.error(`proof: no board at ${boardArg}`);
49
+ process.exit(1);
50
+ }
51
+
52
+ const esc = (s) =>
53
+ String(s ?? "")
54
+ .replace(/&/g, "&")
55
+ .replace(/</g, "&lt;")
56
+ .replace(/>/g, "&gt;")
57
+ .replace(/"/g, "&quot;");
58
+
59
+ function fieldsOf(text) {
60
+ const fields = {};
61
+ let key = null;
62
+ for (const line of text.split(/\r?\n/)) {
63
+ const m = line.match(/^- ([a-z][a-z /-]*):\s*(.*)$/i);
64
+ if (m) {
65
+ key = m[1].toLowerCase();
66
+ fields[key] = m[2].trim();
67
+ continue;
68
+ }
69
+ if (key && /^\s+\S/.test(line)) fields[key] = `${fields[key]} ${line.trim()}`.trim();
70
+ }
71
+ return fields;
72
+ }
73
+
74
+ const md = readFileSync(boardPath, "utf8");
75
+ const readSlice = md.match(/## Read this\s*\n([\s\S]*?)(?=\n## |\n### |\n- kind:|$)/);
76
+ if (!readSlice) {
77
+ console.error("proof: the board has no Read this. Write the adaptation first.");
78
+ process.exit(1);
79
+ }
80
+ const read = fieldsOf(readSlice[1]);
81
+ if (!read.colour) {
82
+ console.error("proof: Read this has no colour line.");
83
+ process.exit(1);
84
+ }
85
+
86
+ const lum = (hex) => {
87
+ const [r, g, b] = [1, 3, 5]
88
+ .map((i) => parseInt(hex.slice(i, i + 2), 16) / 255)
89
+ .map((c) => (c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4));
90
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
91
+ };
92
+ const ratio = (a, b) => {
93
+ const [x, y] = [lum(a), lum(b)];
94
+ return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05);
95
+ };
96
+ const verdict = (r, large) => (r >= 7 && !large ? "AAA" : r >= (large ? 3 : 4.5) ? "AA" : "FAIL");
97
+ const expand = (hex) => {
98
+ const h = hex.replace("#", "");
99
+ return `#${(h.length === 3 ? h.replace(/./g, (c) => c + c) : h).toLowerCase()}`;
100
+ };
101
+
102
+ // Each hex keeps the words that follow it, so a role can say "night ground".
103
+ function swatches(line) {
104
+ const found = [];
105
+ const re = /#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})(?:\s*(?:→|->)\s*#([0-9a-fA-F]{6}|[0-9a-fA-F]{3}))?/g;
106
+ let m;
107
+ while ((m = re.exec(line))) found.push({ index: m.index, end: re.lastIndex, hex: expand(m[2] || m[1]) });
108
+ const out = [];
109
+ for (let i = 0; i < found.length; i++) {
110
+ const end = i + 1 < found.length ? found[i + 1].index : line.length;
111
+ const label = line
112
+ .slice(found[i].end, end)
113
+ .replace(/^[\s,;:–\-."']+|[\s,;:–\-."']+$/g, "")
114
+ .replace(/\s+/g, " ")
115
+ .trim();
116
+ if (!out.some((s) => s.hex === found[i].hex)) out.push({ hex: found[i].hex, label });
117
+ }
118
+ return out;
119
+ }
120
+
121
+ const colours = swatches(read.colour);
122
+ if (!colours.length) {
123
+ console.error("proof: no hexes on the colour line.");
124
+ process.exit(1);
125
+ }
126
+
127
+ const nightish = (s) => /night|dark|dusk|lamp|evening/i.test(s.label);
128
+ const byLight = [...colours].sort((a, b) => lum(b.hex) - lum(a.hex));
129
+ const night = colours.filter(nightish);
130
+ const dayPool = colours.filter((s) => !nightish(s));
131
+
132
+ function theme(pool, dark) {
133
+ const list = (pool.length ? pool : colours).slice();
134
+ const sorted = [...list].sort((a, b) => lum(b.hex) - lum(a.hex));
135
+ const ground = dark ? sorted[sorted.length - 1] : sorted[0];
136
+ const text = dark ? sorted[0] : sorted[sorted.length - 1];
137
+ // The accent is the hex furthest from both, so a cream-and-ink pair falls
138
+ // back to the ink rather than inventing a colour.
139
+ const rest = list.filter((s) => s.hex !== ground.hex && s.hex !== text.hex);
140
+ const accent =
141
+ rest.sort((a, b) => Math.abs(lum(a.hex) - lum(ground.hex)) - Math.abs(lum(b.hex) - lum(ground.hex))).pop() || text;
142
+ return { ground, text, accent };
143
+ }
144
+
145
+ const day = theme(dayPool, false);
146
+ const dusk = theme(night.length ? night : byLight, true);
147
+
148
+ function isFaceName(name) {
149
+ const words = name.split(/\s+/);
150
+ return (
151
+ /^[\p{L}\p{N}][\p{L}\p{N} .'’&-]*$/u.test(name) &&
152
+ words.length <= 4 &&
153
+ !/^(body|headline|display|serif|sans|sans-serif|mono|monospace|text|titles?|labels?|small caps)$/i.test(name)
154
+ );
155
+ }
156
+
157
+ const faces = (read.faces || read.type || "")
158
+ .split(/,|\s+and\s+|\s+&\s+|\s+\+\s+|\s+with\s+/i)
159
+ .map((s) => s.replace(/\s+for\s+.*/i, "").replace(/^(a pairing of|pairing of|the)\s+/i, "").trim())
160
+ .filter(isFaceName)
161
+ .slice(0, 3);
162
+
163
+ const serifFallback = "'Iowan Old Style', Palatino, 'Palatino Linotype', Georgia, serif";
164
+ const sansFallback = "'Helvetica Neue', Helvetica, Arial, sans-serif";
165
+ const isSans = (name) =>
166
+ /sans|grotesk|grotesque|gothic|america|aeonik|inter|helvetica|neue|haas|sohne|söhne|geist|satoshi|switzer|untitled|plex sans|mono/i.test(
167
+ name,
168
+ );
169
+ const family = (name) => (!name ? serifFallback : `'${name.replace(/['"\\<>;{}]/g, "")}', ${isSans(name) ? sansFallback : serifFallback}`);
170
+ const fontsHref = () => {
171
+ const list = faces
172
+ .filter((n) => /^[A-Za-z0-9][A-Za-z0-9 ]*$/.test(n))
173
+ .map((n) => `family=${n.trim().replace(/\s+/g, "+")}`);
174
+ return list.length ? `https://fonts.googleapis.com/css2?${list.join("&")}&display=swap` : "";
175
+ };
176
+ const bodyFace = family(faces.find((f) => !isSans(f)) || faces[0] || "");
177
+ const metaFace = family(faces.find((f) => isSans(f)) || faces[1] || faces[0] || "");
178
+
179
+ // The words are this project's, so the direction is judged on real text.
180
+ const projectName = nameArg || basename(root);
181
+ function sampleText() {
182
+ const files = sampleArg ? [sampleArg] : ["PRODUCT.md", "PROJECT.md", "README.md"];
183
+ for (const file of files) {
184
+ const path = resolvePath(file);
185
+ if (!existsSync(path)) continue;
186
+ const plain = readFileSync(path, "utf8")
187
+ .replace(/```[\s\S]*?```/g, "")
188
+ .split(/\r?\n/)
189
+ .filter((l) => l.trim() && !/^[#>\-*|!\[]/.test(l.trim()) && !/^\s*\d+\./.test(l))
190
+ .map((l) => l.trim());
191
+ const para = plain.find((l) => l.length > 120);
192
+ if (para) return { text: para.replace(/\[([^\]]+)\]\([^)]*\)/g, "$1").replace(/[*_`]/g, ""), from: file };
193
+ }
194
+ return {
195
+ text:
196
+ "No paragraph was found in this repository, so this placeholder stands in. Replace it with --sample <file> and look again: the direction is only proved by the project's own words at reading size.",
197
+ from: "placeholder",
198
+ };
199
+ }
200
+ const sample = sampleText();
201
+ const meta = metaArg || "Draft · 1,240 words · edited today";
202
+ const gaps = (read.gaps || "").trim();
203
+ const ownable = (read.ownable || "").trim();
204
+ const control = (read.control || "").trim();
205
+
206
+ function panel(name, t, large) {
207
+ const pairs = [
208
+ { label: "body on ground", r: ratio(t.text.hex, t.ground.hex), large: false },
209
+ { label: "accent on ground", r: ratio(t.accent.hex, t.ground.hex), large: true },
210
+ ];
211
+ const checks = pairs
212
+ .map((p) => {
213
+ const v = verdict(p.r, p.large);
214
+ return `<li class="${v === "FAIL" ? "fail" : "pass"}">${esc(p.label)} ${p.r.toFixed(2)}:1 ${v}</li>`;
215
+ })
216
+ .join("");
217
+ return `<section class="panel${large ? " wide" : " phone"}" style="--ground:${esc(t.ground.hex)};--text:${esc(t.text.hex)};--accent:${esc(t.accent.hex)}">
218
+ <header class="panel-head"><p class="kicker">${esc(name)}</p><ul class="checks">${checks}</ul></header>
219
+ <div class="page">
220
+ <p class="eyebrow">${esc(projectName)}</p>
221
+ <h2 class="piece">${esc(sample.from === "placeholder" ? projectName : `${projectName}, in its own words`)}</h2>
222
+ <p class="meta">${esc(meta)}</p>
223
+ <p class="body">${esc(sample.text)}</p>
224
+ <div class="row"><span class="row-title">An earlier draft</span><span class="row-meta">${esc(meta)}</span></div>
225
+ <div class="row"><span class="row-title">The first page</span><span class="row-meta">${esc(meta)}</span></div>
226
+ ${control ? `<p class="control">${esc(control)}</p>` : ""}
227
+ </div>
228
+ </section>`;
229
+ }
230
+
231
+ const fonts = fontsHref();
232
+ const html = `<!DOCTYPE html>
233
+ <html lang="en">
234
+ <head>
235
+ <meta charset="utf-8">
236
+ <meta name="viewport" content="width=device-width, initial-scale=1">
237
+ <title>Proof</title>
238
+ ${fonts ? `<link rel="stylesheet" href="${esc(fonts)}">\n` : ""}<style>
239
+ :root {
240
+ --body: ${bodyFace};
241
+ --meta: ${metaFace};
242
+ --ui: "Helvetica Neue", Helvetica, Arial, sans-serif;
243
+ --canvas: #e4e4e2;
244
+ --muted: #6f6f6a;
245
+ }
246
+ * { box-sizing: border-box; }
247
+ html { color-scheme: light; }
248
+ body { margin: 0; background: var(--canvas); color: #1c1c1a; font-family: var(--ui); -webkit-font-smoothing: antialiased; }
249
+ .sheet { max-width: 1200px; margin: 0 auto; padding: 52px 28px 80px; }
250
+ h1 { margin: 0 0 6px 2px; font-size: 11px; font-weight: 600; letter-spacing: 0.18em; text-transform: uppercase; color: var(--muted); }
251
+ .lead { margin: 0 0 24px 2px; max-width: 70ch; font-size: 14px; line-height: 1.5; color: var(--muted); }
252
+ .grid { display: grid; grid-template-columns: 1fr 390px; gap: 22px; align-items: start; }
253
+ .panel { border-radius: 22px; overflow: hidden; background: var(--ground); color: var(--text); }
254
+ .panel-head { display: flex; justify-content: space-between; gap: 16px; padding: 18px 24px; background: rgba(127, 127, 127, 0.14); }
255
+ .kicker { margin: 0; font-family: var(--ui); font-size: 11px; font-weight: 600; letter-spacing: 0.16em; text-transform: uppercase; opacity: 0.7; }
256
+ .checks { display: flex; gap: 14px; margin: 0; padding: 0; list-style: none; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 11px; opacity: 0.8; }
257
+ .checks .fail { font-weight: 700; text-decoration: underline; }
258
+ .page { padding: 34px 32px 38px; }
259
+ .eyebrow, .meta, .row-meta { font-family: var(--meta); font-size: 12px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--accent); }
260
+ .eyebrow { margin: 0 0 22px; }
261
+ .piece { margin: 0; font-family: var(--body); font-size: 34px; line-height: 1.12; letter-spacing: -0.02em; font-weight: 500; }
262
+ .meta { margin: 10px 0 24px; }
263
+ .body { margin: 0; max-width: 66ch; font-family: var(--body); font-size: 17px; line-height: 1.62; }
264
+ .row { display: flex; justify-content: space-between; align-items: baseline; gap: 16px; margin-top: 18px; padding-top: 14px; border-top: 1px solid currentColor; opacity: 0.85; }
265
+ .row:first-of-type { margin-top: 34px; }
266
+ .row-title { font-family: var(--body); font-size: 17px; }
267
+ .control { margin: 30px 0 0; font-family: var(--meta); font-size: 14px; color: var(--accent); }
268
+ .phone .page { padding: 26px 22px 30px; }
269
+ .phone .row { flex-direction: column; align-items: flex-start; gap: 4px; }
270
+ .phone .piece { font-size: 26px; }
271
+ .phone .body { font-size: 17px; }
272
+ .notes { margin-top: 26px; display: grid; gap: 10px; }
273
+ .note { margin: 0; max-width: 80ch; font-size: 14px; line-height: 1.5; color: #3a3a38; }
274
+ .note b { font-weight: 600; }
275
+ @media (max-width: 900px) {
276
+ .sheet { padding: 28px 16px 48px; }
277
+ .grid { grid-template-columns: 1fr; }
278
+ }
279
+ </style>
280
+ </head>
281
+ <body>
282
+ <main class="sheet">
283
+ <h1>Proof</h1>
284
+ <p class="lead">The adaptation with ${esc(projectName)}'s own words in it, at reading size. Paper on the left, night on the right, the phone column at 390px. Text from <code>${esc(sample.from)}</code>. This is not the product: no navigation, no feature, nothing invented here.</p>
285
+ <div class="grid">
286
+ ${panel("Paper", day, true)}
287
+ ${panel("Night", dusk, false)}
288
+ </div>
289
+ <div class="notes">
290
+ <p class="note"><b>Faces:</b> ${faces.length ? esc(faces.join(", ")) : "none named in Read this"}. ${faces.length > 2 ? "Three families. Can this be done with fewer?" : "Can this be done with fewer?"}</p>
291
+ ${ownable ? `<p class="note"><b>Ownable:</b> ${esc(ownable)}</p>` : ""}
292
+ ${gaps ? `<p class="note"><b>Not covered by a reference:</b> ${esc(gaps)}</p>` : ""}
293
+ </div>
294
+ </main>
295
+ </body>
296
+ </html>
297
+ `;
298
+
299
+ mkdirSync(dirname(outPath), { recursive: true });
300
+ writeFileSync(outPath, html);
301
+ console.log(outArg);