prototype-3x5/src/3x5.ts

142 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-10-19 02:55:34 +00:00
import { Card, CardVm, GetField } from './lib/card';
import { ALL as HTML } from './lib/html';
import { parse } from './parser';
import { runNoctl, Vm } from './vm';
import { AsHtml, AsText, TextPiece } from './words';
2023-07-22 02:19:10 +00:00
2023-10-16 03:59:04 +00:00
/**
* Updates a card's fields
* @param state VM state
* @param card
* @param fields
*/
function parseFields(card: Card, fields: string) {
const script = parse(fields);
const newFields: Record<string, string> = {};
if (script[0]) {
script[1].forEach(([name, value = undefined]) => {
if (name != undefined) {
newFields[AsText(name as TextPiece)] = value
? AsText(value as TextPiece)
: "";
}
});
card.fields = newFields;
} else {
console.error(script[1]);
}
}
/**
2023-08-06 06:09:30 +00:00
* @param state VM state
* @param code Script to run
* @returns Markup to render / output
*/
2023-10-19 02:55:34 +00:00
function renderCard(state: CardVm, code: string) {
2023-08-05 05:09:33 +00:00
const script = parse(code);
if (script[0]) {
2023-09-08 04:03:48 +00:00
runNoctl(state, script[1], (word) => (state.output += AsHtml(word) + "\n"));
} else {
state.output = script[1];
}
return state.output;
}
/**
* Global state: a single card
*/
2023-08-06 06:09:30 +00:00
let theCard: Card = {
id: 100,
fields: {},
2023-10-16 03:25:13 +00:00
code: "",
};
2023-10-16 03:25:13 +00:00
const TEXTAREA_STYLE: Partial<CSSStyleDeclaration> = {
display: "block",
width: "100%",
};
2023-10-16 03:59:04 +00:00
const fieldInput = document.createElement("textarea");
Object.assign(fieldInput.style, TEXTAREA_STYLE, { height: "8em" });
fieldInput.value = String.raw`
title "Hello, World!"
`.trim();
2023-10-16 03:25:13 +00:00
const codeInput = document.createElement("textarea");
Object.assign(codeInput.style, TEXTAREA_STYLE, { height: "20em" });
codeInput.value = String.raw`
h1 "Hello, World!"
para [2 + 2]
block {
This is a paragraph of text, with one [b bold] word. Yes, this means there has to be some magic in text processing... <b>this</b> won't work.
}
block -red "Beware!"
para "All text should be quoted, it's clearer that way. & blockquotes already should contain paragraphs. (maybe normalize nested paragraphs)"
block {
First block
} {
Second block
Is this markdown-parsed?
[button "No we want to render UI" \\{noop}]
} {
Since we want escapes to work, these blocks [i will] be subject to substitutions, maybe via a relaxed bareWordTmpl.
}
# A comment
para {
line endings escaped\
one slash
not escaped if \\
two slashes
escaped with a slash if \\\
three slashes
not escaped with two slashes if \\\\
four slashes
escaped with two slashes if \\\\\
five slashes
not escaped with three slashes if \\\\\\
six slashes
}
`.trim();
const rerender = Object.assign(document.createElement("button"), {
onclick: () => render(),
textContent: "Render",
});
const state = document.createElement("pre");
const display = document.createElement("blockquote");
const debugDisplay = document.createElement("pre");
function render() {
2023-10-16 03:59:04 +00:00
parseFields(theCard, fieldInput.value);
2023-10-16 03:25:13 +00:00
theCard.code = codeInput.value;
2023-10-19 02:55:34 +00:00
const vm: CardVm = {
2023-08-06 06:09:30 +00:00
mode: "render",
2023-10-19 02:55:34 +00:00
commands: { ...HTML, get: GetField },
2023-07-28 22:49:43 +00:00
output: "",
2023-10-19 02:55:34 +00:00
card: theCard,
2023-07-28 22:49:43 +00:00
};
const html = renderCard(vm, theCard.code);
state.textContent = JSON.stringify(theCard, null, 2);
display.innerHTML = html;
debugDisplay.textContent = html;
}
render();
2023-10-16 03:25:13 +00:00
document.body.append(
fieldInput,
codeInput,
rerender,
display,
2023-10-19 02:55:34 +00:00
state,
2023-10-16 03:25:13 +00:00
debugDisplay
);