web-capture 0.3.13

CLI and microservice to render web pages as HTML, Markdown, or PNG
Documentation
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
//! web-capture CLI and HTTP server
//!
//! A CLI tool and microservice to render web pages as HTML, Markdown, or PNG.
//!
//! ## Usage
//!
//! ### Server Mode
//! ```bash
//! web-capture --serve --port 3000
//! ```
//!
//! ### Capture Mode
//! ```bash
//! web-capture https://example.com --format html
//! web-capture https://example.com --format markdown --output page.md
//! web-capture https://example.com --format png --output screenshot.png
//! ```

use axum::{
    extract::Query,
    http::StatusCode,
    response::{IntoResponse, Response},
    routing::get,
    Router,
};
use clap::Parser;
use serde::Deserialize;
use std::net::SocketAddr;
use std::path::PathBuf;
use tokio::fs;
use tower_http::trace::TraceLayer;
use tracing::{debug, error, info};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use url::Url;

use web_capture::{
    capture_screenshot, convert_html_to_markdown_enhanced, convert_relative_urls, convert_to_utf8,
    fetch_html, html, render_html, EnhancedOptions,
};

/// CLI arguments
#[derive(Parser, Debug)]
#[command(
    name = "web-capture",
    about = "Capture web pages as HTML, Markdown, or PNG",
    version
)]
#[allow(clippy::struct_excessive_bools)]
struct Args {
    /// URL to capture (required in capture mode)
    #[arg(index = 1)]
    url: Option<String>,

    /// Start as HTTP API server
    #[arg(short, long)]
    serve: bool,

    /// Port to listen on (server mode)
    #[arg(short, long, default_value = "3000", env = "PORT")]
    port: u16,

    /// Output format: markdown/md, html, image/png
    #[arg(short, long, default_value = "markdown")]
    format: String,

    /// Output file path (default: stdout for text, auto-generated for images)
    #[arg(short, long)]
    output: Option<PathBuf>,

    /// Extract LaTeX formulas from img.formula, `KaTeX`, `MathJax` (default: true).
    /// Use --no-extract-latex to disable.
    #[arg(long, default_value_t = true, env = "WEB_CAPTURE_EXTRACT_LATEX")]
    extract_latex: bool,

    /// Extract article metadata (author, date, hubs, tags) (default: true).
    /// Use --no-extract-metadata to disable.
    #[arg(long, default_value_t = true, env = "WEB_CAPTURE_EXTRACT_METADATA")]
    extract_metadata: bool,

    /// Apply post-processing (unicode normalization, LaTeX spacing) (default: true).
    /// Use --no-post-process to disable.
    #[arg(long, default_value_t = true, env = "WEB_CAPTURE_POST_PROCESS")]
    post_process: bool,

    /// Detect and correct code block languages (default: true).
    /// Use --no-detect-code-language to disable.
    #[arg(long, default_value_t = true, env = "WEB_CAPTURE_DETECT_CODE_LANGUAGE")]
    detect_code_language: bool,

    /// CSS selector used to scope markdown conversion while preserving full-page metadata extraction.
    #[arg(long, env = "WEB_CAPTURE_CONTENT_SELECTOR")]
    content_selector: Option<String>,

    /// CSS selector for article body markdown; prepends the selected article title when available.
    #[arg(long, env = "WEB_CAPTURE_BODY_SELECTOR")]
    body_selector: Option<String>,

    /// Keep images as inline base64 data URIs instead of extracting to files (default: false).
    /// Use --embed-images to keep base64 inline.
    #[arg(long, default_value_t = false, env = "WEB_CAPTURE_EMBED_IMAGES")]
    embed_images: bool,

    /// Directory name for extracted images, relative to output file (default: images)
    #[arg(long, default_value = "images", env = "WEB_CAPTURE_IMAGES_DIR")]
    images_dir: String,

    /// Base directory for auto-derived output paths when -o is omitted (default: ./data/web-capture)
    #[arg(
        long,
        default_value = "./data/web-capture",
        env = "WEB_CAPTURE_DATA_DIR"
    )]
    data_dir: String,

    /// Create archive output. Formats: zip (default), 7z, tar.gz (alias gz), tar
    #[arg(long, num_args = 0..=1, default_missing_value = "zip")]
    archive: Option<String>,

    /// Disable HTML pretty-printing (output minified HTML instead of indented).
    #[arg(long, default_value_t = false, env = "WEB_CAPTURE_NO_PRETTY_HTML")]
    no_pretty_html: bool,

    /// Alias for --embed-images: keep images inline as base64
    #[arg(long, default_value_t = false)]
    no_extract_images: bool,

    /// Keep original remote image URLs instead of downloading or extracting.
    /// Base64 data URIs are stripped (no original URL to restore).
    #[arg(long, default_value_t = false, env = "WEB_CAPTURE_KEEP_ORIGINAL_LINKS")]
    keep_original_links: bool,

    /// Capture both light and dark theme screenshots
    #[arg(long, default_value_t = false)]
    dual_theme: bool,

    /// Path to batch configuration file (JSON)
    #[arg(long)]
    config_file: Option<PathBuf>,

    /// Process all articles in batch configuration
    #[arg(long, default_value_t = false)]
    all: bool,

    /// Show what would be done without making changes
    #[arg(long, default_value_t = false)]
    dry_run: bool,

    /// Show detailed output
    #[arg(long, default_value_t = false)]
    verbose: bool,

    /// API token for authenticated capture (e.g., Google Docs private documents).
    /// Can also be set via `API_TOKEN` env variable.
    #[arg(long, env = "API_TOKEN")]
    api_token: Option<String>,

    /// Capture method: browser (default) or api (direct HTTP fetch, for Google Docs etc.)
    #[arg(long, default_value = "browser")]
    capture: String,
}

/// Query parameters for API endpoints
#[derive(Debug, Deserialize)]
struct UrlQuery {
    url: String,
}

/// Query parameters for /markdown endpoint
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct MarkdownQuery {
    url: String,
    #[serde(default, rename = "embedImages")]
    embed_images: bool,
    #[serde(default = "default_true", rename = "keepOriginalLinks")]
    keep_original_links: bool,
    #[serde(default, rename = "contentSelector")]
    content_selector: Option<String>,
    #[serde(default, rename = "bodySelector")]
    body_selector: Option<String>,
}

const fn default_true() -> bool {
    true
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut args = Args::parse();

    // Initialize tracing after parsing args so --verbose can enable detailed logs.
    let default_filter = if args.verbose {
        "web_capture=debug,tower_http=debug"
    } else {
        "web_capture=info,tower_http=info"
    };
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| default_filter.into()),
        )
        .with(tracing_subscriber::fmt::layer())
        .init();

    // --no-extract-images is an alias for --embed-images
    if args.no_extract_images {
        args.embed_images = true;
    }

    // --archive flag validation and format override
    let effective_format = if let Some(ref archive_fmt) = args.archive {
        let fmt = if archive_fmt.is_empty() {
            "zip"
        } else {
            archive_fmt.as_str()
        };
        match fmt {
            "zip" | "7z" | "tar.gz" | "gz" | "tar" => {}
            other => {
                eprintln!("Error: Unsupported archive format \"{other}\". Supported: zip, 7z, tar.gz, gz, tar");
                std::process::exit(1);
            }
        }
        "archive".to_string()
    } else {
        args.format.clone()
    };

    if args.serve {
        // Server mode
        start_server(args.port).await?;
    } else if let Some(ref url) = args.url {
        // Capture mode
        capture_url(url, &effective_format, args.output.as_ref(), &args).await?;
    } else {
        eprintln!("Error: Missing URL or --serve flag");
        eprintln!("Run with --help for usage information");
        std::process::exit(1);
    }

    Ok(())
}

/// Start the HTTP server
async fn start_server(port: u16) -> anyhow::Result<()> {
    let app = Router::new()
        .route("/html", get(html_handler))
        .route("/markdown", get(markdown_handler))
        .route("/image", get(image_handler))
        .route("/fetch", get(fetch_handler))
        .route("/stream", get(stream_handler))
        .route("/animation", get(animation_handler))
        .route("/figures", get(figures_handler))
        .route("/themed-image", get(themed_image_handler))
        .layer(TraceLayer::new_for_http());

    let addr = SocketAddr::from(([0, 0, 0, 0], port));
    info!("web-capture server listening on http://{}", addr);
    info!("");
    info!("Available endpoints:");
    info!("  GET /html?url=<URL>       - Render page as HTML");
    info!("  GET /markdown?url=<URL>   - Convert page to Markdown");
    info!("  GET /image?url=<URL>      - Screenshot page as PNG");
    info!("  GET /fetch?url=<URL>      - Proxy fetch content");
    info!("  GET /stream?url=<URL>     - Stream content");
    info!("  GET /animation?url=<URL>  - Capture animation frames");
    info!("  GET /figures?url=<URL>    - Extract figure images");
    info!("  GET /themed-image?url=<URL> - Dual-theme screenshots");
    info!("");
    info!("Press Ctrl+C to stop the server");

    let listener = tokio::net::TcpListener::bind(addr).await?;
    axum::serve(listener, app)
        .with_graceful_shutdown(shutdown_signal())
        .await?;

    Ok(())
}

/// Graceful shutdown signal handler
async fn shutdown_signal() {
    tokio::signal::ctrl_c()
        .await
        .expect("Failed to install Ctrl+C handler");
    info!("Shutdown signal received, closing server...");
}

/// HTML endpoint handler
async fn html_handler(Query(params): Query<UrlQuery>) -> Response {
    let url = match normalize_url(&params.url) {
        Ok(url) => url,
        Err(e) => return (StatusCode::BAD_REQUEST, e).into_response(),
    };

    // First try to fetch HTML directly
    let html_result = match fetch_html(&url).await {
        Ok(html) => html,
        Err(e) => {
            error!("Failed to fetch HTML: {}", e);
            return (StatusCode::INTERNAL_SERVER_ERROR, "Error fetching HTML").into_response();
        }
    };

    // Check if it's valid HTML and contains JavaScript
    let needs_render = !html::is_html(&html_result) || html::has_javascript(&html_result);

    let final_html = if needs_render {
        // Use browser to get rendered HTML
        match render_html(&url).await {
            Ok(rendered) => {
                let utf8_html = convert_to_utf8(&rendered);
                convert_relative_urls(&utf8_html, &url)
            }
            Err(e) => {
                error!("Failed to render HTML: {}", e);
                return (StatusCode::INTERNAL_SERVER_ERROR, "Error rendering HTML").into_response();
            }
        }
    } else {
        let utf8_html = convert_to_utf8(&html_result);
        convert_relative_urls(&utf8_html, &url)
    };

    (
        StatusCode::OK,
        [("Content-Type", "text/html; charset=utf-8")],
        final_html,
    )
        .into_response()
}

/// Markdown endpoint handler
async fn markdown_handler(Query(params): Query<MarkdownQuery>) -> Response {
    let url = match normalize_url(&params.url) {
        Ok(url) => url,
        Err(e) => return (StatusCode::BAD_REQUEST, e).into_response(),
    };

    let html = match fetch_html(&url).await {
        Ok(html) => html,
        Err(e) => {
            error!("Failed to fetch HTML: {}", e);
            return (StatusCode::INTERNAL_SERVER_ERROR, "Error fetching HTML").into_response();
        }
    };

    let options = EnhancedOptions {
        content_selector: params.content_selector,
        body_selector: params.body_selector,
        ..EnhancedOptions::default()
    };
    let mut markdown = match convert_html_to_markdown_enhanced(&html, Some(&url), &options) {
        Ok(result) => result.markdown,
        Err(e) => {
            error!("Failed to convert to Markdown: {}", e);
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                "Error converting to Markdown",
            )
                .into_response();
        }
    };

    if !params.embed_images {
        let result = web_capture::extract_images::strip_base64_images(&markdown);
        markdown = result.markdown;
    }

    (
        StatusCode::OK,
        [("Content-Type", "text/markdown")],
        markdown,
    )
        .into_response()
}

/// Image/screenshot endpoint handler
async fn image_handler(Query(params): Query<UrlQuery>) -> Response {
    let url = match normalize_url(&params.url) {
        Ok(url) => url,
        Err(e) => return (StatusCode::BAD_REQUEST, e).into_response(),
    };

    let screenshot = match capture_screenshot(&url).await {
        Ok(data) => data,
        Err(e) => {
            error!("Failed to capture screenshot: {}", e);
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                "Error capturing screenshot",
            )
                .into_response();
        }
    };

    (
        StatusCode::OK,
        [
            ("Content-Type", "image/png"),
            ("Content-Disposition", "inline; filename=\"screenshot.png\""),
        ],
        screenshot,
    )
        .into_response()
}

/// Fetch/proxy endpoint handler
async fn fetch_handler(Query(params): Query<UrlQuery>) -> Response {
    let url = match normalize_url(&params.url) {
        Ok(url) => url,
        Err(e) => return (StatusCode::BAD_REQUEST, e).into_response(),
    };

    let client = match reqwest::Client::builder().build() {
        Ok(client) => client,
        Err(e) => {
            error!("Failed to create HTTP client: {}", e);
            return (StatusCode::INTERNAL_SERVER_ERROR, "Error creating client").into_response();
        }
    };

    let response = match client.get(&url).send().await {
        Ok(resp) => resp,
        Err(e) => {
            error!("Failed to fetch content: {}", e);
            return (StatusCode::INTERNAL_SERVER_ERROR, "Error fetching content").into_response();
        }
    };

    let status = StatusCode::from_u16(response.status().as_u16()).unwrap_or(StatusCode::OK);
    let content_type = response
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("text/plain")
        .to_string();

    let body = match response.bytes().await {
        Ok(bytes) => bytes.to_vec(),
        Err(e) => {
            error!("Failed to read response body: {}", e);
            return (StatusCode::INTERNAL_SERVER_ERROR, "Error reading content").into_response();
        }
    };

    (status, [("Content-Type", content_type.as_str())], body).into_response()
}

/// Stream/proxy endpoint handler (same as fetch for now)
async fn stream_handler(query: Query<UrlQuery>) -> Response {
    // For simplicity, stream and fetch behave the same in this implementation
    fetch_handler(query).await
}

/// Animation endpoint handler
async fn animation_handler(Query(params): Query<UrlQuery>) -> Response {
    let url = match normalize_url(&params.url) {
        Ok(url) => url,
        Err(e) => return (StatusCode::BAD_REQUEST, e).into_response(),
    };

    let options = web_capture::animation::AnimationOptions::default();
    match web_capture::animation::capture_animation_frames(&url, &options).await {
        Ok(result) => {
            let json = serde_json::json!({
                "frameCount": result.frames.len(),
                "loopDetected": result.loop_detected,
                "loopFrame": result.loop_frame,
                "duration": result.duration,
                "totalFrames": result.total_frames,
            });
            axum::Json(json).into_response()
        }
        Err(e) => {
            error!("Animation capture error: {}", e);
            (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response()
        }
    }
}

/// Figures extraction endpoint handler
async fn figures_handler(Query(params): Query<UrlQuery>) -> Response {
    let url = match normalize_url(&params.url) {
        Ok(url) => url,
        Err(e) => return (StatusCode::BAD_REQUEST, e).into_response(),
    };

    // Fetch the HTML and extract figures
    let html_content = match fetch_html(&url).await {
        Ok(html) => html,
        Err(e) => {
            error!("Failed to fetch HTML for figures: {}", e);
            return (StatusCode::INTERNAL_SERVER_ERROR, "Error fetching HTML").into_response();
        }
    };

    let figures = web_capture::figures::extract_figures(&html_content, &url);
    let downloaded = web_capture::figures::download_figures(&figures).await;

    let json = serde_json::json!({
        "url": url,
        "totalFound": figures.len(),
        "totalDownloaded": downloaded.iter().filter(|d| d.buffer.is_some()).count(),
        "figures": downloaded.iter().map(|f| serde_json::json!({
            "figureNum": f.figure_num,
            "filename": f.filename,
            "caption": f.caption,
            "originalUrl": f.original_url,
            "downloaded": f.buffer.is_some(),
            "error": f.error,
        })).collect::<Vec<_>>(),
    });
    axum::Json(json).into_response()
}

/// Dual-themed screenshot endpoint handler
async fn themed_image_handler(Query(params): Query<UrlQuery>) -> Response {
    let url = match normalize_url(&params.url) {
        Ok(url) => url,
        Err(e) => return (StatusCode::BAD_REQUEST, e).into_response(),
    };

    let options = web_capture::themed_image::ThemedImageOptions::default();
    match web_capture::themed_image::capture_dual_theme_screenshots(&url, &options).await {
        Ok(result) => {
            let json = serde_json::json!({
                "url": result.url,
                "width": result.width,
                "height": result.height,
                "lightSize": result.light_size,
                "darkSize": result.dark_size,
            });
            axum::Json(json).into_response()
        }
        Err(e) => {
            error!("Themed image capture error: {}", e);
            (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response()
        }
    }
}

/// Capture a URL and save/output the result
#[allow(clippy::too_many_lines)]
async fn capture_url(
    url: &str,
    format: &str,
    output: Option<&PathBuf>,
    args: &Args,
) -> anyhow::Result<()> {
    let absolute_url = normalize_url(url).map_err(|e| anyhow::anyhow!(e))?;
    debug!(
        url = %absolute_url,
        format = %format,
        capture = %args.capture,
        has_api_token = args.api_token.is_some(),
        "starting capture"
    );

    // Google Docs capture honors --capture:
    // - browser: load /edit model data
    // - api without token: public export endpoint
    // - api with token: docs.googleapis.com REST API
    if web_capture::gdocs::is_google_docs_url(&absolute_url) {
        let api_token = args.api_token.as_deref();
        let method = web_capture::gdocs::select_capture_method(&args.capture, api_token)?;
        let format_lower = format.to_lowercase();
        let model_format = matches!(
            format_lower.as_str(),
            "archive" | "markdown" | "md" | "html" | "txt" | "text"
        );
        debug!(
            url = %absolute_url,
            method = ?method,
            format = %format_lower,
            model_format,
            has_api_token = api_token.is_some(),
            "selected Google Docs capture method"
        );

        if method == web_capture::gdocs::GDocsCaptureMethod::BrowserModel && !model_format {
            debug!(
                format = %format_lower,
                "Google Docs editor model does not support requested format; using regular browser pipeline"
            );
            // Screenshot-like formats should use the regular browser path below.
        } else if method == web_capture::gdocs::GDocsCaptureMethod::BrowserModel {
            let rendered =
                web_capture::gdocs::fetch_google_doc_from_model(&absolute_url, api_token).await?;
            write_rendered_gdoc(
                &rendered,
                &format_lower,
                &absolute_url,
                output,
                args,
                "Google Doc (browser-model)",
            )
            .await?;
            return Ok(());
        } else if method == web_capture::gdocs::GDocsCaptureMethod::DocsApi {
            let Some(token) = api_token else {
                unreachable!("Docs API capture is only selected when a token exists");
            };
            let rendered =
                web_capture::gdocs::fetch_google_doc_from_docs_api(&absolute_url, token).await?;
            write_rendered_gdoc(
                &rendered,
                &format_lower,
                &absolute_url,
                output,
                args,
                "Google Doc (docs-api)",
            )
            .await?;
            return Ok(());
        } else {
            match format_lower.as_str() {
                "archive" => {
                    let archive_result =
                        web_capture::gdocs::fetch_google_doc_as_archive(&absolute_url, api_token)
                            .await?;
                    let zip_bytes = web_capture::gdocs::create_archive_zip(
                        &archive_result,
                        !args.no_pretty_html,
                    )?;
                    write_archive_capture(
                        &zip_bytes,
                        &absolute_url,
                        output,
                        args.archive.as_deref().unwrap_or("zip"),
                        &args.data_dir,
                        "Google Doc (archive)",
                    )
                    .await?;
                }
                "markdown" | "md" => {
                    let result =
                        web_capture::gdocs::fetch_google_doc_as_markdown(&absolute_url, api_token)
                            .await?;
                    let mut markdown = result.content;
                    if let Some(path) =
                        effective_output_path(&absolute_url, "md", output, &args.data_dir)
                    {
                        if args.embed_images {
                            // Keep base64 data URIs inline
                        } else if args.keep_original_links {
                            let result =
                                web_capture::extract_images::strip_base64_images(&markdown);
                            if result.stripped > 0 {
                                markdown = result.markdown;
                                eprintln!(
                                    "Stripped {} base64 images (keeping original links)",
                                    result.stripped
                                );
                            }
                        } else {
                            let output_dir = path.parent().unwrap_or(&path);
                            let result = web_capture::extract_images::extract_and_save_images(
                                &markdown,
                                output_dir,
                                &args.images_dir,
                            )?;
                            if result.extracted > 0 {
                                markdown = result.markdown;
                                eprintln!(
                                    "Extracted {} images to {}/",
                                    result.extracted, args.images_dir
                                );
                            }
                        }
                        write_text_capture_to_path(&markdown, &path, "Google Doc Markdown").await?;
                    } else {
                        print!("{markdown}");
                    }
                }
                _ => {
                    let gdocs_format = match format_lower.as_str() {
                        "png" | "image" | "screenshot" => "html",
                        other => other,
                    };
                    let result = web_capture::gdocs::fetch_google_doc(
                        &absolute_url,
                        gdocs_format,
                        api_token,
                    )
                    .await?;
                    write_text_capture(
                        &result.content,
                        &absolute_url,
                        gdocs_format,
                        output,
                        &args.data_dir,
                        &format!("Google Doc ({gdocs_format})"),
                    )
                    .await?;
                }
            }
            return Ok(());
        }
    }

    match format.to_lowercase().as_str() {
        "archive" => {
            let archive_fmt = args.archive.as_deref().unwrap_or("zip");
            let ext = match archive_fmt {
                "tar.gz" | "gz" => "tar.gz",
                "7z" => "7z",
                "tar" => "tar",
                _ => "zip",
            };

            let html = capture_html_content(&absolute_url, args).await?;
            let options = EnhancedOptions {
                extract_latex: args.extract_latex,
                extract_metadata: args.extract_metadata,
                post_process: args.post_process,
                detect_code_language: args.detect_code_language,
                content_selector: args.content_selector.clone(),
                body_selector: args.body_selector.clone(),
            };
            let enhanced = convert_html_to_markdown_enhanced(&html, Some(&absolute_url), &options)?;
            let markdown = enhanced.markdown;

            let buffers = web_capture::extract_images::extract_base64_to_buffers(
                &markdown,
                &args.images_dir,
            )?;

            let archive_result = web_capture::gdocs::GDocsArchiveResult {
                html: html.clone(),
                markdown: buffers.markdown,
                images: buffers
                    .images
                    .into_iter()
                    .map(|b| web_capture::gdocs::ExtractedImage {
                        filename: b.filename,
                        data: b.data,
                        mime_type: String::new(),
                    })
                    .collect(),
                document_id: String::new(),
                export_url: absolute_url.clone(),
            };
            let zip_bytes =
                web_capture::gdocs::create_archive_zip(&archive_result, !args.no_pretty_html)?;

            let is_stdout = output.is_some_and(|p| p.as_os_str() == "-");
            let derived;
            let effective_output = if is_stdout {
                None
            } else if let Some(path) = output {
                Some(path.clone())
            } else {
                derived = derive_output_path(&absolute_url, ext, &args.data_dir);
                Some(derived)
            };
            if let Some(ref path) = effective_output {
                if let Some(parent) = path.parent() {
                    std::fs::create_dir_all(parent).ok();
                }
                fs::write(path, &zip_bytes).await?;
                eprintln!("Archive saved to: {}", path.display());
            } else {
                use std::io::Write;
                std::io::stdout().write_all(&zip_bytes)?;
            }
        }
        "markdown" | "md" => {
            let html = capture_html_content(&absolute_url, args).await?;

            // Enhanced conversion is now the default
            let options = EnhancedOptions {
                extract_latex: args.extract_latex,
                extract_metadata: args.extract_metadata,
                post_process: args.post_process,
                detect_code_language: args.detect_code_language,
                content_selector: args.content_selector.clone(),
                body_selector: args.body_selector.clone(),
            };
            let result = convert_html_to_markdown_enhanced(&html, Some(&absolute_url), &options)?;
            let mut markdown = result.markdown;

            let is_stdout = output.is_some_and(|p| p.as_os_str() == "-");
            let derived;
            let effective_output = if is_stdout {
                None
            } else if let Some(path) = output {
                Some(path.clone())
            } else {
                derived = derive_output_path(&absolute_url, "md", &args.data_dir);
                Some(derived)
            };
            if let Some(ref path) = effective_output {
                if args.embed_images {
                    // Keep base64 data URIs inline
                } else if args.keep_original_links {
                    let result = web_capture::extract_images::strip_base64_images(&markdown);
                    if result.stripped > 0 {
                        markdown = result.markdown;
                        eprintln!(
                            "Stripped {} base64 images (keeping original links)",
                            result.stripped
                        );
                    }
                } else {
                    let output_dir = path.parent().unwrap_or(path);
                    let result = web_capture::extract_images::extract_and_save_images(
                        &markdown,
                        output_dir,
                        &args.images_dir,
                    )?;
                    if result.extracted > 0 {
                        markdown = result.markdown;
                        eprintln!(
                            "Extracted {} images to {}/",
                            result.extracted, args.images_dir
                        );
                    }
                }
                if let Some(parent) = path.parent() {
                    std::fs::create_dir_all(parent).ok();
                }
                fs::write(path, &markdown).await?;
                eprintln!("Markdown saved to: {}", path.display());
            } else {
                print!("{markdown}");
            }
        }
        "image" | "png" | "screenshot" => {
            let screenshot = capture_screenshot(&absolute_url).await?;

            if let Some(path) = output {
                fs::write(path, &screenshot).await?;
                eprintln!("Screenshot saved to: {}", path.display());
            } else {
                // Generate default filename
                let parsed_url = Url::parse(&absolute_url)?;
                let hostname = parsed_url.host_str().unwrap_or("unknown");
                let default_filename = format!(
                    "{}_{}.png",
                    hostname.replace('.', "_"),
                    std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)?
                        .as_millis()
                );
                fs::write(&default_filename, &screenshot).await?;
                eprintln!("Screenshot saved to: {default_filename}");
            }
        }
        _ => {
            let html_content = capture_html_content(&absolute_url, args).await?;
            let utf8_html = convert_to_utf8(&html_content);
            let result = convert_relative_urls(&utf8_html, &absolute_url);

            let is_stdout = output.is_some_and(|p| p.as_os_str() == "-");
            let derived;
            let effective_output = if is_stdout {
                None
            } else if let Some(path) = output {
                Some(path.clone())
            } else {
                derived = derive_output_path(&absolute_url, "html", &args.data_dir);
                Some(derived)
            };
            if let Some(ref path) = effective_output {
                if let Some(parent) = path.parent() {
                    std::fs::create_dir_all(parent).ok();
                }
                fs::write(path, &result).await?;
                eprintln!("HTML saved to: {}", path.display());
            } else {
                print!("{result}");
            }
        }
    }

    Ok(())
}

async fn capture_html_content(absolute_url: &str, args: &Args) -> anyhow::Result<String> {
    if capture_uses_browser(&args.capture)? {
        Ok(render_html(absolute_url).await?)
    } else {
        Ok(fetch_html(absolute_url).await?)
    }
}

fn capture_uses_browser(capture: &str) -> anyhow::Result<bool> {
    match capture.to_lowercase().as_str() {
        "browser" => Ok(true),
        "api" => Ok(false),
        other => Err(anyhow::anyhow!(
            "Unsupported capture method \"{other}\". Use \"browser\" or \"api\"."
        )),
    }
}

async fn write_rendered_gdoc(
    rendered: &web_capture::gdocs::GDocsRenderedResult,
    format: &str,
    absolute_url: &str,
    output: Option<&PathBuf>,
    args: &Args,
    label: &str,
) -> anyhow::Result<()> {
    if format == "archive" || format == "zip" {
        let archive =
            web_capture::gdocs::localize_rendered_remote_images_for_archive(rendered).await?;
        let zip_bytes = web_capture::gdocs::create_archive_zip(&archive, !args.no_pretty_html)?;
        write_archive_capture(
            &zip_bytes,
            absolute_url,
            output,
            args.archive.as_deref().unwrap_or("zip"),
            &args.data_dir,
            "Google Doc (archive)",
        )
        .await?;
        return Ok(());
    }

    let (content, ext) = match format {
        "html" => (rendered.html.as_str(), "html"),
        "txt" | "text" => (rendered.text.as_str(), "txt"),
        _ => (rendered.markdown.as_str(), "md"),
    };
    write_text_capture(content, absolute_url, ext, output, &args.data_dir, label).await
}

fn effective_output_path(
    absolute_url: &str,
    ext: &str,
    output: Option<&PathBuf>,
    data_dir: &str,
) -> Option<PathBuf> {
    if output.is_some_and(|path| path.as_os_str() == "-") {
        None
    } else if let Some(path) = output {
        Some(path.clone())
    } else {
        Some(derive_output_path(absolute_url, ext, data_dir))
    }
}

async fn write_text_capture(
    content: &str,
    absolute_url: &str,
    ext: &str,
    output: Option<&PathBuf>,
    data_dir: &str,
    label: &str,
) -> anyhow::Result<()> {
    if let Some(path) = effective_output_path(absolute_url, ext, output, data_dir) {
        write_text_capture_to_path(content, &path, label).await?;
    } else {
        print!("{content}");
    }
    Ok(())
}

async fn write_text_capture_to_path(
    content: &str,
    path: &PathBuf,
    label: &str,
) -> anyhow::Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).ok();
    }
    fs::write(path, content).await?;
    eprintln!("{label} saved to: {}", path.display());
    Ok(())
}

async fn write_archive_capture(
    bytes: &[u8],
    absolute_url: &str,
    output: Option<&PathBuf>,
    archive_fmt: &str,
    data_dir: &str,
    label: &str,
) -> anyhow::Result<()> {
    let ext = match archive_fmt {
        "tar.gz" | "gz" => "tar.gz",
        "7z" => "7z",
        "tar" => "tar",
        _ => "zip",
    };
    if let Some(path) = effective_output_path(absolute_url, ext, output, data_dir) {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).ok();
        }
        fs::write(&path, bytes).await?;
        eprintln!("{label} saved to: {}", path.display());
    } else {
        use std::io::Write;
        std::io::stdout().write_all(bytes)?;
    }
    Ok(())
}

/// Derive an output file path from a URL when -o is not provided.
fn derive_output_path(absolute_url: &str, ext: &str, data_dir: &str) -> PathBuf {
    let parsed =
        Url::parse(absolute_url).unwrap_or_else(|_| Url::parse("https://unknown").unwrap());
    let host = parsed.host_str().unwrap_or("unknown");
    let url_path = parsed.path().trim_start_matches('/').trim_end_matches('/');
    let dir = PathBuf::from(data_dir).join(host).join(url_path);
    std::fs::create_dir_all(&dir).ok();
    dir.join(format!("document.{ext}"))
}

/// Normalize URL to ensure it's absolute
fn normalize_url(url: &str) -> Result<String, String> {
    web_capture::html::normalize_url(url)
}