@jonandev/longbeard 0.5.1 → 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,520 @@
1
+ #!/usr/bin/env node
2
+ // A moodboard of the references. The layout is this file.
3
+ // It shows what we saw. It does not paint a brand, a logo, a line, or a stat.
4
+ //
5
+ // Before the pick (so they choose by looking):
6
+ // node moodboard.mjs .design/board.md --out .design/moodboard.html
7
+ //
8
+ // After the pick (marks the lead, keeps the rest):
9
+ // node moodboard.mjs .design/board.md --out .design/moodboard.html \
10
+ // --leads "<card name>" --also "<card name>,<card name>" --why "<their reason>"
11
+ //
12
+ // --also takes every card they also liked. --count is how many references
13
+ // they asked to see. --imagery adds the board's imagery tiles; leave it off
14
+ // when the look has no pictures in it.
15
+ import { execSync } from "node:child_process";
16
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
17
+ import { dirname, isAbsolute, relative, resolve } from "node:path";
18
+
19
+ const args = process.argv.slice(2);
20
+ const flag = (name) => {
21
+ const i = args.indexOf(`--${name}`);
22
+ if (i === -1) return "";
23
+ const v = args[i + 1] ?? "";
24
+ args.splice(i, 2);
25
+ return v;
26
+ };
27
+ const has = (name) => {
28
+ const i = args.indexOf(`--${name}`);
29
+ if (i === -1) return false;
30
+ args.splice(i, 1);
31
+ return true;
32
+ };
33
+
34
+ const outArg = flag("out");
35
+ const leadsArg = flag("leads");
36
+ const alsoArg = flag("also");
37
+ const whyArg = flag("why");
38
+ const countArg = flag("count");
39
+ const showImagery = has("imagery");
40
+ const boardArg = args[0];
41
+ if (!boardArg || !outArg || args.length !== 1) {
42
+ console.error(
43
+ 'usage: node moodboard.mjs .design/board.md --out .design/moodboard.html [--leads "Name"] [--also "A,B"] [--why "their reason"] [--count 12] [--imagery]',
44
+ );
45
+ process.exit(2);
46
+ }
47
+
48
+ function projectRoot() {
49
+ try {
50
+ return execSync("git rev-parse --show-toplevel", { encoding: "utf8" }).trim();
51
+ } catch {
52
+ return process.cwd();
53
+ }
54
+ }
55
+
56
+ function resolvePath(path) {
57
+ if (!path) return "";
58
+ if (isAbsolute(path)) return path;
59
+ return resolve(projectRoot(), path);
60
+ }
61
+
62
+ const boardPath = resolvePath(boardArg);
63
+ const outPath = resolvePath(outArg);
64
+ if (!existsSync(boardPath)) {
65
+ console.error(`moodboard: no board at ${boardArg}`);
66
+ process.exit(1);
67
+ }
68
+
69
+ const esc = (s) =>
70
+ String(s ?? "")
71
+ .replace(/&/g, "&amp;")
72
+ .replace(/</g, "&lt;")
73
+ .replace(/>/g, "&gt;")
74
+ .replace(/"/g, "&quot;");
75
+
76
+ const norm = (s) => s.trim().toLowerCase().replace(/\s+/g, " ").replace(/\.$/, "");
77
+
78
+ function fieldsOf(text) {
79
+ const fields = {};
80
+ let key = null;
81
+ for (const line of text.split(/\r?\n/)) {
82
+ const m = line.match(/^- ([a-z][a-z /-]*):\s*(.*)$/i);
83
+ if (m) {
84
+ key = m[1].toLowerCase();
85
+ fields[key] = m[2].trim();
86
+ continue;
87
+ }
88
+ if (key && /^\s+\S/.test(line)) fields[key] = `${fields[key]} ${line.trim()}`.trim();
89
+ }
90
+ return fields;
91
+ }
92
+
93
+ function lum(hex) {
94
+ const [r, g, b] = [0, 2, 4]
95
+ .map((i) => parseInt(hex.slice(i + 1, i + 3), 16) / 255)
96
+ .map((c) => (c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4));
97
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
98
+ }
99
+
100
+ function expand(hex) {
101
+ const h = hex.replace("#", "");
102
+ const full = h.length === 3 ? h.replace(/./g, (c) => c + c) : h;
103
+ return `#${full.toLowerCase()}`;
104
+ }
105
+
106
+ function swatches(colour) {
107
+ const found = [];
108
+ 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;
109
+ let m;
110
+ while ((m = re.exec(colour))) found.push({ index: m.index, end: re.lastIndex, hex: expand(m[2] || m[1]) });
111
+ const out = [];
112
+ for (let i = 0; i < found.length; i++) {
113
+ const end = i + 1 < found.length ? found[i + 1].index : colour.length;
114
+ const label = colour
115
+ .slice(found[i].end, end)
116
+ .replace(/^[\s,;:–\-."']+|[\s,;:–\-."']+$/g, "")
117
+ .replace(/\s+/g, " ")
118
+ .trim();
119
+ if (!out.some((s) => s.hex === found[i].hex)) out.push({ hex: found[i].hex, label });
120
+ }
121
+ return out.slice(0, 8);
122
+ }
123
+
124
+ // `faces:` is a list of names. `type:` is prose, so it is only a fallback and
125
+ // anything that reads like a sentence is dropped rather than shown as a face.
126
+ function facesOf(card) {
127
+ const listed = (card.faces || "")
128
+ .split(/,|\s+and\s+|\s+&\s+|\s+\+\s+/i)
129
+ .map((s) => s.trim())
130
+ .filter(Boolean);
131
+ if (listed.length) return listed.filter(isFaceName).slice(0, 3);
132
+ return (card.type || "")
133
+ .split(/,|\s+and\s+|\s+&\s+|\s+\+\s+/i)
134
+ .map((s) => s.replace(/\s+for\s+.*/i, "").replace(/^(a pairing of|pairing of|the)\s+/i, "").trim())
135
+ .filter(isFaceName)
136
+ .slice(0, 2);
137
+ }
138
+
139
+ function isFaceName(name) {
140
+ const words = name.split(/\s+/);
141
+ return (
142
+ /^[\p{L}\p{N}][\p{L}\p{N} .'’&-]*$/u.test(name) &&
143
+ words.length <= 4 &&
144
+ !/^(body|headline|display|serif|sans|sans-serif|mono|monospace|text|titles?|labels?)$/i.test(name)
145
+ );
146
+ }
147
+
148
+ function isSans(name) {
149
+ return /sans|grotesk|grotesque|gothic|america|aeonik|inter|helvetica|neue|haas|sohne|söhne|geist|satoshi|switzer|untitled/i.test(
150
+ name,
151
+ );
152
+ }
153
+
154
+ function cssFamily(name) {
155
+ const serif = "'Iowan Old Style', Palatino, 'Palatino Linotype', Georgia, serif";
156
+ if (!name) return serif;
157
+ // Single quotes, so the value is safe inside a style="" attribute once escaped.
158
+ const safe = name.replace(/['"\\<>;{}]/g, "").slice(0, 80);
159
+ const fallback = isSans(name) ? "'Helvetica Neue', Helvetica, Arial, sans-serif" : serif;
160
+ return `'${safe}', ${fallback}`;
161
+ }
162
+
163
+ // Optimistic: a face that is on Google Fonts renders for real, one that is not
164
+ // falls back. No axes are requested, so a family without 500 still resolves.
165
+ function fontsHref(names) {
166
+ const families = names
167
+ .filter((n) => /^[A-Za-z0-9][A-Za-z0-9 ]*$/.test(n))
168
+ .map((n) => `family=${n.trim().replace(/\s+/g, "+")}`);
169
+ if (!families.length) return "";
170
+ return `https://fonts.googleapis.com/css2?${families.join("&")}&display=swap`;
171
+ }
172
+
173
+ function webPath(fromFile, absTarget) {
174
+ let rel = relative(dirname(fromFile), absTarget).split("\\").join("/");
175
+ if (!rel.startsWith(".")) rel = `./${rel}`;
176
+ return rel;
177
+ }
178
+
179
+ function shotSrc(shot) {
180
+ if (!shot) return "";
181
+ const abs = resolvePath(shot.trim());
182
+ if (!existsSync(abs)) {
183
+ console.error(`moodboard: missing shot ${shot.trim()}`);
184
+ return "";
185
+ }
186
+ return webPath(outPath, abs);
187
+ }
188
+
189
+ function href(url) {
190
+ const value = (url || "").trim().split(/\s+/)[0] || "";
191
+ return /^https?:\/\//i.test(value) ? value : "";
192
+ }
193
+
194
+ const md = readFileSync(boardPath, "utf8");
195
+ const readSlice = md.match(/## Read this\s*\n([\s\S]*?)(?=\n## |\n### |\n- kind:|$)/);
196
+ const read = readSlice ? fieldsOf(readSlice[1]) : {};
197
+
198
+ const cards = [];
199
+ const chunks = md.split(/^### /m).slice(1);
200
+ for (const chunk of chunks) {
201
+ const nl = chunk.indexOf("\n");
202
+ const name = (nl === -1 ? chunk : chunk.slice(0, nl)).trim();
203
+ const body = nl === -1 ? "" : chunk.slice(nl + 1);
204
+ const cut = body.search(/^- kind:/m);
205
+ cards.push({ name, ...fieldsOf((cut === -1 ? body : body.slice(0, cut)).split(/^## /m)[0]) });
206
+ }
207
+
208
+ const leadsName = leadsArg || read.leads || read.primary || "";
209
+ const primary = leadsName ? cards.find((c) => norm(c.name) === norm(leadsName)) : null;
210
+ if (leadsName && !primary) {
211
+ console.error(`moodboard: no card named ${leadsName}`);
212
+ process.exit(1);
213
+ }
214
+ // --also takes every card they said they liked, not only one.
215
+ const alsoNames = (alsoArg || (read.also && !/^none$/i.test(read.also.trim()) ? read.also : ""))
216
+ .split(",")
217
+ .map((s) => s.trim())
218
+ .filter(Boolean);
219
+ const alsoCards = [];
220
+ for (const name of alsoNames) {
221
+ const card = cards.find((c) => norm(c.name) === norm(name));
222
+ if (card) alsoCards.push(card);
223
+ else console.error(`moodboard: no card named ${name}`);
224
+ }
225
+ const also = alsoCards[0] || null;
226
+
227
+ const imagery = [];
228
+ {
229
+ let inCard = false;
230
+ let cur = null;
231
+ const push = () => {
232
+ if (cur?.kind === "imagery" && cur.shot) imagery.push(cur);
233
+ cur = null;
234
+ };
235
+ for (const line of md.split(/\r?\n/)) {
236
+ if (line.startsWith("### ")) {
237
+ inCard = true;
238
+ push();
239
+ continue;
240
+ }
241
+ if (line.startsWith("## ")) {
242
+ inCard = false;
243
+ push();
244
+ continue;
245
+ }
246
+ const m = line.match(/^- ([a-z][a-z /-]*):\s*(.*)$/i);
247
+ if (m && m[1].toLowerCase() === "kind") {
248
+ inCard = false;
249
+ push();
250
+ cur = { kind: m[2].trim().toLowerCase() };
251
+ continue;
252
+ }
253
+ if (inCard) continue;
254
+ if (cur && m) cur[m[1].toLowerCase()] = m[2].trim();
255
+ else if (cur && line.trim() === "") push();
256
+ }
257
+ push();
258
+ }
259
+
260
+ // Screens and sites close to this subject. A product page cropped into a tile
261
+ // reads as noise, and a `form` card is a layout note, not a picture to feel.
262
+ // --count is how many references they said they wanted to see.
263
+ const MAX_PICTURES = Math.max(2, Math.min(24, Number(countArg) || 12));
264
+ const boardHasSlots = cards.some((c) => c.slot);
265
+ function showable(card) {
266
+ if (!card.shot) return false;
267
+ if (boardHasSlots && /product/i.test(card.slot || "")) return false;
268
+ if (/^form$/i.test((card.fit || "").trim())) return false;
269
+ return true;
270
+ }
271
+
272
+ const chosen = [];
273
+ const add = (card, role) => {
274
+ if (!card || chosen.some((c) => c.card === card)) return;
275
+ chosen.push({ card, role });
276
+ };
277
+ if (primary) add(primary, "leads");
278
+ for (const card of alsoCards) add(card, "also");
279
+ for (const card of cards) {
280
+ if (chosen.length >= MAX_PICTURES) break;
281
+ if (showable(card)) add(card, "");
282
+ }
283
+ const pictures = chosen
284
+ .filter((c) => c.role || showable(c.card))
285
+ .flatMap(({ card, role }) => {
286
+ const src = shotSrc(card.shot);
287
+ if (!src) return [];
288
+ const caption = role === "leads" && whyArg.trim() ? whyArg.trim() : (card.tagline || "").trim();
289
+ return [{ src, role, name: card.name, caption, url: href(card.url) }];
290
+ })
291
+ .slice(0, MAX_PICTURES);
292
+
293
+ if (showImagery && imagery.length > 4) console.error(`moodboard: showing 4 of ${imagery.length} imagery`);
294
+ const picturesOfPlace = showImagery
295
+ ? imagery.slice(0, 4).flatMap((item) => {
296
+ const src = shotSrc(item.shot);
297
+ return src ? [{ src, caption: (item.shows || "").trim(), url: href(item.url) }] : [];
298
+ })
299
+ : [];
300
+
301
+ // Before the pick there is no lead yet, so the type and the colour tiles read
302
+ // the cards on show instead.
303
+ const onShow = pictures.map((pic) => cards.find((c) => c.name === pic.name)).filter(Boolean);
304
+ const colourCards = [];
305
+ if (primary?.colour) colourCards.push({ name: primary.name, colours: swatches(primary.colour).slice(0, 6) });
306
+ else {
307
+ const from = onShow.find((c) => c.colour);
308
+ if (from) colourCards.push({ name: from.name, colours: swatches(from.colour).slice(0, 6), show: true });
309
+ }
310
+ // The night palette often comes from the `also` card. Show it, or the picture
311
+ // and the adaptation disagree.
312
+ if (also?.colour && /^register$/i.test((also.fit || "").trim())) {
313
+ colourCards.push({ name: also.name, colours: swatches(also.colour).slice(0, 4) });
314
+ }
315
+ const colourCount = colourCards.reduce((n, c) => n + c.colours.length, 0);
316
+
317
+ const seenFaces = [];
318
+ const faceCards = primary ? [primary, also && !/^form$/i.test((also.fit || "").trim()) ? also : null] : onShow;
319
+ for (const card of faceCards) {
320
+ if (!card) continue;
321
+ for (const face of facesOf(card)) {
322
+ if (!seenFaces.some((f) => norm(f) === norm(face))) seenFaces.push(face);
323
+ }
324
+ }
325
+ const faces = seenFaces.slice(0, 3);
326
+ const family = cssFamily(faces[0] || "");
327
+ const fonts = fontsHref(faces);
328
+ const ink = "#1c1c1a";
329
+
330
+ const tiles = [];
331
+ if (faces.length) {
332
+ const names = faces
333
+ .map((face) => `<p class="face" style="font-family:${esc(cssFamily(face))}">${esc(face)}</p>`)
334
+ .join("");
335
+ tiles.push(`<article class="tile type span-${colourCount ? 2 : 6}">
336
+ <p class="kicker">Type we saw</p>
337
+ <p class="aa">Aa</p>
338
+ <div class="faces">${names}</div>
339
+ </article>`);
340
+ }
341
+ if (colourCount) {
342
+ const span = faces.length ? 4 : 6;
343
+ const rows = colourCards
344
+ .filter((group) => group.colours.length)
345
+ .map((group, i) => {
346
+ const chips = group.colours
347
+ .map((s) => {
348
+ const edge = lum(s.hex) > 0.86 ? " chip-edge" : "";
349
+ return `<div class="swatch"><div class="chip${edge}" style="background:${esc(s.hex)}"></div><p class="hex">${esc(s.hex)}</p>${s.label ? `<p class="role">${esc(s.label)}</p>` : ""}</div>`;
350
+ })
351
+ .join("");
352
+ const label = i === 0 && !group.show ? "" : `<p class="from">${esc(group.name)}</p>`;
353
+ return `${label}<div class="swatches">${chips}</div>`;
354
+ })
355
+ .join("");
356
+ tiles.push(`<article class="tile colour span-${span}"><p class="kicker">Colour we saw</p><div class="rows">${rows}</div></article>`);
357
+ }
358
+ for (const pic of pictures) {
359
+ const span = pic.role ? 3 : 2;
360
+ const kicker = pic.role === "leads" ? "Leads" : pic.role === "also" ? "Also" : "";
361
+ const img = `<img src="${esc(pic.src)}" alt="${esc(pic.name)}" loading="lazy">`;
362
+ const frame = pic.url
363
+ ? `<a class="frame" href="${esc(pic.url)}" target="_blank" rel="noopener noreferrer">${img}</a>`
364
+ : `<div class="frame">${img}</div>`;
365
+ tiles.push(`<figure class="tile shot span-${span}${pic.role ? " marked" : ""}">${frame}<figcaption>${kicker ? `<p class="kicker">${esc(kicker)}</p>` : ""}<p class="name">${esc(pic.name)}</p>${pic.caption ? `<p class="line">${esc(pic.caption)}</p>` : ""}</figcaption></figure>`);
366
+ }
367
+ for (const pic of picturesOfPlace) {
368
+ const img = `<img src="${esc(pic.src)}" alt="${esc(pic.caption || "Imagery")}" loading="lazy">`;
369
+ const frame = pic.url
370
+ ? `<a class="frame" href="${esc(pic.url)}" target="_blank" rel="noopener noreferrer">${img}</a>`
371
+ : `<div class="frame">${img}</div>`;
372
+ tiles.push(`<figure class="tile shot picture span-2">${frame}<figcaption><p class="kicker">Imagery</p>${pic.caption ? `<p class="line">${esc(pic.caption)}</p>` : ""}</figcaption></figure>`);
373
+ }
374
+
375
+ if (!tiles.length) {
376
+ console.error("moodboard: nothing on the board to show");
377
+ process.exit(1);
378
+ }
379
+
380
+ const html = `<!DOCTYPE html>
381
+ <html lang="en">
382
+ <head>
383
+ <meta charset="utf-8">
384
+ <meta name="viewport" content="width=device-width, initial-scale=1">
385
+ <title>Moodboard</title>
386
+ ${fonts ? `<link rel="stylesheet" href="${esc(fonts)}">\n` : ""}<style>
387
+ :root {
388
+ --display: ${family};
389
+ --ink: ${ink};
390
+ --paper: #ffffff;
391
+ --canvas: #e4e4e2;
392
+ --muted: #6f6f6a;
393
+ --ui: "Helvetica Neue", Helvetica, Arial, sans-serif;
394
+ }
395
+ * { box-sizing: border-box; }
396
+ html { color-scheme: light; }
397
+ body {
398
+ margin: 0;
399
+ min-height: 100vh;
400
+ background: var(--canvas);
401
+ color: var(--ink);
402
+ font-family: var(--ui);
403
+ -webkit-font-smoothing: antialiased;
404
+ }
405
+ .sheet { max-width: 1200px; margin: 0 auto; padding: 52px 28px 80px; }
406
+ h1 {
407
+ margin: 0 0 20px 2px;
408
+ font-family: var(--ui);
409
+ font-size: 11px;
410
+ font-weight: 600;
411
+ letter-spacing: 0.18em;
412
+ text-transform: uppercase;
413
+ color: var(--muted);
414
+ }
415
+ .grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 22px; align-items: stretch; }
416
+ .tile { background: var(--paper); border-radius: 22px; overflow: hidden; min-width: 0; }
417
+ .span-2 { grid-column: span 2; }
418
+ .span-3 { grid-column: span 3; }
419
+ .span-4 { grid-column: span 4; }
420
+ .span-6 { grid-column: span 6; }
421
+ .kicker {
422
+ margin: 0;
423
+ font-size: 11px;
424
+ font-weight: 600;
425
+ letter-spacing: 0.16em;
426
+ text-transform: uppercase;
427
+ color: var(--muted);
428
+ }
429
+ .type, .colour { padding: 26px 28px 24px; }
430
+ .type {
431
+ display: flex;
432
+ flex-direction: column;
433
+ min-height: 280px;
434
+ background: #171717;
435
+ color: #f6f5f2;
436
+ }
437
+ .type .kicker { color: rgba(255, 255, 255, 0.55); }
438
+ .aa {
439
+ margin: 24px 0 0;
440
+ font-family: var(--display);
441
+ font-size: 104px;
442
+ line-height: 0.78;
443
+ letter-spacing: -0.045em;
444
+ font-weight: 500;
445
+ color: #f6f5f2;
446
+ }
447
+ .faces { margin-top: 24px; }
448
+ .face {
449
+ margin: 0;
450
+ font-size: 22px;
451
+ line-height: 1.25;
452
+ letter-spacing: -0.03em;
453
+ color: #f6f5f2;
454
+ }
455
+ .face + .face { margin-top: 4px; font-size: 17px; color: rgba(255, 255, 255, 0.68); }
456
+ .colour { display: flex; flex-direction: column; min-height: 280px; }
457
+ .rows { margin-top: auto; padding-top: 24px; display: grid; gap: 18px; }
458
+ .from {
459
+ margin: 0 0 8px;
460
+ font-size: 12px;
461
+ letter-spacing: 0.02em;
462
+ color: var(--muted);
463
+ }
464
+ .swatches {
465
+ display: grid;
466
+ grid-template-columns: repeat(auto-fit, minmax(92px, 1fr));
467
+ gap: 12px 14px;
468
+ }
469
+ .chip { height: 92px; border-radius: 14px; }
470
+ .chip-edge { box-shadow: inset 0 0 0 1px rgba(28, 28, 26, 0.14); }
471
+ .hex {
472
+ margin: 8px 0 0;
473
+ font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
474
+ font-size: 11px;
475
+ letter-spacing: 0.02em;
476
+ color: #3c3c39;
477
+ }
478
+ .role { margin: 3px 0 0; font-size: 12px; line-height: 1.35; color: var(--muted); }
479
+ .shot { margin: 0; display: flex; flex-direction: column; }
480
+ .frame {
481
+ display: block;
482
+ background: #141414;
483
+ aspect-ratio: 1;
484
+ }
485
+ .shot img {
486
+ display: block;
487
+ width: 100%;
488
+ height: 100%;
489
+ object-fit: contain;
490
+ object-position: center;
491
+ }
492
+ .shot figcaption { flex: 1; padding: 16px 18px 18px; background: var(--paper); color: var(--ink); }
493
+ .shot .name { margin: 6px 0 0; font-size: 15px; line-height: 1.3; letter-spacing: -0.01em; }
494
+ .shot .line { margin: 4px 0 0; font-size: 13px; line-height: 1.4; color: var(--muted); }
495
+ .shot figcaption .kicker + .name { margin-top: 6px; }
496
+ .marked .frame { aspect-ratio: 4 / 3; }
497
+ .marked .name { font-family: var(--display); font-size: 22px; letter-spacing: -0.02em; }
498
+ .marked .line { font-size: 15px; color: #3a3a38; }
499
+ @media (max-width: 900px) {
500
+ .sheet { padding: 28px 16px 48px; }
501
+ .grid { gap: 16px; }
502
+ .tile, .span-2, .span-3, .span-4, .span-6 { grid-column: 1 / -1; }
503
+ .aa { font-size: 84px; }
504
+ }
505
+ </style>
506
+ </head>
507
+ <body>
508
+ <main class="sheet">
509
+ <h1>Moodboard</h1>
510
+ <div class="grid">
511
+ ${tiles.join("\n ")}
512
+ </div>
513
+ </main>
514
+ </body>
515
+ </html>
516
+ `;
517
+
518
+ mkdirSync(dirname(outPath), { recursive: true });
519
+ writeFileSync(outPath, html);
520
+ console.log(outArg);