Crates.io | html-generator |
lib.rs | html-generator |
version | |
source | src |
created_at | 2024-10-07 19:38:27.789421 |
updated_at | 2024-12-29 20:40:35.235751 |
description | A robust Rust library designed for transforming Markdown into SEO-optimized, accessible HTML. Featuring front matter extraction, custom header processing, table of contents generation, and performance optimization for web projects of any scale. |
homepage | https://html-generator.co/ |
repository | https://github.com/sebastienrousseau/html-generator |
max_upload_size | |
id | 1400397 |
Cargo.toml error: | TOML parse error at line 35, column 1 | 35 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A high-performance Rust library that transforms Markdown into semantically rich, accessible HTML with WCAG 2.1 compliance.
• Website • Documentation • Report Bug • Request Feature • Contributing Guidelines
HTML Generator is a high-performance Rust library for transforming Markdown into semantically rich, accessible HTML.
ComrakOptions
syntect
Add to your Cargo.toml
:
[dependencies]
html-generator = "0.0.3"
use html_generator::{generate_html, HtmlConfig};
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let markdown = "# Welcome\n\nThis is **HTML Generator**!";
let config = HtmlConfig::default();
let html = generate_html(markdown, &config)?;
println!("{}", html);
Ok(())
}
use html_generator::HtmlConfig;
use html_generator::error::HtmlError;
fn main() -> Result<(), HtmlError> {
let config = HtmlConfig::builder()
.with_language("en-GB")
.with_syntax_highlighting(true, Some("monokai".to_string()))
.build()?;
println!("Built config: {:?}", config);
Ok(())
}
use html_generator::{generate_html, HtmlConfig};
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let markdown = "# Hello from synchronous processing";
let config = HtmlConfig::default();
let html = generate_html(markdown, &config)?;
println!("{}", html);
Ok(())
}
# use html_generator::performance::async_generate_html;
# use std::error::Error;
#
# // We hide the async main to avoid doc-test errors about `.await`.
# // The code inside demonstrates how you'd normally use `async_generate_html`.
# async fn async_main_example() -> Result<(), Box<dyn Error>> {
let markdown = "# Async Processing\n\nThis is **HTML Generator**!";
let html = async_generate_html(markdown).await?;
println!("{}", html);
Ok(())
# }
use html_generator::{generate_html, HtmlConfig};
use html_generator::error::HtmlError;
fn handle_conversion_error(markdown: &str) -> Result<(), HtmlError> {
let config = HtmlConfig::default();
match generate_html(markdown, &config) {
Ok(html) => println!("Conversion successful: {}", html),
Err(HtmlError::InvalidInput(msg)) => {
eprintln!("Invalid input: {}", msg);
},
Err(HtmlError::InputTooLarge(size)) => {
eprintln!("Input too large: {} bytes", size);
},
Err(e) => eprintln!("Unexpected error: {}", e),
}
Ok(())
}
HTML Generator provides many advanced capabilities for accessibility, ARIA attributes, and custom Markdown styling. Below is a summary of what you can explore. For more detailed code, see the src/examples/
directory in this repository.
Add ARIA attributes to common HTML elements (buttons, forms, tables, and more) to ensure accessibility compliance. The library automatically infers roles and labels for screen readers.
Example Snippet (from aria_elements_example.rs
):
use html_generator::accessibility::add_aria_attributes;
use html_generator::error::HtmlError;
fn main() -> Result<(), HtmlError> {
// Basic HTML snippet for a button
let html_button = "<button>Click me</button>";
// Enhance with ARIA attributes
let enhanced_button =
add_aria_attributes(html_button, None).map_err(|e| {
// Convert from an accessibility::Error to an HtmlError
HtmlError::InvalidInput(e.to_string())
})?;
println!("Original: {}", html_button);
println!("Enhanced: {}", enhanced_button);
Ok(())
}
Run the full ARIA demo:
cargo run --example aria
This will print out multiple examples showcasing enhancements for buttons, forms, navigation elements, tables, live regions, and nested components.
Demonstrate transforming extended Markdown features such as:
:::note
, :::warning
).class="..."
directives for images or elements…and much more.
Example Snippet (from style_example.rs
):
use html_generator::error::HtmlError;
use html_generator::generator::markdown_to_html_with_extensions;
fn main() -> Result<(), HtmlError> {
let markdown = r":::note
Custom note block with a specific style
:::";
match markdown_to_html_with_extensions(markdown) {
Ok(html) => println!("Converted:\n{}", html),
Err(e) => println!("Error: {}", e),
}
Ok(())
}
Run the full style demo:
cargo run --example style
This will print out multiple styled examples (custom blocks, images, tables, bullet lists, code blocks, etc.) and show how they render as HTML.
If you’d like to combine accessibility features and custom Markdown styling, you can configure your HtmlConfig
to enable:
…thereby providing a powerful, end-to-end Markdown-to-HTML transformation pipeline suitable for high-performance, semantically rich, and user-friendly content.
Document Scale | Processing Time | Memory Utilization |
---|---|---|
Small (<1KB) | ~0.1ms | Constant O(1) |
Medium (10KB) | ~1ms | Linear O(n) |
Large (100KB) | ~10ms | Linear O(n) |
Platform | Status | Rust Version | Notes |
---|---|---|---|
Linux | ✅ Fully | 1.56+ | Comprehensive support |
macOS | ✅ Fully | 1.56+ | Native performance |
Windows | ✅ Fully | 1.56+ | Complete compatibility |
WebAssembly | ⚠️ Partial | 1.56+ | Limited feature support |
We use GitHub Actions for comprehensive testing:
# use html_generator::{generate_html, HtmlConfig};
# use html_generator::error::HtmlError;
#
fn handle_conversion_error(markdown: &str) -> Result<(), HtmlError> {
// We'll define a config for this snippet:
let config = HtmlConfig::default();
match generate_html(markdown, &config) {
Ok(html) => println!("Conversion successful"),
Err(HtmlError::InvalidInput(msg)) => {
eprintln!("Invalid input: {}", msg);
},
Err(HtmlError::InputTooLarge(size)) => {
eprintln!("Input too large: {} bytes", size);
},
Err(HtmlError::Io(io_error)) => {
eprintln!("I/O error occurred: {}", io_error);
},
// If your crate doesn't actually have a `Markdown` variant, remove this block
// Err(HtmlError::Markdown(markdown_error)) => {
// eprintln!("Markdown processing error: {}", markdown_error);
// },
Err(e) => eprintln!("Unexpected error: {}", e),
}
Ok(())
}
# Basic example
cargo run --example basic
# Accessibility demo
cargo run --example aria
# Performance benchmark
cargo run --example performance
We welcome contributions! See CONTRIBUTING.md for details on:
Dual-licensed: Apache 2.0 & MIT
Special thanks to the Rust community and open-source contributors.