penmanship 0.1.0

A Unicode character lookup library for converting text patterns to Unicode characters
Documentation
//! Generate comprehensive HTML entity mappings documentation
//!
//! This example reads all HTML entity mappings from the library
//! and generates a formatted Markdown table showing all HTML entities.
//!
//! # Usage
//!
//! ```bash
//! cargo run --example generate_mapping_html --features=full --release
//! ```
//!
//! Generates `docs/html-entities.md` with all 2000+ HTML entity mappings.

use penmanship::categories;
use std::fs;
use std::io::Write;

/// Generate the HTML entity documentation as a string
fn generate_html_docs() -> String {
    let mut output = String::new();

    // Header
    output.push_str("# HTML Entity Mappings\n\n");
    output.push_str(
        "This document lists all HTML named character reference patterns supported by penmanship.\n\n",
    );

    // Collect all HTML patterns
    let mut patterns = Vec::new();

    // Collect from all three parts
    for (pattern, (character, _description)) in categories::html::PART1.entries() {
        patterns.push((pattern.to_string(), character.to_string()));
    }
    for (pattern, (character, _description)) in categories::html::PART2.entries() {
        patterns.push((pattern.to_string(), character.to_string()));
    }
    for (pattern, (character, _description)) in categories::html::PART3.entries() {
        patterns.push((pattern.to_string(), character.to_string()));
    }

    // Sort by character first, then by pattern (so aliases group together)
    patterns.sort_by(|a, b| a.1.cmp(&b.1).then_with(|| a.0.cmp(&b.0)));

    // Generate markdown table
    output.push_str(&format!("Total HTML entities: {}\n\n", patterns.len()));
    output.push_str("| Pattern | Character |\n");
    output.push_str("|---------|--------|\n");

    for (pattern, character) in &patterns {
        // Escape pipe characters for markdown
        let escaped_pattern = pattern.replace('|', "\\|");
        // Display special characters with readable names in backticks
        let escaped_character = character
            .replace('|', "\\|")
            .replace('\t', "`tab`")
            .replace('\n', "`newline`")
            .replace('\r', "`return`");
        output.push_str(&format!("| `{escaped_pattern}` | {escaped_character} |\n"));
    }

    output.push('\n');

    // Add footer
    output.push_str("---\n\n");
    output.push_str("*Generated by `scripts/generate_mapping_html.rs`*\n");

    output
}

fn main() -> std::io::Result<()> {
    println!("Generating HTML entity mapping documentation...");

    let output = generate_html_docs();

    // Create docs directory if it doesn't exist
    fs::create_dir_all("docs")?;

    // Write to file
    let mut file = fs::File::create("docs/html-entities.md")?;
    file.write_all(output.as_bytes())?;

    println!("✓ Generated docs/html-entities.md");

    Ok(())
}

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

    #[test]
    fn test_generate_html_docs() {
        // Test the generation logic without writing to disk
        let output = generate_html_docs();

        // Check that output contains expected content
        assert!(output.contains("# HTML Entity Mappings"));
        assert!(output.contains("Total HTML entities:"));
        assert!(output.contains("| Pattern | Character |"));
        assert!(output.len() > 10000); // Should be a large document
    }
}