Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,659 changes: 1,104 additions & 555 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "html_parser"
version = "0.7.0"
authors = ["Mathias Iversen <work@mathiasiversen.com>"]
edition = "2018"
edition = "2024"
repository = "https://github.com/mathiversen/html-parser"
license = "MIT"
description = "A simple and general purpose html/xhtml parser"
Expand All @@ -14,14 +14,19 @@ readme = "README.md"
pest = "2.5.7"
pest_derive = "2.5.7"
thiserror = "1.0.40"
serde = { version = "1.0.159", features = ["derive"] }
serde_derive = "1.0.159"
serde_json = "1.0.95"
doc-comment = "0.3.3"
serde = { version = "1.0.159", features = ["derive"], optional = true }
serde_json = { version = "1.0.95", optional = true }
html-escape = "0.2"
ownable = "1.0.0"

[features]
default = ["source-span"]
source-span = []
test = ["dep:serde", "dep:serde_json"]

[dev-dependencies]
indoc = "2.0.1"
insta = { version = "1.29.0", features = ["json"]}
insta = { version = "1.29.0", features = ["json"] }
tempfile = "3.5.0"
criterion = "0.4.0"
reqwest = { version = "0.11.16", features = ["blocking", "rustls-tls"] }
Expand Down
2 changes: 1 addition & 1 deletion benches/bench_wikipedia.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use criterion::{criterion_group, criterion_main, Criterion};
use criterion::{Criterion, criterion_group, criterion_main};
use html_parser::Dom;

static HTML: &'static str = include_str!("./wikipedia-2020-12-21.html");
Expand Down
8 changes: 5 additions & 3 deletions examples/get_all_href/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use html_parser::{Dom, Node, Result};
use ownable::traits::ToBorrowed;
use std::ops::Deref;

// This example illustrates how to use the library to get all of the anchor-hrefs from a document.

fn main() -> Result<()> {
let html = include_str!("./index.html");
let dom = Dom::parse(html)?;
let iter = dom.children.get(0).unwrap().into_iter();
let iter = dom.children.first().unwrap().into_iter();

let hrefs = iter.filter_map(|item| match item {
Node::Element(ref element) if element.name == "a" => element.attributes["href"].clone(),
Node::Element(element) if element.name == "a" => element.attributes["href"].to_borrowed(),
_ => None,
});

println!("\nThe following links where found:");
for (index, href) in hrefs.enumerate() {
println!("{}: {}", index + 1, href)
println!("{}: {}", index + 1, href.deref())
}

Ok(())
Expand Down
113 changes: 63 additions & 50 deletions examples/simple_parser/main.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,74 @@
use clap::Parser;
use html_parser::{Dom, Result};
use std::{
fs::File,
io::{self, Read},
path::PathBuf,
};

#[derive(Debug, Parser)]
/// A simple and general purpose html/xhtml parser.
struct Opt {
#[arg(short, long)]
/// Pretty-print the output.
pretty_print: bool,

#[arg(short, long)]
/// Debug the parser, this will print errors to the console.
debug: bool,

/// Path to the file, or stdin (piped content).
///
/// This argument can either be a path to the html-file that you would like to parse or the
/// result of stdin. Note: Content over stdin needs to be finite, for now, as it is collected
/// into a string and then processed by the parser.
input: Option<PathBuf>,
fn main() -> html_parser::Result<()> {
#[cfg(feature = "test")]
{
real::main()
}

#[cfg(not(feature = "test"))]
panic!("this example requires the `test` feature to be enabled");
}

fn main() -> Result<()> {
let opt = Opt::parse();
#[cfg(feature = "test")]
mod real {
use clap::Parser;
use html_parser::{Dom, Result};
use std::{
fs::File,
io::{self, Read},
path::PathBuf,
};

let mut content = String::with_capacity(100_000);
#[derive(Debug, Parser)]
/// A simple and general purpose html/xhtml parser.
struct Opt {
#[arg(short, long)]
/// Pretty-print the output.
pretty_print: bool,

// If input is provided then use that as a path
if let Some(path) = opt.input {
let mut file = File::open(path)?;
file.read_to_string(&mut content)?;
#[arg(short, long)]
/// Debug the parser, this will print errors to the console.
debug: bool,

// Else read from stdin, this enables piping
// ex: `cat index.html | html_parser`
} else {
let stdin = io::stdin();
let mut handle = stdin.lock();
handle.read_to_string(&mut content)?;
};
/// Path to the file, or stdin (piped content).
///
/// This argument can either be a path to the html-file that you would like to parse or the
/// result of stdin. Note: Content over stdin needs to be finite, for now, as it is collected
/// into a string and then processed by the parser.
input: Option<PathBuf>,
}

pub(super) fn main() -> Result<()> {
let opt = Opt::parse();

let mut content = String::with_capacity(100_000);

let dom = Dom::parse(&content)?;
// If input is provided then use that as a path
if let Some(path) = opt.input {
let mut file = File::open(path)?;
file.read_to_string(&mut content)?;

if opt.debug {
for error in &dom.errors {
println!("# {}", error);
// Else read from stdin, this enables piping
// ex: `cat index.html | html_parser`
} else {
let stdin = io::stdin();
let mut handle = stdin.lock();
handle.read_to_string(&mut content)?;
};

let dom = Dom::parse(&content)?;

if opt.debug {
for error in &dom.errors {
println!("# {}", error);
}
}
}

if opt.pretty_print {
println!("{}", dom.to_json_pretty()?);
} else {
println!("{}", dom.to_json()?);
}
if opt.pretty_print {
println!("{}", dom.to_json_pretty()?);
} else {
println!("{}", dom.to_json()?);
}

Ok(())
Ok(())
}
}
Loading