fulgur-cli 0.4.5

HTML/CSS to PDF conversion CLI
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
use clap::{Parser, Subcommand};
use fulgur::asset::AssetBundle;
use fulgur::config::{Margin, PageSize};
use fulgur::engine::Engine;
use std::path::PathBuf;

/// Isolate the real stdout from noise emitted by the render pipeline so that
/// PDF bytes written to stdout (`-o -`) cannot be corrupted by incidental
/// output from dependencies.
///
/// `blitz-html` prints `println!("ERROR: {msg}")` for every non-fatal
/// html5ever parse error in its `TreeSink::finish` implementation. When fulgur
/// renders to stdout, those messages would be written before the PDF bytes,
/// producing a file that does not start with `%PDF-` and is therefore invalid.
///
/// The isolator duplicates fd 1 (real stdout), remaps fd 1 to fd 2 (stderr)
/// so any stray `println!` goes to stderr where the user can see it, and
/// writes PDF bytes directly to the saved fd via `libc::write`. On Drop the
/// real stdout is restored.
///
/// This is safe because the CLI is strictly single-threaded during the render
/// phase — unlike the previous `suppress_stdout` helper inside `blitz_adapter`
/// which was shared by multi-threaded library callers and had a race in its
/// `dup2` manipulation (see
/// `docs/plans/2026-04-11-blitz-thread-safety-investigation.md`).
#[cfg(unix)]
struct StdoutIsolator {
    saved_fd: libc::c_int,
}

#[cfg(unix)]
impl StdoutIsolator {
    /// Install the isolator. Returns `None` if either `dup(1)` or `dup2(2, 1)`
    /// fails, in which case the caller should fall back to an unisolated
    /// write.
    fn install() -> Option<Self> {
        use std::io::Write;
        // Flush any pending stdout buffers before redirecting so nothing is
        // left in std's userland buffer pointing at the old fd.
        let _ = std::io::stdout().flush();

        let saved = unsafe { libc::dup(1) };
        if saved < 0 {
            return None;
        }
        if unsafe { libc::dup2(2, 1) } < 0 {
            unsafe { libc::close(saved) };
            return None;
        }
        Some(Self { saved_fd: saved })
    }

    /// Write the given bytes to the saved real stdout fd, bypassing the
    /// process-wide fd 1 (which is currently pointing at stderr).
    ///
    /// Retries on `EINTR` to match the semantics of
    /// `std::io::Write::write_all`, which we are replacing at the syscall
    /// level. Without this, a signal delivered mid-write (e.g. `SIGWINCH`
    /// on terminal resize, `SIGCHLD` from a child process, or a timer
    /// signal) would surface as a spurious `Interrupted system call`
    /// failure. A `0`-byte return on a non-empty buffer is treated as
    /// `WriteZero` to prevent an infinite loop.
    fn write_all(&self, mut data: &[u8]) -> std::io::Result<()> {
        while !data.is_empty() {
            let written = unsafe {
                libc::write(
                    self.saved_fd,
                    data.as_ptr() as *const libc::c_void,
                    data.len(),
                )
            };
            if written < 0 {
                let err = std::io::Error::last_os_error();
                if err.kind() == std::io::ErrorKind::Interrupted {
                    continue;
                }
                return Err(err);
            }
            if written == 0 {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::WriteZero,
                    "libc::write returned 0 bytes for a non-empty buffer",
                ));
            }
            data = &data[written as usize..];
        }
        Ok(())
    }
}

#[cfg(unix)]
impl Drop for StdoutIsolator {
    fn drop(&mut self) {
        // Flush any bytes still sitting in Rust's `io::Stdout` buffer while
        // fd 1 is still pointing at stderr. `io::Stdout` is a LineWriter that
        // flushes on newline, so the common case of `println!("ERROR: ...")`
        // from dependencies is already flushed inline. This flush is
        // defense-in-depth for writes without a trailing newline (e.g. a
        // future dependency using `print!`, or blitz changing its error
        // sink), and keeps the Drop symmetric with `install()` which also
        // flushes before manipulating fd 1.
        use std::io::Write;
        let _ = std::io::stdout().flush();
        // Restore fd 1 to the real stdout so any final messages the process
        // prints (e.g. panic output on failure paths) land in the right place.
        unsafe {
            libc::dup2(self.saved_fd, 1);
            libc::close(self.saved_fd);
        }
    }
}

#[derive(Parser)]
#[command(name = "fulgur", version, about = "HTML to PDF converter")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
#[allow(clippy::large_enum_variant)]
enum Commands {
    /// Render HTML to PDF
    #[command(after_long_help = "\
\x1b[1;4mTemplate filters:\x1b[0m

  When using --data, the input HTML is processed as a MiniJinja template.
  The following filters are available:

  \x1b[1mBuilt-in filters (MiniJinja):\x1b[0m
    String:  upper, lower, title, capitalize, trim, replace, split, lines
    List:    first, last, length, reverse, sort, unique, join, slice, batch
    Select:  select, reject, selectattr, rejectattr, map, groupby, chain, zip
    Dict:    items, dictsort, attr
    Type:    int, float, bool, string, list, abs, round, sum, min, max
    Format:  format (printf-style), tojson, pprint, urlencode, indent
    Other:   default (d), safe, escape (e)

  \x1b[1mCustom filters:\x1b[0m
    numformat(spec)  Python-style numeric formatting
      {{ price | numformat(\",\") }}      → 1,234,567       (comma separator)
      {{ price | numformat(\",.2f\") }}   → 1,234,567.89    (comma + 2 decimals)
      {{ rate  | numformat(\".2f\") }}    → 10.50            (2 decimal places)
      {{ seq   | numformat(\"04d\") }}    → 0005             (zero-padded)
")]
    Render {
        /// Input HTML file (omit for --stdin)
        #[arg()]
        input: Option<PathBuf>,

        /// Read HTML from stdin
        #[arg(long)]
        stdin: bool,

        /// Output PDF file path (use "-" for stdout)
        #[arg(short, long)]
        output: PathBuf,

        /// Page size (A4, Letter, A3)
        #[arg(short, long)]
        size: Option<String>,

        /// Landscape orientation
        #[arg(short, long)]
        landscape: bool,

        /// PDF title
        #[arg(long)]
        title: Option<String>,

        /// Page margins in mm (CSS shorthand: "20", "20 30", "10 20 30", "10 20 30 40")
        #[arg(long)]
        margin: Option<String>,

        /// Author name (can be specified multiple times)
        #[arg(long = "author")]
        authors: Vec<String>,

        /// Document description
        #[arg(long)]
        description: Option<String>,

        /// Keywords (can be specified multiple times)
        #[arg(long = "keyword")]
        keywords: Vec<String>,

        /// Language code (e.g. ja, en)
        #[arg(long)]
        language: Option<String>,

        /// Creator application name
        #[arg(long)]
        creator: Option<String>,

        /// PDF producer (default: fulgur vX.Y.Z)
        #[arg(long)]
        producer: Option<String>,

        /// Creation date in ISO 8601 format (e.g. 2026-03-22)
        #[arg(long)]
        creation_date: Option<String>,

        /// Font files to bundle (can be specified multiple times)
        #[arg(long = "font", short = 'f')]
        fonts: Vec<PathBuf>,

        /// CSS files to include (can be specified multiple times)
        #[arg(long = "css")]
        css_files: Vec<PathBuf>,

        /// Image files to bundle (name=path, can be specified multiple times)
        /// Example: --image logo.png=assets/logo.png
        #[arg(long = "image", short = 'i')]
        images: Vec<String>,

        /// MiniJinja JSON data file for template rendering ("-" for stdin, see `fulgur template`)
        #[arg(long = "data", short = 'd')]
        data: Option<PathBuf>,

        /// Generate PDF bookmarks (outline) from h1-h6 headings.
        #[arg(long)]
        bookmarks: bool,
    },
    /// Template utilities (powered by MiniJinja)
    Template {
        #[command(subcommand)]
        command: TemplateCommands,
    },
}

#[derive(Subcommand)]
enum TemplateCommands {
    /// Extract JSON Schema from a MiniJinja HTML template.
    /// Analyzes template syntax to infer variable names and types.
    /// With --data, uses actual JSON values for precise type inference.
    Schema {
        /// Input HTML template file
        #[arg()]
        input: PathBuf,

        /// Sample JSON data file for precise type inference (use "-" for stdin)
        #[arg(long = "data", short = 'd')]
        data: Option<PathBuf>,

        /// Output file (default: stdout)
        #[arg(short, long)]
        output: Option<PathBuf>,
    },
}

fn parse_page_size(s: &str) -> PageSize {
    match s.to_uppercase().as_str() {
        "A4" => PageSize::A4,
        "A3" => PageSize::A3,
        "LETTER" => PageSize::LETTER,
        _ => {
            eprintln!("Unknown page size '{}', defaulting to A4", s);
            PageSize::A4
        }
    }
}

fn parse_margin(s: &str) -> Margin {
    let tokens: Vec<&str> = s.split_whitespace().collect();
    let values: Vec<f32> = tokens.iter().filter_map(|v| v.parse().ok()).collect();
    if values.len() != tokens.len() {
        eprintln!(
            "Invalid margin '{}': all values must be numbers (mm). Using default 20mm",
            s
        );
        return Margin::default();
    }
    let to_pt = |mm: f32| mm * 72.0 / 25.4;
    match values.as_slice() {
        [all] => Margin::uniform(to_pt(*all)),
        [vert, horiz] => Margin::symmetric(to_pt(*vert), to_pt(*horiz)),
        [top, horiz, bottom] => Margin {
            top: to_pt(*top),
            right: to_pt(*horiz),
            bottom: to_pt(*bottom),
            left: to_pt(*horiz),
        },
        [top, right, bottom, left] => Margin {
            top: to_pt(*top),
            right: to_pt(*right),
            bottom: to_pt(*bottom),
            left: to_pt(*left),
        },
        _ => {
            eprintln!("Invalid margin '{}', using default 20mm", s);
            Margin::default()
        }
    }
}

fn main() {
    let cli = Cli::parse();

    match cli.command {
        Commands::Render {
            input,
            stdin,
            output,
            size,
            landscape,
            title,
            margin,
            authors,
            description,
            keywords,
            language,
            creator,
            producer,
            creation_date,
            fonts,
            css_files,
            images,
            data,
            bookmarks,
        } => {
            if stdin && data.as_ref().is_some_and(|p| p.as_os_str() == "-") {
                eprintln!("Error: cannot use --stdin and --data - together (both read stdin)");
                std::process::exit(1);
            }

            // Compute base_path before consuming input
            let base_path = if stdin {
                std::env::current_dir().ok()
            } else {
                input.as_ref().and_then(|p| {
                    p.canonicalize()
                        .ok()
                        .and_then(|abs| abs.parent().map(|d| d.to_path_buf()))
                        .or_else(|| {
                            p.parent()
                                .map(|d| d.to_path_buf())
                                .filter(|d| !d.as_os_str().is_empty())
                        })
                        .or_else(|| std::env::current_dir().ok())
                })
            };

            let input_content = if stdin {
                let mut buf = String::new();
                std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
                    .expect("Failed to read stdin");
                buf
            } else if let Some(ref input) = input {
                std::fs::read_to_string(input).unwrap_or_else(|e| {
                    eprintln!("Error reading {}: {e}", input.display());
                    std::process::exit(1);
                })
            } else {
                eprintln!("Error: provide an input HTML file or use --stdin");
                std::process::exit(1);
            };

            // Build assets if fonts, CSS, or images provided
            let assets = if !fonts.is_empty() || !css_files.is_empty() || !images.is_empty() {
                let mut bundle = AssetBundle::new();
                for font_path in &fonts {
                    bundle.add_font_file(font_path).unwrap_or_else(|e| {
                        eprintln!("Warning: failed to load font {}: {e}", font_path.display());
                    });
                }
                for css_path in &css_files {
                    bundle.add_css_file(css_path).unwrap_or_else(|e| {
                        eprintln!("Warning: failed to load CSS {}: {e}", css_path.display());
                    });
                }
                for image_spec in &images {
                    if let Some((name, path)) = image_spec.split_once('=') {
                        bundle.add_image_file(name, path).unwrap_or_else(|e| {
                            eprintln!("Warning: failed to load image {}: {e}", path);
                        });
                    } else {
                        // Use filename as the image name
                        let path = std::path::Path::new(image_spec);
                        let name = path
                            .file_name()
                            .and_then(|n| n.to_str())
                            .unwrap_or(image_spec);
                        bundle.add_image_file(name, path).unwrap_or_else(|e| {
                            eprintln!("Warning: failed to load image {}: {e}", image_spec);
                        });
                    }
                }
                Some(bundle)
            } else {
                None
            };

            let mut builder = Engine::builder();
            if let Some(ref s) = size {
                builder = builder.page_size(parse_page_size(s));
            }
            if landscape {
                builder = builder.landscape(landscape);
            }

            if let Some(ref m) = margin {
                builder = builder.margin(parse_margin(m));
            }
            if let Some(title) = title {
                builder = builder.title(title);
            }
            if !authors.is_empty() {
                builder = builder.authors(authors);
            }
            if let Some(description) = description {
                builder = builder.description(description);
            }
            if !keywords.is_empty() {
                builder = builder.keywords(keywords);
            }
            if let Some(language) = language {
                builder = builder.lang(language);
            }
            if let Some(creator) = creator {
                builder = builder.creator(creator);
            }
            if let Some(producer) = producer {
                builder = builder.producer(producer);
            }
            if let Some(creation_date) = creation_date {
                builder = builder.creation_date(creation_date);
            }
            if bookmarks {
                builder = builder.bookmarks(true);
            }
            if let Some(ref base_path) = base_path {
                builder = builder.base_path(base_path);
            }
            if let Some(assets) = assets {
                builder = builder.assets(assets);
            }

            // Template mode: add template and data to builder
            if let Some(ref data_path) = data {
                let json_str = if data_path.as_os_str() == "-" {
                    let mut buf = String::new();
                    std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
                        .expect("Failed to read JSON from stdin");
                    buf
                } else {
                    std::fs::read_to_string(data_path).unwrap_or_else(|e| {
                        eprintln!("Error reading data file {}: {e}", data_path.display());
                        std::process::exit(1);
                    })
                };
                let json_data: serde_json::Value =
                    serde_json::from_str(&json_str).unwrap_or_else(|e| {
                        eprintln!("Error parsing JSON: {e}");
                        std::process::exit(1);
                    });
                let template_name = input
                    .as_ref()
                    .and_then(|p| p.file_name())
                    .and_then(|n| n.to_str())
                    .unwrap_or("template.html");
                builder = builder
                    .template(template_name, &input_content)
                    .data(json_data);
            }

            let engine = builder.build();

            // Isolate stdout BEFORE rendering for both output modes: blitz-html
            // prints `println!("ERROR: {e}")` for every non-fatal html5ever
            // parse-error recovery, and we want those on stderr where they
            // belong (a) so they don't corrupt the PDF stream in `-o -` mode,
            // and (b) so they don't pollute the user's terminal stdout in
            // file-output mode. The isolator redirects fd 1 -> fd 2 for the
            // duration of the render.
            //
            // Install failure handling depends on the output mode:
            // * `-o -` (writing PDF to stdout) MUST have the isolator — a
            //   failure would leave dependency noise free to corrupt the PDF
            //   bytes. Abort with a clear error so the user can investigate.
            // * File output can tolerate an install failure: the worst case
            //   is blitz parse-error lines leaking to the user's terminal,
            //   which is UX noise but not a correctness bug.
            let to_stdout = output.as_os_str() == "-";
            #[cfg(unix)]
            let stdout_isolator = {
                let iso = StdoutIsolator::install();
                if to_stdout && iso.is_none() {
                    eprintln!(
                        "Error: failed to isolate stdout for `-o -` output. \
                         Refusing to write PDF bytes without protection — \
                         dependency output could corrupt the stream. \
                         Retry with `-o <file>` or investigate the environment \
                         (fd 1 closed? per-process fd limit reached?)."
                    );
                    std::process::exit(1);
                }
                iso
            };

            let pdf = if data.is_some() {
                engine.render()
            } else {
                engine.render_html(&input_content)
            }
            .unwrap_or_else(|e| {
                eprintln!("Error: {e}");
                std::process::exit(1);
            });

            if to_stdout {
                #[cfg(unix)]
                {
                    // Install is verified above for `-o -` mode, so the
                    // isolator is guaranteed to be Some here.
                    let iso = stdout_isolator
                        .as_ref()
                        .expect("isolator install verified non-None for -o -");
                    iso.write_all(&pdf).unwrap_or_else(|e| {
                        eprintln!("Error writing to stdout: {e}");
                        std::process::exit(1);
                    });
                }
                #[cfg(not(unix))]
                {
                    // Non-unix build: no StdoutIsolator available. Dependency
                    // output may interleave with the PDF bytes; the Unix path
                    // is the supported configuration for `-o -`.
                    use std::io::Write;
                    std::io::stdout().write_all(&pdf).unwrap_or_else(|e| {
                        eprintln!("Error writing to stdout: {e}");
                        std::process::exit(1);
                    });
                }
            } else {
                std::fs::write(&output, &pdf).unwrap_or_else(|e| {
                    eprintln!("Error writing to {}: {e}", output.display());
                    std::process::exit(1);
                });
                eprintln!("PDF written to {}", output.display());
            }
        }
        Commands::Template { command } => match command {
            TemplateCommands::Schema {
                input,
                data,
                output,
            } => {
                let template_str = std::fs::read_to_string(&input).unwrap_or_else(|e| {
                    eprintln!("Error reading {}: {e}", input.display());
                    std::process::exit(1);
                });
                let template_name = input
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("template.html");

                let schema = if let Some(ref data_path) = data {
                    let json_str = if data_path.as_os_str() == "-" {
                        let mut buf = String::new();
                        std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
                            .expect("Failed to read JSON from stdin");
                        buf
                    } else {
                        std::fs::read_to_string(data_path).unwrap_or_else(|e| {
                            eprintln!("Error reading {}: {e}", data_path.display());
                            std::process::exit(1);
                        })
                    };
                    let json_data: serde_json::Value = serde_json::from_str(&json_str)
                        .unwrap_or_else(|e| {
                            eprintln!("Error parsing JSON: {e}");
                            std::process::exit(1);
                        });
                    fulgur::schema::extract_schema_with_data(
                        &template_str,
                        template_name,
                        &json_data,
                    )
                } else {
                    fulgur::schema::extract_schema(&template_str, template_name)
                }
                .unwrap_or_else(|e| {
                    eprintln!("Error extracting schema: {e}");
                    std::process::exit(1);
                });

                let json_output = serde_json::to_string_pretty(&schema).unwrap();

                if let Some(ref output_path) = output {
                    std::fs::write(output_path, &json_output).unwrap_or_else(|e| {
                        eprintln!("Error writing to {}: {e}", output_path.display());
                        std::process::exit(1);
                    });
                    eprintln!("Schema written to {}", output_path.display());
                } else {
                    println!("{json_output}");
                }
            }
        },
    }
}