@projektemacher/vivliostyle-batch-cli 0.0.1

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.
package/LICENCE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright © 2010-2024 three.js authors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # Vivliostyle CLI Wrapper
2
+
3
+ A powerful CLI wrapper for [Vivliostyle](https://vivliostyle.org/) with enhanced static asset handling and HTML parsing capabilities.
4
+
5
+ ## Features
6
+
7
+ - Build PDF, EPUB, and WebPub output from HTML or publication manifests
8
+ - Preview documents in the browser with live reloading
9
+ - Automatic detection and mapping of static assets from HTML (`<link>`, `<script>`, `<img>`, etc.)
10
+ - Support for custom static asset mappings via `--static`
11
+ - Map external URLs (CDNs, etc.) to local directories using `--asset-base`
12
+ - Ignore specific assets with `--ignore-asset`
13
+ - Forward extra Vivliostyle options using `--`
14
+ - Debug mode with detailed logging
15
+ - Temporary file management and cleanup
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ #npm install -g @vivliostyle/cli
21
+ npm install -g @projektemacher/vivliostyle-batch-cli
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ### Build a PDF from an HTML file
27
+
28
+ ```bash
29
+ vivliostyle-cli -i index.html -o output.pdf
30
+ ```
31
+
32
+ ### Preview an HTML file in the browser
33
+
34
+ ```bash
35
+ vivliostyle-cli -i index.html --preview
36
+ ```
37
+
38
+ ### Build with custom static asset mappings
39
+
40
+ ```bash
41
+ vivliostyle-cli -i index.html -o output.pdf \
42
+ --static /assets:/home/user/project/assets \
43
+ --static /fonts:/home/user/project/fonts
44
+ ```
45
+
46
+ ### Map an external CDN URL to a local cache
47
+
48
+ ```bash
49
+ vivliostyle-cli -i index.html -o output.pdf \
50
+ --asset-base https://cdn.example.com/=/home/user/cdn-cache
51
+ ```
52
+
53
+ ### Pass extra Vivliostyle options after `--`
54
+
55
+ ```bash
56
+ vivliostyle-cli -i index.html -o output.pdf -- --timeout 60000 --debug
57
+ ```
58
+
59
+ ## Options
60
+
61
+ | Option | Description |
62
+ | ---------------------------------- | ---------------------------------------------------------------------------- |
63
+ | `-i, --input <input>` | Input HTML or publication manifest file (required) |
64
+ | `-o, --output <file>` | Output file path (default: `output.pdf`) |
65
+ | `--title <title>` | Document title (overrides source) |
66
+ | `--author <author>` | Document author |
67
+ | `--language <lang>` | Document language tag (e.g. `en`, `de`, `ja`) (default: `de`) |
68
+ | `--static <mapping>` | Map virtual path to local path: `/virtual/path:/local/path` (repeatable) |
69
+ | `--no-scripts` | Do not map `<script src>` tags as static assets (recommended for PDF builds) |
70
+ | `--asset-base <urlBase=localBase>` | Map URLs starting with `urlBase` to files under `localBase` (repeatable) |
71
+ | `--ignore-asset <path>` | Skip specific virtual paths when deriving static mappings (repeatable) |
72
+ | `--cwd <dir>` | Working directory for Vivliostyle (default: directory of `--input`) |
73
+ | `--format <format>` | Output format: `pdf`, `epub`, or `webpub` (default: `pdf`) |
74
+ | `--log-level <level>` | Log level: `silent`, `info`, `verbose`, or `debug` (default: `info`) |
75
+ | `--mode <mode>` | Execution mode: `build` or `preview` (default: `build`) |
76
+ | `--preview` | Shorthand for `--mode preview` — open result in browser |
77
+ | `-d, --debug` | Enable debug mode (sets log level to `debug`) |
78
+
79
+ ## Notes
80
+
81
+ - HTML input (`.html`/`.htm`) is auto-detected by file extension.
82
+ - For HTML input, `<link href>` and `<script src>` tags are parsed and automatically mapped.
83
+ - Options after `--` are forwarded verbatim to Vivliostyle.
84
+ - `--debug` automatically sets `--log-level` to `debug`.
85
+ - `--preview` and `--mode preview` are equivalent.
86
+ - In **preview mode**, Vivliostyle serves everything via its own Vite server.
87
+ - In **build mode**, an Express server is started to serve assets.
88
+ - `--asset-base` local directories are also used as fallback roots for CSS-referenced assets (fonts, images, etc.).
89
+
90
+ ## Examples
91
+
92
+ ```bash
93
+ # Build a PDF from an HTML file
94
+ vivliostyle-cli -i index.html -o output.pdf
95
+
96
+ # Preview an HTML file in browser
97
+ vivliostyle-cli -i index.html --preview
98
+
99
+ # Build with explicit static asset mapping
100
+ vivliostyle-cli -i index.html -o output.pdf \
101
+ --static /assets:/dist/assets --static /fonts:/dist/fonts
102
+
103
+ # Map an absolute CDN URL to a local directory
104
+ vivliostyle-cli -i index.html -o output.pdf \
105
+ --asset-base https://cdn.example.com/=/home/user/cdn-cache
106
+
107
+ # Pass extra Vivliostyle options after --
108
+ vivliostyle-cli -i index.html -o output.pdf -- --timeout 60000
109
+ ```
110
+
111
+ ## License
112
+
113
+ MIT
@@ -0,0 +1,105 @@
1
+ type AssetBaseMapping = {
2
+ urlBase: string;
3
+ localBase: string;
4
+ };
5
+ type CliOptions = {
6
+ input: string;
7
+ output: string;
8
+ title?: string;
9
+ author?: string;
10
+ language?: string;
11
+ static: string[];
12
+ scripts: boolean;
13
+ cwd?: string;
14
+ format: string;
15
+ logLevel: string;
16
+ debug?: boolean;
17
+ assetBase: string[];
18
+ ignoreAsset: string[];
19
+ mode: string;
20
+ preview?: boolean;
21
+ };
22
+ type Dbg = (_label: string, _value?: unknown) => void;
23
+ /**
24
+ * Returns true when the input file should be treated as HTML.
25
+ * Vivliostyle also accepts publication manifests (JSON/TOML/JS config files);
26
+ * we detect HTML by extension to avoid trying to parse those as markup.
27
+ */
28
+ declare function isHtmlInput(inputAbs: string): boolean;
29
+ declare function splitArgsAtDoubleDash(argv: string[]): {
30
+ cliArgv: string[];
31
+ extraArgs: string[];
32
+ };
33
+ type StaticServer = {
34
+ baseUrl: string;
35
+ close: () => void;
36
+ };
37
+ declare function startStaticServer(staticMap: Record<string, string>, assetBases: AssetBaseMapping[], htmlFilePath: string | null, dbg: Dbg, port?: number): Promise<StaticServer>;
38
+ declare function rewriteAbsoluteUrlsInDom(document: Document, assetBases: AssetBaseMapping[]): boolean;
39
+ declare function rewriteVirtualPathsToServerInDom(document: Document, staticMapKeys: string[], serverBaseUrl: string): boolean;
40
+ declare function rewriteAbsoluteUrls(htmlContent: string, assetBases: AssetBaseMapping[]): string;
41
+ declare function rewriteVirtualPathsToServer(htmlContent: string, staticMap: Record<string, string>, serverBaseUrl: string): string;
42
+ declare function extractUrlsFromHtml(htmlPath: string, includeScripts?: boolean, doc?: Document): string[];
43
+ /**
44
+ * Rewrites absolute external URLs to root-relative virtual paths and writes
45
+ * the result as a sibling of the original input file so that
46
+ * cwd = dirname(inputAbs) remains valid for Vivliostyle's path resolution.
47
+ *
48
+ * The output filename uses a non-dotfile prefix so Vite's static server
49
+ * (which ignores dotfiles by default) can serve it correctly.
50
+ *
51
+ * Also derives extra static mounts from each assetBase so that secondary
52
+ * assets referenced from CSS (fonts, images) are served by Vivliostyle's
53
+ * own Vite server without needing Express.
54
+ *
55
+ * Returns:
56
+ * - htmlPath – sibling file path, or inputAbs if no rewriting was needed
57
+ * - extraStatic – virtual→local entries for assetBase roots to merge into
58
+ * the static map passed to Vivliostyle
59
+ * - cleanup – deletes the sibling file (no-op if inputAbs was returned)
60
+ */
61
+ declare function buildPreviewHtml(inputAbs: string, assetBases: AssetBaseMapping[], dbg: Dbg): {
62
+ htmlPath: string;
63
+ extraStatic: Record<string, string>;
64
+ cleanup: () => void;
65
+ };
66
+ /**
67
+ * Prepares the HTML for BUILD mode.
68
+ * Both rewrite passes (absolute → virtual, virtual → absolute server URL)
69
+ * are applied in a single DOM parse.
70
+ * If nothing changed the original file path is returned with a no-op cleanup.
71
+ */
72
+ declare function prepareInputHtmlForBuild(inputAbs: string, assetBases: AssetBaseMapping[], staticMap: Record<string, string>, serverBaseUrl: string, dbg: Dbg): {
73
+ vivliostyleInput: string;
74
+ cleanup: () => void;
75
+ };
76
+ declare function normalizeUrlBase(urlBase: string): string;
77
+ declare function normalizeIgnoreAssetPath(pathValue: string): string;
78
+ declare function shouldIgnoreVirtualPath(virtualPath: string, ignoredAssets: Set<string>): boolean;
79
+ declare function parseAssetBaseMapping(value: string): AssetBaseMapping;
80
+ declare function mapAbsoluteUrlToLocal(url: string, assetBases: AssetBaseMapping[]): {
81
+ virtualPath: string;
82
+ localPath: string;
83
+ } | null;
84
+ type MappingResult = {
85
+ kind: "mapped";
86
+ mapping: string;
87
+ } | {
88
+ kind: "skipped";
89
+ url: string;
90
+ reason: string;
91
+ };
92
+ declare function urlToStaticMapping(url: string, htmlDir: string, assetBases: AssetBaseMapping[], ignoredAssets: Set<string>, dbg: Dbg): MappingResult;
93
+ declare function parseStaticMapping(mapping: string): {
94
+ virtual: string;
95
+ local: string;
96
+ };
97
+ declare function parseExtraArgs(extraArgs: string[]): Record<string, unknown>;
98
+ declare function printHelp(): void;
99
+ declare function parseArgs(argv: string[]): {
100
+ options: CliOptions;
101
+ extraArgs: string[];
102
+ } | null;
103
+ declare function execute(options: CliOptions, extraArgs?: string[]): Promise<void>;
104
+
105
+ export { buildPreviewHtml, execute, extractUrlsFromHtml, isHtmlInput, mapAbsoluteUrlToLocal, normalizeIgnoreAssetPath, normalizeUrlBase, parseArgs, parseAssetBaseMapping, parseExtraArgs, parseStaticMapping, prepareInputHtmlForBuild, printHelp, rewriteAbsoluteUrls, rewriteAbsoluteUrlsInDom, rewriteVirtualPathsToServer, rewriteVirtualPathsToServerInDom, shouldIgnoreVirtualPath, splitArgsAtDoubleDash, startStaticServer, urlToStaticMapping };