2 releases

0.1.0 Aug 9, 2024
0.1.0-a Oct 9, 2024

#2188 in Parser implementations

Download history 112/week @ 2024-08-08 6/week @ 2024-08-15 9/week @ 2024-09-12 6/week @ 2024-09-19 5/week @ 2024-09-26 60/week @ 2024-10-03 74/week @ 2024-10-10 5/week @ 2024-10-17

140 downloads per month

MIT license

14KB
343 lines

html_forge

A robust and efficient HTML parsing library for Rust

πŸš€ Features

  • Element Parsing: Easily parse HTML elements with attributes and nested children.
  • Text Parsing: Extract and manage text nodes within your documents.
  • Attribute Parsing: Handle element attributes with both single and double quotes.
  • Comment Parsing: Parse comments safely, even those with special characters or nested hyphens.
  • Error Handling: Gracefully manage parsing errors such as mismatched tags and unexpected end-of-file (EOF).

πŸ“¦ Installation

To use html_forge, add the following to your Cargo.toml:

[dependencies]
html_forge = "0.1.0"

Usage

1. Basic Parsing Example:

Here’s how you can parse a simple HTML snippet using html_forge:

use html_forge::parser::Parser;
use html_forge::dom::Node;

fn main() {
    let input = "<div class='container'><p>Hello, world!</p></div>".to_string();
    let mut parser = Parser::new(input);
    
    match parser.parse() {
        Ok(node) => println!("Parsed Node: {:?}", node),
        Err(err) => eprintln!("Parsing error: {:?}", err),
    }
}

2. Handling Errors:

html_forge gracefully handles common errors during parsing:

use html_forge::{parser::Parser, errors::ParseError};

fn main() {
    let input = "<div><p>Unclosed div".to_string();
    let mut parser = Parser::new(input);

    match parser.parse() {
        Err(ParseError::UnexpectedEOF) => eprintln!("Error: Unexpected end of file"),
        Err(ParseError::MismatchedClosingTag) => eprintln!("Error: Mismatched closing tag"),
        Ok(node) => println!("Parsed Node: {:?}", node),
        Err(err) => eprintln!("Other parsing error: {:?}", err),
    }
}

πŸ§ͺ Testing

To run the tests, use:

cargo test

πŸ‘₯ Contributing

Contributions are welcome! Feel free to open issues, submit pull requests, or fork the repository to make improvements.

πŸ“ License

This library is open-source and available under the MIT LICENSE.

Happy forging with html_forge! πŸ› οΈπŸš€

No runtime deps