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";
|
2021-02-10 22:03:48 +00:00
|
|
|
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";
|
2021-02-11 20:57:08 +00:00
|
|
|
|
|
|
|
const cli = yargs(process.argv.slice(2))
|
2021-02-12 21:17:00 +00:00
|
|
|
.config("config", function (path) {
|
2021-02-15 20:56:41 +00:00
|
|
|
return JSON.parse(readFileSync(path), "utf-8");
|
2021-02-12 21:17:00 +00:00
|
|
|
})
|
2021-02-11 20:57:08 +00:00
|
|
|
.scriptName("gmi-web")
|
2021-02-11 23:26:29 +00:00
|
|
|
.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")
|
2021-02-11 23:26:29 +00:00
|
|
|
.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.")
|
2021-02-11 20:57:08 +00:00
|
|
|
)
|
|
|
|
.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
|
|
|
},
|
2021-02-11 23:26:29 +00:00
|
|
|
html: {
|
|
|
|
type: "string",
|
|
|
|
requiresArg: true,
|
2021-02-19 19:52:02 +00:00
|
|
|
group: "Core:",
|
|
|
|
},
|
|
|
|
css: {
|
|
|
|
requiresArg: true,
|
|
|
|
group: "Core:",
|
2021-02-11 20:57:08 +00:00
|
|
|
},
|
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
|
|
|
},
|
2021-02-12 21:59:18 +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
|
|
|
},
|
2021-02-12 21:59:18 +00:00
|
|
|
descriptions: {
|
2021-02-19 19:52:02 +00:00
|
|
|
coerce: (arg) => (typeof arg === "number" ? arg : arg ? Infinity : 0),
|
|
|
|
group: "HTML:",
|
2021-02-12 21:59:18 +00:00
|
|
|
},
|
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-18 16:43:25 +00:00
|
|
|
},
|
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-15 20:56:41 +00:00
|
|
|
Object.keys(CSS_VARS).map((key) => {
|
|
|
|
cli.option(key, { default: CSS_VARS[key] });
|
2021-02-15 22:56:53 +00:00
|
|
|
cli.conflicts(key, "core");
|
|
|
|
cli.conflicts(key, "none");
|
2021-02-11 20:57:08 +00:00
|
|
|
return key;
|
|
|
|
});
|
2021-02-15 22:31:08 +00:00
|
|
|
cli.group(Object.keys(CSS_VARS), "CSS:");
|
2021-01-29 22:32:30 +00:00
|
|
|
|
2021-02-11 20:57:08 +00:00
|
|
|
const argv = cli
|
2021-02-12 21:59:18 +00:00
|
|
|
.conflicts("author", "body")
|
2021-02-11 23:26:29 +00:00
|
|
|
.conflicts("html", "body")
|
|
|
|
.alias("html", "language")
|
|
|
|
.alias("html", "lang")
|
2021-02-11 20:57:08 +00:00
|
|
|
.showHelpOnFail(true)
|
2021-01-28 06:16:46 +00:00
|
|
|
.help().argv;
|
2021-02-11 20:57:08 +00:00
|
|
|
|
2021-02-15 20:56:41 +00:00
|
|
|
if (!argv.html && !argv.body) {
|
2021-02-11 23:26:29 +00:00
|
|
|
cli.showHelp();
|
|
|
|
console.error(`\nMissing required argument: --html or --body`);
|
|
|
|
cli.exit(1);
|
2021-02-11 20:57:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 21:22:37 +00:00
|
|
|
argv.inlineCSS = CSS.inline(argv);
|
|
|
|
argv.styleTag = CSS.style(argv);
|
|
|
|
|
2021-02-11 20:57:08 +00:00
|
|
|
if (!argv.files) {
|
2021-02-11 23:26:29 +00:00
|
|
|
let gemtext;
|
2021-02-11 20:57:08 +00:00
|
|
|
try {
|
2021-02-15 20:56:41 +00:00
|
|
|
gemtext = readFileSync(process.stdin.fd, "utf-8");
|
2021-02-11 20:57:08 +00:00
|
|
|
} catch (e) {
|
2021-02-12 01:59:02 +00:00
|
|
|
cli.showHelp();
|
|
|
|
console.error("\nMissing files: pipe from stdin or provide [files..]");
|
2021-02-11 23:26:29 +00:00
|
|
|
cli.exit(1);
|
2021-02-11 20:57:08 +00:00
|
|
|
}
|
2021-02-15 22:31:08 +00:00
|
|
|
console.log(toHTML(gemtext, argv));
|
2021-02-11 20:57:08 +00:00
|
|
|
} else {
|
|
|
|
fs.src(argv.files)
|
2021-02-15 22:31:08 +00:00
|
|
|
.pipe(streamHTML(argv))
|
2021-02-11 20:57:08 +00:00
|
|
|
.pipe(fs.dest((file) => path.dirname(file.path)));
|
|
|
|
}
|