gmi-web/cli.js

128 lines
3 KiB
JavaScript
Raw Normal View History

2021-01-28 06:16:46 +00:00
#!/usr/bin/env node
2021-02-15 20:56:41 +00:00
import { readFileSync, existsSync } from "fs";
import path from "path";
import fs from "vinyl-fs";
import yargs from "yargs";
2021-02-15 20:56:41 +00:00
import * as CSS from "./css.js";
2021-02-15 22:31:08 +00:00
import { streamHTML, toHTML } from "./html.js";
const cli = yargs(process.argv.slice(2))
.config("config", function (path) {
2021-02-15 20:56:41 +00:00
return JSON.parse(readFileSync(path), "utf-8");
})
.scriptName("gmi-web")
.command("$0 [files..]", "Convert text/gemini to text/html.", (yargs) =>
yargs
2021-02-15 20:56:41 +00:00
.example("$0 --body < ~/my-capsule/index.gmi")
.example("$0 --html en $(find ~/my-capsule -name '*.gmi')")
2021-02-12 20:41:43 +00:00
.example(
"$0 --foreground '#000000' --background '#EEEEEE' --html en < doc.gmi"
)
2021-02-11 23:49:39 +00:00
.example("$0 --image jpg --audio mp3 --image png --body < doc.gmi")
2021-02-16 22:16:11 +00:00
.epilog("See the gmi-web(1) man page for more information.")
)
.options({
2021-02-11 23:49:39 +00:00
body: {
type: "boolean",
2021-02-19 19:52:02 +00:00
group: "Core:",
2021-02-11 23:49:39 +00:00
},
html: {
type: "string",
requiresArg: true,
2021-02-19 19:52:02 +00:00
group: "Core:",
},
css: {
requiresArg: true,
group: "Core:",
},
2021-02-15 17:17:16 +00:00
dir: {
type: "string",
2021-02-19 19:32:28 +00:00
hidden: true,
2021-02-15 17:17:16 +00:00
choices: ["rtl", "ltr"],
default: "ltr",
requiresArg: true,
2021-02-19 19:52:02 +00:00
group: "HTML:",
2021-02-15 17:17:16 +00:00
},
author: {
2021-02-12 01:59:02 +00:00
type: "string",
requiresArg: true,
2021-02-19 19:52:02 +00:00
group: "HTML:",
2021-02-12 01:59:02 +00:00
},
descriptions: {
2021-02-19 19:52:02 +00:00
coerce: (arg) => (typeof arg === "number" ? arg : arg ? Infinity : 0),
group: "HTML:",
},
2021-02-19 19:52:02 +00:00
charset: {
type: "string",
2021-02-19 19:32:28 +00:00
hidden: true,
2021-02-19 19:52:02 +00:00
default: "utf-8",
requiresArg: true,
group: "HTML:",
},
2021-02-15 20:56:41 +00:00
image: {
type: "array",
requiresArg: true,
2021-02-19 19:52:02 +00:00
group: "Media:",
2021-02-15 20:56:41 +00:00
},
audio: {
type: "array",
requiresArg: true,
2021-02-19 19:52:02 +00:00
group: "Media:",
2021-02-15 20:56:41 +00:00
},
video: {
type: "array",
requiresArg: true,
2021-02-19 19:52:02 +00:00
group: "Media:",
2021-02-15 20:56:41 +00:00
},
2021-02-19 19:52:02 +00:00
inline: {
type: "boolean",
2021-02-15 22:31:08 +00:00
hidden: true,
2021-02-19 19:52:02 +00:00
group: "CSS:",
2021-02-15 22:31:08 +00:00
},
2021-02-19 19:52:02 +00:00
});
2021-02-16 20:41:27 +00:00
2021-02-20 20:16:36 +00:00
const CSS_VARS = CSS.rootVariables(CSS.load({ css: "gmi-web.css" }));
2021-02-20 21:37:35 +00:00
Object.keys(CSS_VARS).forEach((key) => {
2021-02-20 21:38:58 +00:00
const opt = key.replace("--", "");
2021-02-20 21:37:35 +00:00
cli.option(opt, { default: CSS_VARS[key] });
cli.conflicts(opt, "core");
cli.conflicts(opt, "none");
});
2021-02-20 21:38:58 +00:00
cli.group(
Object.keys(CSS_VARS).map((key) => key.replace("--", "")),
"CSS:"
);
2021-01-29 22:32:30 +00:00
const argv = cli
.conflicts("author", "body")
.conflicts("html", "body")
.alias("html", "language")
.alias("html", "lang")
.showHelpOnFail(true)
2021-01-28 06:16:46 +00:00
.help().argv;
2021-02-15 20:56:41 +00:00
if (!argv.html && !argv.body) {
cli.showHelp();
console.error(`\nMissing required argument: --html or --body`);
cli.exit(1);
}
2021-02-20 21:22:37 +00:00
argv.inlineCSS = CSS.inline(argv);
argv.styleTag = CSS.style(argv);
if (!argv.files) {
let gemtext;
try {
2021-02-15 20:56:41 +00:00
gemtext = readFileSync(process.stdin.fd, "utf-8");
} catch (e) {
2021-02-12 01:59:02 +00:00
cli.showHelp();
console.error("\nMissing files: pipe from stdin or provide [files..]");
cli.exit(1);
}
2021-02-15 22:31:08 +00:00
console.log(toHTML(gemtext, argv));
} else {
fs.src(argv.files)
2021-02-15 22:31:08 +00:00
.pipe(streamHTML(argv))
.pipe(fs.dest((file) => path.dirname(file.path)));
}