unhwp-cli 0.2.4

Command-line tool for converting HWP/HWPX documents to Markdown
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
//! unhwp CLI - HWP/HWPX document extraction tool
//!
//! A command-line tool for extracting content from HWP and HWPX files.

mod update;

use clap::{Parser, Subcommand, ValueEnum};
use colored::*;
use indicatif::{ProgressBar, ProgressStyle};
use std::fs;
use std::io::{self, Write};
use std::path::PathBuf;
use unhwp::{parse_file, render, RenderOptions, TableFallback};

/// HWP/HWPX document extraction to Markdown, text, and JSON
#[derive(Parser)]
#[command(
    name = "unhwp",
    author = "iyulab",
    version,
    about = "Extract content from HWP/HWPX documents",
    long_about = "unhwp - High-performance HWP/HWPX document extraction tool.\n\n\
                  Converts HWP and HWPX files to Markdown, plain text, or JSON.\n\n\
                  Usage:\n  \
                  unhwp <file>              Extract all formats to output directory\n  \
                  unhwp <file> <output>     Extract to specified directory\n  \
                  unhwp md <file>           Convert to Markdown only"
)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,

    /// Input file path (for default conversion)
    #[arg(global = false)]
    input: Option<PathBuf>,

    /// Output directory (for default conversion)
    #[arg(global = false)]
    output: Option<PathBuf>,

    /// Apply text cleanup preset
    #[arg(long, global = true)]
    cleanup: Option<CleanupMode>,
}

#[derive(Subcommand)]
enum Commands {
    /// Convert a document (default command - extracts all formats)
    Convert {
        /// Input file path
        input: PathBuf,

        /// Output directory (default: <filename>_output)
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// Apply text cleanup
        #[arg(long)]
        cleanup: Option<CleanupMode>,
    },

    /// Convert a document to Markdown
    #[command(visible_alias = "md")]
    Markdown {
        /// Input file path
        input: PathBuf,

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

        /// Include YAML frontmatter with metadata
        #[arg(short, long)]
        frontmatter: bool,

        /// Table rendering mode
        #[arg(long, default_value = "markdown")]
        table_mode: TableMode,

        /// Apply text cleanup
        #[arg(long)]
        cleanup: Option<CleanupMode>,

        /// Maximum heading level (1-6, default: 4)
        #[arg(long, default_value = "4")]
        max_heading: u8,
    },

    /// Convert a document to plain text
    Text {
        /// Input file path
        input: PathBuf,

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

        /// Apply text cleanup
        #[arg(long)]
        cleanup: Option<CleanupMode>,
    },

    /// Convert a document to JSON
    Json {
        /// Input file path
        input: PathBuf,

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

        /// Output compact JSON (no indentation)
        #[arg(long)]
        compact: bool,
    },

    /// Show document information and metadata
    Info {
        /// Input file path
        input: PathBuf,
    },

    /// Extract resources (images, media) from a document
    Extract {
        /// Input file path
        input: PathBuf,

        /// Output directory for resources
        #[arg(short, long, default_value = ".")]
        output: PathBuf,
    },

    /// Update unhwp to the latest version
    Update {
        /// Check only, don't install
        #[arg(long)]
        check: bool,

        /// Force update even if on latest version
        #[arg(long)]
        force: bool,
    },

    /// Show version information
    Version,
}

/// Table rendering mode
#[derive(Clone, ValueEnum)]
enum TableMode {
    /// Standard Markdown tables
    Markdown,
    /// HTML tables (for complex layouts)
    Html,
    /// Skip tables
    Skip,
}

impl From<TableMode> for TableFallback {
    fn from(mode: TableMode) -> Self {
        match mode {
            TableMode::Markdown => TableFallback::SimplifiedMarkdown,
            TableMode::Html => TableFallback::Html,
            TableMode::Skip => TableFallback::Skip,
        }
    }
}

/// Cleanup mode
#[derive(Clone, ValueEnum)]
enum CleanupMode {
    /// No cleanup
    None,
    /// Minimal cleanup
    Minimal,
    /// Standard cleanup (default)
    Standard,
    /// Aggressive cleanup
    Aggressive,
}

/// Check if we should perform background update check.
/// Skip for update/version commands to avoid redundant checks.
fn should_check_update(cli: &Cli) -> bool {
    !matches!(
        &cli.command,
        Some(Commands::Update { .. }) | Some(Commands::Version)
    )
}

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

    // Start background update check (except for update/version commands)
    let update_rx = if should_check_update(&cli) {
        Some(update::check_update_async())
    } else {
        None
    };

    // Run the main command
    let result = run(cli);

    // Check for update result and show notification if available
    if let Some(rx) = update_rx {
        if let Some(update_result) = update::try_get_update_result(&rx) {
            update::print_update_notification(&update_result);
        }
    }

    // Handle errors
    if let Err(e) = result {
        eprintln!("{}: {}", "Error".red().bold(), e);
        std::process::exit(1);
    }
}

fn run(cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
    // Handle default command (unhwp <file> [output])
    if cli.command.is_none() {
        if let Some(input) = cli.input {
            return run_convert(&input, cli.output.as_ref(), cli.cleanup);
        } else {
            // No input provided, show help
            use clap::CommandFactory;
            Cli::command().print_help()?;
            return Ok(());
        }
    }

    match cli.command.unwrap() {
        Commands::Convert {
            input,
            output,
            cleanup,
        } => {
            run_convert(&input, output.as_ref(), cleanup)?;
        }

        Commands::Markdown {
            input,
            output,
            frontmatter,
            table_mode,
            cleanup,
            max_heading,
        } => {
            let pb = create_spinner("Parsing document...");

            let doc = parse_file(&input)?;
            pb.set_message("Rendering to Markdown...");

            let mut options = RenderOptions::default()
                .with_table_fallback(table_mode.into())
                .with_max_heading_level(max_heading);

            if frontmatter {
                options = options.with_frontmatter();
            }

            apply_cleanup(&mut options, cleanup);

            let markdown = render::render_markdown(&doc, &options)?;

            pb.finish_and_clear();
            write_output(output.as_ref(), &markdown)?;

            if output.is_some() {
                println!(
                    "{} Converted to Markdown: {}",
                    "".green().bold(),
                    output.unwrap().display()
                );
            }
        }

        Commands::Text {
            input,
            output,
            cleanup,
        } => {
            let pb = create_spinner("Parsing document...");

            let doc = parse_file(&input)?;
            pb.set_message("Extracting text...");

            let text = doc.plain_text();

            pb.finish_and_clear();
            write_output(output.as_ref(), &text)?;

            if output.is_some() {
                println!(
                    "{} Converted to text: {}",
                    "".green().bold(),
                    output.unwrap().display()
                );
            }

            // Suppress unused variable warning - cleanup is accepted for API consistency
            let _ = cleanup;
        }

        Commands::Json {
            input,
            output,
            compact,
        } => {
            let pb = create_spinner("Parsing document...");

            let doc = parse_file(&input)?;
            pb.set_message("Rendering to JSON...");

            let json = if compact {
                serde_json::to_string(&doc)?
            } else {
                serde_json::to_string_pretty(&doc)?
            };

            pb.finish_and_clear();
            write_output(output.as_ref(), &json)?;

            if output.is_some() {
                println!(
                    "{} Converted to JSON: {}",
                    "".green().bold(),
                    output.unwrap().display()
                );
            }
        }

        Commands::Info { input } => {
            let pb = create_spinner("Analyzing document...");

            let format = unhwp::detect_format_from_path(&input)?;
            let doc = parse_file(&input)?;

            pb.finish_and_clear();

            println!("{}", "Document Information".cyan().bold());
            println!("{}", "".repeat(40));
            println!(
                "{}: {}",
                "File".bold(),
                input.file_name().unwrap_or_default().to_string_lossy()
            );
            println!("{}: {:?}", "Format".bold(), format);
            println!("{}: {}", "Sections".bold(), doc.sections.len());
            println!("{}: {}", "Resources".bold(), doc.resources.len());

            if let Some(ref title) = doc.metadata.title {
                println!("{}: {}", "Title".bold(), title);
            }
            if let Some(ref author) = doc.metadata.author {
                println!("{}: {}", "Author".bold(), author);
            }
            if let Some(ref created) = doc.metadata.created {
                println!("{}: {}", "Created".bold(), created);
            }
            if let Some(ref modified) = doc.metadata.modified {
                println!("{}: {}", "Modified".bold(), modified);
            }
            if doc.metadata.is_distribution {
                println!("{}: {}", "Distribution".bold(), "Yes (DRM protected)");
            }

            let text = doc.plain_text();
            let word_count = text.split_whitespace().count();
            let char_count = text.len();
            println!("\n{}", "Content Statistics".cyan().bold());
            println!("{}", "".repeat(40));
            println!("{}: {}", "Words".bold(), word_count);
            println!("{}: {}", "Characters".bold(), char_count);
            println!("{}: {}", "Paragraphs".bold(), doc.paragraph_count());
        }

        Commands::Extract { input, output } => {
            let pb = create_spinner("Extracting resources...");

            let doc = parse_file(&input)?;

            fs::create_dir_all(&output)?;

            let mut count = 0;
            for (name, resource) in &doc.resources {
                let path = output.join(name);
                fs::write(&path, &resource.data)?;
                count += 1;
            }

            pb.finish_and_clear();

            if count > 0 {
                println!(
                    "{} Extracted {} resources to {}",
                    "".green().bold(),
                    count,
                    output.display()
                );
            } else {
                println!("{} No resources found in document", "!".yellow().bold());
            }
        }

        Commands::Update { check, force } => {
            if let Err(e) = update::run_update(check, force) {
                eprintln!("{}: {}", "Error".red().bold(), e);
                std::process::exit(1);
            }
        }

        Commands::Version => {
            print_version();
        }
    }

    Ok(())
}

/// Run the default convert command - extracts all formats to output directory
fn run_convert(
    input: &PathBuf,
    output: Option<&PathBuf>,
    cleanup: Option<CleanupMode>,
) -> Result<(), Box<dyn std::error::Error>> {
    let pb = create_spinner("Parsing document...");

    // Determine output directory
    let output_dir = match output {
        Some(p) => p.clone(),
        None => {
            let stem = input
                .file_stem()
                .unwrap_or_default()
                .to_string_lossy()
                .to_string();
            let parent = input.parent().unwrap_or(std::path::Path::new("."));
            parent.join(format!("{}_output", stem))
        }
    };

    // Create output directory
    fs::create_dir_all(&output_dir)?;

    // Parse document
    let doc = parse_file(input)?;

    // Prepare render options
    let mut options = RenderOptions::default()
        .with_frontmatter()
        .with_image_prefix("images/");

    apply_cleanup(&mut options, cleanup);

    // Generate Markdown
    pb.set_message("Generating Markdown...");
    let markdown = render::render_markdown(&doc, &options)?;
    let md_path = output_dir.join("extract.md");
    fs::write(&md_path, &markdown)?;

    // Generate plain text
    pb.set_message("Generating text...");
    let text = doc.plain_text();
    let txt_path = output_dir.join("extract.txt");
    fs::write(&txt_path, &text)?;

    // Generate JSON
    pb.set_message("Generating JSON...");
    let json = serde_json::to_string_pretty(&doc)?;
    let json_path = output_dir.join("content.json");
    fs::write(&json_path, &json)?;

    // Extract resources
    let mut image_count = 0;
    if !doc.resources.is_empty() {
        pb.set_message("Extracting resources...");
        let images_dir = output_dir.join("images");
        fs::create_dir_all(&images_dir)?;

        for (name, resource) in &doc.resources {
            fs::write(images_dir.join(name), &resource.data)?;
            image_count += 1;
        }
    }

    pb.finish_and_clear();

    // Print summary
    println!("{}", "Conversion Complete".green().bold());
    println!("{}", "".repeat(40));
    println!("{}: {}", "Output".bold(), output_dir.display());
    println!("  {} extract.md", "".green());
    println!("  {} extract.txt", "".green());
    println!("  {} content.json", "".green());
    if image_count > 0 {
        println!("  {} images/ ({} files)", "".green(), image_count);
    }

    // Print statistics
    let word_count = text.split_whitespace().count();
    println!("\n{}", "Statistics".cyan().bold());
    println!("{}", "".repeat(40));
    println!("{}: {}", "Sections".bold(), doc.sections.len());
    println!("{}: {}", "Words".bold(), word_count);
    println!("{}: {}", "Resources".bold(), image_count);

    Ok(())
}

fn apply_cleanup(options: &mut RenderOptions, cleanup: Option<CleanupMode>) {
    if let Some(mode) = cleanup {
        match mode {
            CleanupMode::None => {}
            CleanupMode::Minimal => *options = options.clone().with_minimal_cleanup(),
            CleanupMode::Standard => *options = options.clone().with_cleanup(),
            CleanupMode::Aggressive => *options = options.clone().with_aggressive_cleanup(),
        }
    }
}

fn print_version() {
    println!("{} {}", "unhwp".green().bold(), env!("CARGO_PKG_VERSION"));
    println!("High-performance HWP/HWPX document extraction to Markdown");
    println!();
    println!("Supported formats: HWP 5.0, HWPX, HWP 3.x");
    println!("Repository: https://github.com/iyulab/unhwp");
}

fn create_spinner(message: &str) -> ProgressBar {
    let pb = ProgressBar::new_spinner();
    pb.set_style(
        ProgressStyle::default_spinner()
            .tick_strings(&["", "", "", "", "", "", "", "", "", ""])
            .template("{spinner:.blue} {msg}")
            .unwrap(),
    );
    pb.set_message(message.to_string());
    pb.enable_steady_tick(std::time::Duration::from_millis(100));
    pb
}

fn write_output(path: Option<&PathBuf>, content: &str) -> Result<(), Box<dyn std::error::Error>> {
    match path {
        Some(p) => {
            fs::write(p, content)?;
        }
        None => {
            let stdout = io::stdout();
            let mut handle = stdout.lock();
            writeln!(handle, "{}", content)?;
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cli_parse() {
        use clap::CommandFactory;
        Cli::command().debug_assert();
    }
}