2023-10-03 12:16:33 +00:00
|
|
|
import {
|
|
|
|
addParentPointers,
|
|
|
|
findParentMatching,
|
|
|
|
ParseTree,
|
|
|
|
renderToText,
|
|
|
|
} from "$sb/lib/tree.ts";
|
|
|
|
import { PageMeta } from "$sb/types.ts";
|
|
|
|
import { editor, markdown } from "$sb/syscalls.ts";
|
2022-10-28 14:17:40 +00:00
|
|
|
|
|
|
|
import { evalDirectiveRenderer } from "./eval_directive.ts";
|
|
|
|
import { queryDirectiveRenderer } from "./query_directive.ts";
|
|
|
|
import {
|
|
|
|
cleanTemplateInstantiations,
|
|
|
|
templateDirectiveRenderer,
|
|
|
|
} from "./template_directive.ts";
|
|
|
|
|
2023-09-05 08:24:50 +00:00
|
|
|
/** An error that occurs while a directive is being rendered.
|
|
|
|
* Mostly annotates the underlying error with page metadata.
|
|
|
|
*/
|
|
|
|
export class RenderDirectiveError extends Error {
|
|
|
|
pageMeta: PageMeta;
|
|
|
|
directive: string;
|
|
|
|
cause: Error;
|
|
|
|
|
|
|
|
constructor(pageMeta: PageMeta, directive: string, cause: Error) {
|
|
|
|
super(`In directive "${directive}" from "${pageMeta.name}": ${cause}`, {
|
|
|
|
cause: cause,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.pageMeta = pageMeta;
|
|
|
|
this.directive = directive;
|
|
|
|
this.cause = cause;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 19:04:20 +00:00
|
|
|
export const directiveStartRegex =
|
|
|
|
/<!--\s*#(use|use-verbose|include|eval|query)\s+(.*?)-->/i;
|
|
|
|
|
2022-10-28 14:17:40 +00:00
|
|
|
export const directiveRegex =
|
|
|
|
/(<!--\s*#(use|use-verbose|include|eval|query)\s+(.*?)-->)(.+?)(<!--\s*\/\2\s*-->)/gs;
|
|
|
|
/**
|
|
|
|
* Looks for directives in the text dispatches them based on name
|
|
|
|
*/
|
2022-12-14 19:04:20 +00:00
|
|
|
export async function directiveDispatcher(
|
2023-07-02 09:25:32 +00:00
|
|
|
pageMeta: PageMeta,
|
2022-12-14 19:04:20 +00:00
|
|
|
directiveTree: ParseTree,
|
2022-10-28 14:17:40 +00:00
|
|
|
directiveRenderers: Record<
|
|
|
|
string,
|
2022-12-14 19:04:20 +00:00
|
|
|
(
|
|
|
|
directive: string,
|
2023-07-02 09:25:32 +00:00
|
|
|
pageMeta: PageMeta,
|
2022-12-14 19:04:20 +00:00
|
|
|
arg: string | ParseTree,
|
|
|
|
) => Promise<string>
|
2022-10-28 14:17:40 +00:00
|
|
|
>,
|
|
|
|
): Promise<string> {
|
2022-12-14 19:04:20 +00:00
|
|
|
const directiveStart = directiveTree.children![0]; // <!-- #directive -->
|
|
|
|
const directiveEnd = directiveTree.children![2]; // <!-- /directive -->
|
|
|
|
|
|
|
|
const directiveStartText = renderToText(directiveStart).trim();
|
|
|
|
const directiveEndText = renderToText(directiveEnd).trim();
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
const firstPart = directiveStart.children![0].text!;
|
|
|
|
if (firstPart?.includes("#query")) {
|
|
|
|
// #query
|
|
|
|
const newBody = await directiveRenderers["query"](
|
|
|
|
"query",
|
|
|
|
pageMeta,
|
|
|
|
directiveStart.children![1].children![0], // The query ParseTree
|
|
|
|
);
|
|
|
|
const result =
|
|
|
|
`${directiveStartText}\n${newBody.trim()}\n${directiveEndText}`;
|
|
|
|
return result;
|
|
|
|
} else if (firstPart?.includes("#eval")) {
|
|
|
|
console.log("Eval stuff", directiveStart.children![1].children![0]);
|
|
|
|
const newBody = await directiveRenderers["eval"](
|
|
|
|
"eval",
|
|
|
|
pageMeta,
|
|
|
|
directiveStart.children![1].children![0],
|
|
|
|
);
|
|
|
|
const result =
|
|
|
|
`${directiveStartText}\n${newBody.trim()}\n${directiveEndText}`;
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
// Everything not #query and #eval
|
2022-12-14 19:04:20 +00:00
|
|
|
const match = directiveStartRegex.exec(directiveStart.children![0].text!);
|
|
|
|
if (!match) {
|
|
|
|
throw Error("No match");
|
|
|
|
}
|
|
|
|
|
|
|
|
let [_fullMatch, type, arg] = match;
|
|
|
|
try {
|
2022-10-28 14:17:40 +00:00
|
|
|
arg = arg.trim();
|
2023-07-02 09:25:32 +00:00
|
|
|
const newBody = await directiveRenderers[type](type, pageMeta, arg);
|
2022-12-14 19:04:20 +00:00
|
|
|
const result =
|
|
|
|
`${directiveStartText}\n${newBody.trim()}\n${directiveEndText}`;
|
|
|
|
return result;
|
|
|
|
} catch (e: any) {
|
|
|
|
return `${directiveStartText}\n**ERROR:** ${e.message}\n${directiveEndText}`;
|
|
|
|
}
|
|
|
|
}
|
2022-10-28 14:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function renderDirectives(
|
2023-07-02 09:25:32 +00:00
|
|
|
pageMeta: PageMeta,
|
2022-12-14 19:04:20 +00:00
|
|
|
directiveTree: ParseTree,
|
2022-10-28 14:17:40 +00:00
|
|
|
): Promise<string> {
|
2023-09-05 08:24:50 +00:00
|
|
|
try {
|
|
|
|
const replacementText = await directiveDispatcher(pageMeta, directiveTree, {
|
|
|
|
use: templateDirectiveRenderer,
|
|
|
|
include: templateDirectiveRenderer,
|
|
|
|
query: queryDirectiveRenderer,
|
|
|
|
eval: evalDirectiveRenderer,
|
|
|
|
});
|
|
|
|
return cleanTemplateInstantiations(replacementText);
|
|
|
|
} catch (e) {
|
|
|
|
throw new RenderDirectiveError(
|
|
|
|
pageMeta,
|
|
|
|
renderToText(directiveTree.children![0].children![1]).trim(),
|
|
|
|
e,
|
|
|
|
);
|
|
|
|
}
|
2022-10-28 14:17:40 +00:00
|
|
|
}
|