2022-10-28 14:17:40 +00:00
|
|
|
import Handlebars from "handlebars";
|
|
|
|
|
2023-08-28 15:12:15 +00:00
|
|
|
import { space } from "$sb/syscalls.ts";
|
|
|
|
import type { PageMeta } from "../../web/types.ts";
|
2023-07-24 09:09:17 +00:00
|
|
|
import { handlebarHelpers } from "./handlebar_helpers.ts";
|
2022-10-28 14:17:40 +00:00
|
|
|
|
2022-04-29 11:37:31 +00:00
|
|
|
const maxWidth = 70;
|
2022-11-20 09:24:42 +00:00
|
|
|
|
|
|
|
export function defaultJsonTransformer(_k: string, v: any) {
|
|
|
|
if (v === undefined) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return "" + v;
|
|
|
|
}
|
|
|
|
|
2022-04-21 09:46:33 +00:00
|
|
|
// Nicely format an array of JSON objects as a Markdown table
|
|
|
|
export function jsonToMDTable(
|
|
|
|
jsonArray: any[],
|
2022-11-20 09:24:42 +00:00
|
|
|
valueTransformer: (k: string, v: any) => string = defaultJsonTransformer,
|
2022-04-21 09:46:33 +00:00
|
|
|
): string {
|
2022-10-14 13:11:33 +00:00
|
|
|
const fieldWidths = new Map<string, number>();
|
2022-10-15 17:02:56 +00:00
|
|
|
for (const entry of jsonArray) {
|
|
|
|
for (const k of Object.keys(entry)) {
|
2022-04-29 11:37:31 +00:00
|
|
|
let fieldWidth = fieldWidths.get(k);
|
|
|
|
if (!fieldWidth) {
|
|
|
|
fieldWidth = valueTransformer(k, entry[k]).length;
|
|
|
|
} else {
|
|
|
|
fieldWidth = Math.max(valueTransformer(k, entry[k]).length, fieldWidth);
|
|
|
|
}
|
|
|
|
fieldWidths.set(k, fieldWidth);
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-29 11:37:31 +00:00
|
|
|
|
|
|
|
let fullWidth = 0;
|
2022-10-15 17:02:56 +00:00
|
|
|
for (const v of fieldWidths.values()) {
|
2022-04-29 11:37:31 +00:00
|
|
|
fullWidth += v + 1;
|
|
|
|
}
|
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
const headerList = [...fieldWidths.keys()];
|
|
|
|
const lines = [];
|
2022-04-29 11:37:31 +00:00
|
|
|
lines.push(
|
|
|
|
"|" +
|
|
|
|
headerList
|
|
|
|
.map(
|
|
|
|
(headerName) =>
|
|
|
|
headerName +
|
2022-10-12 09:47:13 +00:00
|
|
|
charPad(" ", fieldWidths.get(headerName)! - headerName.length),
|
2022-04-29 11:37:31 +00:00
|
|
|
)
|
|
|
|
.join("|") +
|
2022-10-12 09:47:13 +00:00
|
|
|
"|",
|
2022-04-29 11:37:31 +00:00
|
|
|
);
|
|
|
|
lines.push(
|
|
|
|
"|" +
|
|
|
|
headerList
|
|
|
|
.map((title) => charPad("-", fieldWidths.get(title)!))
|
|
|
|
.join("|") +
|
2022-10-12 09:47:13 +00:00
|
|
|
"|",
|
2022-04-29 11:37:31 +00:00
|
|
|
);
|
2022-04-21 09:46:33 +00:00
|
|
|
for (const val of jsonArray) {
|
2022-10-15 17:02:56 +00:00
|
|
|
const el = [];
|
|
|
|
for (const prop of headerList) {
|
|
|
|
const s = valueTransformer(prop, val[prop]);
|
2022-04-29 11:37:31 +00:00
|
|
|
el.push(s + charPad(" ", fieldWidths.get(prop)! - s.length));
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
|
|
|
lines.push("|" + el.join("|") + "|");
|
|
|
|
}
|
|
|
|
return lines.join("\n");
|
2022-04-29 11:37:31 +00:00
|
|
|
|
|
|
|
function charPad(ch: string, length: number) {
|
|
|
|
if (fullWidth > maxWidth && ch === "") {
|
|
|
|
return "";
|
|
|
|
} else if (fullWidth > maxWidth && ch === "-") {
|
|
|
|
return "--";
|
|
|
|
}
|
|
|
|
if (length < 1) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return new Array(length + 1).join(ch);
|
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
}
|
2022-10-28 14:17:40 +00:00
|
|
|
|
|
|
|
export async function renderTemplate(
|
2023-07-02 09:25:32 +00:00
|
|
|
pageMeta: PageMeta,
|
2022-10-28 14:17:40 +00:00
|
|
|
renderTemplate: string,
|
|
|
|
data: any[],
|
|
|
|
): Promise<string> {
|
2023-05-23 18:53:53 +00:00
|
|
|
let templateText = await space.readPage(renderTemplate);
|
|
|
|
templateText = `{{#each .}}\n${templateText}\n{{/each}}`;
|
|
|
|
const template = Handlebars.compile(templateText, { noEscape: true });
|
2023-07-02 09:25:32 +00:00
|
|
|
return template(data, buildHandebarOptions(pageMeta));
|
2023-05-23 18:53:53 +00:00
|
|
|
}
|
|
|
|
|
2023-07-02 09:25:32 +00:00
|
|
|
export function buildHandebarOptions(pageMeta: PageMeta) {
|
|
|
|
return {
|
|
|
|
helpers: handlebarHelpers(pageMeta.name),
|
|
|
|
data: { page: pageMeta },
|
|
|
|
};
|
|
|
|
}
|