// Copyright ยฉ 2024 StaticDataGen. All rights reserved. // SPDX-License-Identifier: Apache-2.0 OR MIT //! # StaticDataGen Preprocessor Examples //! //! This program demonstrates the preprocessing capabilities of the //! StaticDataGen library, showing various content transformations //! and preparations before final generation. use regex::Regex; use staticdatagen::modules::preprocessor::preprocess_content; use std::error::Error; /// Entry point for the StaticDataGen Preprocessor Examples program. /// /// Demonstrates various preprocessing scenarios including class /// attribute updates, image handling, and content transformations. /// /// # Errors /// /// Returns a `Result` containing a `Box` if any error /// occurs during the execution of the examples. pub fn main() -> Result<(), Box> { println!("\n๐Ÿงช StaticDataGen Preprocessor Examples\n"); class_attributes_example()?; image_processing_example()?; code_block_example()?; custom_components_example()?; frontmatter_example()?; shortcode_example()?; syntax_highlighting_example()?; validation_example()?; println!("\n๐ŸŽ‰ All preprocessor examples completed successfully!"); Ok(()) } /// Demonstrates class attribute processing. fn class_attributes_example() -> Result<(), Box> { println!("๐Ÿฆ€ Class Attributes Example"); println!("---------------------------------------------"); let content = r#"

Large text paragraph

Small text paragraph

{.custom-class} # Heading with Custom Class {.highlight} This paragraph will have a highlight class. "#; let class_regex = Regex::new(r#"

"#)?; let img_regex = Regex::new(r"(]+)(/>)")?; match preprocess_content(content, &class_regex, &img_regex) { Ok(processed) => { println!(" โœ… Processed content with class attributes:"); println!("{}", processed); } Err(e) => println!(" โŒ Processing error: {:?}", e), } Ok(()) } /// Demonstrates image tag processing. fn image_processing_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Image Processing Example"); println!("---------------------------------------------"); let content = r#" # Image Examples ![Basic Image](image.jpg) ![Responsive Image](photo.jpg){.responsive} HTML Image ![Image with Title](banner.jpg "Banner Image"){.hero .center} "#; let class_regex = Regex::new(r#"

"#)?; let img_regex = Regex::new(r"(]+)(/>)")?; match preprocess_content(content, &class_regex, &img_regex) { Ok(processed) => { println!(" โœ… Processed content with images:"); println!("{}", processed); } Err(e) => println!(" โŒ Processing error: {:?}", e), } Ok(()) } /// Demonstrates code block processing. fn code_block_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Code Block Example"); println!("---------------------------------------------"); let content = r#" # Code Examples ```rust fn main() { println!("Hello, World!"); } ``` ```html

Some HTML content

``` {.code-wrapper} ```css .container { max-width: 1200px; margin: 0 auto; } ``` "#; let class_regex = Regex::new(r#"

"#)?; let img_regex = Regex::new(r"(]+)(/>)")?; match preprocess_content(content, &class_regex, &img_regex) { Ok(processed) => { println!(" โœ… Processed content with code blocks:"); println!("{}", processed); } Err(e) => println!(" โŒ Processing error: {:?}", e), } Ok(()) } /// Demonstrates custom component processing. fn custom_components_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Custom Components Example"); println!("---------------------------------------------"); let content = r#" # Custom Components {{> alert type="info" }} This is an info alert message {{/ alert}} {{> card title="Feature" }} This is a feature card content {{/ card}} {.custom-wrapper} {{> tabs }} - Tab 1 content - Tab 2 content {{/ tabs}} "#; let class_regex = Regex::new(r#"

"#)?; let img_regex = Regex::new(r"(]+)(/>)")?; match preprocess_content(content, &class_regex, &img_regex) { Ok(processed) => { println!( " โœ… Processed content with custom components:" ); println!("{}", processed); } Err(e) => println!(" โŒ Processing error: {:?}", e), } Ok(()) } /// Demonstrates frontmatter processing. fn frontmatter_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Frontmatter Example"); println!("---------------------------------------------"); let content = r#"--- title: Example Page description: A page showing frontmatter processing date: 2024-02-20 tags: [example, preprocessing] layout: default --- # Main Content This is the main content of the page. {.highlight} This paragraph has a custom class. "#; let class_regex = Regex::new(r#"

"#)?; let img_regex = Regex::new(r"(]+)(/>)")?; match preprocess_content(content, &class_regex, &img_regex) { Ok(processed) => { println!(" โœ… Processed content with frontmatter:"); println!("{}", processed); } Err(e) => println!(" โŒ Processing error: {:?}", e), } Ok(()) } /// Demonstrates shortcode processing. fn shortcode_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Shortcode Example"); println!("---------------------------------------------"); let content = r#" # Shortcode Examples {{% youtube id="12345" %}} {{% tweet id="67890" %}} {.shortcode-wrapper} {{% gallery folder="vacation" %}} "#; let class_regex = Regex::new(r#"

"#)?; let img_regex = Regex::new(r"(]+)(/>)")?; match preprocess_content(content, &class_regex, &img_regex) { Ok(processed) => { println!(" โœ… Processed content with shortcodes:"); println!("{}", processed); } Err(e) => println!(" โŒ Processing error: {:?}", e), } Ok(()) } /// Demonstrates syntax highlighting preprocessing. fn syntax_highlighting_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Syntax Highlighting Example"); println!("---------------------------------------------"); let content = r#" # Code Examples with Syntax Highlighting ```rust,linenos fn main() { // A comment let x = 42; println!("The answer is: {}", x); } ``` {.code-block} ```python,emphasize=2-3 def greet(name): # Highlighted lines message = f"Hello, {name}!" return message ``` "#; let class_regex = Regex::new(r#"

"#)?; let img_regex = Regex::new(r"(]+)(/>)")?; match preprocess_content(content, &class_regex, &img_regex) { Ok(processed) => { println!( " โœ… Processed content with syntax highlighting:" ); println!("{}", processed); } Err(e) => println!(" โŒ Processing error: {:?}", e), } Ok(()) } /// Demonstrates content validation during preprocessing. fn validation_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Validation Example"); println!("---------------------------------------------"); let test_cases = vec![ ( "Valid content with class\n{.valid}\nTest content", true, "Valid class syntax", ), ( "Invalid class syntax\n{invalid}\nTest content", true, "Invalid class syntax", ), ( "Valid image\n![Alt](image.jpg){.img}\n", true, "Valid image syntax", ), ( "Invalid image\n![Alt](image.jpg{.img}\n", false, "Invalid image syntax", ), ]; let class_regex = Regex::new(r#"

"#)?; let img_regex = Regex::new(r"(]+)(/>)")?; for (content, should_succeed, case) in test_cases { match preprocess_content(content, &class_regex, &img_regex) { Ok(_) => { if should_succeed { println!(" โœ… {}: Processed successfully", case); } else { println!(" โŒ {}: Unexpected success", case); } } Err(e) => { if !should_succeed { println!(" โœ… {}: Failed as expected", case); } else { println!( " โŒ {}: Unexpected error: {:?}", case, e ); } } } } Ok(()) }