-
Notifications
You must be signed in to change notification settings - Fork 166
/
cli.js
executable file
·190 lines (139 loc) · 4.8 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env node
import { readFileSync } from 'node:fs';
import { cliopts, namespacedOptions } from './src/cli-opts.js';
import { pdf, epub, html, md } from './index.js';
const { command, opts, operands } = cliopts(process.argv.slice(2));
const pkg = JSON.parse(
readFileSync(new URL('./package.json', import.meta.url))
);
if (opts.debug) {
console.error({
command,
operands,
opts
});
}
if (opts.version) {
console.log(pkg.version);
process.exit(0);
}
if (opts.help) {
outputHelp();
}
if (!operands.length) {
operands.push('-');
}
if (opts.markdownOptions) {
console.error(
`Unsupported option 'markdownOptions'. Did you mean '--md.<option>=<value>'?`
);
process.exit(1);
}
switch (command) {
case 'pdf':
if (opts.output === '-') {
console.error(
`Output to <stdout> is only supported for commands: 'html', 'md'.`
);
process.exit(1);
}
pdf(operands, opts);
break;
case 'epub':
if (opts.output === '-') {
console.error(
`Output to <stdout> is only supported for commands: 'html', 'md'.`
);
process.exit(1);
}
epub(operands, opts);
break;
case 'html':
html(operands, opts);
break;
case 'md':
md(operands, {
...opts,
markdownOptions: namespacedOptions(opts, 'md')
});
break;
default:
outputHelp(true);
}
/*
Help & version
--------------
*/
function outputHelp(error) {
const helpText = `percollate v${pkg.version}
Usage:
percollate <command> [options] url [url]...
Commands:
pdf Bundle web pages as a PDF file.
epub Bundle web pages as an EPUB file.
html Bundle web pages as a HTML file.
md Bundle web pages as a Markdown file.
Common options:
-h, --help Output usage information.
-V, --version Output program version.
--debug Print more detailed information.
-o <output>, Path for the generated bundle.
--output=<path> Use '-' to output to standard output ('stdout').
--template=<path> Path to a custom HTML template.
--style=<path> Path to a custom CSS file.
--css=<style> Additional inline CSS style.
-u, --url=<url> Sets the base URL when HTML is provided on stdin.
Multiple URL options can be specified.
-w, --wait=<sec> Process the provided URLs sequentially,
pausing a number of seconds between items.
-t <title>, The bundle title.
--title=<title>
-a <author>, The bundle author.
--author=<author>
--individual Export each web page as an individual file.
--toc Generate a Table of Contents.
Implicitly enabled when bundling more than one item.
--toc-level=<level> A number between 1 (default) and 6, representing
the maximum level of headings to include in the
Table of Contents. Implies '--toc'.
--cover Generate a cover for the PDF / EPUB.
Implicitly enabled when bundling more than one item
or the --title option is provided.
--browser=<browser> One of 'chrome' (default), 'firefox'.
Used for producing PDF and the cover image for EPUB.
--hyphenate Enable hyphenation. Enabled by default for PDF.
--inline Embed images inline with the content.
Fetches and converts images to Base64 'data:' URLs.
--unsafe Disable some validations in JSDOM to suppress some
errors thrown for invalid HTML inputs.
Options to disable features:
--no-amp Don't prefer the AMP version of the web page.
--no-toc Don't generate a table of contents.
--no-cover Don't generate a cover.
--no-hyphenate Disable hyphenation.
PDF options:
--no-sandbox Passed to Puppeteer.
Markdown options:
--md.<option>=<value> Options to pass to the Markdown stringifier,
the 'mdast-util-to-markdown' library.
Operands:
percollate accepts one or more URLs.
Use the hyphen character ('-') to specify
that the HTML should be read from stdin.
Examples:
Single web page to PDF:
percollate pdf --output my.pdf https://example.com
Single web page read from stdin to PDF:
curl https://example.com | percollate pdf -o my.pdf -u https://example.com -
Several web pages to a single PDF:
percollate pdf --output my.pdf https://example.com/1 https://example.com/2
Custom page size and font size:
percollate pdf --output my.pdf --css "@page { size: A3 landscape } html { font-size: 18pt }" https://example.com
`;
if (error) {
console.error(helpText);
process.exit(1);
}
console.log(helpText);
process.exit(0);
}