@christang/keel 5.190.0 → 5.191.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,211 @@
1
+ "use strict";
2
+
3
+ // `keel docx`: Markdown with embedded SVG to Word, on request (#405).
4
+ //
5
+ // The converter ships with the `keel-md-docx` skill; its third-party packages
6
+ // do not. `setup` lists them and installs nothing until it is given `--yes`,
7
+ // which the skill asks the user for first, and then installs only into the
8
+ // environment Keel manages under `$KEEL_HOME/docx/` — a Python virtual
9
+ // environment and an npm prefix, never the system's.
10
+
11
+ const fs = require("fs");
12
+ const os = require("os");
13
+ const path = require("path");
14
+ const { spawnSync } = require("child_process");
15
+ const { resolveInterpreter, describeTried, MINIMUM_PYTHON } = require("../../scripts/run_python");
16
+ const PACKAGE_JSON = require("../../package.json");
17
+
18
+ const SKILL_SCRIPTS = path.join(__dirname, "..", "..", "plugins", "keel", "skills", "keel-md-docx", "scripts");
19
+
20
+ // What setup installs, as the registries listed it on 2026-10-21 for macOS
21
+ // arm64 (#405); the versions that install are the ones requirements.txt and
22
+ // package.json pin.
23
+ const PACKAGES = [
24
+ { name: "python-docx", via: "pip", version: ">=1.1,<2 (1.2.0)", licence: "MIT", size: "0.25 MB" },
25
+ { name: "lxml", via: "pip", version: ">=5,<7 (6.0.2)", licence: "BSD-3-Clause", size: "8.6 MB" },
26
+ { name: "Pillow", via: "pip", version: ">=10,<13 (12.0.0)", licence: "MIT-CMU", size: "4.65 MB" },
27
+ { name: "marked", via: "npm", version: "17.0.5", licence: "MIT", size: "0.44 MB" },
28
+ {
29
+ name: "sharp",
30
+ via: "npm",
31
+ version: "0.35.5",
32
+ licence: "Apache-2.0",
33
+ size: "0.96 MB, plus its platform binary (Apache-2.0) and libvips (LGPL-3.0-or-later, 18.2 MB)",
34
+ },
35
+ ];
36
+
37
+ const USAGE = [
38
+ "Usage:",
39
+ " keel docx setup [--yes] list what the export needs; install it into the Keel-managed environment with --yes",
40
+ " keel docx convert <input.md> [-o <output.docx>] [--template <template.docx>] [--replace]",
41
+ " convert in that environment; an existing output is kept unless --replace is given",
42
+ ].join("\n");
43
+
44
+ function docxHome() {
45
+ return path.join(process.env.KEEL_HOME || path.join(os.homedir(), ".keel"), "docx");
46
+ }
47
+
48
+ function environmentPython(home) {
49
+ return process.platform === "win32"
50
+ ? path.join(home, "python", "Scripts", "python.exe")
51
+ : path.join(home, "python", "bin", "python");
52
+ }
53
+
54
+ function npmCommand() {
55
+ return process.env.KEEL_DOCX_NPM || (process.platform === "win32" ? "npm.cmd" : "npm");
56
+ }
57
+
58
+ function printPlan(home) {
59
+ const lines = [
60
+ `keel docx: Markdown with SVG to Word needs these packages, installed into ${home}:`,
61
+ ...PACKAGES.map((item) => ` ${item.name} ${item.version} (${item.via}) — ${item.licence} — ${item.size}`),
62
+ ` Python ${MINIMUM_PYTHON.join(".")} or newer runs a virtual environment in ${path.join(home, "python")};`,
63
+ ` npm installs into ${path.join(home, "node")}. Nothing goes into the system Python or Node,`,
64
+ " and no font or runtime is downloaded. Sizes are registry figures, not measured installs.",
65
+ ];
66
+ process.stdout.write(`${lines.join("\n")}\n`);
67
+ }
68
+
69
+ function step(name, command, args) {
70
+ const result = spawnSync(command, args, {
71
+ stdio: "inherit",
72
+ shell: process.platform === "win32",
73
+ });
74
+ if (result.error || result.status !== 0) {
75
+ const why = result.error ? result.error.message : `exited ${result.status}`;
76
+ process.stderr.write(`keel docx setup: ${name} failed (${why}); the environment is not ready.\n`);
77
+ return false;
78
+ }
79
+ return true;
80
+ }
81
+
82
+ function setup(argv) {
83
+ let yes = false;
84
+ for (const arg of argv) {
85
+ if (arg === "--yes") yes = true;
86
+ else {
87
+ process.stderr.write(`keel docx setup: unexpected argument ${arg}\n${USAGE}\n`);
88
+ return 2;
89
+ }
90
+ }
91
+ const home = docxHome();
92
+ const ready = path.join(home, "ready.json");
93
+ printPlan(home);
94
+ if (!yes) {
95
+ const state = fs.existsSync(ready) ? "The environment is ready." : "Nothing was installed.";
96
+ process.stdout.write(`${state} To install or reinstall, run \`keel docx setup --yes\`.\n`);
97
+ return 0;
98
+ }
99
+
100
+ const { candidate, version, tried } = resolveInterpreter();
101
+ if (!candidate) {
102
+ process.stderr.write(
103
+ `keel docx setup: Python ${MINIMUM_PYTHON.join(".")} or newer is required. `
104
+ + `Tried ${describeTried(tried) || "nothing"}. Install one, or set KEEL_PYTHON.\n`
105
+ );
106
+ return 1;
107
+ }
108
+ // Written last, so an interrupted setup reads as not set up.
109
+ fs.rmSync(ready, { force: true });
110
+ fs.mkdirSync(path.join(home, "node"), { recursive: true });
111
+ fs.copyFileSync(path.join(SKILL_SCRIPTS, "package.json"), path.join(home, "node", "package.json"));
112
+ const ok = step("python -m venv", candidate.command, [...candidate.prefixArgs, "-m", "venv", path.join(home, "python")])
113
+ && step("pip install", environmentPython(home), ["-m", "pip", "install", "-r", path.join(SKILL_SCRIPTS, "requirements.txt")])
114
+ && step("npm install", npmCommand(), ["install", "--prefix", path.join(home, "node"), "--no-audit", "--no-fund"]);
115
+ if (!ok) return 1;
116
+ fs.writeFileSync(ready, `${JSON.stringify({
117
+ keel: PACKAGE_JSON.version,
118
+ python: version,
119
+ packages: PACKAGES.map((item) => `${item.name} ${item.version}`),
120
+ }, null, 2)}\n`);
121
+ process.stdout.write(`keel docx: the environment in ${home} is ready.\n`);
122
+ return 0;
123
+ }
124
+
125
+ // The real path of a file that may not exist yet: its directory's, plus its name.
126
+ function realTarget(file) {
127
+ const absolute = path.resolve(file);
128
+ try {
129
+ return fs.realpathSync(absolute);
130
+ } catch {
131
+ try {
132
+ return path.join(fs.realpathSync(path.dirname(absolute)), path.basename(absolute));
133
+ } catch {
134
+ return absolute;
135
+ }
136
+ }
137
+ }
138
+
139
+ function convert(argv) {
140
+ const fail = (message) => {
141
+ process.stderr.write(`keel docx convert: ${message}\n`);
142
+ return 1;
143
+ };
144
+ let input = null;
145
+ let output = null;
146
+ let template = null;
147
+ let replace = false;
148
+ for (let index = 0; index < argv.length; index += 1) {
149
+ const arg = argv[index];
150
+ if (arg === "--replace") replace = true;
151
+ else if ((arg === "-o" || arg === "--output" || arg === "--template") && argv[index + 1] !== undefined) {
152
+ index += 1;
153
+ if (arg === "--template") template = argv[index];
154
+ else output = argv[index];
155
+ } else if (!arg.startsWith("-") && input === null) input = arg;
156
+ else {
157
+ process.stderr.write(`keel docx convert: unexpected argument ${arg}\n${USAGE}\n`);
158
+ return 2;
159
+ }
160
+ }
161
+ if (input === null) {
162
+ process.stderr.write(`keel docx convert: no input file given\n${USAGE}\n`);
163
+ return 2;
164
+ }
165
+
166
+ const source = realTarget(input);
167
+ if (!fs.existsSync(source) || !fs.statSync(source).isFile()) return fail(`${input} does not exist.`);
168
+ const target = realTarget(output === null ? `${source.slice(0, source.length - path.extname(source).length)}.docx` : output);
169
+ if (target === source) return fail(`the output ${target} is the input; name a different output with -o.`);
170
+ const styles = template === null ? null : realTarget(template);
171
+ if (styles !== null && !fs.existsSync(styles)) return fail(`the template ${template} does not exist.`);
172
+ const home = docxHome();
173
+ if (!fs.existsSync(path.join(home, "ready.json"))) {
174
+ return fail(`the environment in ${home} is not ready; run \`keel docx setup\` to see what it installs.`);
175
+ }
176
+ if (fs.existsSync(target) && !replace) {
177
+ return fail(`${target} already exists and was left as it is; give --replace to overwrite it.`);
178
+ }
179
+
180
+ const env = {
181
+ ...process.env,
182
+ MD2WORD_NODE: process.execPath,
183
+ MD2WORD_NODE_MODULES: path.join(home, "node", "node_modules"),
184
+ };
185
+ if (process.platform === "darwin" && !env.FONTCONFIG_FILE) {
186
+ env.FONTCONFIG_FILE = path.join(SKILL_SCRIPTS, "fonts-macos.conf");
187
+ }
188
+ const args = [path.join(SKILL_SCRIPTS, "md2word.py"), source, "-o", target];
189
+ if (styles !== null) args.push("--template", styles);
190
+ // The converter writes a temporary file and renames it over the output, so
191
+ // a failed run leaves the previous output as it was.
192
+ const result = spawnSync(environmentPython(home), args, { stdio: "inherit", env });
193
+ if (result.error) {
194
+ return fail(`the environment's Python did not run (${result.error.message}); rerun \`keel docx setup --yes\`.`);
195
+ }
196
+ return result.status === null ? 1 : result.status;
197
+ }
198
+
199
+ function runDocx(argv) {
200
+ const [sub, ...rest] = argv;
201
+ if (sub === undefined || sub === "--help" || sub === "-h" || sub === "help") {
202
+ process.stdout.write(`${USAGE}\n`);
203
+ return sub === undefined ? 2 : 0;
204
+ }
205
+ if (sub === "setup") return setup(rest);
206
+ if (sub === "convert") return convert(rest);
207
+ process.stderr.write(`keel docx: unknown subcommand ${sub}\n${USAGE}\n`);
208
+ return 2;
209
+ }
210
+
211
+ module.exports = { runDocx, docxHome, PACKAGES, SKILL_SCRIPTS };