euv-docs 0.48.0

Cross-platform documentation viewer and static site generator powered by euv and euv-ui.
Documentation
#[cfg(not(target_arch = "wasm32"))]
use std::{env, path::PathBuf, process::exit};

#[cfg(not(target_arch = "wasm32"))]
use euv_docs::{
    DOCS_FLAG_DEBUG, DOCS_FLAG_GENERATE, DOCS_FLAG_HEADLESS, DOCS_FLAG_INTERACTIVE,
    DOCS_FLAG_LOCALE, DOCS_FLAG_RELEASE, DOCS_FLAG_STATIC_HTML, DocSite, DocSiteConfig,
    DocsBuildOptions, GeneratedSiteReport, build_docs_bundle, build_multilocale_wasm_site,
    load_doc_site_with_options, load_site_config, render_docs_app,
};

#[cfg(not(target_arch = "wasm32"))]
const DEFAULT_DOCS_DIR: &str = "docs/docs/en";
#[cfg(not(target_arch = "wasm32"))]
const DEFAULT_OUT_DIR: &str = "dist/docs";
#[cfg(not(target_arch = "wasm32"))]
const FLAG_OUT: &str = "--out";
#[cfg(not(target_arch = "wasm32"))]
const FLAG_OUT_PREFIX: &str = "--out=";
#[cfg(not(target_arch = "wasm32"))]
const FLAG_TITLE: &str = "--title";
#[cfg(not(target_arch = "wasm32"))]
const FLAG_BASE: &str = "--base";
#[cfg(not(target_arch = "wasm32"))]
const FLAG_LOCALE_PREFIX: &str = "--locale=";
#[cfg(not(target_arch = "wasm32"))]
const FLAG_HELP_SHORT: &str = "-h";
#[cfg(not(target_arch = "wasm32"))]
const FLAG_HELP_LONG: &str = "--help";
#[cfg(not(target_arch = "wasm32"))]
const FLAG_VERSION_SHORT: &str = "-v";
#[cfg(not(target_arch = "wasm32"))]
const FLAG_VERSION_LONG: &str = "--version";

/// Entry point stub when `euv-docs` binary is checked for `wasm32`.
#[cfg(target_arch = "wasm32")]
fn main() {}

/// Entry point for the `euv-docs` multi-locale WASM site builder, static HTML generator, and native documentation viewer.
#[cfg(not(target_arch = "wasm32"))]
fn main() {
    let args: Vec<String> = env::args().collect();
    let mut src_dir: Option<PathBuf> = None;
    let mut out_dir: PathBuf = PathBuf::from(DEFAULT_OUT_DIR);
    let mut title_override: Option<String> = None;
    let mut base_url: String = String::new();
    let mut locale_filter: Option<String> = None;
    let mut generate_mode: bool = false;
    let mut release_mode: bool = true;
    let mut force_static_html: bool = false;
    let mut idx: usize = 1;
    while idx < args.len() {
        let arg: &str = args[idx].as_str();
        if arg == FLAG_HELP_SHORT || arg == FLAG_HELP_LONG {
            print_usage();
            return;
        }
        if arg == FLAG_VERSION_SHORT || arg == FLAG_VERSION_LONG {
            println!("euv-docs {}", env!("CARGO_PKG_VERSION"));
            return;
        }
        if arg == DOCS_FLAG_GENERATE {
            generate_mode = true;
            if let Some(dir_val) = args.get(idx + 1)
                && !dir_val.starts_with('-')
            {
                src_dir = Some(PathBuf::from(dir_val));
                idx += 1;
            }
            if let Some(out_val) = args.get(idx + 1)
                && !out_val.starts_with('-')
            {
                out_dir = PathBuf::from(out_val);
                idx += 1;
            }
        } else if arg == FLAG_OUT {
            generate_mode = true;
            if let Some(val) = args.get(idx + 1) {
                out_dir = PathBuf::from(val);
                idx += 1;
            }
        } else if let Some(val) = arg.strip_prefix(FLAG_OUT_PREFIX) {
            generate_mode = true;
            out_dir = PathBuf::from(val);
        } else if arg == FLAG_TITLE {
            if let Some(val) = args.get(idx + 1) {
                title_override = Some(val.clone());
                idx += 1;
            }
        } else if arg == FLAG_BASE {
            if let Some(val) = args.get(idx + 1) {
                base_url = val.clone();
                idx += 1;
            }
        } else if arg == DOCS_FLAG_LOCALE {
            if let Some(val) = args.get(idx + 1) {
                locale_filter = Some(val.clone());
                idx += 1;
            }
        } else if let Some(val) = arg.strip_prefix(FLAG_LOCALE_PREFIX) {
            locale_filter = Some(val.to_string());
        } else if arg == DOCS_FLAG_RELEASE {
            release_mode = true;
        } else if arg == DOCS_FLAG_DEBUG {
            release_mode = false;
        } else if arg == DOCS_FLAG_STATIC_HTML {
            force_static_html = true;
        } else if !arg.starts_with('-') && src_dir.is_none() {
            src_dir = Some(PathBuf::from(arg));
            generate_mode = true;
        }
        idx += 1;
    }
    let resolved_src: PathBuf = src_dir.unwrap_or_else(|| PathBuf::from(DEFAULT_DOCS_DIR));
    if generate_mode {
        let has_locales: bool = !force_static_html
            && load_site_config(&resolved_src)
                .is_some_and(|cfg: DocSiteConfig| !cfg.get_locales().is_empty());
        if has_locales {
            match build_multilocale_wasm_site(
                &resolved_src,
                &out_dir,
                release_mode,
                locale_filter.as_deref(),
            ) {
                Ok(report) => print_report(&report),
                Err(err) => {
                    eprintln!("{err}");
                    exit(1);
                }
            }
            return;
        }
        let mut opts: DocsBuildOptions = DocsBuildOptions::default();
        opts.set_title(title_override)
            .set_base_url(base_url)
            .set_clean_urls(true);
        match build_docs_bundle(&resolved_src, &out_dir, &opts) {
            Ok(report) => print_report(&report),
            Err(err) => {
                eprintln!("{err}");
                exit(1);
            }
        }
        return;
    }
    let site: DocSite = match load_doc_site_with_options(&resolved_src, title_override.as_deref()) {
        Ok(loaded) => loaded,
        Err(err) => {
            eprintln!("{err}");
            exit(1);
        }
    };
    let headless: bool = args.iter().any(|arg: &String| arg == DOCS_FLAG_HEADLESS);
    let interactive: bool = args.iter().any(|arg: &String| arg == DOCS_FLAG_INTERACTIVE);
    match render_docs_app(&site, headless, interactive) {
        Ok(stats) => {
            println!(
                "pages={} draw_commands={} frame_hash={:#018x}",
                site.get_pages().len(),
                stats.get_draw_commands(),
                stats.get_frame_hash()
            );
        }
        Err(err) => {
            eprintln!("{err}");
            exit(1);
        }
    }
}

/// Prints CLI usage instructions for `euv-docs`.
#[cfg(not(target_arch = "wasm32"))]
fn print_usage() {
    println!(
        "euv-docs {} - build multi-locale WASM or static HTML documentation sites and native viewer.\n\
         USAGE:\n  \
           euv-docs <SRC_DIR> --out <OUT_DIR> [--release|--debug] [--locale <LOCALE>] [--static-html]\n  \
           euv-docs [--headless|--interactive]",
        env!("CARGO_PKG_VERSION")
    );
}

/// Prints a concise build summary for a generated documentation site.
///
/// # Arguments
///
/// - `&GeneratedSiteReport` - Shared reference to a `GeneratedSiteReport`.
#[cfg(not(target_arch = "wasm32"))]
fn print_report(report: &GeneratedSiteReport) {
    println!(
        "generated_pages={} index={} timestamp={}",
        report.get_page_count(),
        report.get_index_path(),
        report.get_generated_at()
    );
}