asset忘了上传了,重新传一下,增加了模板引擎(见README),修复了printable的目录bug
This commit is contained in:
1 parent
0817d33309
commit
8b88de4942
7268 files changed
+1257020
No files matched your search
@@ -0,0 +1,101 @@
|
||||
import { parse as parseToml } from "@std/toml";
|
||||
import nunjucks from "nunjucks";
|
||||
|
||||
export type TemplateVariables = Record<string, unknown>;
|
||||
|
||||
const engine = new nunjucks.Environment(undefined, {
|
||||
autoescape: false,
|
||||
throwOnUndefined: false,
|
||||
});
|
||||
|
||||
function valueFromCli(value: unknown): unknown {
|
||||
if (typeof value !== "string") return value;
|
||||
if (value === "true") return true;
|
||||
if (value === "false") return false;
|
||||
if (value === "null") return null;
|
||||
if (value.trim() !== "" && Number.isFinite(Number(value))) {
|
||||
return Number(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function mergeObjects(...values: Array<unknown>): TemplateVariables {
|
||||
const result: TemplateVariables = {};
|
||||
for (const value of values) {
|
||||
if (value && typeof value === "object" && !Array.isArray(value)) {
|
||||
Object.assign(result, value);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function cliVariables(args: Record<string, unknown>): TemplateVariables {
|
||||
const reserved = new Set([
|
||||
"_",
|
||||
"config",
|
||||
"name",
|
||||
"vars-file",
|
||||
"vars_file",
|
||||
"v",
|
||||
"from",
|
||||
]);
|
||||
const variables: TemplateVariables = {};
|
||||
for (const [key, value] of Object.entries(args)) {
|
||||
if (!reserved.has(key) && !key.startsWith("_")) {
|
||||
variables[key] = valueFromCli(value);
|
||||
}
|
||||
}
|
||||
return variables;
|
||||
}
|
||||
|
||||
async function readVariablesFile(path: string): Promise<TemplateVariables> {
|
||||
const text = await Deno.readTextFile(path);
|
||||
const parsed = parseToml(text);
|
||||
return mergeObjects(parsed);
|
||||
}
|
||||
|
||||
function builtinVariables(config: Record<string, unknown>): TemplateVariables {
|
||||
const now = new Date();
|
||||
const iso = now.toISOString();
|
||||
return {
|
||||
now: iso,
|
||||
currentTime: iso,
|
||||
currentDate: iso.slice(0, 10),
|
||||
year: now.getFullYear(),
|
||||
month: now.getMonth() + 1,
|
||||
day: now.getDate(),
|
||||
weekday: now.toLocaleDateString("en-US", { weekday: "long" }),
|
||||
timestamp: now.getTime(),
|
||||
unixTimestamp: Math.floor(now.getTime() / 1000),
|
||||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||
locale: Intl.DateTimeFormat().resolvedOptions().locale,
|
||||
platform: Deno.build.os,
|
||||
architecture: Deno.build.arch,
|
||||
runtime: `Deno ${Deno.version.deno}`,
|
||||
config,
|
||||
};
|
||||
}
|
||||
|
||||
export async function createTemplateVariables(
|
||||
args: Record<string, unknown> = {},
|
||||
config: Record<string, unknown> = {},
|
||||
): Promise<TemplateVariables> {
|
||||
const journal = (config.journal ?? {}) as Record<string, unknown>;
|
||||
const varsFile = args["vars-file"] ?? args.vars_file ?? args.v;
|
||||
const fromFile = typeof varsFile === "string"
|
||||
? await readVariablesFile(varsFile)
|
||||
: {};
|
||||
return mergeObjects(
|
||||
builtinVariables(config),
|
||||
journal.variables,
|
||||
fromFile,
|
||||
cliVariables(args),
|
||||
);
|
||||
}
|
||||
|
||||
export function renderTemplate(
|
||||
source: string,
|
||||
variables: TemplateVariables,
|
||||
): string {
|
||||
return engine.renderString(source, variables);
|
||||
}
|
||||
Reference in new issue
Block a user