Skip to content

Commit 936a458

Browse files
committed
add meta shortcodes and front matter
1 parent 449a4b6 commit 936a458

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

tools/document-generator/main.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as mod from "https://deno.land/std/yaml/mod.ts";
2+
13
type GeneratorFunction<T> = (context: GeneratorContext) => T;
24

35
type Attr = {
@@ -34,12 +36,14 @@ type Span = WithAttr & {
3436
content: Inline[];
3537
};
3638

37-
type Inline = Code | Emph | Str | Space | Span;
39+
type Inline = Code | Emph | Str | Space | Span | Shortcode;
3840
const isCode = (inline: Inline): inline is Code => inline.type === "Code";
3941
const isEmph = (inline: Inline): inline is Emph => inline.type === "Emph";
4042
const isStr = (inline: Inline): inline is Str => inline.type === "Str";
4143
const isSpace = (inline: Inline): inline is Space => inline.type === "Space";
4244
const isSpan = (inline: Inline): inline is Span => inline.type === "Span";
45+
const isShortcode = (inline: Inline): inline is Shortcode =>
46+
inline.type === "Shortcode";
4347

4448
type Para = {
4549
type: "Para";
@@ -52,6 +56,13 @@ const isPara = (block: Block): block is Para => block.type === "Para";
5256
type Document = {
5357
type: "Document";
5458
blocks: Block[];
59+
meta: Record<string, unknown>;
60+
};
61+
62+
type Shortcode = {
63+
type: "Shortcode";
64+
content: string;
65+
escaped?: boolean;
5566
};
5667

5768
class RenderContext {
@@ -86,6 +97,12 @@ class RenderContext {
8697
}
8798
}
8899

100+
renderShortcode(shortcode: Shortcode) {
101+
const open = shortcode.escaped ? "{{{<" : "{{<";
102+
const close = shortcode.escaped ? ">}}}" : ">}}";
103+
this.content.push(`${open} ${shortcode.content} ${close}`);
104+
}
105+
89106
renderInline(inline: Inline) {
90107
if (isCode(inline)) {
91108
this.content.push("`" + inline.text + "`");
@@ -111,6 +128,9 @@ class RenderContext {
111128
if (isSpan(inline)) {
112129
this.renderSpan(inline);
113130
}
131+
if (isShortcode(inline)) {
132+
this.renderShortcode(inline);
133+
}
114134
}
115135

116136
renderPara(para: Para) {
@@ -131,6 +151,11 @@ class RenderContext {
131151
}
132152

133153
renderDocument(document: Document) {
154+
if (Object.entries(document.meta).length > 0) {
155+
this.content.push("---\n");
156+
this.content.push(mod.stringify(document.meta));
157+
this.content.push("---\n\n");
158+
}
134159
for (const block of document.blocks) {
135160
this.renderBlock(block);
136161
}
@@ -155,6 +180,7 @@ class GeneratorContext {
155180
emph: number;
156181
code: number;
157182
span: number;
183+
shortcode: number;
158184
};
159185

160186
sizes: {
@@ -166,6 +192,8 @@ class GeneratorContext {
166192
classes: string[];
167193
ids: string[];
168194

195+
meta: Record<string, unknown>;
196+
169197
////////////////////////////////////////////////////////////////////////////////
170198
// helpers
171199

@@ -180,13 +208,23 @@ class GeneratorContext {
180208
return result;
181209
}
182210

211+
assign(other: GeneratorContext) {
212+
this.probabilities = other.probabilities;
213+
this.sizes = other.sizes;
214+
this.classes = other.classes;
215+
this.ids = other.ids;
216+
this.meta = other.meta;
217+
}
218+
183219
smaller(): GeneratorContext {
184220
const newContext = new GeneratorContext();
221+
newContext.assign(this);
185222
newContext.sizes = {
186223
...this.sizes,
187224
inline: ~~(this.sizes.inline * 0.5),
188225
block: ~~(this.sizes.block * 0.5),
189226
};
227+
190228
return newContext;
191229
}
192230

@@ -260,10 +298,23 @@ class GeneratorContext {
260298
if (Math.random() < this.probabilities.emph) {
261299
return "Emph";
262300
}
301+
if (Math.random() < this.probabilities.shortcode) {
302+
return "InlineShortcode";
303+
}
263304

264305
return "Null";
265306
}
266307

308+
generateInlineShortcode(): Shortcode {
309+
const metaKey = this.freshId();
310+
const metaValue = this.freshId();
311+
this.meta[metaKey] = metaValue;
312+
return {
313+
type: "Shortcode",
314+
content: `meta ${metaKey}`,
315+
};
316+
}
317+
267318
generateStr(): Str {
268319
return {
269320
type: "Str",
@@ -322,6 +373,7 @@ class GeneratorContext {
322373
Code: () => this.generateCode(),
323374
Emph: () => this.generateEmph(),
324375
Span: () => this.generateSpan(),
376+
InlineShortcode: () => this.generateInlineShortcode(),
325377
Null: () => {},
326378
};
327379
return dispatch[this.chooseInlineType()]();
@@ -394,10 +446,12 @@ class GeneratorContext {
394446
blocks.push(small.generateBlock());
395447
}
396448

397-
return {
449+
const result: Document = {
398450
type: "Document",
399451
blocks,
452+
meta: this.meta,
400453
};
454+
return result;
401455
}
402456

403457
////////////////////////////////////////////////////////////////////////////////
@@ -413,12 +467,14 @@ class GeneratorContext {
413467
code: 0.5,
414468
span: 0.5,
415469
emph: 0.5,
470+
shortcode: 0.5,
416471
};
417472
this.sizes = {
418473
inline: 10,
419474
block: 10,
420475
sentence: 10,
421476
};
477+
this.meta = {};
422478
}
423479
}
424480

0 commit comments

Comments
 (0)